diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 56c669a..812f6c9 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -397,6 +397,13 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) void ApplicationClass::Shutdown() { + // Release the physics object. + if (m_Physics) + { + delete m_Physics; + m_Physics = 0; + } + // Release the frustum class object. if (m_Frustum) { @@ -723,10 +730,16 @@ bool ApplicationClass::Frame(InputClass* Input) for (auto object : m_object) { - m_Physics->ApplyGravity(object, frameTime); - if (XMVectorGetY(object->GetPosition()) < -10.0f) + if (object != nullptr) // Vérifie que l'objet n'est pas nullptr { - object->SetPosition(XMVectorSet(0.0f, 20.0f, 0.0f, 0.0f)); + 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)); + } } } diff --git a/enginecustom/physics.cpp b/enginecustom/physics.cpp index 3e7eff7..def234b 100644 --- a/enginecustom/physics.cpp +++ b/enginecustom/physics.cpp @@ -1,32 +1,42 @@ #include "physics.h" + Physics::Physics() { - m_gravity = -9.81f; + m_gravity = -9.81f; // Initilize the gravity value } - Physics::Physics(const Physics& other) { - m_gravity = other.m_gravity; // Copy gravity value from the other object + m_gravity = other.m_gravity; // Copy the gravity value } - Physics::~Physics() { } -float Physics::GetGravity() // Changed the method to return the gravity value +// Get the gravity value +float Physics::GetGravity() { return m_gravity; } +// Define the gravity value +void Physics::SetGravity(float gravity) +{ + m_gravity = gravity; +} + +// Apply gravity to an object void Physics::ApplyGravity(Object* object, float frameTime) { - // Update the position of the object by adding the change in position due to gravity. - XMVECTOR position = object->GetPosition(); // Get the current position - position = XMVectorSetY(position, XMVectorGetY(position) + m_gravity * frameTime); // Update the y value - object->SetPosition(position); // Set the updated position + if (object == nullptr) // Verify if the object is not null + { + return; + } - return; + // Update the object position + XMVECTOR position = object->GetPosition(); + position = XMVectorSetY(position, XMVectorGetY(position) + m_gravity * frameTime); // Update the Y position + object->SetPosition(position); } \ No newline at end of file diff --git a/enginecustom/physics.h b/enginecustom/physics.h index cfe9153..125846d 100644 --- a/enginecustom/physics.h +++ b/enginecustom/physics.h @@ -3,20 +3,19 @@ #include "object.h" -class Physics +class Physics : public Object { public: - public: Physics(); - Physics(const Physics&); + explicit Physics(const Physics&); // Use explicit to avoid implicit conversion ~Physics(); - float GetGravity(); - void ApplyGravity(Object*, float); + float GetGravity(); // Get the gravity value + void SetGravity(float gravity); // Define the gravity value + void ApplyGravity(Object*, float); // Apply gravity to an object private: float m_gravity; }; -#endif - +#endif \ No newline at end of file