From ebccd2cf6863d54fa6d83370f2d736cb6887ca24 Mon Sep 17 00:00:00 2001 From: StratiX0 Date: Tue, 9 Apr 2024 12:30:37 +0200 Subject: [PATCH] feat: ajout de la trainee (drag) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit j'espere que c'est bien comme ça que ça marche --- enginecustom/applicationclass.cpp | 19 ++++++++++----- enginecustom/applicationclass.h | 1 + enginecustom/enginecustom.vcxproj.filters | 8 +++++-- enginecustom/imgui.ini | 6 ++--- enginecustom/object.cpp | 12 ++++++++++ enginecustom/object.h | 5 ++++ enginecustom/physics.cpp | 29 +++++++++++++++++++---- enginecustom/physics.h | 2 ++ 8 files changed, 67 insertions(+), 15 deletions(-) diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 812f6c9..c1caa1c 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -728,18 +728,25 @@ bool ApplicationClass::Frame(InputClass* Input) //// Update the z position variable each frame. //z -= 0.0174532925f * 0.2f; + + for (auto object : m_object) { - if (object != nullptr) // Vérifie que l'objet n'est pas nullptr + if (object != nullptr) // Check if the object is not null { - m_Physics->ApplyGravity(object, frameTime); if (XMVectorGetY(object->GetPosition()) < -10.0f) { - // Obtenez la position actuelle de l'objet - XMVECTOR currentPosition = object->GetPosition(); - // Définissez la nouvelle position y tout en conservant les positions x et z actuelles - object->SetPosition(XMVectorSetY(currentPosition, 20.0f)); + XMVECTOR currentPosition = object->GetPosition(); // Obtain the current position of the object + object->SetPosition(XMVectorSetY(currentPosition, 20.0f)); // Define the new position of the object } + m_Physics->ApplyGravity(object, frameTime); + m_Physics->ApplyDrag(object, 0.1f, frameTime); + // Update object position based on its velocity + XMVECTOR position = object->GetPosition(); + XMVECTOR velocity = object->GetVelocity(); + position = position + velocity * frameTime; + object->SetPosition(position); + object->m_previousPosition = object->GetPosition(); } } diff --git a/enginecustom/applicationclass.h b/enginecustom/applicationclass.h index 48a1f8d..87550cc 100644 --- a/enginecustom/applicationclass.h +++ b/enginecustom/applicationclass.h @@ -160,6 +160,7 @@ private : Physics* m_Physics; float m_gravity; + XMVECTOR m_previousPosition; }; #endif \ No newline at end of file diff --git a/enginecustom/enginecustom.vcxproj.filters b/enginecustom/enginecustom.vcxproj.filters index 76766ec..c1fdf02 100644 --- a/enginecustom/enginecustom.vcxproj.filters +++ b/enginecustom/enginecustom.vcxproj.filters @@ -382,8 +382,12 @@ shader - - + + shader + + + shader + diff --git a/enginecustom/imgui.ini b/enginecustom/imgui.ini index 8f6e492..c6ae898 100644 --- a/enginecustom/imgui.ini +++ b/enginecustom/imgui.ini @@ -3,14 +3,14 @@ Pos=60,60 Size=400,400 [Window][Khaotic Engine] -Pos=819,137 +Pos=587,47 Size=694,210 [Window][Objects] -Pos=222,19 +Pos=91,47 Size=492,353 [Window][Terrain] -Pos=890,19 +Pos=1120,255 Size=418,94 diff --git a/enginecustom/object.cpp b/enginecustom/object.cpp index a4304c0..35354a1 100644 --- a/enginecustom/object.cpp +++ b/enginecustom/object.cpp @@ -7,6 +7,8 @@ Object::Object() : ModelClass() m_translateMatrix = XMMatrixIdentity(); m_srMatrix = XMMatrixIdentity(); m_worldMatrix = XMMatrixIdentity(); + m_previousPosition = XMVectorZero(); + m_velocity = XMVectorZero(); } Object::~Object() @@ -161,4 +163,14 @@ std::string Object::GetName() void Object::SetName(std::string name) { m_name = name; +} + +void Object::SetVelocity(XMVECTOR velocity) +{ + m_velocity = velocity; +} + +XMVECTOR Object::GetVelocity() +{ + return m_velocity; } \ No newline at end of file diff --git a/enginecustom/object.h b/enginecustom/object.h index c7045f3..5f63163 100644 --- a/enginecustom/object.h +++ b/enginecustom/object.h @@ -27,6 +27,9 @@ public: XMVECTOR GetRotation(); XMVECTOR GetScale(); + void SetVelocity(XMVECTOR); + XMVECTOR GetVelocity(); + void UpdateWorldMatrix(); void UpdateSRMatrix(); void UpdateScaleMatrix(); @@ -40,6 +43,8 @@ public: public : bool m_demoSpinning = false; + XMVECTOR m_previousPosition; + XMVECTOR m_velocity; private: XMMATRIX m_scaleMatrix; diff --git a/enginecustom/physics.cpp b/enginecustom/physics.cpp index def234b..f2a371e 100644 --- a/enginecustom/physics.cpp +++ b/enginecustom/physics.cpp @@ -35,8 +35,29 @@ void Physics::ApplyGravity(Object* object, float frameTime) return; } - // Update the object position - XMVECTOR position = object->GetPosition(); - position = XMVectorSetY(position, XMVectorGetY(position) + m_gravity * frameTime); // Update the Y position - object->SetPosition(position); + // Get the object velocity + XMVECTOR velocity = object->GetVelocity(); + + // Update the Y component of the velocity + velocity = XMVectorSetY(velocity, XMVectorGetY(velocity) + m_gravity * frameTime); + + // Set the new velocity + object->SetVelocity(velocity); +} + +void Physics::ApplyDrag(Object* object, float dragValue, float frameTime) +{ + if (object == nullptr) // Verify if the object is not null + { + return; + } + + // Get the velocity of the object + XMVECTOR velocity = object->GetVelocity(); + + // Calculate the new velocity + XMVECTOR newVelocity = velocity - (velocity * dragValue * frameTime); + + // Update the velocity of the object + object->SetVelocity(newVelocity); } \ No newline at end of file diff --git a/enginecustom/physics.h b/enginecustom/physics.h index 125846d..0084708 100644 --- a/enginecustom/physics.h +++ b/enginecustom/physics.h @@ -13,6 +13,8 @@ public: float GetGravity(); // Get the gravity value void SetGravity(float gravity); // Define the gravity value void ApplyGravity(Object*, float); // Apply gravity to an object + void ApplyDrag(Object*, float, float); + private: float m_gravity;