Minor update - ui x physix
This commit is contained in:
parent
c707e49561
commit
39fa32603f
@ -34,11 +34,6 @@ ApplicationClass::~ApplicationClass()
|
|||||||
{
|
{
|
||||||
m_ShouldQuit = true;
|
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;
|
m_Physics = new Physics;
|
||||||
|
|
||||||
std::thread physicsThread(&ApplicationClass::PhysicsThreadFunction, this);
|
|
||||||
physicsThread.detach();
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (const std::exception& e)
|
catch (const std::exception& e)
|
||||||
@ -774,6 +766,8 @@ bool ApplicationClass::Frame(InputClass* Input)
|
|||||||
m_Inputs.m_KeyUp = Input->IsUpArrowPressed();
|
m_Inputs.m_KeyUp = Input->IsUpArrowPressed();
|
||||||
m_Inputs.m_KeyDown = Input->IsDownArrowPressed();
|
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.
|
// Render the scene to a render texture.
|
||||||
result = RenderSceneToTexture(rotation);
|
result = RenderSceneToTexture(rotation);
|
||||||
if (!result)
|
if (!result)
|
||||||
@ -1397,6 +1391,8 @@ void ApplicationClass::GenerateTerrain()
|
|||||||
|
|
||||||
newTerrain->SetName(filenameWithoutExtension);
|
newTerrain->SetName(filenameWithoutExtension);
|
||||||
|
|
||||||
|
newTerrain->SetType(ObjectType::Cube);
|
||||||
|
|
||||||
m_terrainChunk.push_back(newTerrain);
|
m_terrainChunk.push_back(newTerrain);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1462,7 +1458,6 @@ void ApplicationClass::AddKobject(WCHAR* filepath)
|
|||||||
newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 50.0f, 0.0f));
|
newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 50.0f, 0.0f));
|
||||||
newObject->SetName(filename);
|
newObject->SetName(filename);
|
||||||
newObject->SetId(m_ObjectId);
|
newObject->SetId(m_ObjectId);
|
||||||
newObject->SetType(ObjectType::Cube);
|
|
||||||
|
|
||||||
m_ObjectId++;
|
m_ObjectId++;
|
||||||
|
|
||||||
@ -1880,115 +1875,105 @@ void ApplicationClass::ConstructFrustum()
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool ApplicationClass::RenderPhysics(bool keyLeft, bool keyRight, bool keyUp, bool keyDown, float deltaTime) {
|
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)
|
for (auto& object : m_object) {
|
||||||
{
|
if (object == nullptr) {
|
||||||
if (object == nullptr)
|
|
||||||
{
|
|
||||||
Logger::Get().Log("Object is null", __FILE__, __LINE__, Logger::LogLevel::Error);
|
Logger::Get().Log("Object is null", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset acceleration for the new frame
|
if (!object->IsPhysicsEnabled()) {
|
||||||
object->SetAcceleration(XMVectorZero());
|
continue;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& object2 : m_object)
|
for (int step = 0; step < subSteps; ++step) {
|
||||||
{
|
// Reset acceleration for the new frame
|
||||||
if (object->GetId() == object2->GetId())
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!m_Physics->IsColliding(object, object2))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop movement in any direction
|
|
||||||
object->SetVelocity(XMVectorZero());
|
|
||||||
object->SetAcceleration(XMVectorZero());
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void ApplicationClass::PhysicsThreadFunction()
|
void ApplicationClass::PhysicsThreadFunction()
|
||||||
{
|
{
|
||||||
auto lastTime = std::chrono::high_resolution_clock::now();
|
|
||||||
|
|
||||||
while (!m_ShouldQuit)
|
while (!m_ShouldQuit)
|
||||||
{
|
{
|
||||||
auto currentTime = std::chrono::high_resolution_clock::now();
|
bool result = RenderPhysics(m_Inputs.m_KeyLeft, m_Inputs.m_KeyRight, m_Inputs.m_KeyUp, m_Inputs.m_KeyDown, m_Timer->GetTime());
|
||||||
std::chrono::duration<float> 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);
|
|
||||||
if (!result)
|
if (!result)
|
||||||
{
|
{
|
||||||
Logger::Get().Log("Could not render the physics scene", __FILE__, __LINE__, Logger::LogLevel::Error);
|
Logger::Get().Log("Could not render the physics scene", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attendre 20 millisecondes (50 fois par seconde)
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,6 @@
|
|||||||
|
|
||||||
#include <WICTextureLoader.h>
|
#include <WICTextureLoader.h>
|
||||||
#include <comdef.h> // Pour _com_error
|
#include <comdef.h> // Pour _com_error
|
||||||
#include <thread>
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
|
|
||||||
@ -155,8 +154,6 @@ private :
|
|||||||
HWND m_hwnd;
|
HWND m_hwnd;
|
||||||
bool m_windowed;
|
bool m_windowed;
|
||||||
|
|
||||||
int m_PhysicTickRate = 50; // physics execute 50 times per second
|
|
||||||
|
|
||||||
// ------------------------------------- //
|
// ------------------------------------- //
|
||||||
// ------------- RENDERING ------------- //
|
// ------------- RENDERING ------------- //
|
||||||
// ------------------------------------- //
|
// ------------------------------------- //
|
||||||
|
@ -259,6 +259,29 @@ void imguiManager::WidgetObjectWindow(ApplicationClass* app)
|
|||||||
// Physics
|
// Physics
|
||||||
std::string physicsLabel = "Physics##" + std::to_string(index);
|
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();
|
ImGui::Separator();
|
||||||
|
@ -47,7 +47,8 @@ private :
|
|||||||
bool showShaderWindow = false;
|
bool showShaderWindow = false;
|
||||||
bool showEngineSettingsWindow = false;
|
bool showEngineSettingsWindow = false;
|
||||||
|
|
||||||
private:
|
bool m_isPhyiscsEnabled = false;
|
||||||
|
|
||||||
ImGuiIO* io;
|
ImGuiIO* io;
|
||||||
|
|
||||||
ID3D11Device* m_device;
|
ID3D11Device* m_device;
|
||||||
|
@ -97,7 +97,7 @@ private:
|
|||||||
XMVECTOR m_acceleration;
|
XMVECTOR m_acceleration;
|
||||||
float m_mass;
|
float m_mass;
|
||||||
bool m_isGrounded;
|
bool m_isGrounded;
|
||||||
bool m_isPhysicsEnabled = false;
|
bool m_isPhysicsEnabled;
|
||||||
|
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
ObjectType m_type = ObjectType::Unknown;
|
ObjectType m_type = ObjectType::Unknown;
|
||||||
|
@ -30,7 +30,7 @@ void Physics::SetGravity(XMVECTOR gravity)
|
|||||||
// Apply gravity to an object
|
// Apply gravity to an object
|
||||||
void Physics::ApplyGravity(Object* object, float dragValue)
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user