From fa83e2bc4c3b1edbe6442f385d9f48b09f95e719 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Wed, 23 Nov 2022 01:01:12 +0800 Subject: [PATCH 1/2] Modified Shape.Raycast functions to return RaycastResult --- SHADE_Engine/src/Math/Geometry/SHBox.cpp | 15 ++++++++++++--- SHADE_Engine/src/Math/Geometry/SHBox.h | 4 ++-- SHADE_Engine/src/Math/Geometry/SHShape.h | 4 ++-- SHADE_Engine/src/Math/Geometry/SHSphere.cpp | 15 ++++++++++++--- SHADE_Engine/src/Math/Geometry/SHSphere.h | 10 +++++----- SHADE_Managed/src/Components/Collider.cxx | 4 ++-- 6 files changed, 35 insertions(+), 17 deletions(-) diff --git a/SHADE_Engine/src/Math/Geometry/SHBox.cpp b/SHADE_Engine/src/Math/Geometry/SHBox.cpp index a52cf0d2..7261749b 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBox.cpp +++ b/SHADE_Engine/src/Math/Geometry/SHBox.cpp @@ -178,14 +178,23 @@ namespace SHADE /* Public Function Member Definitions */ /*-----------------------------------------------------------------------------------*/ - bool SHBox::TestPoint(const SHVec3& point) noexcept + bool SHBox::TestPoint(const SHVec3& point) const noexcept { return BoundingBox::Contains(point); } - bool SHBox::Raycast(const SHRay& ray, float& distance) noexcept + SHRaycastResult SHBox::Raycast(const SHRay& ray) const noexcept { - return BoundingBox::Intersects(ray.position, ray.direction, distance); + SHRaycastResult result; + + result.hit = Intersects(ray.position, ray.direction, result.distance); + if (result.hit) + { + result.position = ray.position + ray.direction * result.distance; + result.angle = SHVec3::Angle(ray.position, result.position); + } + + return result; } bool SHBox::Contains(const SHBox& rhs) const noexcept diff --git a/SHADE_Engine/src/Math/Geometry/SHBox.h b/SHADE_Engine/src/Math/Geometry/SHBox.h index 19c80bd2..a0ca9458 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBox.h +++ b/SHADE_Engine/src/Math/Geometry/SHBox.h @@ -76,8 +76,8 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] bool TestPoint (const SHVec3& point) noexcept override; - [[nodiscard]] bool Raycast (const SHRay& ray, float& distance) noexcept override; + [[nodiscard]] bool TestPoint (const SHVec3& point) const noexcept override; + [[nodiscard]] SHRaycastResult Raycast(const SHRay& ray) const noexcept override; [[nodiscard]] bool Contains (const SHBox& rhs) const noexcept; [[nodiscard]] float Volume () const noexcept; diff --git a/SHADE_Engine/src/Math/Geometry/SHShape.h b/SHADE_Engine/src/Math/Geometry/SHShape.h index 62198897..812cb169 100644 --- a/SHADE_Engine/src/Math/Geometry/SHShape.h +++ b/SHADE_Engine/src/Math/Geometry/SHShape.h @@ -69,8 +69,8 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] virtual bool TestPoint (const SHVec3& point) noexcept = 0; - [[nodiscard]] virtual bool Raycast (const SHRay& ray, float& distance) noexcept = 0; + [[nodiscard]] virtual bool TestPoint (const SHVec3& point) const noexcept = 0; + [[nodiscard]] virtual SHRaycastResult Raycast (const SHRay& ray) const noexcept = 0; protected: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/Geometry/SHSphere.cpp b/SHADE_Engine/src/Math/Geometry/SHSphere.cpp index d310e30e..54935251 100644 --- a/SHADE_Engine/src/Math/Geometry/SHSphere.cpp +++ b/SHADE_Engine/src/Math/Geometry/SHSphere.cpp @@ -138,14 +138,23 @@ namespace SHADE /* Public Function Member Definitions */ /*-----------------------------------------------------------------------------------*/ - bool SHSphere::TestPoint(const SHVec3& point) noexcept + bool SHSphere::TestPoint(const SHVec3& point) const noexcept { return BoundingSphere::Contains(point); } - bool SHSphere::Raycast(const SHRay& ray, float& distance) noexcept + SHRaycastResult SHSphere::Raycast(const SHRay& ray) const noexcept { - return Intersects(ray.position, ray.direction, distance); + SHRaycastResult result; + + result.hit = Intersects(ray.position, ray.direction, result.distance); + if (result.hit) + { + result.position = ray.position + ray.direction * result.distance; + result.angle = SHVec3::Angle(ray.position, result.position); + } + + return result; } bool SHSphere::Contains(const SHSphere& rhs) const noexcept diff --git a/SHADE_Engine/src/Math/Geometry/SHSphere.h b/SHADE_Engine/src/Math/Geometry/SHSphere.h index c13076aa..e056f21a 100644 --- a/SHADE_Engine/src/Math/Geometry/SHSphere.h +++ b/SHADE_Engine/src/Math/Geometry/SHSphere.h @@ -64,12 +64,12 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] bool TestPoint (const SHVec3& point) noexcept override; - [[nodiscard]] bool Raycast (const SHRay& ray, float& distance) noexcept override; + [[nodiscard]] bool TestPoint (const SHVec3& point) const noexcept override; + [[nodiscard]] SHRaycastResult Raycast(const SHRay& ray) const noexcept override; - [[nodiscard]] bool Contains (const SHSphere& rhs) const noexcept; - [[nodiscard]] float Volume () const noexcept; - [[nodiscard]] float SurfaceArea () const noexcept; + [[nodiscard]] bool Contains (const SHSphere& rhs) const noexcept; + [[nodiscard]] float Volume () const noexcept; + [[nodiscard]] float SurfaceArea () const noexcept; /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Components/Collider.cxx b/SHADE_Managed/src/Components/Collider.cxx index 41910d66..b5f1444f 100644 --- a/SHADE_Managed/src/Components/Collider.cxx +++ b/SHADE_Managed/src/Components/Collider.cxx @@ -168,7 +168,7 @@ namespace SHADE } bool BoxCollider::Raycast(Ray ray, float maxDistance) { - return getNativeCollisionShape().Raycast(Convert::ToNative(ray), maxDistance); + return getNativeCollisionShape().Raycast(Convert::ToNative(ray)); } /*---------------------------------------------------------------------------------*/ @@ -200,7 +200,7 @@ namespace SHADE } bool SphereCollider::Raycast(Ray ray, float maxDistance) { - return getNativeCollisionShape().Raycast(Convert::ToNative(ray), maxDistance); + return getNativeCollisionShape().Raycast(Convert::ToNative(ray)); } /*---------------------------------------------------------------------------------*/ -- 2.40.1 From 2523613732b07a9c8b428969e57b5d7df1525172 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Wed, 23 Nov 2022 01:01:29 +0800 Subject: [PATCH 2/2] Fixed fatal bug that prevent scene changing while the editor is enabled --- .../src/Physics/PhysicsObject/SHPhysicsObjectManager.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SHADE_Engine/src/Physics/PhysicsObject/SHPhysicsObjectManager.cpp b/SHADE_Engine/src/Physics/PhysicsObject/SHPhysicsObjectManager.cpp index 389983c3..bd68de82 100644 --- a/SHADE_Engine/src/Physics/PhysicsObject/SHPhysicsObjectManager.cpp +++ b/SHADE_Engine/src/Physics/PhysicsObject/SHPhysicsObjectManager.cpp @@ -265,11 +265,11 @@ namespace SHADE return; } - const int NUM_SHAPES = static_cast(componentGroup.colliderComponent->GetCollisionShapes().size()); - for (int i = 0; i < NUM_SHAPES; ++i) - physicsObject->AddCollisionShape(i); + //const int NUM_SHAPES = static_cast(componentGroup.colliderComponent->GetCollisionShapes().size()); + //for (int i = 0; i < NUM_SHAPES; ++i) + // physicsObject->AddCollisionShape(i); - physicsObject->SyncColliders(*componentGroup.colliderComponent); + //physicsObject->SyncColliders(*componentGroup.colliderComponent); } void SHPhysicsObjectManager::removeRigidBody(const QueueCommand&, SHPhysicsObject* physicsObject, const PhysicsComponentGroup& componentGroup) -- 2.40.1