# Khaotic Engine Reborn: ecs::PhysicsComponent Class Reference

Loading...
Searching...
No Matches
ecs::PhysicsComponent Class Reference
Inheritance diagram for ecs::PhysicsComponent:

## 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
Component & | operator= (const Component &)=delete
| Component (Component &&)=default
Component & | operator= (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.

{
m_Velocity = XMVectorZero();
m_Acceleration = XMVectorZero();
m_PreviousPosition = XMVectorZero();
m_Mass = 1.0f;
m_BoundingRadius = 1.0f;
m_IsGrounded = false;
m_IsPhysicsEnabled = false;
m_GravityEnabled = true;
}

## 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.

{ 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.

{ 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.

{ 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.

{ 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.

{ 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.

{
// Initialisation du composant physique
}

## 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.

{ 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.

{ 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.

{ 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

  alpha | The launch angle in degrees.
  initialStretch | The initial stretch of the spring.
  springConstant | The spring constant.

Definition at line 70 of file physics_component.h.

{
// Constants
const float gravity = -9.81f;
// Convert alpha from degrees to radians if needed
float alphaRadians = alpha * (XM_PI / 180.0f);
// Scale factors to make the physics simulation more visible
float scaleFactor = 200.0f; // Adjust this based on your world scale
// Calculate initial velocity magnitude
float velocityMagnitude = initialStretch * sqrtf(springConstant / m_Mass) *
sqrtf(1.0f - powf((m_Mass * gravity * sinf(alphaRadians) /
(springConstant * initialStretch)), 2.0f));
// Apply scale factor
velocityMagnitude *= scaleFactor;
// Calculate velocity components
XMVECTOR velocity = XMVectorSet(
velocityMagnitude * cosf(alphaRadians), // vx = v0 * cos(alpha)
velocityMagnitude * sinf(alphaRadians), // vy = v0 * sin(alpha)
0.0f, // z-component (0 for 2D trajectory)
0.0f
);
// Apply velocity
SetVelocity(velocity);
// Enable physics and reset grounded state
SetPhysicsEnabled(true);
SetGrounded(false);
}
ecs::PhysicsComponent::SetVelocity
void SetVelocity(XMVECTOR velocity)
Definition physics_component.h:108
ecs::PhysicsComponent::SetGrounded
void SetGrounded(bool isGrounded)
Definition physics_component.h:123
ecs::PhysicsComponent::SetPhysicsEnabled
void SetPhysicsEnabled(bool enabled)
Definition physics_component.h:128

## 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.

{ m_Acceleration = acceleration; }

## SetBoundingRadius()

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

inline

Set the bounding radius of the object.

Parameters

  radius | The bounding radius to set.

Definition at line 133 of file physics_component.h.

{ m_BoundingRadius = radius; }

## SetGravityEnabled()

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

inline

Enable or disable gravity for the object.

Parameters

  enabled | True to enable gravity, false to disable.

Definition at line 143 of file physics_component.h.

{ m_GravityEnabled = enabled; }

## SetGrounded()

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

inline

Set the grounded state of the object.

Parameters

  isGrounded | True if the object is grounded, false otherwise.

Definition at line 123 of file physics_component.h.

{ m_IsGrounded = isGrounded; }

## SetMass()

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

inline

Set the mass of the object.

Parameters

  mass | The mass to set.

Definition at line 118 of file physics_component.h.

{ m_Mass = mass; }

## SetPhysicsEnabled()

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

inline

Enable or disable physics for the object.

Parameters

  enabled | True to enable physics, false to disable.

Definition at line 128 of file physics_component.h.

{ m_IsPhysicsEnabled = enabled; }

## SetPreviousPosition()

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

inline

Set the previous position of the object.

Parameters

  position | The previous position to set.

Definition at line 138 of file physics_component.h.

{ 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

  callback | The callback function that takes an XMVECTOR as a parameter.

Definition at line 149 of file physics_component.h.

{ 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.

{ 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

  deltaTime | The time elapsed since the last frame.

Reimplemented from ecs::Component.

Definition at line 48 of file physics_component.h.

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

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

- enginecustom/src/inc/system/ecs/components/physics_component.h