Ajout collision semi fonctionnelle entre 2 spheres

feat:
+ collision entre 2 spheres, fonctionne a moitie, overlap entre les 2
This commit is contained in:
StratiX0 2024-04-24 12:59:40 +02:00
parent e57de4f1be
commit 291afe9424
4 changed files with 38 additions and 8 deletions

View File

@ -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);

View File

@ -11,6 +11,6 @@ Pos=-1,299
Size=492,353
[Window][Terrain]
Pos=692,769
Pos=692,768
Size=418,94

View File

@ -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;
}

View File

@ -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;