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
4#include <glm/glm.hpp>
5#include <glm/gtc/quaternion.hpp>
6#include <entt/entt.hpp>
7
8class btCollisionShape;
9
10namespace EngineCore {
11 class Scene;
13
28
29 public:
30 static constexpr bool IsUnique = false;
31
37 CollisionComponent(Scene* scene, entt::entity entity);
38
39 ~CollisionComponent() override;
40
41 void beginPlay() override;
42 void endPlay() override;
43
44 // =========================================================================
45 // Shape Management
46 // =========================================================================
47
53 virtual btCollisionShape* createShape() = 0;
54
58 virtual void destroyShape();
59
64 void markDirty();
65
69 void rebuild();
70
75 [[nodiscard]] btCollisionShape* getShape() const { return shape_; }
76
77 // =========================================================================
78 // Local Transform (offset from body center)
79 // =========================================================================
80
85 void setLocalOffset(const glm::vec3& offset);
86
91 [[nodiscard]] glm::vec3 getLocalOffset() const { return localOffset_; }
92
97 void setLocalRotation(const glm::quat& rotation);
98
103 [[nodiscard]] glm::quat getLocalRotation() const { return localRotation_; }
104
109 [[nodiscard]] glm::mat4 getLocalTransform() const;
110
111 // =========================================================================
112 // Trigger Mode
113 // =========================================================================
114
119 void setIsTrigger(bool trigger);
120
125 [[nodiscard]] bool isTrigger() const { return isTrigger_; }
126
127 // =========================================================================
128 // Collision Callbacks
129 // =========================================================================
130
135 virtual void onCollisionBegin(CollisionComponent* other);
136
141 virtual void onCollisionEnd(CollisionComponent* other);
142
147 virtual void onCollisionStay(CollisionComponent* other);
148
153 [[nodiscard]] PhysicsBodyComponent* getBodyComponent() const { return bodyComponent_; }
154
155 protected:
156 entt::entity entity_;
157 btCollisionShape* shape_ = nullptr;
158 glm::vec3 localOffset_{0.0f};
159 glm::quat localRotation_{glm::identity<glm::quat>()};
161 bool isTrigger_ = false;
162 bool isDirty_ = false;
163
164 private:
165 void registerWithBody();
166 void unregisterFromBody();
167 };
168
169} // namespace EngineCore
Base class for collision shape components.
btCollisionShape * getShape() const
Gets the current collision shape.
glm::mat4 getLocalTransform() const
Gets the local transform matrix.
void endPlay() override
Called when the component is removed or the game ends.
virtual void destroyShape()
Destroys the current shape.
PhysicsBodyComponent * bodyComponent_
virtual void onCollisionStay(CollisionComponent *other)
Called every frame while colliding with another object.
virtual void onCollisionBegin(CollisionComponent *other)
Called when a collision with another object begins.
void setIsTrigger(bool trigger)
Sets whether this collision is a trigger (overlap only, no physics response).
virtual void onCollisionEnd(CollisionComponent *other)
Called when a collision with another object ends.
virtual btCollisionShape * createShape()=0
Creates the Bullet collision shape. Must be implemented by derived classes.
bool isTrigger() const
Checks if this collision is a trigger.
glm::vec3 getLocalOffset() const
Gets the local position offset.
void setLocalOffset(const glm::vec3 &offset)
Sets the local position offset relative to the body.
void beginPlay() override
Called when the component is added to the scene or the game starts.
void setLocalRotation(const glm::quat &rotation)
Sets the local rotation relative to the body.
glm::quat getLocalRotation() const
Gets the local rotation.
PhysicsBodyComponent * getBodyComponent() const
Gets the physics body component this collision is registered with.
CollisionComponent(Scene *scene, entt::entity entity)
Constructs a CollisionComponent.
void markDirty()
Marks the shape as needing rebuild. Call after changing shape properties (halfExtents,...
void rebuild()
Rebuilds the shape and notifies PhysicsBodyComponent.
LogicComponent(Scene *owningScene)
Component that manages a physics rigid body for an entity.
A scene is the overarching structure which can spawn actors.
Definition Scene.h:17
Log category system implementation.