From ea1dd57996bf684e8d954feebb3161079a03efd2 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Fri, 30 Dec 2022 00:45:01 +0800 Subject: [PATCH] Added stub functions for collision detection algorithms --- .../Narrowphase/SHCapsuleVsCapsule.cpp | 35 +++++++++++ .../Narrowphase/SHCapsuleVsConvex.cpp | 49 +++++++++++++++ .../Collision/Narrowphase/SHCollision.h | 47 +++++++++++++- .../Narrowphase/SHCollisionDispatch.cpp | 14 ++--- .../Narrowphase/SHConvexVsConvex.cpp | 62 +++++++++++++++++++ .../Narrowphase/SHSphereVsCapsule.cpp | 48 ++++++++++++++ .../Narrowphase/SHSphereVsConvex.cpp | 58 +++++++++++++++++ 7 files changed, 302 insertions(+), 11 deletions(-) create mode 100644 SHADE_Engine/src/Physics/Collision/Narrowphase/SHCapsuleVsCapsule.cpp create mode 100644 SHADE_Engine/src/Physics/Collision/Narrowphase/SHCapsuleVsConvex.cpp create mode 100644 SHADE_Engine/src/Physics/Collision/Narrowphase/SHConvexVsConvex.cpp create mode 100644 SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsCapsule.cpp create mode 100644 SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsConvex.cpp diff --git a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCapsuleVsCapsule.cpp b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCapsuleVsCapsule.cpp new file mode 100644 index 00000000..c75437cb --- /dev/null +++ b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCapsuleVsCapsule.cpp @@ -0,0 +1,35 @@ +/**************************************************************************************** + * \file SHCapsuleVsCapsule.cpp + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for the Detecting Collisions between two capsules + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#include + +// Primary Header +#include "SHCollision.h" + +// Project Headers +#include "Math/SHMathHelpers.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Public Member Functions Definitions */ + /*-----------------------------------------------------------------------------------*/ + + bool SHCollision::CapsuleVsCapsule(const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + return false; + } + + bool SHCollision::CapsuleVsCapsule(SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + return false; + } + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCapsuleVsConvex.cpp b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCapsuleVsConvex.cpp new file mode 100644 index 00000000..1b03feab --- /dev/null +++ b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCapsuleVsConvex.cpp @@ -0,0 +1,49 @@ +/**************************************************************************************** + * \file SHCapsuleVsConvex.cpp + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for the Detecting Collisions between a capsule and a convex + * polyhedron. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#include + +// Primary Header +#include "SHCollision.h" + +// Project Headers +#include "Math/SHMathHelpers.h" +#include "Physics/Collision/CollisionShapes/SHBoxCollisionShape.h" + +// TODO + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Public Member Functions Definitions */ + /*-----------------------------------------------------------------------------------*/ + + bool SHCollision::CapsuleVsConvex(const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + return false; + } + + bool SHCollision::ConvexVsCapsule(const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + return CapsuleVsConvex(B, A); + } + + bool SHCollision::CapsuleVsConvex(SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + return false; + } + + bool SHCollision::ConvexVsCapsule(SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + return CapsuleVsConvex(manifold, B, A); + } + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCollision.h b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCollision.h index a64c101c..b99cc202 100644 --- a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCollision.h +++ b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCollision.h @@ -35,18 +35,59 @@ namespace SHADE /* Spheres VS X */ - [[nodiscard]] static bool SphereVsSphere (const SHCollisionShape& A, const SHCollisionShape& B) noexcept; - [[nodiscard]] static bool SphereVsSphere (SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept; - + [[nodiscard]] static bool SphereVsSphere (const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + [[nodiscard]] static bool SphereVsSphere (SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + + [[nodiscard]] static bool SphereVsCapsule (const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + [[nodiscard]] static bool SphereVsCapsule (SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + + [[nodiscard]] static bool SphereVsConvex (const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + [[nodiscard]] static bool SphereVsConvex (SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept; /* Capsule VS X */ + [[nodiscard]] static bool CapsuleVsSphere (const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + [[nodiscard]] static bool CapsuleVsSphere (SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + + [[nodiscard]] static bool CapsuleVsCapsule (const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + [[nodiscard]] static bool CapsuleVsCapsule (SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + + [[nodiscard]] static bool CapsuleVsConvex (const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + [[nodiscard]] static bool CapsuleVsConvex (SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + /* Polygon VS X */ + [[nodiscard]] static bool ConvexVsSphere (const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + [[nodiscard]] static bool ConvexVsSphere (SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + + [[nodiscard]] static bool ConvexVsCapsule (const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + [[nodiscard]] static bool ConvexVsCapsule (SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + + [[nodiscard]] static bool ConvexVsConvex (const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + [[nodiscard]] static bool ConvexVsConvex (SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + private: /*---------------------------------------------------------------------------------*/ /* Member Functions */ /*---------------------------------------------------------------------------------*/ + static bool isMinkowskiFace(const SHVec3& a, const SHVec3& b, const SHVec3& c, const SHVec3& d) noexcept; + + // TODO: buildMinkowskiFace, queryEdgeDirection, distanceBetweenTwoEdges, + + /* + * TODO: + * static FaceQuery queryFaceDirections (const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + * static EdgeQuery queryEdgeDirections (const SHCollisionShape& A, const SHCollisionShape& B) noexcept; + * static bool buildMinkowskiFace (const SHHalfEdge& edgeA, const SHHalfEdge& edgeB) noexcept; + * static float distanceBetweenEdges(const SHHalfEdge& edgeA, const SHHalfEdge& edgeB, SHCollisionShape& poly) noexcept; + * static uint32_t clip + * + * ! References + * https://ia801303.us.archive.org/30/items/GDC2013Gregorius/GDC2013-Gregorius.pdf + * https://github.com/RandyGaul/qu3e/blob/master/src/collision/q3Collide.cpp + */ + + }; } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCollisionDispatch.cpp b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCollisionDispatch.cpp index c06e30ae..4a949d77 100644 --- a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCollisionDispatch.cpp +++ b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCollisionDispatch.cpp @@ -26,22 +26,20 @@ namespace SHADE const SHCollisionDispatcher::ManifoldCollide SHCollisionDispatcher::manifoldCollide[NUM_SHAPES][NUM_SHAPES] { - // TODO // vs Sphere / Box / Capsule - { SHCollision::SphereVsSphere, nullptr, nullptr } // Sphere - , { nullptr, nullptr, nullptr } // Box - , { nullptr, nullptr, nullptr } // Capsule + { SHCollision::SphereVsSphere, SHCollision::SphereVsConvex, SHCollision::SphereVsCapsule } // Sphere + , { SHCollision::ConvexVsSphere, SHCollision::ConvexVsConvex, SHCollision::ConvexVsCapsule } // Box + , { SHCollision::CapsuleVsSphere, SHCollision::CapsuleVsConvex, SHCollision::CapsuleVsCapsule } // Capsule }; const SHCollisionDispatcher::TriggerCollide SHCollisionDispatcher::triggerCollide[NUM_SHAPES][NUM_SHAPES] { - // TODO // vs Sphere / Box / Capsule - { SHCollision::SphereVsSphere, nullptr, nullptr } // Sphere - , { nullptr, nullptr, nullptr } // Box - , { nullptr, nullptr, nullptr } // Capsule + { SHCollision::SphereVsSphere, SHCollision::SphereVsConvex, SHCollision::SphereVsCapsule } // Sphere + , { SHCollision::ConvexVsSphere, SHCollision::ConvexVsConvex, SHCollision::ConvexVsCapsule } // Box + , { SHCollision::CapsuleVsSphere, SHCollision::CapsuleVsConvex, SHCollision::CapsuleVsCapsule } // Capsule }; const bool SHCollisionDispatcher::collisionTable[NUM_TYPES][NUM_TYPES] diff --git a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHConvexVsConvex.cpp b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHConvexVsConvex.cpp new file mode 100644 index 00000000..d73097bb --- /dev/null +++ b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHConvexVsConvex.cpp @@ -0,0 +1,62 @@ +/**************************************************************************************** + * \file SHConvexVsConvex.cpp + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for the Detecting Collisions between two convex polyhedrons. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#include + +// Primary Header +#include "SHCollision.h" + +// Project Headers +#include "Math/SHMathHelpers.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Public Member Functions Definitions */ + /*-----------------------------------------------------------------------------------*/ + + bool SHCollision::ConvexVsConvex(const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + /* + * TODO: + * + * 1. Query face directiosn of a to b. Exit early if separation found. + * 2. query face directions of b to a. Exit early if separation found. + * 3. Query edge directions of a & b. Exit early if separation found. + */ + + return false; + } + + bool SHCollision::ConvexVsConvex(SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + /* + * TODO: + * + * 1. Query face directions of a to b. Exit early if separation found. + * 2. Query face directions of b to a. Exit early if separation found. + * 3. Query edge directions of a & b. Exit early if separation found. + * + * (*)!! Apply weight to improve frame coherence of normal directions. DONT FORGET FLIP FLOP! + * 4. From above, save the axis of minimum penetration (reference face) + * 5. Find the most anti-parallel face on other shape (incident face) + * 6. Clip incident face against side planes of reference face. (Sutherland-Hodgeman Clipping). + * Keep all vertices below reference face. + * 7. Reduce manifold to 4 contact points. We only need 4 contact points for a stable manifold. + * + * Remember to save IDs in queries. + * During generation of incident face, store IDs of face. + * + */ + + return false; + } + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsCapsule.cpp b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsCapsule.cpp new file mode 100644 index 00000000..3e308aed --- /dev/null +++ b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsCapsule.cpp @@ -0,0 +1,48 @@ +/**************************************************************************************** + * \file SHSphereVsCapsule.cpp + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for the Detecting Collisions between a sphere and a capsule. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#include + +// Primary Header +#include "SHCollision.h" + +// Project Headers +#include "Math/SHMathHelpers.h" +#include "Physics/Collision/CollisionShapes/SHSphereCollisionShape.h" + +// TODO + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Public Member Functions Definitions */ + /*-----------------------------------------------------------------------------------*/ + + bool SHCollision::SphereVsCapsule(const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + return false; + } + + bool SHCollision::CapsuleVsSphere(const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + return SphereVsCapsule(B, A); + } + + bool SHCollision::SphereVsCapsule(SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + return false; + } + + bool SHCollision::CapsuleVsSphere(SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + return SphereVsCapsule(manifold, B, A); + } + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsConvex.cpp b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsConvex.cpp new file mode 100644 index 00000000..a8a9cd48 --- /dev/null +++ b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsConvex.cpp @@ -0,0 +1,58 @@ +/**************************************************************************************** + * \file SHSphereVsConvex.cpp + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for the Detecting Collisions between a sphere and a convex + * polyhedron. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#include + +// Primary Header +#include "SHCollision.h" + +// Project Headers +#include "Math/SHMathHelpers.h" +#include "Physics/Collision/CollisionShapes/SHCollisionShape.h" +#include "Physics/Collision/CollisionShapes/SHBoxCollisionShape.h" + +// When testing against convex polyhedrons, we do not care so much as whether it is a box +// or something else. We only need the vertices to build half edge structures for use +// with a gauss map. Regardless, we still cast it to the type just to get vertices +// since spheres and capsules do not implement the same method. + +// I did consider having another base class that encapsulates methods that convex polyhedrons +// would implement, but for the sake of my sanity, I won't do that. + +// TODO + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Public Member Functions Definitions */ + /*-----------------------------------------------------------------------------------*/ + + bool SHCollision::SphereVsConvex(const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + return false; + } + + bool SHCollision::ConvexVsSphere(const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + return SphereVsConvex(B, A); + } + + bool SHCollision::SphereVsConvex(SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + return false; + } + + bool SHCollision::ConvexVsSphere(SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept + { + return SphereVsConvex(manifold, B, A); + } + +} // namespace SHADE \ No newline at end of file