From 291afe942420da5b13d555e6fe782f93802adf36 Mon Sep 17 00:00:00 2001 From: StratiX0 Date: Wed, 24 Apr 2024 12:59:40 +0200 Subject: [PATCH] Ajout collision semi fonctionnelle entre 2 spheres feat: + collision entre 2 spheres, fonctionne a moitie, overlap entre les 2 --- enginecustom/applicationclass.cpp | 16 +++++++++++----- enginecustom/imgui.ini | 2 +- enginecustom/physics.cpp | 26 ++++++++++++++++++++++++-- enginecustom/physics.h | 2 ++ 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 29abc16..4b20a4b 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -748,8 +748,14 @@ bool ApplicationClass::Frame(InputClass* Input) { if (m_Physics->IsColliding(object, chunk)) { + + // Stop vertical movement, like gravity object->SetVelocity(XMVectorSetY(object->GetVelocity(), 0.0f)); object->SetAcceleration(XMVectorSetY(object->GetAcceleration(), 0.0f)); + + //// Stop movement in any direction + //object->SetVelocity(XMVectorZero()); + //object->SetAcceleration(XMVectorZero()); object->SetGrounded(true); } } @@ -1265,21 +1271,21 @@ void ApplicationClass::GenerateTerrain() int scaleX, scaleY, scaleZ; scaleX = 10.0f; - scaleY = 1.0f; + scaleY = 10.0f; scaleZ = 10.0f; scaleMatrix = XMMatrixScaling(scaleX, scaleY, scaleZ); // Set the file name of the model. - strcpy_s(modelFilename, "cube.txt"); + strcpy_s(modelFilename, "sphere.txt"); strcpy_s(textureFilename, "stone01.tga"); strcpy_s(textureFilename2, "moss01.tga"); strcpy_s(textureFilename3, "alpha01.tga"); // for loop to generate terrain chunks for a 10x10 grid - for (int i = 0; i < 10; i++) + for (int i = 0; i < 1; i++) { - for (int j = 0; j < 10; j++) + for (int j = 0; j < 1; j++) { Object* newTerrain = new Object(); newTerrain->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename, textureFilename2, textureFilename3); @@ -1288,7 +1294,7 @@ void ApplicationClass::GenerateTerrain() newTerrain->SetTranslateMatrix(XMMatrixTranslation(i / 2 * (scaleX * 2), -10.0f, j * (scaleZ * 2))); - newTerrain->SetName("cube"); + newTerrain->SetName("sphere"); m_terrainChunk.push_back(newTerrain); diff --git a/enginecustom/imgui.ini b/enginecustom/imgui.ini index d762592..5912ed4 100644 --- a/enginecustom/imgui.ini +++ b/enginecustom/imgui.ini @@ -11,6 +11,6 @@ Pos=-1,299 Size=492,353 [Window][Terrain] -Pos=692,769 +Pos=692,768 Size=418,94 diff --git a/enginecustom/physics.cpp b/enginecustom/physics.cpp index 940b2e3..dc63360 100644 --- a/enginecustom/physics.cpp +++ b/enginecustom/physics.cpp @@ -87,8 +87,10 @@ bool Physics::IsColliding(Object* object1, Object* object2) { return CubesOverlap(object1, object2); } - - // Add more collision checks for other types of objects here... + if (type1 == "sphere" && type2 == "sphere") + { + return SpheresOverlap(object1, object2); + } return false; } @@ -110,3 +112,23 @@ bool Physics::CubesOverlap(Object* cube1, Object* cube2) min1.m128_f32[1] <= max2.m128_f32[1] && max1.m128_f32[1] >= min2.m128_f32[1] && min1.m128_f32[2] <= max2.m128_f32[2] && max1.m128_f32[2] >= min2.m128_f32[2]); } + +bool Physics::SpheresOverlap(Object* sphere1, Object* sphere2) +{ + XMVECTOR position1 = sphere1->GetPosition(); + XMVECTOR position2 = sphere2->GetPosition(); + + XMVECTOR scale1 = sphere1->GetScale(); + XMVECTOR scale2 = sphere2->GetScale(); + + float distance = sqrt( + (position1.m128_f32[0] - position2.m128_f32[0]) * (position1.m128_f32[0] - position2.m128_f32[0]) + + (position1.m128_f32[1] - position2.m128_f32[1]) * (position1.m128_f32[1] - position2.m128_f32[1]) + + (position1.m128_f32[2] - position2.m128_f32[2]) * (position1.m128_f32[2] - position2.m128_f32[2]) + ); + + float radius1 = XMVectorGetX(XMVector3Length(scale1)); + float radius2 = XMVectorGetX(XMVector3Length(scale2) / 2); + + return distance < radius1 + radius2; +} diff --git a/enginecustom/physics.h b/enginecustom/physics.h index cbf0df0..5a51a08 100644 --- a/enginecustom/physics.h +++ b/enginecustom/physics.h @@ -2,6 +2,7 @@ #define _PHYSICS_H_ #include "object.h" +#include "math.h" class Physics : public Object { @@ -16,6 +17,7 @@ public: void AddForce(Object*, XMVECTOR); bool IsColliding(Object*, Object*); bool CubesOverlap(Object*, Object*); + bool SpheresOverlap(Object*, Object*); private: XMVECTOR m_gravity;