From 13729b62fcea4e8539e8db6cf59ffb0d3cbaeff1 Mon Sep 17 00:00:00 2001 From: StratiX0 Date: Thu, 11 Apr 2024 10:37:56 +0200 Subject: [PATCH] feat: acceleration, mass, mouvement cube implementation de l'acceleration, de la masse (je sais pas si c'est une bonne facon de faire mais on va dire oui) mouvements avec un code tres sale dans application class frame --- enginecustom/applicationclass.cpp | 66 +++++++++++++++++++++++++------ enginecustom/imgui.ini | 4 +- enginecustom/inputclass.cpp | 21 ++++++++++ enginecustom/inputclass.h | 2 + enginecustom/object.cpp | 22 +++++++++++ enginecustom/object.h | 7 ++++ enginecustom/physics.cpp | 61 ++++++++++++++++++++-------- enginecustom/physics.h | 1 + 8 files changed, 153 insertions(+), 31 deletions(-) diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index ce87f5f..d9b699c 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -628,7 +628,7 @@ void ApplicationClass::Shutdown() bool ApplicationClass::Frame(InputClass* Input) { int mouseX, mouseY, currentMouseX, currentMouseY; - bool result, leftMouseDown, rightMouseDown, keyDown, buttonQ, buttonD, buttonZ, buttonS, buttonA, buttonE; + bool result, leftMouseDown, rightMouseDown, keyLeft, keyRight, keyUp, keyDown, buttonQ, buttonD, buttonZ, buttonS, buttonA, buttonE; float rotationY, rotationX, positionX, positionY, positionZ; float frameTime; @@ -673,11 +673,11 @@ bool ApplicationClass::Frame(InputClass* Input) m_Position->SetFrameTime(m_Timer->GetTime()); // Check if the left or right arrow key has been pressed, if so rotate the camera accordingly. - keyDown = Input->IsLeftArrowPressed(); - m_Position->TurnLeft(keyDown); + //keyDown = Input->IsLeftArrowPressed(); + //m_Position->TurnLeft(keyDown); - keyDown = Input->IsRightArrowPressed(); - m_Position->TurnRight(keyDown); + //keyDown = Input->IsRightArrowPressed(); + //m_Position->TurnRight(keyDown); m_Position->TurnMouse(deltaX, deltaY, rightMouseDown); @@ -728,27 +728,66 @@ bool ApplicationClass::Frame(InputClass* Input) //// Update the z position variable each frame. //z -= 0.0174532925f * 0.2f; - + keyLeft = Input->IsLeftArrowPressed(); + keyRight = Input->IsRightArrowPressed(); + keyUp = Input->IsUpArrowPressed(); + keyDown = Input->IsDownArrowPressed(); for (auto object : m_object) { if (object != nullptr) // Check if the object is not null { + // Reset acceleration for the new frame + object->SetAcceleration(XMVectorZero()); + + // Apply forces + m_Physics->ApplyGravity(object, frameTime); + m_Physics->ApplyDrag(object, 1.0f, frameTime); + + 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) + { + forceY = -40.0f; + } + + XMVECTOR force = XMVectorSet(forceX, forceY, forceZ, forceW); + m_Physics->ApplyForce(object, force); + + // Update velocity based on acceleration + XMVECTOR velocity = object->GetVelocity(); + velocity = velocity + object->GetAcceleration() * frameTime; + object->SetVelocity(velocity); + + // Update position based on velocity + XMVECTOR position = object->GetPosition(); + position = position + velocity * frameTime; + object->SetPosition(position); + + // Check if the object has fallen below the ground if (XMVectorGetY(object->GetPosition()) < -10.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, 1.0f, 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(); } } + + // Render the scene to a render texture. @@ -1266,6 +1305,7 @@ void ApplicationClass::AddKobject(WCHAR* filepath) Object* newObject = new Object(); newObject->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename, textureFilename2, textureFilename3); + newObject->SetMass(1.0f); newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 0.0f, 0.0f)); newObject->SetName(filename); diff --git a/enginecustom/imgui.ini b/enginecustom/imgui.ini index c6ae898..771a0fe 100644 --- a/enginecustom/imgui.ini +++ b/enginecustom/imgui.ini @@ -3,11 +3,11 @@ Pos=60,60 Size=400,400 [Window][Khaotic Engine] -Pos=587,47 +Pos=596,31 Size=694,210 [Window][Objects] -Pos=91,47 +Pos=87,45 Size=492,353 [Window][Terrain] diff --git a/enginecustom/inputclass.cpp b/enginecustom/inputclass.cpp index e6944fd..27840ea 100644 --- a/enginecustom/inputclass.cpp +++ b/enginecustom/inputclass.cpp @@ -276,6 +276,27 @@ bool InputClass::IsRightArrowPressed() return false; } +bool InputClass::IsUpArrowPressed() +{ + if (m_keyboardState[DIK_UP] & 0x80) + { + return true; + } + + return false; +} + + +bool InputClass::IsDownArrowPressed() +{ + if (m_keyboardState[DIK_DOWN] & 0x80) + { + return true; + } + + return false; +} + /////////////////////////////////////////////////// // Les touches correspondent aux claviers QWERTY // /////////////////////////////////////////////////// diff --git a/enginecustom/inputclass.h b/enginecustom/inputclass.h index 6aa5955..4354212 100644 --- a/enginecustom/inputclass.h +++ b/enginecustom/inputclass.h @@ -39,6 +39,8 @@ public: void KeyUp(unsigned int); bool IsLeftArrowPressed(); bool IsRightArrowPressed(); + bool IsUpArrowPressed(); + bool IsDownArrowPressed(); bool IsAPressed(); bool IsDPressed(); bool IsWPressed(); diff --git a/enginecustom/object.cpp b/enginecustom/object.cpp index 35354a1..1ae2f60 100644 --- a/enginecustom/object.cpp +++ b/enginecustom/object.cpp @@ -9,6 +9,8 @@ Object::Object() : ModelClass() m_worldMatrix = XMMatrixIdentity(); m_previousPosition = XMVectorZero(); m_velocity = XMVectorZero(); + m_acceleration = XMVectorZero(); + m_mass = 1.0f; } Object::~Object() @@ -173,4 +175,24 @@ void Object::SetVelocity(XMVECTOR velocity) XMVECTOR Object::GetVelocity() { return m_velocity; +} + +void Object::SetAcceleration(XMVECTOR acceleration) +{ + m_acceleration = acceleration; +} + +XMVECTOR Object::GetAcceleration() +{ + return m_acceleration; +} + +void Object::SetMass(float mass) +{ + m_mass = mass; +} + +float Object::GetMass() +{ + return m_mass; } \ No newline at end of file diff --git a/enginecustom/object.h b/enginecustom/object.h index 5f63163..bb98a8b 100644 --- a/enginecustom/object.h +++ b/enginecustom/object.h @@ -29,6 +29,10 @@ public: void SetVelocity(XMVECTOR); XMVECTOR GetVelocity(); + void SetAcceleration(XMVECTOR); + XMVECTOR GetAcceleration(); + void SetMass(float); + float GetMass(); void UpdateWorldMatrix(); void UpdateSRMatrix(); @@ -53,5 +57,8 @@ private: XMMATRIX m_srMatrix; XMMATRIX m_worldMatrix; + XMVECTOR m_acceleration; + float m_mass; + std::string m_name; }; diff --git a/enginecustom/physics.cpp b/enginecustom/physics.cpp index cc7561d..a7f6648 100644 --- a/enginecustom/physics.cpp +++ b/enginecustom/physics.cpp @@ -29,34 +29,63 @@ void Physics::SetGravity(XMVECTOR gravity) // Apply gravity to an object void Physics::ApplyGravity(Object* object, float frameTime) { - if (object == nullptr) // Verify if the object is not null - { - return; - } + if (object == nullptr) // Verify if the object is not null + { + return; + } - // Get the object velocity - XMVECTOR velocity = object->GetVelocity(); + // Calculate the acceleration caused by gravity + XMVECTOR gravityAcceleration = m_gravity / object->GetMass(); - // Update the velocity with gravity - velocity += m_gravity * frameTime; + // Add the gravity acceleration to the object's current acceleration + object->SetAcceleration(object->GetAcceleration() + gravityAcceleration); - // Set the new velocity - object->SetVelocity(velocity); + // Get the object velocity + XMVECTOR velocity = object->GetVelocity(); + + // Update the velocity with the object's acceleration + velocity += object->GetAcceleration() * 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; + } + + // Calculate the acceleration caused by drag + XMVECTOR dragAcceleration = -object->GetVelocity() * dragValue / object->GetMass(); + + // Add the drag acceleration to the object's current acceleration + object->SetAcceleration(object->GetAcceleration() + dragAcceleration); + + // Get the velocity of the object + XMVECTOR velocity = object->GetVelocity(); + + // Update the velocity with the object's acceleration + velocity += object->GetAcceleration() * frameTime; + + // Set the new velocity + object->SetVelocity(velocity); +} + +void Physics::ApplyForce(Object* object, XMVECTOR force) { if (object == nullptr) // Verify if the object is not null { return; } - // Get the velocity of the object - XMVECTOR velocity = object->GetVelocity(); + // Get the mass of the object + float mass = object->GetMass(); - // Calculate the new velocity - XMVECTOR newVelocity = velocity - (velocity * dragValue * frameTime); + // Calculate the acceleration caused by the force + XMVECTOR acceleration = force / mass; - // Update the velocity of the object - object->SetVelocity(newVelocity); + // Add the acceleration to the object's current acceleration + object->SetAcceleration(object->GetAcceleration() + acceleration); } \ No newline at end of file diff --git a/enginecustom/physics.h b/enginecustom/physics.h index 1a2893d..58445c3 100644 --- a/enginecustom/physics.h +++ b/enginecustom/physics.h @@ -14,6 +14,7 @@ public: void SetGravity(XMVECTOR gravity); // Define the gravity value void ApplyGravity(Object*, float); // Apply gravity to an object void ApplyDrag(Object*, float, float); + void ApplyForce(Object*, XMVECTOR); private: XMVECTOR m_gravity;