SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
RigidbodyComponent.cpp
Go to the documentation of this file.
1
#include <
Physics/RigidbodyComponent.hpp
>
2
#include <
Core/GameObject.hpp
>
3
#include <
Camera/Camera.hpp
>
4
#include <
ECS/Components/TransformComponent.hpp
>
5
6
namespace
Sleak
{
7
8
RigidbodyComponent::RigidbodyComponent
(
GameObject
*
owner
,
BodyType
type)
9
:
Component
(
owner
), m_bodyType(type) {}
10
11
bool
RigidbodyComponent::Initialize
() {
12
bIsInitialized
=
true
;
13
return
true
;
14
}
15
16
void
RigidbodyComponent::Update
(
float
/*deltaTime*/
) {
17
// Gravity integration is handled by PhysicsWorld::Step
18
}
19
20
void
RigidbodyComponent::ClearCollisionState
() {
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
29
void
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
Camera.hpp
GameObject.hpp
RigidbodyComponent.hpp
TransformComponent.hpp
Sleak::Camera
Definition
Camera.hpp:59
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::GameObject::GetComponent
T * GetComponent()
Finds the first attached component of type T, or nullptr.
Definition
GameObject.hpp:101
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::Zero
static Vector3D Zero()
Definition
Vector.hpp:496
Sleak::RigidbodyComponent::ClearCollisionState
void ClearCollisionState()
Resets HadCollision/HadGroundCollision/HadWallCollision for the next frame.
Definition
RigidbodyComponent.cpp:20
Sleak::RigidbodyComponent::Update
void Update(float deltaTime) override
Definition
RigidbodyComponent.cpp:16
Sleak::RigidbodyComponent::RigidbodyComponent
RigidbodyComponent(GameObject *owner, BodyType type=BodyType::Kinematic)
Definition
RigidbodyComponent.cpp:8
Sleak::RigidbodyComponent::Initialize
bool Initialize() override
One-time setup, called after the component is attached and the owner is initialized.
Definition
RigidbodyComponent.cpp:11
Sleak::RigidbodyComponent::ResolveCollision
void ResolveCollision(const Math::Vector3D &normal, float penetration)
Pushes the body out along normal by penetration and updates ground/wall collision state.
Definition
RigidbodyComponent.cpp:29
Sleak::TransformComponent
Represents the position, rotation, and scale of an entity in 3D space.
Definition
TransformComponent.hpp:26
Sleak::BodyType
BodyType
Definition
RigidbodyComponent.hpp:12
Sleak::BodyType::Static
@ Static
Definition
RigidbodyComponent.hpp:13
Sleak::BodyType::Dynamic
@ Dynamic
Definition
RigidbodyComponent.hpp:15
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
src
Physics
RigidbodyComponent.cpp
Generated on
for SleakEngine by
1.18.0