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 bool drawImGui() override;
45
46 // =========================================================================
47 // Shape Management
48 // =========================================================================
49
55 virtual btCollisionShape* createShape() = 0;
56
60 virtual void destroyShape();
61
66 void markDirty();
67
71 void rebuild();
72
77 [[nodiscard]] btCollisionShape* getShape() const { return shape_; }
78
79 // =========================================================================
80 // Local Transform (offset from body center)
81 // =========================================================================
82
87 void setLocalOffset(const glm::vec3& offset);
88
93 [[nodiscard]] glm::vec3 getLocalOffset() const { return localOffset_; }
94
99 void setLocalRotation(const glm::quat& rotation);
100
105 [[nodiscard]] glm::quat getLocalRotation() const { return localRotation_; }
106
111 [[nodiscard]] glm::mat4 getLocalTransform() const;
112
113 // =========================================================================
114 // Trigger Mode
115 // =========================================================================
116
121 void setIsTrigger(bool trigger);
122
127 [[nodiscard]] bool isTrigger() const { return isTrigger_; }
128
129 // =========================================================================
130 // Collision Callbacks
131 // =========================================================================
132
138
144
150
155 [[nodiscard]] Components::PhysicsBody* getBodyComponent() const { return bodyComponent_; }
156
157 // =========================================================================
158 // Debug Visualization
159 // =========================================================================
160
169 virtual void debugDraw(const glm::mat4& worldTransform) const;
170
171 void setDebugColorSeed(std::uint64_t seed);
172
173 void setDebugName(std::string name);
174 [[nodiscard]] const std::string& getDebugName() const { return debugName_; }
175
180 static void setDebugDrawEnabled(bool enabled);
181
186 [[nodiscard]] static bool isDebugDrawEnabled();
187
188 protected:
189 [[nodiscard]] glm::vec4 getDebugDrawColor(const glm::vec4& fallbackColor) const;
190
191 static bool debugDrawEnabled_;
192 entt::entity entity_;
193 btCollisionShape* shape_ = nullptr;
194 glm::vec3 localOffset_{0.0f};
195 glm::quat localRotation_{glm::identity<glm::quat>()};
197 std::string debugName_;
198 std::uint64_t debugColorSeed_ = 0;
199 bool hasDebugColorSeed_ = false;
200 bool isTrigger_ = false;
201 bool isDirty_ = false;
202
203 private:
206 };
207
208} // 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.
bool drawImGui() override
Draws component-specific ImGui inspector controls.
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:62