SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
ColliderComponent.hpp
Go to the documentation of this file.
1#ifndef _COLLIDER_COMPONENT_HPP_
2#define _COLLIDER_COMPONENT_HPP_
3
4#include <ECS/Component.hpp>
5#include <Core/OSDef.hpp>
7
8namespace Sleak {
9
10 struct MeshData;
11
12 /// Attaches a collision shape to a GameObject and registers it with PhysicsWorld's broadphase.
13 ///
14 /// Attach the shape that matches the object: Physics::AABB for boxes
15 /// and level geometry, Physics::BoundingSphere for projectiles and
16 /// simple characters, Physics::BoundingCapsule for upright figures.
17 /// Two more constructors derive a shape from MeshData, either a
18 /// bounding volume of a type you pick or an exact triangle mesh.
19 ///
20 /// Registration is automatic. Adding the owning GameObject to a scene
21 /// registers the collider (and any on child objects) with that scene's
22 /// Physics::PhysicsWorld, and removing the object unregisters it. The
23 /// shape you supply is in local space; GetWorldAABB() applies the
24 /// owner's current transform.
25 ///
26 /// Layer and mask filter both collision response and the world query
27 /// API, so put triggers, terrain, and characters on distinct layers and
28 /// a raycast can ignore the ones it does not care about. Marking a
29 /// collider as a trigger keeps it in the broadphase while skipping
30 /// physical response.
31 ///
32 /// @code{.cpp}
33 /// // Static level geometry
34 /// floor->AddComponent<Sleak::ColliderComponent>(
35 /// Sleak::Physics::AABB(
36 /// Sleak::Math::Vector3D(-0.5f, -0.5f, -0.5f),
37 /// Sleak::Math::Vector3D( 0.5f, 0.5f, 0.5f)));
38 /// floor->AddComponent<Sleak::RigidbodyComponent>(
39 /// Sleak::BodyType::Static);
40 /// AddObject(floor); // registers with the scene's PhysicsWorld
41 ///
42 /// // An upright character, raised to sit on the ground
43 /// player->AddComponent<Sleak::ColliderComponent>(
44 /// Sleak::Physics::BoundingCapsule(
45 /// Sleak::Math::Vector3D(0, 0, 0), 0.35f, 0.9f));
46 ///
47 /// // A pickup volume that reports overlaps but never pushes
48 /// if (auto* c = pickup->GetComponent<Sleak::ColliderComponent>()) {
49 /// c->SetTrigger(true);
50 /// c->SetLayer(LAYER_PICKUP);
51 /// }
52 /// @endcode
53 ///
54 /// @see RigidbodyComponent, Physics::PhysicsWorld, Physics::AABB,
55 /// Physics::BoundingSphere, Physics::BoundingCapsule
56 /// @ingroup physics
57 class ENGINE_API ColliderComponent : public Component {
58 public:
59 /// Wraps a pre-built axis-aligned box shape.
61 /// Wraps a pre-built sphere shape.
63 /// Wraps a pre-built capsule shape.
65
66 /// Derives a bounding shape of the given type from the mesh's vertex positions.
68
69 /// Builds an exact triangle-mesh collider when asMesh is true;
70 /// otherwise falls back to an AABB computed from the mesh's vertices.
71 ColliderComponent(GameObject* owner, const MeshData& meshData, bool asMesh);
72
73 ~ColliderComponent() override = default;
74
75 bool Initialize() override;
76 void Update(float deltaTime) override;
77
78 const Physics::ColliderShape& GetShape() const { return m_shape; }
79 Physics::ColliderType GetType() const { return m_type; }
80
81 /// Local shape transformed into world space by the owner's current transform.
82 Physics::AABB GetWorldAABB() const;
83
84 // Offset from owner's transform
85 void SetOffset(const Math::Vector3D& offset) { m_offset = offset; }
86 Math::Vector3D GetOffset() const { return m_offset; }
87
88 // Layer/mask for filtering
89 void SetLayer(uint32_t layer) { m_layer = layer; }
90 uint32_t GetLayer() const { return m_layer; }
91 void SetMask(uint32_t mask) { m_mask = mask; }
92 uint32_t GetMask() const { return m_mask; }
93
94 // Trigger (no physics response, just events)
95 void SetTrigger(bool trigger) { m_isTrigger = trigger; }
96 bool IsTrigger() const { return m_isTrigger; }
97
98 // Broadphase proxy
99 void SetProxyId(int id) { m_proxyId = id; }
100 int GetProxyId() const { return m_proxyId; }
101
102 private:
105 Math::Vector3D m_offset;
106 uint32_t m_layer = 0xFFFFFFFF;
107 uint32_t m_mask = 0xFFFFFFFF;
108 bool m_isTrigger = false;
109 int m_proxyId = -1;
110 };
111
112} // namespace Sleak
113
114#endif // _COLLIDER_COMPONENT_HPP_
void Update(float deltaTime) override
void SetMask(uint32_t mask)
const Physics::ColliderShape & GetShape() const
void SetLayer(uint32_t layer)
void SetTrigger(bool trigger)
ColliderComponent(GameObject *owner, const Physics::AABB &aabb)
Wraps a pre-built axis-aligned box shape.
Math::Vector3D GetOffset() const
~ColliderComponent() override=default
void SetOffset(const Math::Vector3D &offset)
Physics::ColliderType GetType() const
bool Initialize() override
One-time setup, called after the component is attached and the owner is initialized.
GameObject * owner
Definition Component.hpp:81
Component(GameObject *object)
Definition Component.hpp:61
std::variant< AABB, BoundingSphere, BoundingCapsule, TriangleMesh > ColliderShape
Tagged union of the shapes a ColliderComponent can hold.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10