Khaotic Engine Reborn
Loading...
Searching...
No Matches
ecs::PhysicsComponent Class Reference
Inheritance diagram for ecs::PhysicsComponent:
ecs::Component

Public Member Functions

 PhysicsComponent ()
 
void Initialize () override
 
void Update (float deltaTime) override
 
void LaunchObject (float alpha, float initialStretch, float springConstant)
 
void SetVelocity (XMVECTOR velocity)
 
void SetAcceleration (XMVECTOR acceleration)
 
void SetMass (float mass)
 
void SetGrounded (bool isGrounded)
 
void SetPhysicsEnabled (bool enabled)
 
void SetBoundingRadius (float radius)
 
void SetPreviousPosition (XMVECTOR position)
 
void SetGravityEnabled (bool enabled)
 
void SetUpdatePositionCallback (std::function< void(XMVECTOR)> callback)
 
XMVECTOR GetVelocity () const
 
XMVECTOR GetAcceleration () const
 
float GetMass () const
 
bool IsGrounded () const
 
bool IsPhysicsEnabled () const
 
float GetBoundingRadius () const
 
XMVECTOR GetPreviousPosition () const
 
bool IsGravityEnabled () const
 
- Public Member Functions inherited from ecs::Component
 Component (const Component &)=delete
 
Componentoperator= (const Component &)=delete
 
 Component (Component &&)=default
 
Componentoperator= (Component &&)=default
 

Detailed Description

Definition at line 9 of file physics_component.h.

Constructor & Destructor Documentation

◆ PhysicsComponent()

ecs::PhysicsComponent::PhysicsComponent ( )
inline

Builder for the PhysicsComponent class. Use default values for velocity, acceleration, mass, bounding radius, and grounded state.

Definition at line 15 of file physics_component.h.

15 {
16 m_Velocity = XMVectorZero();
17 m_Acceleration = XMVectorZero();
18 m_PreviousPosition = XMVectorZero();
19 m_Mass = 1.0f;
20 m_BoundingRadius = 1.0f;
21 m_IsGrounded = false;
22 m_IsPhysicsEnabled = false;
23 m_GravityEnabled = true;
24 }

Member Function Documentation

◆ GetAcceleration()

XMVECTOR ecs::PhysicsComponent::GetAcceleration ( ) const
inline

Get the current acceleration of the object.

Returns
The acceleration as an XMVECTOR.

Definition at line 160 of file physics_component.h.

160{ return m_Acceleration; }

◆ GetBoundingRadius()

float ecs::PhysicsComponent::GetBoundingRadius ( ) const
inline

Get the bounding radius of the object.

Returns
The bounding radius as a float.

Definition at line 180 of file physics_component.h.

180{ return m_BoundingRadius; }

◆ GetMass()

float ecs::PhysicsComponent::GetMass ( ) const
inline

Get the mass of the object.

Returns
The mass as a float.

Definition at line 165 of file physics_component.h.

165{ return m_Mass; }

◆ GetPreviousPosition()

XMVECTOR ecs::PhysicsComponent::GetPreviousPosition ( ) const
inline

Get the previous position of the object. This is used to calculate the movement and collision detection.

Returns
The previous position as an XMVECTOR.

Definition at line 186 of file physics_component.h.

186{ return m_PreviousPosition; }

◆ GetVelocity()

XMVECTOR ecs::PhysicsComponent::GetVelocity ( ) const
inline

Get the current velocity of the object.

Returns
The velocity as an XMVECTOR.

Definition at line 155 of file physics_component.h.

155{ return m_Velocity; }

◆ Initialize()

void ecs::PhysicsComponent::Initialize ( )
inlineoverridevirtual

Initialize the component. This method is called when the component is added to an entity. It can be used to set up initial values or perform any necessary setup.

Reimplemented from ecs::Component.

Definition at line 33 of file physics_component.h.

33 {
34 // Initialisation du composant physique
35 }

◆ IsGravityEnabled()

bool ecs::PhysicsComponent::IsGravityEnabled ( ) const
inline

Check if gravity is enabled for the object.

Returns
True if gravity is enabled, false otherwise.

Definition at line 191 of file physics_component.h.

191{ return m_GravityEnabled; }

◆ IsGrounded()

bool ecs::PhysicsComponent::IsGrounded ( ) const
inline

Get the grounded state of the object.

Returns
True if the object is grounded, false otherwise.

Definition at line 170 of file physics_component.h.

170{ return m_IsGrounded; }

◆ IsPhysicsEnabled()

bool ecs::PhysicsComponent::IsPhysicsEnabled ( ) const
inline

Check if physics is enabled for the object.

Returns
True if physics is enabled, false otherwise.

Definition at line 175 of file physics_component.h.

175{ return m_IsPhysicsEnabled; }

◆ LaunchObject()

void ecs::PhysicsComponent::LaunchObject ( float alpha,
float initialStretch,
float springConstant )
inline

Launch an object with a spring-like force. This method calculates the initial velocity based on the angle, initial stretch, and spring constant.

This method will be removed in the future

Parameters
alphaThe launch angle in degrees.
initialStretchThe initial stretch of the spring.
springConstantThe spring constant.

Definition at line 70 of file physics_component.h.

70 {
71 // Constants
72 const float gravity = -9.81f;
73
74 // Convert alpha from degrees to radians if needed
75 float alphaRadians = alpha * (XM_PI / 180.0f);
76
77 // Scale factors to make the physics simulation more visible
78 float scaleFactor = 200.0f; // Adjust this based on your world scale
79
80 // Calculate initial velocity magnitude
81 float velocityMagnitude = initialStretch * sqrtf(springConstant / m_Mass) *
82 sqrtf(1.0f - powf((m_Mass * gravity * sinf(alphaRadians) /
83 (springConstant * initialStretch)), 2.0f));
84
85 // Apply scale factor
86 velocityMagnitude *= scaleFactor;
87
88 // Calculate velocity components
89 XMVECTOR velocity = XMVectorSet(
90 velocityMagnitude * cosf(alphaRadians), // vx = v0 * cos(alpha)
91 velocityMagnitude * sinf(alphaRadians), // vy = v0 * sin(alpha)
92 0.0f, // z-component (0 for 2D trajectory)
93 0.0f
94 );
95
96 // Apply velocity
97 SetVelocity(velocity);
98
99 // Enable physics and reset grounded state
100 SetPhysicsEnabled(true);
101 SetGrounded(false);
102 }
void SetVelocity(XMVECTOR velocity)
void SetGrounded(bool isGrounded)
void SetPhysicsEnabled(bool enabled)

◆ SetAcceleration()

void ecs::PhysicsComponent::SetAcceleration ( XMVECTOR acceleration)
inline

Set the acceleration of the object.

Parameters
acceleration

Definition at line 113 of file physics_component.h.

113{ m_Acceleration = acceleration; }

◆ SetBoundingRadius()

void ecs::PhysicsComponent::SetBoundingRadius ( float radius)
inline

Set the bounding radius of the object.

Parameters
radiusThe bounding radius to set.

Definition at line 133 of file physics_component.h.

133{ m_BoundingRadius = radius; }

◆ SetGravityEnabled()

void ecs::PhysicsComponent::SetGravityEnabled ( bool enabled)
inline

Enable or disable gravity for the object.

Parameters
enabledTrue to enable gravity, false to disable.

Definition at line 143 of file physics_component.h.

143{ m_GravityEnabled = enabled; }

◆ SetGrounded()

void ecs::PhysicsComponent::SetGrounded ( bool isGrounded)
inline

Set the grounded state of the object.

Parameters
isGroundedTrue if the object is grounded, false otherwise.

Definition at line 123 of file physics_component.h.

123{ m_IsGrounded = isGrounded; }

◆ SetMass()

void ecs::PhysicsComponent::SetMass ( float mass)
inline

Set the mass of the object.

Parameters
massThe mass to set.

Definition at line 118 of file physics_component.h.

118{ m_Mass = mass; }

◆ SetPhysicsEnabled()

void ecs::PhysicsComponent::SetPhysicsEnabled ( bool enabled)
inline

Enable or disable physics for the object.

Parameters
enabledTrue to enable physics, false to disable.

Definition at line 128 of file physics_component.h.

128{ m_IsPhysicsEnabled = enabled; }

◆ SetPreviousPosition()

void ecs::PhysicsComponent::SetPreviousPosition ( XMVECTOR position)
inline

Set the previous position of the object.

Parameters
positionThe previous position to set.

Definition at line 138 of file physics_component.h.

138{ m_PreviousPosition = position; }

◆ SetUpdatePositionCallback()

void ecs::PhysicsComponent::SetUpdatePositionCallback ( std::function< void(XMVECTOR)> callback)
inline

Set the callback to update the position of the object. This callback will be connected to the TransformComponent to update the position.

Parameters
callbackThe callback function that takes an XMVECTOR as a parameter.

Definition at line 149 of file physics_component.h.

149{ m_UpdatePositionCallback = callback; }

◆ SetVelocity()

void ecs::PhysicsComponent::SetVelocity ( XMVECTOR velocity)
inline

Set the velocity of the object.

Parameters
velocity

Definition at line 108 of file physics_component.h.

108{ m_Velocity = velocity; }

◆ Update()

void ecs::PhysicsComponent::Update ( float deltaTime)
inlineoverridevirtual

Update the physics component. This method is called every frame to update the physics state.

This method is not the final update method It will be called by the EntityManager's in the physics Thread. This is due to the fact that the physics system is not updated every frame. The physics thread is called at a fixed time step (50 FPS by default).

Parameters
deltaTimeThe time elapsed since the last frame.

Reimplemented from ecs::Component.

Definition at line 48 of file physics_component.h.

48 {
49 if (!m_IsPhysicsEnabled) return;
50
51 // Mise à jour de la vélocité basée sur l'accélération
52 m_Velocity = m_Velocity + m_Acceleration * deltaTime;
53
54 // Si la physique est activée et qu'une fonction de mise à jour de position est définie
55 if (m_UpdatePositionCallback) {
56 m_UpdatePositionCallback(m_Velocity * deltaTime);
57 }
58 }

The documentation for this class was generated from the following file: