@@ -34,11 +34,6 @@ ApplicationClass::~ApplicationClass()
{
m_ShouldQuit = true ;
// wait for the physics thread to finish
while ( m_Physics )
{
std : : this_thread : : sleep_for ( std : : chrono : : milliseconds ( 100 ) ) ;
}
}
@@ -409,9 +404,6 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
m_Physics = new Physics ;
std : : thread physicsThread ( & ApplicationClass : : PhysicsThreadFunction , this ) ;
physicsThread . detach ( ) ;
}
catch ( const std : : exception & e )
@@ -774,6 +766,8 @@ bool ApplicationClass::Frame(InputClass* Input)
m_Inputs . m_KeyUp = Input - > IsUpArrowPressed ( ) ;
m_Inputs . m_KeyDown = Input - > IsDownArrowPressed ( ) ;
RenderPhysics ( m_Inputs . m_KeyLeft , m_Inputs . m_KeyRight , m_Inputs . m_KeyUp , m_Inputs . m_KeyDown , frameTime ) ;
// Render the scene to a render texture.
result = RenderSceneToTexture ( rotation ) ;
if ( ! result )
@@ -1397,6 +1391,8 @@ void ApplicationClass::GenerateTerrain()
newTerrain - > SetName ( filenameWithoutExtension ) ;
newTerrain - > SetType ( ObjectType : : Cube ) ;
m_terrainChunk . push_back ( newTerrain ) ;
}
@@ -1462,7 +1458,6 @@ 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 + + ;
@@ -1880,115 +1875,105 @@ void ApplicationClass::ConstructFrustum()
}
bool ApplicationClass : : RenderPhysics ( bool keyLeft , bool keyRight , bool keyUp , bool keyDown , float deltaTime ) {
const float maxSpeed = 50.0f ; // Limite de vitesse maximale
const int subSteps = 10 ; // Nombre de sous-<2D> tapes pour la simulation
const float subDeltaTime = deltaTime / subSteps ;
for ( auto & object : m_object )
{
if ( object = = nullptr )
{
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 ) ;
if ( ! object - > IsPhysicsEnabled ( ) ) {
continue ;
}
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 ( ) ) ;
for ( int step = 0 ; step < subSteps ; + + step ) {
// Reset acceleration for the new frame
object - > SetAcceleration ( XMVectorZero ( ) ) ;
object - > SetGrounded ( false ) ;
for ( auto & chunk : m_terrainChunk ) {
if ( ! m_Physics - > IsColliding ( object , chunk ) ) {
continue ;
}
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 ;
}
object - > SetVelocity ( XMVectorZero ( ) ) ;
object - > SetAcceleration ( XMVectorZero ( ) ) ;
}
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 ) ;
object - > AddVelocity ( subDeltaTime ) ;
XMVECTOR velocity = object - > GetVelocity ( ) ;
float speed = XMVectorGetX ( XMVector3Length ( velocity ) ) ;
if ( speed > maxSpeed ) {
velocity = XMVectorScale ( velocity , maxSpeed / speed ) ;
object - > SetVelocity ( velocity ) ;
}
XMVECTOR position = object - > GetPosition ( ) ;
position = position + object - > GetVelocity ( ) * subDeltaTime ;
object - > SetPosition ( position ) ;
m_Physics - > ApplyGravity ( object , subDeltaTime ) ;
if ( XMVectorGetY ( object - > GetPosition ( ) ) < - 30.0f ) {
XMVECTOR currentPosition = object - > GetPosition ( ) ;
object - > SetPosition ( XMVectorSetY ( currentPosition , 50.0f ) ) ;
}
object - > m_previousPosition = object - > GetPosition ( ) ;
}
// 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 ) ;
bool result = RenderPhysics ( m_Inputs . m_KeyLeft , m_Inputs . m_KeyRight , m_Inputs . m_KeyUp , m_Inputs . m_KeyDown , m_Timer - > GetTime ( ) ) ;
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 ) ) ;
}
}