Definition at line 9 of file physics_component.h.
◆ 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 }
◆ 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 |
◆ 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.
◆ 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
-
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.
70 {
71
72 const float gravity = -9.81f;
73
74
75 float alphaRadians = alpha * (XM_PI / 180.0f);
76
77
78 float scaleFactor = 200.0f;
79
80
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
86 velocityMagnitude *= scaleFactor;
87
88
89 XMVECTOR velocity = XMVectorSet(
90 velocityMagnitude * cosf(alphaRadians),
91 velocityMagnitude * sinf(alphaRadians),
92 0.0f,
93 0.0f
94 );
95
96
98
99
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
-
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
-
radius | The 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
-
enabled | True 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
-
isGrounded | True 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 |
◆ 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.
128{ 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.
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
-
callback | The 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
-
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
-
deltaTime | The 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
52 m_Velocity = m_Velocity + m_Acceleration * deltaTime;
53
54
55 if (m_UpdatePositionCallback) {
56 m_UpdatePositionCallback(m_Velocity * deltaTime);
57 }
58 }
The documentation for this class was generated from the following file: