Merge pull request #274 from SHADE-DP/SP3-2-Physics

Fixed collision listener bug & incorrect transform forward. Added collision tags to raycasts.
This commit is contained in:
XiaoQiDigipen 2022-11-24 15:15:33 +08:00 committed by GitHub
commit a182f9a8fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 86 additions and 43 deletions

View File

@ -74,7 +74,9 @@ namespace Sandbox
SHSystemManager::CreateSystem<SHScriptEngine>(); SHSystemManager::CreateSystem<SHScriptEngine>();
SHSystemManager::CreateSystem<SHTransformSystem>(); SHSystemManager::CreateSystem<SHTransformSystem>();
SHSystemManager::CreateSystem<SHPhysicsSystem>(); SHSystemManager::CreateSystem<SHPhysicsSystem>();
#ifndef _PUBLISH
SHSystemManager::CreateSystem<SHPhysicsDebugDrawSystem>(); SHSystemManager::CreateSystem<SHPhysicsDebugDrawSystem>();
#endif
SHSystemManager::CreateSystem<SHAudioSystem>(); SHSystemManager::CreateSystem<SHAudioSystem>();
SHSystemManager::CreateSystem<SHCameraSystem>(); SHSystemManager::CreateSystem<SHCameraSystem>();
@ -114,7 +116,9 @@ namespace Sandbox
SHSystemManager::RegisterRoutine<SHPhysicsSystem, SHPhysicsSystem::PhysicsFixedUpdate>(); SHSystemManager::RegisterRoutine<SHPhysicsSystem, SHPhysicsSystem::PhysicsFixedUpdate>();
SHSystemManager::RegisterRoutine<SHPhysicsSystem, SHPhysicsSystem::PhysicsPostUpdate>(); SHSystemManager::RegisterRoutine<SHPhysicsSystem, SHPhysicsSystem::PhysicsPostUpdate>();
#ifndef _PUBLISH
SHSystemManager::RegisterRoutine<SHPhysicsDebugDrawSystem, SHPhysicsDebugDrawSystem::PhysicsDebugDrawRoutine>(); SHSystemManager::RegisterRoutine<SHPhysicsDebugDrawSystem, SHPhysicsDebugDrawSystem::PhysicsDebugDrawRoutine>();
#endif
SHSystemManager::RegisterRoutine<SHTransformSystem, SHTransformSystem::TransformPostPhysicsUpdate>(); SHSystemManager::RegisterRoutine<SHTransformSystem, SHTransformSystem::TransformPostPhysicsUpdate>();
SHSystemManager::RegisterRoutine<SHDebugDrawSystem, SHDebugDrawSystem::ProcessPointsRoutine>(); SHSystemManager::RegisterRoutine<SHDebugDrawSystem, SHDebugDrawSystem::ProcessPointsRoutine>();
@ -189,6 +193,13 @@ namespace Sandbox
SHSystemManager::RunRoutines(false, SHFrameRateController::GetRawDeltaTime()); SHSystemManager::RunRoutines(false, SHFrameRateController::GetRawDeltaTime());
#endif #endif
// TODO: Move into an Editor menu // TODO: Move into an Editor menu
static bool drawContacts = false;
if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::F9))
{
drawContacts = !drawContacts;
SHSystemManager::GetSystem<SHPhysicsDebugDrawSystem>()->SetDebugDrawFlag(SHPhysicsDebugDrawSystem::DebugDrawFlags::CONTACT_POINTS, drawContacts);
SHSystemManager::GetSystem<SHPhysicsDebugDrawSystem>()->SetDebugDrawFlag(SHPhysicsDebugDrawSystem::DebugDrawFlags::CONTACT_NORMALS, drawContacts);
}
static bool drawColliders = false; static bool drawColliders = false;
if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::F10)) if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::F10))
{ {
@ -201,13 +212,7 @@ namespace Sandbox
drawRays = !drawRays; drawRays = !drawRays;
SHSystemManager::GetSystem<SHPhysicsDebugDrawSystem>()->SetDebugDrawFlag(SHPhysicsDebugDrawSystem::DebugDrawFlags::RAYCASTS, drawRays); SHSystemManager::GetSystem<SHPhysicsDebugDrawSystem>()->SetDebugDrawFlag(SHPhysicsDebugDrawSystem::DebugDrawFlags::RAYCASTS, drawRays);
} }
static bool drawContacts = false;
if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::F9))
{
drawContacts = !drawContacts;
SHSystemManager::GetSystem<SHPhysicsDebugDrawSystem>()->SetDebugDrawFlag(SHPhysicsDebugDrawSystem::DebugDrawFlags::CONTACT_POINTS, drawContacts);
SHSystemManager::GetSystem<SHPhysicsDebugDrawSystem>()->SetDebugDrawFlag(SHPhysicsDebugDrawSystem::DebugDrawFlags::CONTACT_NORMALS, drawContacts);
}
} }
// Finish all graphics jobs first // Finish all graphics jobs first
graphicsSystem->AwaitGraphicsExecution(); graphicsSystem->AwaitGraphicsExecution();

View File

@ -84,13 +84,26 @@ namespace SHADE
{ {
const SHCollisionInfo& C_INFO = *eventIter; const SHCollisionInfo& C_INFO = *eventIter;
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 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()); if (INVALID_ENTITY)
{
if (CLEAR_EVENT || INVALID_ENTITY || INACTIVE_OBJECT)
eventIter = container.erase(eventIter); eventIter = container.erase(eventIter);
continue;
}
else else
{
const bool CLEAR_EVENT = C_INFO.GetCollisionState() == SHCollisionInfo::State::EXIT || C_INFO.GetCollisionState() == SHCollisionInfo::State::INVALID;
const bool INACTIVE_OBJECT = !SHSceneManager::CheckNodeAndComponentsActive<SHColliderComponent>(C_INFO.GetEntityA())
|| !SHSceneManager::CheckNodeAndComponentsActive<SHColliderComponent>(C_INFO.GetEntityB());
if (CLEAR_EVENT || INACTIVE_OBJECT)
{
eventIter = container.erase(eventIter);
continue;
}
}
++eventIter; ++eventIter;
} }
}; };

View File

@ -63,7 +63,7 @@ namespace SHADE
raycasts.clear(); raycasts.clear();
} }
SHPhysicsRaycastResult SHPhysicsRaycaster::Raycast(const SHRay& ray, float distance) noexcept SHPhysicsRaycastResult SHPhysicsRaycaster::Raycast(const SHRay& ray, float distance, const SHCollisionTag& collisionTag) noexcept
{ {
// Reset temp // Reset temp
temp = SHPhysicsRaycastResult{}; temp = SHPhysicsRaycastResult{};
@ -78,13 +78,13 @@ namespace SHADE
// If distance in infinity, cast to the default max distance of 2 km. // If distance in infinity, cast to the default max distance of 2 km.
if (distance == std::numeric_limits<float>::infinity()) if (distance == std::numeric_limits<float>::infinity())
{ {
world->raycast(ray, this); world->raycast(ray, this, collisionTag);
} }
else else
{ {
const SHVec3 END_POINT = ray.position + ray.direction * distance; const SHVec3 END_POINT = ray.position + ray.direction * distance;
const rp3d::Ray RP3D_RAY{ ray.position, END_POINT }; const rp3d::Ray RP3D_RAY{ ray.position, END_POINT };
world->raycast(RP3D_RAY, this); world->raycast(RP3D_RAY, this, collisionTag);
} }
// If a hit was found, populate temp info for return. // If a hit was found, populate temp info for return.
@ -98,7 +98,7 @@ namespace SHADE
return temp; return temp;
} }
SHPhysicsRaycastResult SHPhysicsRaycaster::Linecast(const SHVec3& start, const SHVec3& end) noexcept SHPhysicsRaycastResult SHPhysicsRaycaster::Linecast(const SHVec3& start, const SHVec3& end, const SHCollisionTag& collisionTag) noexcept
{ {
temp = SHPhysicsRaycastResult{}; temp = SHPhysicsRaycastResult{};
temp.distance = SHVec3::Distance(start, end); temp.distance = SHVec3::Distance(start, end);
@ -110,7 +110,7 @@ namespace SHADE
} }
const rp3d::Ray RP3D_RAY{ start, end }; const rp3d::Ray RP3D_RAY{ start, end };
world->raycast(RP3D_RAY, this); world->raycast(RP3D_RAY, this, collisionTag);
if (temp.hit) if (temp.hit)
{ {

View File

@ -19,6 +19,7 @@
#include "Physics/PhysicsObject/SHPhysicsObjectManager.h" #include "Physics/PhysicsObject/SHPhysicsObjectManager.h"
#include "Physics/SHPhysicsWorld.h" #include "Physics/SHPhysicsWorld.h"
#include "SH_API.h" #include "SH_API.h"
#include "SHCollisionTags.h"
#include "SHPhysicsRaycastResult.h" #include "SHPhysicsRaycastResult.h"
namespace SHADE namespace SHADE
@ -69,12 +70,14 @@ namespace SHADE
( (
const SHRay& ray const SHRay& ray
, float distance = std::numeric_limits<float>::infinity() , float distance = std::numeric_limits<float>::infinity()
, const SHCollisionTag& collisionTag = SHCollisionTag{}
) noexcept; ) noexcept;
SHPhysicsRaycastResult Linecast SHPhysicsRaycastResult Linecast
( (
const SHVec3& start const SHVec3& start
, const SHVec3& end , const SHVec3& end
, const SHCollisionTag& collisionTag = SHCollisionTag{}
) noexcept; ) noexcept;
SHPhysicsRaycastResult ColliderRaycast SHPhysicsRaycastResult ColliderRaycast

View File

@ -206,7 +206,7 @@ namespace SHADE
} }
// Set the half extents relative to world scale // Set the half extents relative to world scale
const SHVec3 WORLD_EXTENTS = correctedHalfExtents * COLLIDER->GetScale() * 0.5f; const SHVec3 WORLD_EXTENTS = correctedHalfExtents * COLLIDER->GetScale();
if (type != Type::BOX) if (type != Type::BOX)
{ {

View File

@ -173,7 +173,11 @@ namespace SHADE
} }
worldState.CreateWorld(factory); worldState.CreateWorld(factory);
#ifdef _PUBLISH
worldState.world->setIsDebugRenderingEnabled(false);
#else
worldState.world->setIsDebugRenderingEnabled(true); worldState.world->setIsDebugRenderingEnabled(true);
#endif
// Link Collision Listener & Raycaster // Link Collision Listener & Raycaster
collisionListener.BindToWorld(worldState.world); collisionListener.BindToWorld(worldState.world);
@ -254,14 +258,14 @@ namespace SHADE
} }
} }
SHPhysicsRaycastResult SHPhysicsSystem::Raycast(const SHRay& ray, float distance) noexcept SHPhysicsRaycastResult SHPhysicsSystem::Raycast(const SHRay& ray, float distance, const SHCollisionTag& collisionTag) noexcept
{ {
return raycaster.Raycast(ray, distance); return raycaster.Raycast(ray, distance, collisionTag);
} }
SHPhysicsRaycastResult SHPhysicsSystem::Linecast(const SHVec3& start, const SHVec3& end) noexcept SHPhysicsRaycastResult SHPhysicsSystem::Linecast(const SHVec3& start, const SHVec3& end, const SHCollisionTag& collisionTag) noexcept
{ {
return raycaster.Linecast(start, end); return raycaster.Linecast(start, end, collisionTag);
} }
SHPhysicsRaycastResult SHPhysicsSystem::ColliderRaycast(EntityID eid, const SHRay& ray, float distance) noexcept SHPhysicsRaycastResult SHPhysicsSystem::ColliderRaycast(EntityID eid, const SHRay& ray, float distance) noexcept
@ -438,7 +442,11 @@ namespace SHADE
return onPlayEvent->handle; return onPlayEvent->handle;
worldState.CreateWorld(factory); worldState.CreateWorld(factory);
#ifdef _PUBLISH
worldState.world->setIsDebugRenderingEnabled(false);
#else
worldState.world->setIsDebugRenderingEnabled(true); worldState.world->setIsDebugRenderingEnabled(true);
#endif
// Link Collision Listener & Raycaster // Link Collision Listener & Raycaster
collisionListener.BindToWorld(worldState.world); collisionListener.BindToWorld(worldState.world);

View File

@ -85,24 +85,28 @@ namespace SHADE
* @brief Casts a ray into the world. * @brief Casts a ray into the world.
* @param ray The ray to cast. * @param ray The ray to cast.
* @param distance The distance to cast the ray. Defaults to infinity. * @param distance The distance to cast the ray. Defaults to infinity.
* @param collisionTag The collision tag to use for filtering the raycast.
* @return The result of the raycast. * @return The result of the raycast.
*/ */
SHPhysicsRaycastResult Raycast SHPhysicsRaycastResult Raycast
( (
const SHRay& ray const SHRay& ray
, float distance = std::numeric_limits<float>::infinity() , float distance = std::numeric_limits<float>::infinity()
, const SHCollisionTag& collisionTag = SHCollisionTag{}
) noexcept; ) noexcept;
/** /**
* @brief Casts a bounded ray into the world. * @brief Casts a bounded ray into the world.
* @param start The starting point of the ray. * @param start The starting point of the ray.
* @param end The end point of the ray. * @param end The end point of the ray.
* @param collisionTag The collision tag to use for filtering the bounded raycast.
* @return The result of the raycast. * @return The result of the raycast.
*/ */
SHPhysicsRaycastResult Linecast SHPhysicsRaycastResult Linecast
( (
const SHVec3& start const SHVec3& start
, const SHVec3& end , const SHVec3& end
, const SHCollisionTag& collisionTag = SHCollisionTag{}
) noexcept; ) noexcept;
/** /**

View File

@ -20,6 +20,7 @@
#include "Scripting/SHScriptEngine.h" #include "Scripting/SHScriptEngine.h"
#include "Input/SHInputManager.h" #include "Input/SHInputManager.h"
#include "Physics/Collision/SHCollisionTagMatrix.h"
/*-------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------*/
/* Local Functions */ /* Local Functions */
@ -403,4 +404,13 @@ void testFunction()
rb->AddForce(SHVec3::UnitX * forceModifier); rb->AddForce(SHVec3::UnitX * forceModifier);
} }
} }
// Cast rays
auto* tag = SHCollisionTagMatrix::GetTag(1);
tag->SetLayerState(SHCollisionTag::Layer::_1, false);
tag->SetLayerState(SHCollisionTag::Layer::_2, true);
SHRay ray { SHVec3{3.0f, 3.5f, 0.0f}, -SHVec3::UnitX };
auto* physicsSystem = SHSystemManager::GetSystem<SHPhysicsSystem>();
physicsSystem->Raycast(ray, std::numeric_limits<float>::infinity(), *tag);
} }

View File

@ -107,7 +107,7 @@ namespace SHADE
Vector3 Transform::Forward::get() Vector3 Transform::Forward::get()
{ {
const SHVec3 DIRECTION = SHVec3::Rotate(SHVec3::UnitZ, Convert::ToNative(GlobalRotation)); const SHVec3 DIRECTION = SHVec3::Rotate(-SHVec3::UnitZ, Convert::ToNative(GlobalRotation));
return Convert::ToCLI(DIRECTION); return Convert::ToCLI(DIRECTION);
} }