feat: Collision cube sphere

feat:
+ Ajout des collisions entre une sphere et un cube

bug:
+ Les collisions impliquant les spheres sont imparfaites, il y aura un fort clipping
This commit is contained in:
StratiX0 2024-04-25 10:03:30 +02:00
parent 291afe9424
commit 73e4a63de0
4 changed files with 55 additions and 7 deletions

View File

@ -1282,10 +1282,13 @@ void ApplicationClass::GenerateTerrain()
strcpy_s(textureFilename2, "moss01.tga");
strcpy_s(textureFilename3, "alpha01.tga");
std::filesystem::path p(modelFilename);
std::string filenameWithoutExtension = p.stem().string();
// for loop to generate terrain chunks for a 10x10 grid
for (int i = 0; i < 1; i++)
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 1; j++)
for (int j = 0; j < 10; j++)
{
Object* newTerrain = new Object();
newTerrain->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename, textureFilename2, textureFilename3);
@ -1294,7 +1297,7 @@ void ApplicationClass::GenerateTerrain()
newTerrain->SetTranslateMatrix(XMMatrixTranslation(i / 2 * (scaleX * 2), -10.0f, j * (scaleZ * 2)));
newTerrain->SetName("sphere");
newTerrain->SetName(filenameWithoutExtension);
m_terrainChunk.push_back(newTerrain);

View File

@ -7,7 +7,7 @@ Pos=-1,652
Size=694,210
[Window][Objects]
Pos=-1,299
Pos=6,299
Size=492,353
[Window][Terrain]

View File

@ -91,10 +91,25 @@ bool Physics::IsColliding(Object* object1, Object* object2)
{
return SpheresOverlap(object1, object2);
}
if (type1 == "cube" && type2 == "sphere" || (type1 == "sphere" && type2 == "cube"))
{
if (type1 == "cube")
{
return SphereCubeOverlap(object1, object2);
}
else if (type1 == "sphere")
{
return SphereCubeOverlap(object2, object1);
}
}
return false;
}
/////////////////////////////////////////
// AABB method for collision detection //
/////////////////////////////////////////
bool Physics::CubesOverlap(Object* cube1, Object* cube2)
{
XMVECTOR position1 = cube1->GetPosition();
@ -118,8 +133,8 @@ bool Physics::SpheresOverlap(Object* sphere1, Object* sphere2)
XMVECTOR position1 = sphere1->GetPosition();
XMVECTOR position2 = sphere2->GetPosition();
XMVECTOR scale1 = sphere1->GetScale();
XMVECTOR scale2 = sphere2->GetScale();
XMVECTOR scale1 = sphere1->GetScale() / 2;
XMVECTOR scale2 = sphere2->GetScale() / 2;
float distance = sqrt(
(position1.m128_f32[0] - position2.m128_f32[0]) * (position1.m128_f32[0] - position2.m128_f32[0]) +
@ -128,7 +143,36 @@ bool Physics::SpheresOverlap(Object* sphere1, Object* sphere2)
);
float radius1 = XMVectorGetX(XMVector3Length(scale1));
float radius2 = XMVectorGetX(XMVector3Length(scale2) / 2);
float radius2 = XMVectorGetX(XMVector3Length(scale2));
return distance < radius1 + radius2;
}
bool Physics::SphereCubeOverlap(Object* cube, Object* sphere)
{
XMVECTOR position1 = cube->GetPosition();
XMVECTOR position2 = sphere->GetPosition();
XMVECTOR scale1 = cube->GetScale();
XMVECTOR scale2 = sphere->GetScale() / 2;
XMVECTOR min1 = position1 - scale1;
XMVECTOR max1 = position1 + scale1;
// Get box closest point to sphere center by clamping
float x = max(min1.m128_f32[0], min(position2.m128_f32[0], max1.m128_f32[0]));
float y = max(min1.m128_f32[1], min(position2.m128_f32[1], max1.m128_f32[1]));
float z = max(min1.m128_f32[2], min(position2.m128_f32[2], max1.m128_f32[2]));
// This is the same as SpheresOverlap
float distance = sqrt(
(x - position2.m128_f32[0]) * (x - position2.m128_f32[0]) +
(y - position2.m128_f32[1]) * (y - position2.m128_f32[1]) +
(z - position2.m128_f32[2]) * (z - position2.m128_f32[2])
);
float radius = XMVectorGetX(XMVector3Length(scale2));
return distance < radius;
}

View File

@ -18,6 +18,7 @@ public:
bool IsColliding(Object*, Object*);
bool CubesOverlap(Object*, Object*);
bool SpheresOverlap(Object*, Object*);
bool SphereCubeOverlap(Object*, Object*);
private:
XMVECTOR m_gravity;