Major Update - Physique Fixed Update And Thread

Physics process run in a fixed update in a thread
This commit is contained in:
2025-01-17 15:47:09 +01:00
parent 15217a5df8
commit 4ae55e73b2
7 changed files with 142 additions and 114 deletions

View File

@@ -35,6 +35,12 @@ ApplicationClass::~ApplicationClass()
{
m_ShouldQuit = true;
// Joindre le thread pour s'assurer qu'il se termine correctement
if (m_PhysicsThread.joinable())
{
m_PhysicsThread.join();
}
}
@@ -416,6 +422,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
m_Physics = new Physics;
m_PhysicsThread = std::thread(&ApplicationClass::PhysicsThreadFunction, this);
}
catch (const std::exception& e)
@@ -430,7 +437,6 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
return true;
}
void ApplicationClass::Shutdown()
{
Logger::Get().Log("Shutting down application class", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
@@ -785,7 +791,9 @@ bool ApplicationClass::Frame(InputClass* Input)
m_Inputs.m_KeyUp = Input->IsUpArrowPressed();
m_Inputs.m_KeyDown = Input->IsDownArrowPressed();
RenderPhysics(m_Inputs.m_KeyLeft, m_Inputs.m_KeyRight, m_Inputs.m_KeyUp, m_Inputs.m_KeyDown, frameTime);
//RenderPhysics(m_Inputs.m_KeyLeft, m_Inputs.m_KeyRight, m_Inputs.m_KeyUp, m_Inputs.m_KeyDown, frameTime);
// Render the scene to a render texture.
result = RenderSceneToTexture(rotation);
@@ -952,8 +960,6 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t
// Generate the view matrix based on the camera's position.
m_Camera->Render();
// Get the world, view, and projection matrices from the camera and d3d objects.
worldMatrix = m_Direct3D->GetWorldMatrix();
@@ -1909,8 +1915,6 @@ void ApplicationClass::ConstructFrustum()
bool ApplicationClass::RenderPhysics(bool keyLeft, bool keyRight, bool keyUp, bool keyDown, float deltaTime) {
const float maxSpeed = 50.0f; // Limite de vitesse maximale
const int subSteps = 10; // Nombre de sous-<2D>tapes pour la simulation
const float subDeltaTime = deltaTime / subSteps;
for (auto& object : m_object) {
if (object == nullptr) {
@@ -1922,91 +1926,99 @@ bool ApplicationClass::RenderPhysics(bool keyLeft, bool keyRight, bool keyUp, bo
continue;
}
for (int step = 0; step < subSteps; ++step) {
// Reset acceleration for the new frame
object->SetAcceleration(XMVectorZero());
object->SetGrounded(false);
// Reset acceleration for the new frame
object->SetAcceleration(XMVectorZero());
object->SetGrounded(false);
for (auto& chunk : m_terrainChunk) {
if (!m_Physics->IsColliding(object, chunk)) {
continue;
}
object->SetVelocity(XMVectorSetY(object->GetVelocity(), 0.0f));
object->SetAcceleration(XMVectorSetY(object->GetAcceleration(), 0.0f));
object->SetGrounded(true);
for (auto& chunk : m_terrainChunk) {
if (!m_Physics->IsColliding(object, chunk)) {
continue;
}
for (auto& object2 : m_object) {
if (object->GetId() == object2->GetId()) {
continue;
}
if (!m_Physics->IsColliding(object, object2)) {
continue;
}
object->SetVelocity(XMVectorZero());
object->SetAcceleration(XMVectorZero());
}
float forceX = 0, forceY = 0, forceZ = 0, forceW = 0;
if (keyLeft) {
forceX = -10.0f;
}
if (keyRight) {
forceX = 10.0f;
}
if (keyUp) {
forceY = 40.0f;
}
if (keyDown && !object->IsGrounded()) {
forceY = -40.0f;
}
XMVECTOR force = XMVectorSet(forceX, forceY, forceZ, forceW);
m_Physics->AddForce(object, force);
object->AddVelocity(subDeltaTime);
XMVECTOR velocity = object->GetVelocity();
float speed = XMVectorGetX(XMVector3Length(velocity));
if (speed > maxSpeed) {
velocity = XMVectorScale(velocity, maxSpeed / speed);
object->SetVelocity(velocity);
}
XMVECTOR position = object->GetPosition();
position = position + object->GetVelocity() * subDeltaTime;
object->SetPosition(position);
m_Physics->ApplyGravity(object, subDeltaTime);
if (XMVectorGetY(object->GetPosition()) < -30.0f) {
XMVECTOR currentPosition = object->GetPosition();
object->SetPosition(XMVectorSetY(currentPosition, 50.0f));
}
object->m_previousPosition = object->GetPosition();
object->SetVelocity(XMVectorSetY(object->GetVelocity(), 0.0f));
object->SetAcceleration(XMVectorSetY(object->GetAcceleration(), 0.0f));
object->SetGrounded(true);
}
for (auto& object2 : m_object) {
if (object->GetId() == object2->GetId()) {
continue;
}
if (!m_Physics->IsColliding(object, object2)) {
continue;
}
object->SetVelocity(XMVectorZero());
object->SetAcceleration(XMVectorZero());
}
float forceX = 0, forceY = 0, forceZ = 0, forceW = 0;
if (keyLeft) {
forceX = -10.0f;
}
if (keyRight) {
forceX = 10.0f;
}
if (keyUp) {
forceY = 40.0f;
}
if (keyDown && !object->IsGrounded()) {
forceY = -40.0f;
}
XMVECTOR force = XMVectorSet(forceX, forceY, forceZ, forceW);
m_Physics->AddForce(object, force);
object->AddVelocity(deltaTime);
XMVECTOR velocity = object->GetVelocity();
float speed = XMVectorGetX(XMVector3Length(velocity));
if (speed > maxSpeed) {
velocity = XMVectorScale(velocity, maxSpeed / speed);
object->SetVelocity(velocity);
}
XMVECTOR position = object->GetPosition();
position = position + object->GetVelocity() * deltaTime;
object->SetPosition(position);
m_Physics->ApplyGravity(object, deltaTime);
if (XMVectorGetY(object->GetPosition()) < -30.0f) {
XMVECTOR currentPosition = object->GetPosition();
object->SetPosition(XMVectorSetY(currentPosition, 50.0f));
}
object->m_previousPosition = object->GetPosition();
}
return true;
}
void ApplicationClass::PhysicsThreadFunction()
{
auto fixedUpdateInterval = std::chrono::milliseconds(1000 / m_PhysicsTickRate);
auto lastTime = std::chrono::steady_clock::now();
while (!m_ShouldQuit)
{
bool result = RenderPhysics(m_Inputs.m_KeyLeft, m_Inputs.m_KeyRight, m_Inputs.m_KeyUp, m_Inputs.m_KeyDown, m_Timer->GetTime());
if (!result)
auto now = std::chrono::steady_clock::now();
if (now - lastTime >= fixedUpdateInterval)
{
Logger::Get().Log("Could not render the physics scene", __FILE__, __LINE__, Logger::LogLevel::Error);
return;
lastTime = now;
float deltaTime = 1.0f / static_cast<float>(m_PhysicsTickRate);
bool result = RenderPhysics(m_Inputs.m_KeyLeft, m_Inputs.m_KeyRight, m_Inputs.m_KeyUp, m_Inputs.m_KeyDown, deltaTime);
if (!result)
{
Logger::Get().Log("Could not render the physics scene", __FILE__, __LINE__, Logger::LogLevel::Error);
return;
}
}
// Attendre un peu pour <20>viter de surcharger le CPU
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}