Fixed Physics Bugs #271

Merged
direnbharwani merged 3 commits from SP3-2-Physics into main 2022-11-24 02:10:54 +08:00
5 changed files with 30 additions and 11 deletions

View File

@ -14,6 +14,7 @@
#include "SHCollisionListener.h"
// Project Headers
#include "ECS_Base/Managers/SHEntityManager.h"
#include "Physics/PhysicsObject/SHPhysicsObject.h"
#include "Physics/System/SHPhysicsSystem.h"
#include "Scene/SHSceneManager.h"
@ -83,13 +84,11 @@ namespace SHADE
{
const SHCollisionInfo& C_INFO = *eventIter;
const bool CLEAR_EVENT = C_INFO.GetCollisionState() == SHCollisionInfo::State::EXIT
|| C_INFO.GetCollisionState() == SHCollisionInfo::State::INVALID;
const bool CLEAR_EVENT = C_INFO.GetCollisionState() == SHCollisionInfo::State::EXIT || C_INFO.GetCollisionState() == SHCollisionInfo::State::INVALID;
const bool INVALID_ENTITY = !SHEntityManager::IsValidEID(C_INFO.GetEntityA()) || !SHEntityManager::IsValidEID(C_INFO.GetEntityB());
const bool INACTIVE_OBJECT = !SHSceneManager::CheckNodeAndComponentsActive<SHColliderComponent>(C_INFO.GetEntityA()) || !SHSceneManager::CheckNodeAndComponentsActive<SHColliderComponent>(C_INFO.GetEntityB());
const bool INACTIVE_OBJECT = !SHSceneManager::CheckNodeAndComponentsActive<SHColliderComponent>(C_INFO.GetEntityA())
|| !SHSceneManager::CheckNodeAndComponentsActive<SHColliderComponent>(C_INFO.GetEntityB());
if (CLEAR_EVENT || INACTIVE_OBJECT)
if (CLEAR_EVENT || INVALID_ENTITY || INACTIVE_OBJECT)
eventIter = container.erase(eventIter);
else
++eventIter;

View File

@ -380,6 +380,11 @@ namespace SHADE
}
auto* physicsObject = system->GetPhysicsObject(GetEID());
if (!physicsObject)
{
SHLOGV_ERROR("Unable to retrieve physics object of Entity {}", GetEID())
return;
}
physicsObject->GetRigidBody()->setLinearVelocity(newLinearVelocity);
}
@ -394,6 +399,11 @@ namespace SHADE
}
auto* physicsObject = system->GetPhysicsObject(GetEID());
if (!physicsObject)
{
SHLOGV_ERROR("Unable to retrieve physics object of Entity {}", GetEID())
return;
}
physicsObject->GetRigidBody()->setAngularVelocity(newAngularVelocity);
}

View File

@ -148,7 +148,14 @@ namespace SHADE
objectManager.AddRigidBody(EID);
if (SHComponentManager::HasComponent<SHColliderComponent>(EID))
{
objectManager.AddCollider(EID);
auto* COLLIDER = SHComponentManager::GetComponent<SHColliderComponent>(EID);
for (size_t i = 0; i < COLLIDER->GetCollisionShapes().size(); ++i)
objectManager.AddCollisionShape(EID, i);
}
};
////////////////////////////////
@ -156,6 +163,8 @@ namespace SHADE
// Destroy an existing world
if (worldState.world != nullptr)
{
objectManager.RemoveAllObjects();
objectManager.SetWorld(nullptr);

View File

@ -55,7 +55,7 @@ namespace SHADE
auto phySystem = SHSystemManager::GetSystem<SHPhysicsSystem>();
if (phySystem)
{
return phySystem->GetFixedUpdateRate();
return 1.0 / phySystem->GetFixedUpdateRate();
}
SHLOGV_WARNING("Failed to get fixed delta time. 0.0 returned instead.");

View File

@ -140,9 +140,10 @@ namespace SHADE
}
const double FIXED_DT = physicsSystem->fixedDT;
//to be remove =======================================================
// HACK: Clamp DT here to prevent a ridiculous amount of updates. This limits updates from large dt to 2.
// HACK: This should be done by the FRC and not here for predictable behaviour.
dt = std::clamp(dt, 0.0, 1.0 / 30.0);
//to be remove =======================================================
accumulatedTime += dt;
//testFunction();
@ -394,13 +395,13 @@ void testFunction()
if (rb)
{
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::W))
rb->AddForce(-SHVec3::UnitZ * forceModifier);
rb->AddForce(SHVec3::UnitZ * forceModifier);
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::A))
rb->AddForce(-SHVec3::UnitX * forceModifier);
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::S))
rb->AddForce(SHVec3::UnitZ * forceModifier);
rb->AddForce(-SHVec3::UnitZ * forceModifier);
if (SHInputManager::GetKey(SHInputManager::SH_KEYCODE::D))
rb->AddForce(SHVec3::UnitX * forceModifier);