SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
ColliderComponent.cpp
Go to the documentation of this file.
1
#include <
Physics/ColliderComponent.hpp
>
2
#include <
Core/GameObject.hpp
>
3
#include <
Camera/Camera.hpp
>
4
#include <
ECS/Components/TransformComponent.hpp
>
5
#include <
Runtime/MeshData.hpp
>
6
7
namespace
Sleak
{
8
9
ColliderComponent::ColliderComponent
(
GameObject
*
owner
,
const
Physics::AABB
& aabb)
10
:
Component
(
owner
), m_shape(aabb), m_type(
Physics
::ColliderType::
AABB
) {}
11
12
ColliderComponent::ColliderComponent
(
GameObject
*
owner
,
const
Physics::BoundingSphere
& sphere)
13
:
Component
(
owner
), m_shape(sphere), m_type(
Physics
::ColliderType::Sphere) {}
14
15
ColliderComponent::ColliderComponent
(
GameObject
*
owner
,
const
Physics::BoundingCapsule
& capsule)
16
:
Component
(
owner
), m_shape(capsule), m_type(
Physics
::ColliderType::Capsule) {}
17
18
ColliderComponent::ColliderComponent
(
GameObject
*
owner
,
const
MeshData
& meshData,
Physics::ColliderType
preferred)
19
:
Component
(
owner
) {
20
// Compute AABB from mesh vertices
21
const
Vertex
* verts = meshData.
vertices
.
GetData
();
22
size_t
count = meshData.
vertices
.
GetSize
();
23
24
Physics::AABB
bounds =
Physics::AABB::FromVertices
(
25
&verts[0].px, count,
sizeof
(
Vertex
));
26
27
switch
(preferred) {
28
case
Physics::ColliderType::Sphere
: {
29
// Compute tight bounding sphere from vertices (not from AABB corners)
30
Math::Vector3D
center = bounds.
GetCenter
();
31
float
maxDistSq = 0.0f;
32
for
(
size_t
i = 0; i < count; ++i) {
33
Math::Vector3D
v(verts[i].px, verts[i].py, verts[i].pz);
34
Math::Vector3D
diff = v - center;
35
float
dSq = diff.
Dot
(diff);
36
if
(dSq > maxDistSq) maxDistSq = dSq;
37
}
38
m_shape =
Physics::BoundingSphere
(
39
Physics::Vector3D
(center.
GetX
(), center.
GetY
(), center.
GetZ
()),
40
std::sqrt(maxDistSq));
41
m_type =
Physics::ColliderType::Sphere
;
42
break
;
43
}
44
case
Physics::ColliderType::Capsule
: {
45
m_shape =
Physics::BoundingCapsule::FromAABB
(bounds);
46
m_type =
Physics::ColliderType::Capsule
;
47
break
;
48
}
49
default
: {
50
m_shape = bounds;
51
m_type =
Physics::ColliderType::AABB
;
52
break
;
53
}
54
}
55
}
56
57
ColliderComponent::ColliderComponent
(
GameObject
*
owner
,
const
MeshData
& meshData,
bool
asMesh)
58
:
Component
(
owner
) {
59
if
(asMesh) {
60
const
Vertex
* verts = meshData.
vertices
.
GetData
();
61
size_t
vertCount = meshData.
vertices
.
GetSize
();
62
const
IndexType
* indices = meshData.
indices
.
GetData
();
63
size_t
indexCount = meshData.
indices
.
GetSize
();
64
65
Physics::TriangleMesh
mesh;
66
mesh.
Build
(&verts[0].px, vertCount,
sizeof
(
Vertex
),
67
indices, indexCount);
68
m_shape = std::move(mesh);
69
m_type =
Physics::ColliderType::Mesh
;
70
}
else
{
71
const
Vertex
* verts = meshData.
vertices
.
GetData
();
72
size_t
count = meshData.
vertices
.
GetSize
();
73
m_shape =
Physics::AABB::FromVertices
(&verts[0].px, count,
sizeof
(
Vertex
));
74
m_type =
Physics::ColliderType::AABB
;
75
}
76
}
77
78
bool
ColliderComponent::Initialize
() {
79
bIsInitialized
=
true
;
80
return
true
;
81
}
82
83
void
ColliderComponent::Update
(
float
/*deltaTime*/
) {
84
// Collider shape is static relative to owner - no per-frame work needed
85
}
86
87
Physics::AABB
ColliderComponent::GetWorldAABB
()
const
{
88
Math::Vector3D
worldPos;
89
Math::Vector3D
worldScale(1, 1, 1);
90
91
auto
* transform =
const_cast<
GameObject
*
>
(
owner
)->GetComponent<TransformComponent>();
92
if
(transform) {
93
worldPos = transform->GetWorldPosition() + m_offset;
94
worldScale = transform->GetWorldScale();
95
}
else
if
(
auto
* cam =
dynamic_cast<
Camera
*
>
(
owner
)) {
96
worldPos = cam->GetPosition() + m_offset;
97
}
98
99
return
Physics::GetWorldAABB
(m_shape, worldPos, worldScale);
100
}
101
102
}
// namespace Sleak
Camera.hpp
ColliderComponent.hpp
GameObject.hpp
MeshData.hpp
TransformComponent.hpp
Sleak::Camera
Definition
Camera.hpp:59
Sleak::ColliderComponent::Update
void Update(float deltaTime) override
Definition
ColliderComponent.cpp:83
Sleak::ColliderComponent::GetWorldAABB
Physics::AABB GetWorldAABB() const
Local shape transformed into world space by the owner's current transform.
Definition
ColliderComponent.cpp:87
Sleak::ColliderComponent::ColliderComponent
ColliderComponent(GameObject *owner, const Physics::AABB &aabb)
Wraps a pre-built axis-aligned box shape.
Definition
ColliderComponent.cpp:9
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::bIsInitialized
bool bIsInitialized
Definition
Component.hpp:85
Sleak::Component::owner
GameObject * owner
Definition
Component.hpp:84
Sleak::Component::Component
Component(GameObject *object)
Definition
Component.hpp:61
Sleak::List::GetData
T * GetData()
Definition
List.hpp:157
Sleak::List::GetSize
size_t GetSize() const
Definition
List.hpp:151
Sleak::Math::Vector3D
Definition
Vector.hpp:354
Sleak::Math::Vector3D::GetY
float GetY() const
Definition
Vector.hpp:362
Sleak::Math::Vector3D::Dot
float Dot(const Vector3D &other) const
Definition
Vector.hpp:418
Sleak::Math::Vector3D::GetX
float GetX() const
Definition
Vector.hpp:361
Sleak::Math::Vector3D::GetZ
float GetZ() const
Definition
Vector.hpp:363
Sleak::VertexGroup::GetSize
size_t GetSize() const
Definition
MeshData.hpp:91
Sleak::VertexGroup::GetData
const Vertex * GetData() const
Definition
MeshData.hpp:88
Sleak::Physics::ColliderType
ColliderType
Definition
Colliders.hpp:225
Sleak::Physics::ColliderType::AABB
@ AABB
Definition
Colliders.hpp:226
Sleak::Physics::ColliderType::Capsule
@ Capsule
Definition
Colliders.hpp:228
Sleak::Physics::ColliderType::Mesh
@ Mesh
Definition
Colliders.hpp:229
Sleak::Physics::ColliderType::Sphere
@ Sphere
Definition
Colliders.hpp:227
Sleak::Physics
Collision shapes, the broadphase tree, and the world that steps them.
Definition
SceneBase.hpp:29
Sleak::Physics::GetWorldAABB
AABB GetWorldAABB(const ColliderShape &shape, const Vector3D &worldPos, const Vector3D &worldScale)
Transforms a local-space collider shape into a world-space AABB for broadphase queries.
Definition
Colliders.hpp:236
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
Sleak::IndexType
uint32_t IndexType
Definition
MeshData.hpp:11
Sleak::Math::AABB
Definition
AABB.hpp:11
Sleak::MeshData
Definition
MeshData.hpp:103
Sleak::MeshData::indices
IndexGroup indices
Definition
MeshData.hpp:105
Sleak::MeshData::vertices
VertexGroup vertices
Definition
MeshData.hpp:104
Sleak::Physics::AABB
Definition
Colliders.hpp:20
Sleak::Physics::AABB::GetCenter
Vector3D GetCenter() const
Definition
Colliders.hpp:27
Sleak::Physics::AABB::FromVertices
static AABB FromVertices(const float *positions, size_t count, size_t stride)
Computes a tight AABB over raw interleaved vertex positions.
Definition
Colliders.hpp:72
Sleak::Physics::BoundingCapsule
Definition
Colliders.hpp:133
Sleak::Physics::BoundingCapsule::FromAABB
static BoundingCapsule FromAABB(const AABB &aabb)
Fits a capsule inside the AABB, picking its longest axis as the capsule axis.
Definition
Colliders.hpp:179
Sleak::Physics::BoundingSphere
Definition
Colliders.hpp:99
Sleak::Physics::TriangleMesh
Definition
Colliders.hpp:204
Sleak::Physics::TriangleMesh::Build
void Build(const float *positions, size_t count, size_t stride, const uint32_t *indexData, size_t indexCount)
Copies vertex positions and indices out of raw mesh buffers and recomputes bounds.
Definition
Colliders.hpp:210
Sleak::Vertex
Definition
MeshData.hpp:16
src
Physics
ColliderComponent.cpp
Generated on
for SleakEngine by
1.18.0