Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
CollisionComponent.h
Go to the documentation of this file.
1#pragma once
2
5#include <glm/glm.hpp>
6#include <glm/gtc/quaternion.hpp>
7#include <entt/entt.hpp>
8#include <string>
9
11{
12 class Scene;
13} // namespace Engine::Entities
14class btCollisionShape;
15
16namespace Engine::Components {
17
27 class Collision : public Logic {
28 friend class PhysicsBody;
29
30 public:
31 static constexpr bool IsUnique = false;
32 static constexpr const char* ComponentName = "Collision";
33
39 Collision(Engine::Entities::Scene* scene, entt::entity entity);
40 ~Collision() override;
41
42 void beginPlay() override;
43 void endPlay() override;
44
45 // =========================================================================
46 // Shape Management
47 // =========================================================================
48
54 virtual btCollisionShape* createShape() = 0;
55
59 virtual void destroyShape();
60
65 void markDirty();
66
70 void rebuild();
71
76 [[nodiscard]] btCollisionShape* getShape() const { return shape_; }
77
78 // =========================================================================
79 // Local Transform (offset from body center)
80 // =========================================================================
81
86 void setLocalOffset(const glm::vec3& offset);
87
92 [[nodiscard]] glm::vec3 getLocalOffset() const { return localOffset_; }
93
98 void setLocalRotation(const glm::quat& rotation);
99
104 [[nodiscard]] glm::quat getLocalRotation() const { return localRotation_; }
105
110 [[nodiscard]] glm::mat4 getLocalTransform() const;
111
112 // =========================================================================
113 // Trigger Mode
114 // =========================================================================
115
120 void setIsTrigger(bool trigger);
121
126 [[nodiscard]] bool isTrigger() const { return isTrigger_; }
127
128 // =========================================================================
129 // Collision Callbacks
130 // =========================================================================
131
137
143
149
154 [[nodiscard]] Components::PhysicsBody* getBodyComponent() const { return bodyComponent_; }
155
156 // =========================================================================
157 // Debug Visualization
158 // =========================================================================
159
168 virtual void debugDraw(const glm::mat4& worldTransform) const;
169
170 void setDebugColorSeed(std::uint64_t seed);
171
172 void setDebugName(std::string name);
173 [[nodiscard]] const std::string& getDebugName() const { return debugName_; }
174
179 static void setDebugDrawEnabled(bool enabled);
180
185 [[nodiscard]] static bool isDebugDrawEnabled();
186
187 protected:
188 [[nodiscard]] glm::vec4 getDebugDrawColor(const glm::vec4& fallbackColor) const;
189
190 static bool debugDrawEnabled_;
191 entt::entity entity_;
192 btCollisionShape* shape_ = nullptr;
193 glm::vec3 localOffset_{0.0f};
194 glm::quat localRotation_{glm::identity<glm::quat>()};
196 std::string debugName_;
197 std::uint64_t debugColorSeed_ = 0;
198 bool hasDebugColorSeed_ = false;
199 bool isTrigger_ = false;
200 bool isDirty_ = false;
201
202 private:
205 };
206
207} // namespace EngineCore
Base class for collision shape components.
btCollisionShape * getShape() const
Gets the current collision shape.
virtual void onCollisionStay(Components::Collision *other)
Called every frame while colliding with another object.
void setIsTrigger(bool trigger)
Sets whether this collision is a trigger (overlap only, no physics response).
static void setDebugDrawEnabled(bool enabled)
Enable or disable debug drawing for all collision components.
static constexpr const char * ComponentName
virtual void debugDraw(const glm::mat4 &worldTransform) const
Draws debug visualization of the collision shape.
void markDirty()
Marks the shape as needing rebuild. Call after changing shape properties (halfExtents,...
void setLocalRotation(const glm::quat &rotation)
Sets the local rotation relative to the body.
void endPlay() override
Called when the component is removed or the game ends.
glm::vec3 getLocalOffset() const
Gets the local position offset.
Components::PhysicsBody * bodyComponent_
void setLocalOffset(const glm::vec3 &offset)
Sets the local position offset relative to the body.
const std::string & getDebugName() const
virtual void destroyShape()
Destroys the current shape.
void setDebugColorSeed(std::uint64_t seed)
void beginPlay() override
Called when the component is added to the scene or the game starts.
glm::mat4 getLocalTransform() const
Gets the local transform matrix.
Components::PhysicsBody * getBodyComponent() const
Gets the physics body component this collision is registered with.
void setDebugName(std::string name)
virtual void onCollisionBegin(Components::Collision *other)
Called when a collision with another object begins.
bool isTrigger() const
Checks if this collision is a trigger.
static bool isDebugDrawEnabled()
Check if debug drawing is enabled globally.
void rebuild()
Rebuilds the shape and notifies PhysicsBodyComponent.
Collision(Engine::Entities::Scene *scene, entt::entity entity)
Constructs a CollisionComponent.
glm::quat getLocalRotation() const
Gets the local rotation.
virtual btCollisionShape * createShape()=0
Creates the Bullet collision shape. Must be implemented by derived classes.
virtual void onCollisionEnd(Components::Collision *other)
Called when a collision with another object ends.
glm::vec4 getDebugDrawColor(const glm::vec4 &fallbackColor) const
Logic(Entities::Scene *owningScene)
Component that manages a physics rigid body for an entity.
A scene is the overarching structure which can spawn, contain and destroy actors or entities.
Definition Scene.h:56