Ajout de la collision entre 2 cubes
feat: + collision entre 2 cubes refactor: + changements dans quelques fonctions + renommage de certaines fonctions
This commit is contained in:
parent
fadca179e8
commit
e57de4f1be
@ -742,9 +742,19 @@ bool ApplicationClass::Frame(InputClass* Input)
|
|||||||
// Reset acceleration for the new frame
|
// Reset acceleration for the new frame
|
||||||
object->SetAcceleration(XMVectorZero());
|
object->SetAcceleration(XMVectorZero());
|
||||||
|
|
||||||
|
object->SetGrounded(false);
|
||||||
|
|
||||||
|
for (auto& chunk : m_terrainChunk)
|
||||||
|
{
|
||||||
|
if (m_Physics->IsColliding(object, chunk))
|
||||||
|
{
|
||||||
|
object->SetVelocity(XMVectorSetY(object->GetVelocity(), 0.0f));
|
||||||
|
object->SetAcceleration(XMVectorSetY(object->GetAcceleration(), 0.0f));
|
||||||
|
object->SetGrounded(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Apply forces
|
// Apply forces
|
||||||
|
|
||||||
|
|
||||||
float forceX = 0, forceY = 0, forceZ = 0, forceW = 0;
|
float forceX = 0, forceY = 0, forceZ = 0, forceW = 0;
|
||||||
|
|
||||||
if (keyLeft)
|
if (keyLeft)
|
||||||
@ -759,44 +769,23 @@ bool ApplicationClass::Frame(InputClass* Input)
|
|||||||
{
|
{
|
||||||
forceY = 40.0f;
|
forceY = 40.0f;
|
||||||
}
|
}
|
||||||
if (keyDown)
|
if (keyDown && !object->GetGrounded())
|
||||||
{
|
{
|
||||||
forceY = -40.0f;
|
forceY = -40.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isGrounded = object->GetGrounded();
|
|
||||||
|
|
||||||
for (auto& chunk : m_terrainChunk)
|
|
||||||
{
|
|
||||||
if (m_Physics->IsColliding(object, chunk))
|
|
||||||
{
|
|
||||||
isGrounded = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isGrounded)
|
|
||||||
{
|
|
||||||
XMVECTOR force = XMVectorSet(forceX, forceY, forceZ, forceW);
|
XMVECTOR force = XMVectorSet(forceX, forceY, forceZ, forceW);
|
||||||
m_Physics->ApplyForce(object, force);
|
m_Physics->AddForce(object, force);
|
||||||
|
|
||||||
// Update velocity based on acceleration
|
// Update velocity based on acceleration
|
||||||
XMVECTOR velocity = object->GetVelocity();
|
object->AddVelocity(frameTime);
|
||||||
velocity = velocity + object->GetAcceleration() * frameTime;
|
|
||||||
object->SetVelocity(velocity);
|
|
||||||
|
|
||||||
// Update position based on velocity
|
// Update position based on velocity
|
||||||
XMVECTOR position = object->GetPosition();
|
XMVECTOR position = object->GetPosition();
|
||||||
position = position + velocity * frameTime;
|
position = position + object->GetVelocity() * frameTime;
|
||||||
object->SetPosition(position);
|
object->SetPosition(position);
|
||||||
|
|
||||||
m_Physics->ApplyGravity(object, 1.0f, frameTime);
|
m_Physics->ApplyGravity(object, 1.0f, frameTime);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
object->SetVelocity(XMVectorZero());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Check if the object has fallen below a certain position
|
// Check if the object has fallen below a certain position
|
||||||
if (XMVectorGetY(object->GetPosition()) < -30.0f)
|
if (XMVectorGetY(object->GetPosition()) < -30.0f)
|
||||||
@ -940,7 +929,7 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto object : m_object)
|
for (auto& object : m_object)
|
||||||
{
|
{
|
||||||
scaleMatrix = object->GetScaleMatrix();
|
scaleMatrix = object->GetScaleMatrix();
|
||||||
if (object->m_demoSpinning)
|
if (object->m_demoSpinning)
|
||||||
@ -965,7 +954,7 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Render terrain
|
// Render terrain
|
||||||
for (auto chunk : m_terrainChunk)
|
for (auto& chunk : m_terrainChunk)
|
||||||
{
|
{
|
||||||
|
|
||||||
scaleMatrix = chunk->GetScaleMatrix();
|
scaleMatrix = chunk->GetScaleMatrix();
|
||||||
@ -1282,7 +1271,7 @@ void ApplicationClass::GenerateTerrain()
|
|||||||
scaleMatrix = XMMatrixScaling(scaleX, scaleY, scaleZ);
|
scaleMatrix = XMMatrixScaling(scaleX, scaleY, scaleZ);
|
||||||
|
|
||||||
// Set the file name of the model.
|
// Set the file name of the model.
|
||||||
strcpy_s(modelFilename, "plane.txt");
|
strcpy_s(modelFilename, "cube.txt");
|
||||||
strcpy_s(textureFilename, "stone01.tga");
|
strcpy_s(textureFilename, "stone01.tga");
|
||||||
strcpy_s(textureFilename2, "moss01.tga");
|
strcpy_s(textureFilename2, "moss01.tga");
|
||||||
strcpy_s(textureFilename3, "alpha01.tga");
|
strcpy_s(textureFilename3, "alpha01.tga");
|
||||||
@ -1297,7 +1286,9 @@ void ApplicationClass::GenerateTerrain()
|
|||||||
|
|
||||||
newTerrain->SetScaleMatrix(scaleMatrix);
|
newTerrain->SetScaleMatrix(scaleMatrix);
|
||||||
|
|
||||||
newTerrain->SetTranslateMatrix(XMMatrixTranslation(i / 2 * (scaleX * 2), -5.0f, j * (scaleZ * 2)));
|
newTerrain->SetTranslateMatrix(XMMatrixTranslation(i / 2 * (scaleX * 2), -10.0f, j * (scaleZ * 2)));
|
||||||
|
|
||||||
|
newTerrain->SetName("cube");
|
||||||
|
|
||||||
m_terrainChunk.push_back(newTerrain);
|
m_terrainChunk.push_back(newTerrain);
|
||||||
|
|
||||||
@ -1329,7 +1320,7 @@ void ApplicationClass::AddKobject(WCHAR* filepath)
|
|||||||
newObject->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename, textureFilename2, textureFilename3);
|
newObject->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename, textureFilename2, textureFilename3);
|
||||||
newObject->SetMass(1.0f);
|
newObject->SetMass(1.0f);
|
||||||
|
|
||||||
newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 10.0f, 0.0f));
|
newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 50.0f, 0.0f));
|
||||||
newObject->SetName(filename);
|
newObject->SetName(filename);
|
||||||
|
|
||||||
m_object.push_back(newObject);
|
m_object.push_back(newObject);
|
||||||
|
@ -3,14 +3,14 @@ Pos=60,60
|
|||||||
Size=400,400
|
Size=400,400
|
||||||
|
|
||||||
[Window][Khaotic Engine]
|
[Window][Khaotic Engine]
|
||||||
Pos=27,634
|
Pos=-1,652
|
||||||
Size=694,210
|
Size=694,210
|
||||||
|
|
||||||
[Window][Objects]
|
[Window][Objects]
|
||||||
Pos=31,268
|
Pos=-1,299
|
||||||
Size=492,353
|
Size=492,353
|
||||||
|
|
||||||
[Window][Terrain]
|
[Window][Terrain]
|
||||||
Pos=755,731
|
Pos=692,769
|
||||||
Size=418,94
|
Size=418,94
|
||||||
|
|
||||||
|
@ -173,6 +173,11 @@ void Object::SetVelocity(XMVECTOR velocity)
|
|||||||
m_velocity = velocity;
|
m_velocity = velocity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Object::AddVelocity(float frameTime)
|
||||||
|
{
|
||||||
|
m_velocity += m_acceleration * frameTime;
|
||||||
|
}
|
||||||
|
|
||||||
XMVECTOR Object::GetVelocity() const
|
XMVECTOR Object::GetVelocity() const
|
||||||
{
|
{
|
||||||
return m_velocity;
|
return m_velocity;
|
||||||
|
@ -28,6 +28,7 @@ public:
|
|||||||
XMVECTOR GetScale();
|
XMVECTOR GetScale();
|
||||||
|
|
||||||
void SetVelocity(XMVECTOR);
|
void SetVelocity(XMVECTOR);
|
||||||
|
void AddVelocity(float);
|
||||||
XMVECTOR GetVelocity() const;
|
XMVECTOR GetVelocity() const;
|
||||||
void SetAcceleration(XMVECTOR);
|
void SetAcceleration(XMVECTOR);
|
||||||
XMVECTOR GetAcceleration() const;
|
XMVECTOR GetAcceleration() const;
|
||||||
|
@ -35,6 +35,8 @@ void Physics::ApplyGravity(Object* object, float dragValue, float frameTime)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!object->GetGrounded()) // Verify if the object is grounded
|
||||||
|
{
|
||||||
// Calculate the acceleration caused by gravity
|
// Calculate the acceleration caused by gravity
|
||||||
XMVECTOR gravityAcceleration = m_gravity / object->GetMass();
|
XMVECTOR gravityAcceleration = m_gravity / object->GetMass();
|
||||||
|
|
||||||
@ -56,8 +58,9 @@ void Physics::ApplyGravity(Object* object, float dragValue, float frameTime)
|
|||||||
// Set the new velocity
|
// Set the new velocity
|
||||||
object->SetVelocity(velocity);
|
object->SetVelocity(velocity);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Physics::ApplyForce(Object* object, XMVECTOR force)
|
void Physics::AddForce(Object* object, XMVECTOR force)
|
||||||
{
|
{
|
||||||
if (object == nullptr) // Verify if the object is not null
|
if (object == nullptr) // Verify if the object is not null
|
||||||
{
|
{
|
||||||
@ -77,30 +80,33 @@ void Physics::ApplyForce(Object* object, XMVECTOR force)
|
|||||||
bool Physics::IsColliding(Object* object1, Object* object2)
|
bool Physics::IsColliding(Object* object1, Object* object2)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (object1 == nullptr || object2 == nullptr) // Verify if the objects are not null
|
std::string type1 = object1->GetName();
|
||||||
|
std::string type2 = object2->GetName();
|
||||||
|
|
||||||
|
if (type1 == "cube" && type2 == "cube")
|
||||||
{
|
{
|
||||||
|
return CubesOverlap(object1, object2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add more collision checks for other types of objects here...
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the positions of the two objects
|
bool Physics::CubesOverlap(Object* cube1, Object* cube2)
|
||||||
XMVECTOR position1 = object1->GetPosition();
|
{
|
||||||
XMVECTOR position2 = object2->GetPosition();
|
XMVECTOR position1 = cube1->GetPosition();
|
||||||
|
XMVECTOR position2 = cube2->GetPosition();
|
||||||
|
|
||||||
// Get the scale of the two objects (assuming the scale represents the size of the cube)
|
XMVECTOR scale1 = cube1->GetScale();
|
||||||
XMVECTOR scale1 = object1->GetScale();
|
XMVECTOR scale2 = cube2->GetScale();
|
||||||
XMVECTOR scale2 = object2->GetScale();
|
|
||||||
|
|
||||||
// Calculate the min and max coordinates of the two cubes
|
XMVECTOR min1 = position1 - scale1;
|
||||||
XMVECTOR min1 = position1 - scale1 / 2.0f;
|
XMVECTOR max1 = position1 + scale1;
|
||||||
XMVECTOR max1 = position1 + scale1 / 2.0f;
|
XMVECTOR min2 = position2 - scale2;
|
||||||
XMVECTOR min2 = position2 - scale2 / 2.0f;
|
XMVECTOR max2 = position2 + scale2;
|
||||||
XMVECTOR max2 = position2 + scale2 / 2.0f;
|
|
||||||
|
|
||||||
// Check if the two cubes overlap on all three axes
|
return (min1.m128_f32[0] <= max2.m128_f32[0] && max1.m128_f32[0] >= min2.m128_f32[0] &&
|
||||||
bool overlapX = max1.m128_f32[0] > min2.m128_f32[0] && min1.m128_f32[0] < max2.m128_f32[0];
|
min1.m128_f32[1] <= max2.m128_f32[1] && max1.m128_f32[1] >= min2.m128_f32[1] &&
|
||||||
bool overlapY = max1.m128_f32[1] > min2.m128_f32[1] && min1.m128_f32[1] < max2.m128_f32[1];
|
min1.m128_f32[2] <= max2.m128_f32[2] && max1.m128_f32[2] >= min2.m128_f32[2]);
|
||||||
bool overlapZ = max1.m128_f32[2] > min2.m128_f32[2] && min1.m128_f32[2] < max2.m128_f32[2];
|
|
||||||
|
|
||||||
// If the cubes overlap on all three axes, they are colliding
|
|
||||||
return overlapX && overlapY && overlapZ;
|
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,9 @@ public:
|
|||||||
XMVECTOR GetGravity() const; // Get the gravity value
|
XMVECTOR GetGravity() const; // Get the gravity value
|
||||||
void SetGravity(XMVECTOR gravity); // Define the gravity value
|
void SetGravity(XMVECTOR gravity); // Define the gravity value
|
||||||
void ApplyGravity(Object*, float, float); // Apply gravity to an object
|
void ApplyGravity(Object*, float, float); // Apply gravity to an object
|
||||||
void ApplyForce(Object*, XMVECTOR);
|
void AddForce(Object*, XMVECTOR);
|
||||||
bool IsColliding(Object*, Object*);
|
bool IsColliding(Object*, Object*);
|
||||||
|
bool CubesOverlap(Object*, Object*);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
XMVECTOR m_gravity;
|
XMVECTOR m_gravity;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user