# Khaotic Engine Reborn: enginecustom/src/inc/system/ecs/components/physics_component.h Source File

Loading...
Searching...
No Matches
physics_component.h
#pragma once
#include "../component.h"
#include <DirectXMath.h>
using namespace DirectX;
namespace ecs {
class PhysicsComponent : public Component {
public:
PhysicsComponent() {
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;
}
~PhysicsComponent() = default;
void Initialize() override {
// Initialisation du composant physique
}
void Update(float deltaTime) override {
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);
}
}
void LaunchObject(float alpha, float initialStretch, float springConstant) {
// 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);
}
void SetVelocity(XMVECTOR velocity) { m_Velocity = velocity; }
void SetAcceleration(XMVECTOR acceleration) { m_Acceleration = acceleration; }
void SetMass(float mass) { m_Mass = mass; }
void SetGrounded(bool isGrounded) { m_IsGrounded = isGrounded; }
void SetPhysicsEnabled(bool enabled) { m_IsPhysicsEnabled = enabled; }
void SetBoundingRadius(float radius) { m_BoundingRadius = radius; }
void SetPreviousPosition(XMVECTOR position) { m_PreviousPosition = position; }
void SetGravityEnabled(bool enabled) { m_GravityEnabled = enabled; }
void SetUpdatePositionCallback(std::function<void(XMVECTOR)> callback) { m_UpdatePositionCallback = callback; }
XMVECTOR GetVelocity() const { return m_Velocity; }
XMVECTOR GetAcceleration() const { return m_Acceleration; }
float GetMass() const { return m_Mass; }
bool IsGrounded() const { return m_IsGrounded; }
bool IsPhysicsEnabled() const { return m_IsPhysicsEnabled; }
float GetBoundingRadius() const { return m_BoundingRadius; }
XMVECTOR GetPreviousPosition() const { return m_PreviousPosition; }
bool IsGravityEnabled() const { return m_GravityEnabled; }
private:
XMVECTOR m_Velocity;
XMVECTOR m_Acceleration;
XMVECTOR m_PreviousPosition;
float m_Mass;
float m_BoundingRadius;
bool m_IsGrounded;
bool m_IsPhysicsEnabled;
bool m_GravityEnabled;
// Callback pour mettre à jour la position (sera connecté au TransformComponent)
std::function<void(XMVECTOR)> m_UpdatePositionCallback;
};
} // namespace ecs
ecs::Component
Definition component.h:12
ecs::PhysicsComponent
Definition physics_component.h:9
ecs::PhysicsComponent::PhysicsComponent
PhysicsComponent()
Definition physics_component.h:15
ecs::PhysicsComponent::LaunchObject
void LaunchObject(float alpha, float initialStretch, float springConstant)
Definition physics_component.h:70
ecs::PhysicsComponent::SetGravityEnabled
void SetGravityEnabled(bool enabled)
Definition physics_component.h:143
ecs::PhysicsComponent::IsGravityEnabled
bool IsGravityEnabled() const
Definition physics_component.h:191
ecs::PhysicsComponent::SetAcceleration
void SetAcceleration(XMVECTOR acceleration)
Definition physics_component.h:113
ecs::PhysicsComponent::SetVelocity
void SetVelocity(XMVECTOR velocity)
Definition physics_component.h:108
ecs::PhysicsComponent::Update
void Update(float deltaTime) override
Definition physics_component.h:48
ecs::PhysicsComponent::SetUpdatePositionCallback
void SetUpdatePositionCallback(std::function< void(XMVECTOR)> callback)
Definition physics_component.h:149
ecs::PhysicsComponent::SetPreviousPosition
void SetPreviousPosition(XMVECTOR position)
Definition physics_component.h:138
ecs::PhysicsComponent::IsPhysicsEnabled
bool IsPhysicsEnabled() const
Definition physics_component.h:175
ecs::PhysicsComponent::Initialize
void Initialize() override
Definition physics_component.h:33
ecs::PhysicsComponent::SetGrounded
void SetGrounded(bool isGrounded)
Definition physics_component.h:123
ecs::PhysicsComponent::SetBoundingRadius
void SetBoundingRadius(float radius)
Definition physics_component.h:133
ecs::PhysicsComponent::GetVelocity
XMVECTOR GetVelocity() const
Definition physics_component.h:155
ecs::PhysicsComponent::GetBoundingRadius
float GetBoundingRadius() const
Definition physics_component.h:180
ecs::PhysicsComponent::GetMass
float GetMass() const
Definition physics_component.h:165
ecs::PhysicsComponent::IsGrounded
bool IsGrounded() const
Definition physics_component.h:170
ecs::PhysicsComponent::GetAcceleration
XMVECTOR GetAcceleration() const
Definition physics_component.h:160
ecs::PhysicsComponent::SetMass
void SetMass(float mass)
Definition physics_component.h:118
ecs::PhysicsComponent::GetPreviousPosition
XMVECTOR GetPreviousPosition() const
Definition physics_component.h:186
ecs::PhysicsComponent::SetPhysicsEnabled
void SetPhysicsEnabled(bool enabled)
Definition physics_component.h:128
ecs
Definition component.h:9