SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
RigidbodyComponent.cpp
Go to the documentation of this file.
2#include <Core/GameObject.hpp>
3#include <Camera/Camera.hpp>
5
6namespace Sleak {
7
10
12 bIsInitialized = true;
13 return true;
14}
15
16void RigidbodyComponent::Update(float /*deltaTime*/) {
17 // Gravity integration is handled by PhysicsWorld::Step
18}
19
21 m_hadCollision = false;
22 m_lastCollisionNormal = Math::Vector3D::Zero();
23 m_hadGroundCollision = false;
24 m_hadWallCollision = false;
25 m_wallNormal = Math::Vector3D::Zero();
26 // Note: m_groundNormal is NOT cleared — keep last known ground normal
27}
28
29void RigidbodyComponent::ResolveCollision(const Math::Vector3D& normal, float penetration) {
30 if (m_bodyType == BodyType::Static) return;
31
32 m_lastCollisionNormal = normal;
33 m_hadCollision = true;
34
35 // Position correction — push out of the colliding object
36 Math::Vector3D correction = normal * penetration;
37
38 auto* transform = owner->GetComponent<TransformComponent>();
39 if (transform) {
40 transform->Translate(correction);
41 } else if (auto* cam = dynamic_cast<Camera*>(owner)) {
42 cam->AddPosition(correction);
43 }
44
45 if (m_bodyType != BodyType::Dynamic) return;
46
47 // Classify collision: ground (normal.y > 0.7) vs wall
48 bool isGroundCollision = normal.GetY() > 0.7f;
49
50 if (isGroundCollision) {
51 m_hadGroundCollision = true;
52 m_groundNormal = normal;
53 m_isGrounded = true;
54
55 // Zero out downward velocity when landing on ground
56 if (m_velocity.GetY() < 0.0f) {
57 m_velocity = Math::Vector3D(m_velocity.GetX(), 0.0f, m_velocity.GetZ());
58 }
59 } else {
60 m_hadWallCollision = true;
61 m_wallNormal = normal;
62
63 // Cancel velocity component pushing INTO the wall
64 float dot = m_velocity.Dot(normal);
65 if (dot < 0.0f) {
66 m_velocity = m_velocity - normal * dot;
67 }
68 }
69}
70
71} // namespace Sleak
GameObject * owner
Definition Component.hpp:81
Component(GameObject *object)
Definition Component.hpp:61
float GetY() const
Definition Vector.hpp:361
float Dot(const Vector3D &other) const
Definition Vector.hpp:417
static Vector3D Zero()
Definition Vector.hpp:495
void ClearCollisionState()
Resets HadCollision/HadGroundCollision/HadWallCollision for the next frame.
void Update(float deltaTime) override
RigidbodyComponent(GameObject *owner, BodyType type=BodyType::Kinematic)
bool Initialize() override
One-time setup, called after the component is attached and the owner is initialized.
void ResolveCollision(const Math::Vector3D &normal, float penetration)
Pushes the body out along normal by penetration and updates ground/wall collision state.
Represents the position, rotation, and scale of an entity in 3D space.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10