SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
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
>
6
#include <
Physics/Colliders.hpp
>
7
8
namespace
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.
60
ColliderComponent
(
GameObject
*
owner
,
const
Physics::AABB
& aabb);
61
/// Wraps a pre-built sphere shape.
62
ColliderComponent
(
GameObject
*
owner
,
const
Physics::BoundingSphere
& sphere);
63
/// Wraps a pre-built capsule shape.
64
ColliderComponent
(
GameObject
*
owner
,
const
Physics::BoundingCapsule
& capsule);
65
66
/// Derives a bounding shape of the given type from the mesh's vertex positions.
67
ColliderComponent
(
GameObject
*
owner
,
const
MeshData
& meshData,
Physics::ColliderType
preferred);
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
:
103
Physics::ColliderShape
m_shape;
104
Physics::ColliderType
m_type =
Physics::ColliderType::AABB
;
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_
Colliders.hpp
Component.hpp
OSDef.hpp
Sleak::ColliderComponent::IsTrigger
bool IsTrigger() const
Definition
ColliderComponent.hpp:96
Sleak::ColliderComponent::Update
void Update(float deltaTime) override
Definition
ColliderComponent.cpp:83
Sleak::ColliderComponent::SetMask
void SetMask(uint32_t mask)
Definition
ColliderComponent.hpp:91
Sleak::ColliderComponent::SetProxyId
void SetProxyId(int id)
Definition
ColliderComponent.hpp:99
Sleak::ColliderComponent::GetShape
const Physics::ColliderShape & GetShape() const
Definition
ColliderComponent.hpp:78
Sleak::ColliderComponent::SetLayer
void SetLayer(uint32_t layer)
Definition
ColliderComponent.hpp:89
Sleak::ColliderComponent::SetTrigger
void SetTrigger(bool trigger)
Definition
ColliderComponent.hpp:95
Sleak::ColliderComponent::ColliderComponent
ColliderComponent(GameObject *owner, const Physics::AABB &aabb)
Wraps a pre-built axis-aligned box shape.
Definition
ColliderComponent.cpp:9
Sleak::ColliderComponent::GetOffset
Math::Vector3D GetOffset() const
Definition
ColliderComponent.hpp:86
Sleak::ColliderComponent::~ColliderComponent
~ColliderComponent() override=default
Sleak::ColliderComponent::GetLayer
uint32_t GetLayer() const
Definition
ColliderComponent.hpp:90
Sleak::ColliderComponent::GetMask
uint32_t GetMask() const
Definition
ColliderComponent.hpp:92
Sleak::ColliderComponent::GetProxyId
int GetProxyId() const
Definition
ColliderComponent.hpp:100
Sleak::ColliderComponent::SetOffset
void SetOffset(const Math::Vector3D &offset)
Definition
ColliderComponent.hpp:85
Sleak::ColliderComponent::GetType
Physics::ColliderType GetType() const
Definition
ColliderComponent.hpp:79
Sleak::ColliderComponent::Initialize
bool Initialize() override
One-time setup, called after the component is attached and the owner is initialized.
Definition
ColliderComponent.cpp:78
Sleak::Component::GameObject
friend class GameObject
Definition
Component.hpp:88
Sleak::Component::owner
GameObject * owner
Definition
Component.hpp:84
Sleak::Component::Component
Component(GameObject *object)
Definition
Component.hpp:61
Sleak::Math::Vector3D
Definition
Vector.hpp:354
Sleak::Physics::ColliderType
ColliderType
Definition
Colliders.hpp:225
Sleak::Physics::ColliderType::AABB
@ AABB
Definition
Colliders.hpp:226
Sleak::Physics::ColliderShape
std::variant< AABB, BoundingSphere, BoundingCapsule, TriangleMesh > ColliderShape
Tagged union of the shapes a ColliderComponent can hold.
Definition
Colliders.hpp:233
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
Sleak::MeshData
Definition
MeshData.hpp:103
Sleak::Physics::AABB
Definition
Colliders.hpp:20
Sleak::Physics::BoundingCapsule
Definition
Colliders.hpp:133
Sleak::Physics::BoundingSphere
Definition
Colliders.hpp:99
include
public
Physics
ColliderComponent.hpp
Generated on
for SleakEngine by
1.18.0