Fixed collision listener bug & incorrect transform forward. Added collision tags to raycasts. #274

Merged
direnbharwani merged 5 commits from SP3-2-Physics into main 2022-11-24 15:15:33 +08:00
5 changed files with 46 additions and 29 deletions
Showing only changes of commit 6525a39df2 - Show all commits

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
@ -67,44 +68,46 @@ namespace SHADE
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;
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
( (
EntityID eid EntityID eid
, const SHRay& ray , const SHRay& ray
, float distance = std::numeric_limits<float>::infinity() , float distance = std::numeric_limits<float>::infinity()
) noexcept; ) noexcept;
SHPhysicsRaycastResult ColliderRaycast SHPhysicsRaycastResult ColliderRaycast
( (
EntityID eid EntityID eid
, int shapeIndex , int shapeIndex
, const SHRay& ray , const SHRay& ray
, float distance = std::numeric_limits<float>::infinity() , float distance = std::numeric_limits<float>::infinity()
) noexcept; ) noexcept;
SHPhysicsRaycastResult ColliderLinecast SHPhysicsRaycastResult ColliderLinecast
( (
EntityID eid EntityID eid
, const SHVec3& start , const SHVec3& start
, const SHVec3& end , const SHVec3& end
) noexcept; ) noexcept;
SHPhysicsRaycastResult ColliderLinecast SHPhysicsRaycastResult ColliderLinecast
( (
EntityID eid EntityID eid
, int shapeIndex , int shapeIndex
, const SHVec3& start , const SHVec3& start
, const SHVec3& end , const SHVec3& end
) noexcept; ) noexcept;
rp3d::decimal notifyRaycastHit(const rp3d::RaycastInfo& raycastInfo) override; rp3d::decimal notifyRaycastHit(const rp3d::RaycastInfo& raycastInfo) override;

View File

@ -258,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

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);
} }