Ajout collision avec le terrain, mais tres bancale
feat: + les cubes ajoutes s'arretent lorsqu'il y a collision avec le terrain, seulement sur un seul chunk
This commit is contained in:
parent
4a77df6102
commit
fadca179e8
@ -624,7 +624,6 @@ void ApplicationClass::Shutdown()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool ApplicationClass::Frame(InputClass* Input)
|
||||
{
|
||||
int mouseX, mouseY, currentMouseX, currentMouseY;
|
||||
@ -765,20 +764,39 @@ bool ApplicationClass::Frame(InputClass* Input)
|
||||
forceY = -40.0f;
|
||||
}
|
||||
|
||||
XMVECTOR force = XMVectorSet(forceX, forceY, forceZ, forceW);
|
||||
m_Physics->ApplyForce(object, force);
|
||||
bool isGrounded = object->GetGrounded();
|
||||
|
||||
// Update velocity based on acceleration
|
||||
XMVECTOR velocity = object->GetVelocity();
|
||||
velocity = velocity + object->GetAcceleration() * frameTime;
|
||||
object->SetVelocity(velocity);
|
||||
for (auto& chunk : m_terrainChunk)
|
||||
{
|
||||
if (m_Physics->IsColliding(object, chunk))
|
||||
{
|
||||
isGrounded = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isGrounded)
|
||||
{
|
||||
XMVECTOR force = XMVectorSet(forceX, forceY, forceZ, forceW);
|
||||
m_Physics->ApplyForce(object, force);
|
||||
|
||||
// Update velocity based on acceleration
|
||||
XMVECTOR velocity = object->GetVelocity();
|
||||
velocity = velocity + object->GetAcceleration() * frameTime;
|
||||
object->SetVelocity(velocity);
|
||||
|
||||
// Update position based on velocity
|
||||
XMVECTOR position = object->GetPosition();
|
||||
position = position + velocity * frameTime;
|
||||
object->SetPosition(position);
|
||||
|
||||
m_Physics->ApplyGravity(object, 1.0f, frameTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
object->SetVelocity(XMVectorZero());
|
||||
}
|
||||
|
||||
// Update position based on velocity
|
||||
XMVECTOR position = object->GetPosition();
|
||||
position = position + velocity * frameTime;
|
||||
object->SetPosition(position);
|
||||
|
||||
m_Physics->ApplyGravity(object, 1.0f, frameTime);
|
||||
|
||||
// Check if the object has fallen below a certain position
|
||||
if (XMVectorGetY(object->GetPosition()) < -30.0f)
|
||||
@ -1311,7 +1329,7 @@ void ApplicationClass::AddKobject(WCHAR* filepath)
|
||||
newObject->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename, textureFilename2, textureFilename3);
|
||||
newObject->SetMass(1.0f);
|
||||
|
||||
newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 0.0f, 0.0f));
|
||||
newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 10.0f, 0.0f));
|
||||
newObject->SetName(filename);
|
||||
|
||||
m_object.push_back(newObject);
|
||||
|
@ -3,11 +3,11 @@ Pos=60,60
|
||||
Size=400,400
|
||||
|
||||
[Window][Khaotic Engine]
|
||||
Pos=29,636
|
||||
Pos=27,634
|
||||
Size=694,210
|
||||
|
||||
[Window][Objects]
|
||||
Pos=34,270
|
||||
Pos=31,268
|
||||
Size=492,353
|
||||
|
||||
[Window][Terrain]
|
||||
|
@ -11,6 +11,7 @@ Object::Object() : ModelClass()
|
||||
m_velocity = XMVectorZero();
|
||||
m_acceleration = XMVectorZero();
|
||||
m_mass = NULL;
|
||||
m_isGrounded = false;
|
||||
}
|
||||
|
||||
Object::~Object()
|
||||
@ -195,4 +196,15 @@ void Object::SetMass(float mass)
|
||||
float Object::GetMass() const
|
||||
{
|
||||
return m_mass;
|
||||
}
|
||||
}
|
||||
|
||||
void Object::SetGrounded(bool isGrounded)
|
||||
{
|
||||
m_isGrounded = isGrounded;
|
||||
}
|
||||
|
||||
bool Object::GetGrounded() const
|
||||
{
|
||||
return m_isGrounded;
|
||||
}
|
||||
|
||||
|
@ -33,6 +33,8 @@ public:
|
||||
XMVECTOR GetAcceleration() const;
|
||||
void SetMass(float);
|
||||
float GetMass() const;
|
||||
void SetGrounded(bool);
|
||||
bool GetGrounded() const;
|
||||
|
||||
void UpdateWorldMatrix();
|
||||
void UpdateSRMatrix();
|
||||
@ -59,6 +61,7 @@ private:
|
||||
|
||||
XMVECTOR m_acceleration;
|
||||
float m_mass;
|
||||
bool m_isGrounded;
|
||||
|
||||
std::string m_name;
|
||||
};
|
||||
|
@ -72,4 +72,35 @@ void Physics::ApplyForce(Object* object, XMVECTOR force)
|
||||
|
||||
// Add the acceleration to the object's current acceleration
|
||||
object->SetAcceleration(object->GetAcceleration() + acceleration);
|
||||
}
|
||||
}
|
||||
|
||||
bool Physics::IsColliding(Object* object1, Object* object2)
|
||||
{
|
||||
|
||||
if (object1 == nullptr || object2 == nullptr) // Verify if the objects are not null
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the positions of the two objects
|
||||
XMVECTOR position1 = object1->GetPosition();
|
||||
XMVECTOR position2 = object2->GetPosition();
|
||||
|
||||
// Get the scale of the two objects (assuming the scale represents the size of the cube)
|
||||
XMVECTOR scale1 = object1->GetScale();
|
||||
XMVECTOR scale2 = object2->GetScale();
|
||||
|
||||
// Calculate the min and max coordinates of the two cubes
|
||||
XMVECTOR min1 = position1 - scale1 / 2.0f;
|
||||
XMVECTOR max1 = position1 + scale1 / 2.0f;
|
||||
XMVECTOR min2 = position2 - scale2 / 2.0f;
|
||||
XMVECTOR max2 = position2 + scale2 / 2.0f;
|
||||
|
||||
// Check if the two cubes overlap on all three axes
|
||||
bool overlapX = max1.m128_f32[0] > min2.m128_f32[0] && min1.m128_f32[0] < max2.m128_f32[0];
|
||||
bool overlapY = max1.m128_f32[1] > min2.m128_f32[1] && min1.m128_f32[1] < max2.m128_f32[1];
|
||||
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;
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ public:
|
||||
void SetGravity(XMVECTOR gravity); // Define the gravity value
|
||||
void ApplyGravity(Object*, float, float); // Apply gravity to an object
|
||||
void ApplyForce(Object*, XMVECTOR);
|
||||
bool IsColliding(Object*, Object*);
|
||||
|
||||
private:
|
||||
XMVECTOR m_gravity;
|
||||
|
Loading…
x
Reference in New Issue
Block a user