SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
ColliderComponent.cpp
Go to the documentation of this file.
2#include <Core/GameObject.hpp>
3#include <Camera/Camera.hpp>
6
7namespace Sleak {
8
10 : Component(owner), m_shape(aabb), m_type(Physics::ColliderType::AABB) {}
11
13 : Component(owner), m_shape(sphere), m_type(Physics::ColliderType::Sphere) {}
14
16 : Component(owner), m_shape(capsule), m_type(Physics::ColliderType::Capsule) {}
17
19 : Component(owner) {
20 // Compute AABB from mesh vertices
21 const Vertex* verts = meshData.vertices.GetData();
22 size_t count = meshData.vertices.GetSize();
23
25 &verts[0].px, count, sizeof(Vertex));
26
27 switch (preferred) {
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 }
39 Physics::Vector3D(center.GetX(), center.GetY(), center.GetZ()),
40 std::sqrt(maxDistSq));
42 break;
43 }
45 m_shape = Physics::BoundingCapsule::FromAABB(bounds);
47 break;
48 }
49 default: {
50 m_shape = bounds;
52 break;
53 }
54 }
55}
56
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
66 mesh.Build(&verts[0].px, vertCount, sizeof(Vertex),
67 indices, indexCount);
68 m_shape = std::move(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));
75 }
76}
77
79 bIsInitialized = true;
80 return true;
81}
82
83void ColliderComponent::Update(float /*deltaTime*/) {
84 // Collider shape is static relative to owner - no per-frame work needed
85}
86
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
void Update(float deltaTime) override
Physics::AABB GetWorldAABB() const
Local shape transformed into world space by the owner's current transform.
ColliderComponent(GameObject *owner, const Physics::AABB &aabb)
Wraps a pre-built axis-aligned box shape.
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
T * GetData()
Definition List.hpp:157
size_t GetSize() const
Definition List.hpp:151
float GetY() const
Definition Vector.hpp:361
float Dot(const Vector3D &other) const
Definition Vector.hpp:417
float GetX() const
Definition Vector.hpp:360
float GetZ() const
Definition Vector.hpp:362
size_t GetSize() const
Definition MeshData.hpp:91
const Vertex * GetData() const
Definition MeshData.hpp:88
Collision shapes, the broadphase tree, and the world that steps them.
Definition SceneBase.hpp:29
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.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
uint32_t IndexType
Definition MeshData.hpp:11
IndexGroup indices
Definition MeshData.hpp:105
VertexGroup vertices
Definition MeshData.hpp:104
Vector3D GetCenter() const
Definition Colliders.hpp:27
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
static BoundingCapsule FromAABB(const AABB &aabb)
Fits a capsule inside the AABB, picking its longest axis as the capsule axis.
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.