diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 1dcbc55..29abc16 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -742,9 +742,19 @@ bool ApplicationClass::Frame(InputClass* Input) // Reset acceleration for the new frame 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 - - float forceX = 0, forceY = 0, forceZ = 0, forceW = 0; if (keyLeft) @@ -759,44 +769,23 @@ bool ApplicationClass::Frame(InputClass* Input) { forceY = 40.0f; } - if (keyDown) + if (keyDown && !object->GetGrounded()) { 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); - 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()); - } + XMVECTOR force = XMVectorSet(forceX, forceY, forceZ, forceW); + m_Physics->AddForce(object, force); + // Update velocity based on acceleration + object->AddVelocity(frameTime); + // Update position based on velocity + XMVECTOR position = object->GetPosition(); + position = position + object->GetVelocity() * 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) @@ -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(); if (object->m_demoSpinning) @@ -965,7 +954,7 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z) } // Render terrain - for (auto chunk : m_terrainChunk) + for (auto& chunk : m_terrainChunk) { scaleMatrix = chunk->GetScaleMatrix(); @@ -1282,7 +1271,7 @@ void ApplicationClass::GenerateTerrain() scaleMatrix = XMMatrixScaling(scaleX, scaleY, scaleZ); // Set the file name of the model. - strcpy_s(modelFilename, "plane.txt"); + strcpy_s(modelFilename, "cube.txt"); strcpy_s(textureFilename, "stone01.tga"); strcpy_s(textureFilename2, "moss01.tga"); strcpy_s(textureFilename3, "alpha01.tga"); @@ -1297,7 +1286,9 @@ void ApplicationClass::GenerateTerrain() 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); @@ -1329,7 +1320,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, 10.0f, 0.0f)); + newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 50.0f, 0.0f)); newObject->SetName(filename); m_object.push_back(newObject); diff --git a/enginecustom/imgui.ini b/enginecustom/imgui.ini index b6c1376..d762592 100644 --- a/enginecustom/imgui.ini +++ b/enginecustom/imgui.ini @@ -3,14 +3,14 @@ Pos=60,60 Size=400,400 [Window][Khaotic Engine] -Pos=27,634 +Pos=-1,652 Size=694,210 [Window][Objects] -Pos=31,268 +Pos=-1,299 Size=492,353 [Window][Terrain] -Pos=755,731 +Pos=692,769 Size=418,94 diff --git a/enginecustom/object.cpp b/enginecustom/object.cpp index 87ede9c..3d19f39 100644 --- a/enginecustom/object.cpp +++ b/enginecustom/object.cpp @@ -173,6 +173,11 @@ void Object::SetVelocity(XMVECTOR velocity) m_velocity = velocity; } +void Object::AddVelocity(float frameTime) +{ + m_velocity += m_acceleration * frameTime; +} + XMVECTOR Object::GetVelocity() const { return m_velocity; diff --git a/enginecustom/object.h b/enginecustom/object.h index 2bf07e8..e488087 100644 --- a/enginecustom/object.h +++ b/enginecustom/object.h @@ -28,6 +28,7 @@ public: XMVECTOR GetScale(); void SetVelocity(XMVECTOR); + void AddVelocity(float); XMVECTOR GetVelocity() const; void SetAcceleration(XMVECTOR); XMVECTOR GetAcceleration() const; diff --git a/enginecustom/physics.cpp b/enginecustom/physics.cpp index 6a4d289..940b2e3 100644 --- a/enginecustom/physics.cpp +++ b/enginecustom/physics.cpp @@ -35,29 +35,32 @@ void Physics::ApplyGravity(Object* object, float dragValue, float frameTime) return; } - // Calculate the acceleration caused by gravity - XMVECTOR gravityAcceleration = m_gravity / object->GetMass(); + if (!object->GetGrounded()) // Verify if the object is grounded + { + // Calculate the acceleration caused by gravity + XMVECTOR gravityAcceleration = m_gravity / object->GetMass(); - // Add the gravity acceleration to the object's current acceleration - object->SetAcceleration(object->GetAcceleration() + gravityAcceleration); + // Add the gravity acceleration to the object's current acceleration + object->SetAcceleration(object->GetAcceleration() + gravityAcceleration); - // Calculate the acceleration caused by drag - XMVECTOR dragAcceleration = -object->GetVelocity() * dragValue / object->GetMass(); + // Calculate the acceleration caused by drag + XMVECTOR dragAcceleration = -object->GetVelocity() * dragValue / object->GetMass(); - // Add the drag acceleration to the object's current acceleration - object->SetAcceleration(object->GetAcceleration() + dragAcceleration); + // Add the drag acceleration to the object's current acceleration + object->SetAcceleration(object->GetAcceleration() + dragAcceleration); - // Get the object velocity - XMVECTOR velocity = object->GetVelocity(); + // Get the object velocity + XMVECTOR velocity = object->GetVelocity(); - // Update the velocity with the object's acceleration - velocity += object->GetAcceleration() * frameTime; + // Update the velocity with the object's acceleration + velocity += object->GetAcceleration() * frameTime; - // Set the new velocity - object->SetVelocity(velocity); + // Set the new 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 { @@ -77,30 +80,33 @@ void Physics::ApplyForce(Object* object, XMVECTOR force) 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 false; - } + return CubesOverlap(object1, object2); + } - // Get the positions of the two objects - XMVECTOR position1 = object1->GetPosition(); - XMVECTOR position2 = object2->GetPosition(); + // Add more collision checks for other types of objects here... - // 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; + return false; +} + +bool Physics::CubesOverlap(Object* cube1, Object* cube2) +{ + XMVECTOR position1 = cube1->GetPosition(); + XMVECTOR position2 = cube2->GetPosition(); + + XMVECTOR scale1 = cube1->GetScale(); + XMVECTOR scale2 = cube2->GetScale(); + + XMVECTOR min1 = position1 - scale1; + XMVECTOR max1 = position1 + scale1; + XMVECTOR min2 = position2 - scale2; + XMVECTOR max2 = position2 + scale2; + + return (min1.m128_f32[0] <= max2.m128_f32[0] && max1.m128_f32[0] >= min2.m128_f32[0] && + 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]); } diff --git a/enginecustom/physics.h b/enginecustom/physics.h index a23718b..cbf0df0 100644 --- a/enginecustom/physics.h +++ b/enginecustom/physics.h @@ -13,8 +13,9 @@ public: XMVECTOR GetGravity() const; // Get the gravity value void SetGravity(XMVECTOR gravity); // Define the gravity value void ApplyGravity(Object*, float, float); // Apply gravity to an object - void ApplyForce(Object*, XMVECTOR); + void AddForce(Object*, XMVECTOR); bool IsColliding(Object*, Object*); + bool CubesOverlap(Object*, Object*); private: XMVECTOR m_gravity;