SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Sleak::RigidbodyComponent Class Reference

#include <RigidbodyComponent.hpp>

Inheritance diagram for Sleak::RigidbodyComponent:

Public Member Functions

 RigidbodyComponent (GameObject *owner, BodyType type=BodyType::Kinematic)
 ~RigidbodyComponent () override=default
void AddForce (const Math::Vector3D &force)
void ClearCollisionState ()
 Resets HadCollision/HadGroundCollision/HadWallCollision for the next frame.
BodyType GetBodyType () const
Math::Vector3D GetGravity () const
Math::Vector3D GetGroundNormal () const
Math::Vector3D GetLastCollisionNormal () const
float GetMass () const
float GetTerminalVelocity () const
bool GetUseGravity () const
Math::Vector3D GetVelocity () const
Math::Vector3D GetWallNormal () const
bool HadCollision () const
bool HadGroundCollision () const
bool HadWallCollision () const
bool Initialize () override
 One-time setup, called after the component is attached and the owner is initialized.
bool IsGrounded () const
void ResolveCollision (const Math::Vector3D &normal, float penetration)
 Pushes the body out along normal by penetration and updates ground/wall collision state.
void SetBodyType (BodyType type)
void SetGravity (const Math::Vector3D &gravity)
void SetGrounded (bool grounded)
void SetMass (float mass)
void SetTerminalVelocity (float tv)
void SetUseGravity (bool useGravity)
void SetVelocity (const Math::Vector3D &velocity)
void Update (float deltaTime) override
Public Member Functions inherited from Sleak::Component
 Component (GameObject *object)
virtual ~Component ()=default
virtual void FixedUpdate (float fixedDeltaTime)
GameObjectGetOwner ()
virtual void LateUpdate (float deltaTime)
virtual void OnDestroy ()
 Called when the component is detached or the owner is destroyed.
virtual void OnDisable ()
virtual void OnEnable ()
Public Member Functions inherited from Sleak::Object
 Object (const std::string &name="Object")
virtual ~Object ()=default
const std::string & GetName () const
uint64_t GetUniqueID () const
void SetName (const std::string &value)

Additional Inherited Members

Protected Attributes inherited from Sleak::Component
bool bIsInitialized
GameObjectowner

Detailed Description

Tracks velocity, gravity, and collision state for a GameObject; PhysicsWorld drives its integration.

Pair this with a ColliderComponent to make an object participate in simulation. The collider supplies the shape, the rigidbody supplies the motion state, and Physics::PhysicsWorld integrates and resolves both. Without a collider, the rigidbody moves but never collides.

BodyType decides the response. Static bodies never move. Kinematic bodies move under your control and push others without being pushed. Dynamic bodies respond by mass and fall under their own gravity vector, which defaults to standard earth gravity but is yours to change: platformers and voxel games routinely run heavier gravity for a snappier feel.

Gravity is off by default, so call SetUseGravity(true) on anything that should fall. Collision flags are separated into ground and wall contacts, which is what a character controller needs to tell "landed" from "walked into something". Those flags are per-frame: call ClearCollisionState() once you have consumed them.

player->AddComponent<Sleak::ColliderComponent>(
Sleak::Math::Vector3D(0, 0, 0), 0.3f));
player->AddComponent<Sleak::RigidbodyComponent>(
auto* rb = player->GetComponent<Sleak::RigidbodyComponent>();
rb->SetUseGravity(true);
rb->SetGravity(Sleak::Math::Vector3D(0.0f, -32.0f, 0.0f));
rb->SetMass(80.0f);
// Later, in an update
if (rb->IsGrounded() && jumpPressed) {
v.SetY(6.0f);
rb->SetVelocity(v);
}
if (rb->HadWallCollision()) {
SlideAlong(rb->GetWallNormal());
}
void SetY(float val)
Definition Vector.hpp:366
Math::Vector3D GetVelocity() const
void SetUseGravity(bool useGravity)
void ClearCollisionState()
Resets HadCollision/HadGroundCollision/HadWallCollision for the next frame.
Math::Vector3D GetWallNormal() const
void SetGravity(const Math::Vector3D &gravity)
void SetVelocity(const Math::Vector3D &velocity)
See also
ColliderComponent, Physics::PhysicsWorld, BodyType, FirstPersonController

Definition at line 66 of file RigidbodyComponent.hpp.

Constructor & Destructor Documentation

◆ RigidbodyComponent()

Sleak::RigidbodyComponent::RigidbodyComponent ( GameObject * owner,
BodyType type = BodyType::Kinematic )

Definition at line 8 of file RigidbodyComponent.cpp.

◆ ~RigidbodyComponent()

Sleak::RigidbodyComponent::~RigidbodyComponent ( )
overridedefault

Member Function Documentation

◆ AddForce()

void Sleak::RigidbodyComponent::AddForce ( const Math::Vector3D & force)
inline

Definition at line 96 of file RigidbodyComponent.hpp.

◆ ClearCollisionState()

void Sleak::RigidbodyComponent::ClearCollisionState ( )

Resets HadCollision/HadGroundCollision/HadWallCollision for the next frame.

Definition at line 20 of file RigidbodyComponent.cpp.

◆ GetBodyType()

BodyType Sleak::RigidbodyComponent::GetBodyType ( ) const
inline

Definition at line 77 of file RigidbodyComponent.hpp.

◆ GetGravity()

Math::Vector3D Sleak::RigidbodyComponent::GetGravity ( ) const
inline

Definition at line 99 of file RigidbodyComponent.hpp.

◆ GetGroundNormal()

Math::Vector3D Sleak::RigidbodyComponent::GetGroundNormal ( ) const
inline

Definition at line 86 of file RigidbodyComponent.hpp.

◆ GetLastCollisionNormal()

Math::Vector3D Sleak::RigidbodyComponent::GetLastCollisionNormal ( ) const
inline

Definition at line 82 of file RigidbodyComponent.hpp.

◆ GetMass()

float Sleak::RigidbodyComponent::GetMass ( ) const
inline

Definition at line 109 of file RigidbodyComponent.hpp.

◆ GetTerminalVelocity()

float Sleak::RigidbodyComponent::GetTerminalVelocity ( ) const
inline

Definition at line 113 of file RigidbodyComponent.hpp.

◆ GetUseGravity()

bool Sleak::RigidbodyComponent::GetUseGravity ( ) const
inline

Definition at line 101 of file RigidbodyComponent.hpp.

◆ GetVelocity()

Math::Vector3D Sleak::RigidbodyComponent::GetVelocity ( ) const
inline

Definition at line 94 of file RigidbodyComponent.hpp.

◆ GetWallNormal()

Math::Vector3D Sleak::RigidbodyComponent::GetWallNormal ( ) const
inline

Definition at line 88 of file RigidbodyComponent.hpp.

◆ HadCollision()

bool Sleak::RigidbodyComponent::HadCollision ( ) const
inline

Definition at line 81 of file RigidbodyComponent.hpp.

◆ HadGroundCollision()

bool Sleak::RigidbodyComponent::HadGroundCollision ( ) const
inline

Definition at line 85 of file RigidbodyComponent.hpp.

◆ HadWallCollision()

bool Sleak::RigidbodyComponent::HadWallCollision ( ) const
inline

Definition at line 87 of file RigidbodyComponent.hpp.

◆ Initialize()

bool Sleak::RigidbodyComponent::Initialize ( )
overridevirtual

One-time setup, called after the component is attached and the owner is initialized.

Implements Sleak::Component.

Definition at line 11 of file RigidbodyComponent.cpp.

◆ IsGrounded()

bool Sleak::RigidbodyComponent::IsGrounded ( ) const
inline

Definition at line 105 of file RigidbodyComponent.hpp.

◆ ResolveCollision()

void Sleak::RigidbodyComponent::ResolveCollision ( const Math::Vector3D & normal,
float penetration )

Pushes the body out along normal by penetration and updates ground/wall collision state.

Definition at line 29 of file RigidbodyComponent.cpp.

◆ SetBodyType()

void Sleak::RigidbodyComponent::SetBodyType ( BodyType type)
inline

Definition at line 78 of file RigidbodyComponent.hpp.

◆ SetGravity()

void Sleak::RigidbodyComponent::SetGravity ( const Math::Vector3D & gravity)
inline

Definition at line 100 of file RigidbodyComponent.hpp.

◆ SetGrounded()

void Sleak::RigidbodyComponent::SetGrounded ( bool grounded)
inline

Definition at line 106 of file RigidbodyComponent.hpp.

◆ SetMass()

void Sleak::RigidbodyComponent::SetMass ( float mass)
inline

Definition at line 110 of file RigidbodyComponent.hpp.

◆ SetTerminalVelocity()

void Sleak::RigidbodyComponent::SetTerminalVelocity ( float tv)
inline

Definition at line 114 of file RigidbodyComponent.hpp.

◆ SetUseGravity()

void Sleak::RigidbodyComponent::SetUseGravity ( bool useGravity)
inline

Definition at line 102 of file RigidbodyComponent.hpp.

◆ SetVelocity()

void Sleak::RigidbodyComponent::SetVelocity ( const Math::Vector3D & velocity)
inline

Definition at line 95 of file RigidbodyComponent.hpp.

◆ Update()

void Sleak::RigidbodyComponent::Update ( float deltaTime)
overridevirtual

Implements Sleak::Component.

Definition at line 16 of file RigidbodyComponent.cpp.