Physic rebuild start
This commit is contained in:
@@ -32,6 +32,13 @@ ApplicationClass::ApplicationClass() : m_ShouldQuit(false)
|
||||
|
||||
ApplicationClass::~ApplicationClass()
|
||||
{
|
||||
m_ShouldQuit = true;
|
||||
|
||||
// wait for the physics thread to finish
|
||||
while (m_Physics)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -400,6 +407,11 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
return false;
|
||||
}
|
||||
|
||||
m_Physics = new Physics;
|
||||
|
||||
std::thread physicsThread(&ApplicationClass::PhysicsThreadFunction, this);
|
||||
physicsThread.detach();
|
||||
|
||||
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
@@ -409,7 +421,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
}
|
||||
Logger::Get().Log("Application class initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
|
||||
|
||||
m_Physics = new Physics;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -637,7 +649,7 @@ void ApplicationClass::Shutdown()
|
||||
bool ApplicationClass::Frame(InputClass* Input)
|
||||
{
|
||||
int mouseX, mouseY, currentMouseX, currentMouseY;
|
||||
bool result, leftMouseDown, rightMouseDown, keyLeft, keyRight, keyUp, keyDown, buttonQ, buttonD, buttonZ, buttonS, buttonA, buttonE, scrollUp, scrollDown;
|
||||
bool result, leftMouseDown, rightMouseDown, buttonQ, buttonD, buttonZ, buttonS, buttonA, buttonE, scrollUp, scrollDown;
|
||||
float rotationY, rotationX, positionX, positionY, positionZ;
|
||||
static float textureTranslation = 0.0f;
|
||||
|
||||
@@ -756,104 +768,11 @@ bool ApplicationClass::Frame(InputClass* Input)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
//// Update the x position variable each frame.
|
||||
//x -= 0.0174532925f * 0.6f;
|
||||
|
||||
//y -= 0.0174532925f * 0.2f;
|
||||
|
||||
//// Update the z position variable each frame.
|
||||
//z -= 0.0174532925f * 0.2f;
|
||||
|
||||
keyLeft = Input->IsLeftArrowPressed();
|
||||
keyRight = Input->IsRightArrowPressed();
|
||||
keyUp = Input->IsUpArrowPressed();
|
||||
keyDown = Input->IsDownArrowPressed();
|
||||
|
||||
for (auto& object : m_object)
|
||||
{
|
||||
if (object != nullptr) // Check if the object is not null
|
||||
{
|
||||
// Reset acceleration for the new frame
|
||||
object->SetAcceleration(XMVectorZero());
|
||||
|
||||
object->SetGrounded(false);
|
||||
|
||||
for (auto& chunk : m_terrainChunk)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& object2 : m_object)
|
||||
{
|
||||
if (object->GetId() != object2->GetId() && object2 != nullptr)
|
||||
{
|
||||
if (m_Physics->IsColliding(object, object2))
|
||||
{
|
||||
// Stop movement in any direction
|
||||
object->SetVelocity(XMVectorZero());
|
||||
object->SetAcceleration(XMVectorZero());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Apply forces
|
||||
float forceX = 0, forceY = 0, forceZ = 0, forceW = 0;
|
||||
|
||||
if (keyLeft)
|
||||
{
|
||||
forceX = -10.0f;
|
||||
}
|
||||
if (keyRight)
|
||||
{
|
||||
forceX = 10.0f;
|
||||
}
|
||||
if (keyUp)
|
||||
{
|
||||
forceY = 40.0f;
|
||||
}
|
||||
if (keyDown && !object->IsGrounded())
|
||||
{
|
||||
forceY = -40.0f;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// Check if the object has fallen below a certain position
|
||||
if (XMVectorGetY(object->GetPosition()) < -30.0f)
|
||||
{
|
||||
XMVECTOR currentPosition = object->GetPosition(); // Obtain the current position of the object
|
||||
object->SetPosition(XMVectorSetY(currentPosition, 50.0f)); // Define the new position of the object
|
||||
}
|
||||
|
||||
object->m_previousPosition = object->GetPosition();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
m_Inputs.m_KeyLeft = Input->IsLeftArrowPressed();
|
||||
m_Inputs.m_KeyRight = Input->IsRightArrowPressed();
|
||||
m_Inputs.m_KeyUp = Input->IsUpArrowPressed();
|
||||
m_Inputs.m_KeyDown = Input->IsDownArrowPressed();
|
||||
|
||||
// Render the scene to a render texture.
|
||||
result = RenderSceneToTexture(rotation);
|
||||
@@ -1543,6 +1462,7 @@ void ApplicationClass::AddKobject(WCHAR* filepath)
|
||||
newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 50.0f, 0.0f));
|
||||
newObject->SetName(filename);
|
||||
newObject->SetId(m_ObjectId);
|
||||
newObject->SetType(ObjectType::Cube);
|
||||
|
||||
m_ObjectId++;
|
||||
|
||||
@@ -1957,4 +1877,118 @@ void ApplicationClass::ConstructFrustum()
|
||||
m_Camera->GetViewMatrix(viewMatrix);
|
||||
|
||||
m_FrustumCulling.ConstructFrustum(SCREEN_DEPTH, projectionMatrix, viewMatrix);
|
||||
}
|
||||
}
|
||||
|
||||
bool ApplicationClass::RenderPhysics(bool keyLeft, bool keyRight, bool keyUp, bool keyDown, float deltaTime) {
|
||||
|
||||
for (auto& object : m_object)
|
||||
{
|
||||
if (object == nullptr)
|
||||
{
|
||||
Logger::Get().Log("Object is null", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Reset acceleration for the new frame
|
||||
object->SetAcceleration(XMVectorZero());
|
||||
object->SetGrounded(false);
|
||||
|
||||
for (auto& chunk : m_terrainChunk)
|
||||
{
|
||||
if (!m_Physics->IsColliding(object, chunk))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Stop vertical movement, like gravity
|
||||
object->SetVelocity(XMVectorSetY(object->GetVelocity(), 0.0f));
|
||||
object->SetAcceleration(XMVectorSetY(object->GetAcceleration(), 0.0f));
|
||||
object->SetGrounded(true);
|
||||
}
|
||||
|
||||
for (auto& object2 : m_object)
|
||||
{
|
||||
if (object->GetId() == object2->GetId())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!m_Physics->IsColliding(object, object2))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Stop movement in any direction
|
||||
object->SetVelocity(XMVectorZero());
|
||||
object->SetAcceleration(XMVectorZero());
|
||||
}
|
||||
|
||||
// Apply forces
|
||||
float forceX = 0, forceY = 0, forceZ = 0, forceW = 0;
|
||||
|
||||
if (keyLeft)
|
||||
{
|
||||
forceX = -10.0f;
|
||||
}
|
||||
if (keyRight)
|
||||
{
|
||||
forceX = 10.0f;
|
||||
}
|
||||
if (keyUp)
|
||||
{
|
||||
forceY = 40.0f;
|
||||
}
|
||||
if (keyDown && !object->IsGrounded())
|
||||
{
|
||||
forceY = -40.0f;
|
||||
}
|
||||
|
||||
XMVECTOR force = XMVectorSet(forceX, forceY, forceZ, forceW);
|
||||
m_Physics->AddForce(object, force);
|
||||
|
||||
// Update velocity based on acceleration
|
||||
object->AddVelocity(deltaTime);
|
||||
|
||||
// Update position based on velocity
|
||||
XMVECTOR position = object->GetPosition();
|
||||
position = position + object->GetVelocity() * deltaTime;
|
||||
object->SetPosition(position);
|
||||
|
||||
m_Physics->ApplyGravity(object, deltaTime);
|
||||
|
||||
// Check if the object has fallen below a certain position
|
||||
if (XMVectorGetY(object->GetPosition()) < -30.0f)
|
||||
{
|
||||
XMVECTOR currentPosition = object->GetPosition(); // Obtain the current position of the object
|
||||
object->SetPosition(XMVectorSetY(currentPosition, 50.0f)); // Define the new position of the object
|
||||
}
|
||||
|
||||
object->m_previousPosition = object->GetPosition();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void ApplicationClass::PhysicsThreadFunction()
|
||||
{
|
||||
auto lastTime = std::chrono::high_resolution_clock::now();
|
||||
|
||||
while (!m_ShouldQuit)
|
||||
{
|
||||
auto currentTime = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<float> elapsedTime = currentTime - lastTime;
|
||||
float deltaTime = elapsedTime.count();
|
||||
lastTime = currentTime;
|
||||
|
||||
bool result = RenderPhysics(m_Inputs.m_KeyLeft, m_Inputs.m_KeyRight, m_Inputs.m_KeyUp, m_Inputs.m_KeyDown, deltaTime);
|
||||
if (!result)
|
||||
{
|
||||
Logger::Get().Log("Could not render the physics scene", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return;
|
||||
}
|
||||
|
||||
// Attendre 20 millisecondes (50 fois par seconde)
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user