Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
CollisionComponent.cpp
Go to the documentation of this file.
4
5#include <btBulletDynamicsCommon.h>
6#include <glm/gtc/matrix_transform.hpp>
7
8namespace EngineCore {
9
12 , entity_(entity)
13 {
14 }
15
19
22
23 // Create the shape
24 if (!shape_) {
26 }
27
28 // Register with PhysicsBodyComponent
30 }
31
37
39 if (shape_) {
40 delete shape_;
41 shape_ = nullptr;
42 }
43 }
44
46 isDirty_ = true;
47 }
48
50 if (!isDirty_) return;
51
52 // Recreate the shape
55 isDirty_ = false;
56
57 // Notify body component to rebuild its compound shape
58 if (bodyComponent_) {
59 bodyComponent_->markShapeDirty();
60 bodyComponent_->rebuildIfDirty();
61 }
62 }
63
64 void CollisionComponent::setLocalOffset(const glm::vec3& offset) {
65 localOffset_ = offset;
66 if (bodyComponent_) {
67 bodyComponent_->markShapeDirty();
68 }
69 }
70
71 void CollisionComponent::setLocalRotation(const glm::quat& rotation) {
72 localRotation_ = rotation;
73 if (bodyComponent_) {
74 bodyComponent_->markShapeDirty();
75 }
76 }
77
79 glm::mat4 transform = glm::mat4(1.0f);
80 transform = glm::translate(transform, localOffset_);
81 transform *= glm::mat4_cast(localRotation_);
82 return transform;
83 }
84
86 isTrigger_ = trigger;
87 // Note: Trigger mode is applied at the rigid body level
88 // The PhysicsBodyComponent will handle this during shape rebuild
89 if (bodyComponent_) {
90 bodyComponent_->markShapeDirty();
91 }
92 }
93
95 // Default implementation does nothing
96 // Override in derived classes to handle collision events
97 }
98
100 // Default implementation does nothing
101 }
102
104 // Default implementation does nothing
105 }
106
108 Entity* owner = getOwningEntity();
109 if (!owner) return;
110
111 // Try to find an existing PhysicsBodyComponent
113 if (bodyComponent_) {
114 bodyComponent_->registerCollision(this);
115 }
116 // If no body component exists, we'll be registered when one is added
117 // (PhysicsBodyComponent::beginPlay scans for existing collisions)
118 }
119
121 if (bodyComponent_) {
122 bodyComponent_->unregisterCollision(this);
123 bodyComponent_ = nullptr;
124 }
125 }
126
127} // namespace EngineCore
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.
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.
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.
T * getComponent() const
Returns the first component of type T found.
Definition Entity.h:73
virtual void endPlay()
Called when the component is removed or the game ends.
virtual void beginPlay()
Called when the component is added to the scene or the game starts.
LogicComponent(Scene *owningScene)
Entity * getOwningEntity() const
Gets the entity this component belongs to.
A scene is the overarching structure which can spawn actors.
Definition Scene.h:17
Log category system implementation.