From 39fa32603f8c015c336e8623d99736c3df41ec7b Mon Sep 17 00:00:00 2001 From: CatChow0 Date: Wed, 15 Jan 2025 20:42:42 +0100 Subject: [PATCH] Minor update - ui x physix --- enginecustom/applicationclass.cpp | 173 ++++++++++++++---------------- enginecustom/applicationclass.h | 3 - enginecustom/imguiManager.cpp | 23 ++++ enginecustom/imguiManager.h | 3 +- enginecustom/object.h | 2 +- enginecustom/physics.cpp | 2 +- 6 files changed, 106 insertions(+), 100 deletions(-) diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 7692732..9070466 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -34,11 +34,6 @@ ApplicationClass::~ApplicationClass() { m_ShouldQuit = true; - // wait for the physics thread to finish - while (m_Physics) - { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - } } @@ -409,9 +404,6 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) m_Physics = new Physics; - std::thread physicsThread(&ApplicationClass::PhysicsThreadFunction, this); - physicsThread.detach(); - } catch (const std::exception& e) @@ -774,6 +766,8 @@ 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); + // Render the scene to a render texture. result = RenderSceneToTexture(rotation); if (!result) @@ -1397,6 +1391,8 @@ void ApplicationClass::GenerateTerrain() newTerrain->SetName(filenameWithoutExtension); + newTerrain->SetType(ObjectType::Cube); + m_terrainChunk.push_back(newTerrain); } @@ -1462,7 +1458,6 @@ void ApplicationClass::AddKobject(WCHAR* filepath) newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 50.0f, 0.0f)); newObject->SetName(filename); newObject->SetId(m_ObjectId); - newObject->SetType(ObjectType::Cube); m_ObjectId++; @@ -1880,115 +1875,105 @@ 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-étapes pour la simulation + const float subDeltaTime = deltaTime / subSteps; - for (auto& object : m_object) - { - if (object == nullptr) - { + for (auto& object : m_object) { + if (object == nullptr) { Logger::Get().Log("Object is null", __FILE__, __LINE__, Logger::LogLevel::Error); return 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; - } - - // Stop vertical movement, like gravity - object->SetVelocity(XMVectorSetY(object->GetVelocity(), 0.0f)); - object->SetAcceleration(XMVectorSetY(object->GetAcceleration(), 0.0f)); - object->SetGrounded(true); + if (!object->IsPhysicsEnabled()) { + continue; } - for (auto& object2 : m_object) - { - if (object->GetId() == object2->GetId()) - { - continue; - } - - if (!m_Physics->IsColliding(object, object2)) - { - continue; - } - - // Stop movement in any direction - object->SetVelocity(XMVectorZero()); + for (int step = 0; step < subSteps; ++step) { + // 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& 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(); } - - // Apply forces - 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); - - // Update velocity based on acceleration - object->AddVelocity(deltaTime); - - // Update position based on velocity - XMVECTOR position = object->GetPosition(); - position = position + object->GetVelocity() * deltaTime; - object->SetPosition(position); - - m_Physics->ApplyGravity(object, deltaTime); - - // Check if the object has fallen below a certain position - if (XMVectorGetY(object->GetPosition()) < -30.0f) - { - XMVECTOR currentPosition = object->GetPosition(); // Obtain the current position of the object - object->SetPosition(XMVectorSetY(currentPosition, 50.0f)); // Define the new position of the object - } - - object->m_previousPosition = object->GetPosition(); } return true; } + void ApplicationClass::PhysicsThreadFunction() { - auto lastTime = std::chrono::high_resolution_clock::now(); while (!m_ShouldQuit) { - auto currentTime = std::chrono::high_resolution_clock::now(); - std::chrono::duration elapsedTime = currentTime - lastTime; - float deltaTime = elapsedTime.count(); - lastTime = currentTime; - - bool result = RenderPhysics(m_Inputs.m_KeyLeft, m_Inputs.m_KeyRight, m_Inputs.m_KeyUp, m_Inputs.m_KeyDown, deltaTime); + bool result = RenderPhysics(m_Inputs.m_KeyLeft, m_Inputs.m_KeyRight, m_Inputs.m_KeyUp, m_Inputs.m_KeyDown, m_Timer->GetTime()); if (!result) { Logger::Get().Log("Could not render the physics scene", __FILE__, __LINE__, Logger::LogLevel::Error); return; } - - // Attendre 20 millisecondes (50 fois par seconde) - std::this_thread::sleep_for(std::chrono::milliseconds(20)); } } diff --git a/enginecustom/applicationclass.h b/enginecustom/applicationclass.h index 6f02332..12dc728 100644 --- a/enginecustom/applicationclass.h +++ b/enginecustom/applicationclass.h @@ -33,7 +33,6 @@ #include #include // Pour _com_error -#include #include @@ -155,8 +154,6 @@ private : HWND m_hwnd; bool m_windowed; - int m_PhysicTickRate = 50; // physics execute 50 times per second - // ------------------------------------- // // ------------- RENDERING ------------- // // ------------------------------------- // diff --git a/enginecustom/imguiManager.cpp b/enginecustom/imguiManager.cpp index a88dd4f..90f768f 100644 --- a/enginecustom/imguiManager.cpp +++ b/enginecustom/imguiManager.cpp @@ -259,6 +259,29 @@ void imguiManager::WidgetObjectWindow(ApplicationClass* app) // Physics std::string physicsLabel = "Physics##" + std::to_string(index); + if (ImGui::Checkbox(physicsLabel.c_str(), &m_isPhyiscsEnabled)) + { + object->SetPhysicsEnabled(m_isPhyiscsEnabled); + } + + // 3 radio button on the same line to set the ObjectType + std::string typeLabel = "Type##" + std::to_string(index); + ObjectType type = object->GetType(); + if (ImGui::RadioButton("None", type == ObjectType::Unknown)) + { + object->SetType(ObjectType::Unknown); + } + ImGui::SameLine(); + if (ImGui::RadioButton("Cube", type == ObjectType::Cube)) + { + object->SetType(ObjectType::Cube); + } + ImGui::SameLine(); + if (ImGui::RadioButton("Sphere", type == ObjectType::Sphere)) + { + object->SetType(ObjectType::Sphere); + } + ImGui::Separator(); diff --git a/enginecustom/imguiManager.h b/enginecustom/imguiManager.h index 8a81e2c..f8114af 100644 --- a/enginecustom/imguiManager.h +++ b/enginecustom/imguiManager.h @@ -47,7 +47,8 @@ private : bool showShaderWindow = false; bool showEngineSettingsWindow = false; -private: + bool m_isPhyiscsEnabled = false; + ImGuiIO* io; ID3D11Device* m_device; diff --git a/enginecustom/object.h b/enginecustom/object.h index e342360..0064cd5 100644 --- a/enginecustom/object.h +++ b/enginecustom/object.h @@ -97,7 +97,7 @@ private: XMVECTOR m_acceleration; float m_mass; bool m_isGrounded; - bool m_isPhysicsEnabled = false; + bool m_isPhysicsEnabled; std::string m_name; ObjectType m_type = ObjectType::Unknown; diff --git a/enginecustom/physics.cpp b/enginecustom/physics.cpp index 4c3fffe..15b21c5 100644 --- a/enginecustom/physics.cpp +++ b/enginecustom/physics.cpp @@ -30,7 +30,7 @@ void Physics::SetGravity(XMVECTOR gravity) // Apply gravity to an object void Physics::ApplyGravity(Object* object, float dragValue) { - if (object == nullptr) // Verify if the object is not null + if (this == nullptr || object == nullptr) // Verify if 'this' and 'object' are not null { return; }