Khaotic Engine Reborn
Loading...
Searching...
No Matches
physics_component.h
1#pragma once
2#include "../component.h"
3#include <DirectXMath.h>
4
5using namespace DirectX;
6
7namespace ecs {
8
9class PhysicsComponent : public Component {
10public:
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 }
25
26 ~PhysicsComponent() = default;
27
33 void Initialize() override {
34 // Initialisation du composant physique
35 }
36
48 void Update(float deltaTime) override {
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 }
59
70 void LaunchObject(float alpha, float initialStretch, float springConstant) {
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 }
103
108 void SetVelocity(XMVECTOR velocity) { m_Velocity = velocity; }
113 void SetAcceleration(XMVECTOR acceleration) { m_Acceleration = acceleration; }
118 void SetMass(float mass) { m_Mass = mass; }
123 void SetGrounded(bool isGrounded) { m_IsGrounded = isGrounded; }
128 void SetPhysicsEnabled(bool enabled) { m_IsPhysicsEnabled = enabled; }
133 void SetBoundingRadius(float radius) { m_BoundingRadius = radius; }
138 void SetPreviousPosition(XMVECTOR position) { m_PreviousPosition = position; }
143 void SetGravityEnabled(bool enabled) { m_GravityEnabled = enabled; }
149 void SetUpdatePositionCallback(std::function<void(XMVECTOR)> callback) { m_UpdatePositionCallback = callback; }
150
155 XMVECTOR GetVelocity() const { return m_Velocity; }
160 XMVECTOR GetAcceleration() const { return m_Acceleration; }
165 float GetMass() const { return m_Mass; }
170 bool IsGrounded() const { return m_IsGrounded; }
175 bool IsPhysicsEnabled() const { return m_IsPhysicsEnabled; }
180 float GetBoundingRadius() const { return m_BoundingRadius; }
186 XMVECTOR GetPreviousPosition() const { return m_PreviousPosition; }
191 bool IsGravityEnabled() const { return m_GravityEnabled; }
198private:
199 XMVECTOR m_Velocity;
200 XMVECTOR m_Acceleration;
201 XMVECTOR m_PreviousPosition;
202 float m_Mass;
203 float m_BoundingRadius;
204 bool m_IsGrounded;
205 bool m_IsPhysicsEnabled;
206 bool m_GravityEnabled;
207
208 // Callback pour mettre à jour la position (sera connecté au TransformComponent)
209 std::function<void(XMVECTOR)> m_UpdatePositionCallback;
210};
211
212} // namespace ecs
void LaunchObject(float alpha, float initialStretch, float springConstant)
void SetGravityEnabled(bool enabled)
void SetAcceleration(XMVECTOR acceleration)
void SetVelocity(XMVECTOR velocity)
void Update(float deltaTime) override
void SetUpdatePositionCallback(std::function< void(XMVECTOR)> callback)
void SetPreviousPosition(XMVECTOR position)
void Initialize() override
void SetGrounded(bool isGrounded)
void SetBoundingRadius(float radius)
XMVECTOR GetVelocity() const
float GetBoundingRadius() const
XMVECTOR GetAcceleration() const
XMVECTOR GetPreviousPosition() const
void SetPhysicsEnabled(bool enabled)
Definition component.h:9