Vulkan Schnee 0.0.1 (22840dc8)
High-performance rendering engine
Loading...
Searching...
No Matches
CollisionComponent.h
1#pragma once
2
3#include "Engine/Component/LogicComponent.h"
4#include "Engine/Component/PhysicsBodyComponent.h"
5#include "Engine/Physics/CollisionProfileRegistry.h"
6#include <glm/glm.hpp>
7#include <glm/gtc/quaternion.hpp>
8#include <entt/entt.hpp>
9#include <functional>
10#include <string>
11#include <vector>
12
14{
15 class Scene;
16} // namespace Engine::Entities
17class btCollisionShape;
18
19namespace Engine::Components {
20
32 class Collision : public Logic {
33 friend class PhysicsBody;
34
35 public:
36 static constexpr bool IsUnique = false;
37 static constexpr const char* ComponentName = "Collision";
38
40 using CollisionCallbackHandle = std::uint64_t;
41
47 Collision(Engine::Entities::Scene* scene, entt::entity entity);
48
52 ~Collision() override;
53
57 void beginPlay() override;
58
62 void endPlay() override;
63
68 bool drawImGui() override;
69
70 // =========================================================================
71 // Shape Management
72 // =========================================================================
73
82 virtual btCollisionShape* createShape() = 0;
83
90 virtual void destroyShape();
91
96 void markDirty();
97
103 void rebuild();
104
109 [[nodiscard]] btCollisionShape* getShape() const { return shape_; }
110
111 // =========================================================================
112 // Local Transform (offset from body center)
113 // =========================================================================
114
119 void setLocalOffset(const glm::vec3& offset);
120
125 [[nodiscard]] glm::vec3 getLocalOffset() const { return localOffset_; }
126
131 void setLocalRotation(const glm::quat& rotation);
132
137 [[nodiscard]] glm::quat getLocalRotation() const { return localRotation_; }
138
143 [[nodiscard]] glm::mat4 getLocalTransform() const;
144
145 // =========================================================================
146 // Trigger Mode
147 // =========================================================================
148
157 [[deprecated("Use setCollisionProfile() with an overlap profile instead")]] void setIsTrigger(bool trigger);
158
163 [[nodiscard]] bool isTrigger() const { return isTrigger_; }
164
165 // =========================================================================
166 // Collision Profiles
167 // =========================================================================
168
178
186 void setCollisionProfileByName( const std::string & profileName );
187
193
202 template < typename ProfileEnum >
203 void setCollisionProfile( ProfileEnum profile )
204 {
206 {
207 setCollisionProfile( registry->resolve( profile ) );
208 }
209 }
210
211 // =========================================================================
212 // Collision Callbacks
213 // =========================================================================
214
220
226
232
238
244
250
262 template<typename ActorType, typename Callback>
264 {
266 [callback = std::forward<Callback>( callback )]( Components::Collision * other ) mutable
267 {
268 Components::PhysicsBody * otherBody = other != nullptr ? other->getBodyComponent() : nullptr;
269 auto * actor = otherBody != nullptr
270 ? dynamic_cast<ActorType *>( otherBody->getHitActor() )
271 : nullptr;
272 if ( actor != nullptr )
273 {
274 callback( *actor );
275 }
276 }
277 );
278 }
279
291 template<typename ActorType, typename Callback>
292 CollisionCallbackHandle onOverlapEnd( Callback && callback )
293 {
295 [callback = std::forward<Callback>( callback )]( Components::Collision * other ) mutable
296 {
297 Components::PhysicsBody * otherBody = other != nullptr ? other->getBodyComponent() : nullptr;
298 auto * actor = otherBody != nullptr
299 ? dynamic_cast<ActorType *>( otherBody->getHitActor() )
300 : nullptr;
301 if ( actor != nullptr )
302 {
303 callback( *actor );
304 }
305 }
306 );
307 }
308
315
321
327
333
339
345
351
356 [[nodiscard]] Components::PhysicsBody* getBodyComponent() const { return bodyComponent_; }
357
363
369
370 // =========================================================================
371 // Debug Visualization
372 // =========================================================================
373
382 virtual void debugDraw(const glm::mat4& worldTransform) const;
383
391 void setDebugColorSeed(std::uint64_t seed);
392
397 void setDebugName(std::string name);
398
403 [[nodiscard]] const std::string& getDebugName() const { return debugName_; }
404
409 static void setDebugDrawEnabled(bool enabled);
410
415 [[nodiscard]] static bool isDebugDrawEnabled();
416
417 protected:
428 [[nodiscard]] glm::vec4 getDebugDrawColor(const glm::vec4& fallbackColor) const;
429
430 static bool debugDrawEnabled_;
431 entt::entity entity_;
432 btCollisionShape* shape_ = nullptr;
433 glm::vec3 localOffset_{0.0f};
434 glm::quat localRotation_{glm::identity<glm::quat>()};
436 std::string debugName_;
437 std::uint64_t debugColorSeed_ = 0;
438 bool hasDebugColorSeed_ = false;
439 bool isTrigger_ = false;
440 bool isDirty_ = false;
443
444 private:
450
454
457
459 std::vector<CallbackSubscription> overlapBeginCallbacks_;
460 std::vector<CallbackSubscription> overlapEndCallbacks_;
461 };
462
463} // namespace EngineCore
Base class for collision shape components.
btCollisionShape * getShape() const
Gets the current collision shape.
void dispatchOverlapEnd(Components::Collision *other)
Dispatches overlap end hooks and callbacks.
std::uint64_t CollisionCallbackHandle
Opaque handle for an overlap callback subscription.
void setCollisionProfileByName(const std::string &profileName)
Sets the collision profile by registered profile name.
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.
std::vector< CallbackSubscription > overlapBeginCallbacks_
std::vector< CallbackSubscription > overlapEndCallbacks_
Physics::CollisionHandle collisionHandle_
Runtime handle assigned by the physics engine.
static constexpr const char * ComponentName
Component type name used by engine tools.
void dispatchCollisionEnd(Components::Collision *other)
Dispatches collision end hooks.
static bool debugDrawEnabled_
Global collision debug draw toggle.
virtual void debugDraw(const glm::mat4 &worldTransform) const
Draws debug visualization of the collision shape.
bool drawImGui() override
Draws the base logic component ImGui controls.
void markDirty()
Marks the shape as needing rebuild. Call after changing shape properties (halfExtents,...
void removeOverlapCallback(CollisionCallbackHandle handle)
Removes an overlap callback registered with Collision::onOverlapBegin() or Collision::onOverlapEnd().
void setLocalRotation(const glm::quat &rotation)
Sets the local rotation relative to the body.
void endPlay() override
Clears callbacks, unregisters from the physics body, and destroys the Bullet shape.
glm::vec3 getLocalOffset() const
Gets the local position offset.
btCollisionShape * shape_
Current Bullet shape owned by this component.
void dispatchCollisionStay(Components::Collision *other)
Dispatches collision stay hooks.
CollisionCallbackHandle onOverlapBegin(Callback &&callback)
Registers a typed overlap-begin callback.
Components::PhysicsBody * bodyComponent_
Physics body this collision is registered with.
std::string debugName_
Debug display name.
void setCollisionProfile(ProfileEnum profile)
Sets the collision profile from an enum registered with the active collision profile registry.
virtual void onOverlapStay(Components::Collision *other)
Called every frame while overlapping another collision component.
bool isTrigger_
Legacy trigger flag retained for compatibility.
glm::quat localRotation_
Local rotation relative to the owning body.
void setCollisionHandle(Physics::CollisionHandle handle)
Sets the runtime collision handle assigned by the physics engine.
CollisionCallbackHandle addOverlapEndCallback(std::function< void(Components::Collision *)> callback)
glm::vec3 localOffset_
Local position offset relative to the owning body.
void setLocalOffset(const glm::vec3 &offset)
Sets the local position offset relative to the body.
const std::string & getDebugName() const
Gets the debug display name for this collision component.
std::uint64_t debugColorSeed_
Stable seed for random debug color selection.
virtual void destroyShape()
Destroys the current shape.
void setCollisionProfile(Physics::CollisionProfileHandle profile)
Sets the collision profile handle used for filtering and response lookup.
void setDebugColorSeed(std::uint64_t seed)
Sets a stable seed used for random debug color selection.
bool hasDebugColorSeed_
True when debugColorSeed_ was explicitly set.
void dispatchOverlapStay(Components::Collision *other)
Dispatches overlap stay hooks.
void beginPlay() override
Creates the Bullet shape and registers this component with a same-entity physics body.
Physics::CollisionProfileHandle collisionProfile_
Explicit collision profile override.
void dispatchOverlapBegin(Components::Collision *other)
Dispatches overlap begin hooks and callbacks.
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)
Sets the debug display name for this collision component.
Physics::CollisionProfileHandle getCollisionProfile() const
Gets the effective collision profile handle.
void dispatchCollisionBegin(Components::Collision *other)
Dispatches collision begin hooks.
virtual void onOverlapBegin(Components::Collision *other)
Called when an overlap with another collision component begins.
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.
entt::entity entity_
ECS entity associated with this component.
void rebuild()
Rebuilds the shape and notifies PhysicsBodyComponent.
~Collision() override
Clears callback subscriptions and destroys the current Bullet shape.
CollisionCallbackHandle onOverlapEnd(Callback &&callback)
Registers a typed overlap-end callback.
CollisionCallbackHandle nextCallbackHandle_
bool isDirty_
True when the shape needs rebuild.
virtual void onOverlapEnd(Components::Collision *other)
Called when an overlap with another collision component ends.
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.
CollisionCallbackHandle addOverlapBeginCallback(std::function< void(Components::Collision *)> callback)
Physics::CollisionHandle getCollisionHandle() const
Gets the runtime collision handle assigned by the physics engine.
static constexpr bool IsUnique
Multiple collision components can exist on one entity.
virtual void onCollisionEnd(Components::Collision *other)
Called when a collision with another object ends.
glm::vec4 getDebugDrawColor(const glm::vec4 &fallbackColor) const
Gets the color used by debug drawing.
Logic(Entities::Scene *owningScene)
Constructs a logic component owned by a scene.
Component that manages a physics rigid body for an entity.
Entities::Actor * getHitActor() const
Gets the actor owning this physics body when the owning entity is an actor.
Owns scene entities, actor lifetime, and the scene graph root.
Definition Scene.h:84
Registers collision profile names, enum mappings, and pair responses.
CollisionProfileRegistry * getActiveCollisionProfileRegistry()
Returns the process-wide active collision profile registry.
std::function< void(Components::Collision *)> callback
Runtime handle for a registered collision component.
Registry handle for a collision profile name.