diff --git a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp index ed6b7bd4..765dbc9b 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp +++ b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp @@ -358,7 +358,7 @@ namespace SHADE { SHEditorWidgets::BeginPanel(std::format("{} Box #{}", ICON_FA_CUBE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y }); - auto* box = reinterpret_cast(shape); + auto* box = reinterpret_cast(shape); SHEditorWidgets::DragVec3 ( "Half Extents", { "X", "Y", "Z" }, @@ -368,7 +368,7 @@ namespace SHADE else if (shape->GetType() == SHCollisionShape::Type::SPHERE) { SHEditorWidgets::BeginPanel(std::format("{} Sphere #{}", ICON_MD_CIRCLE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y }); - auto* sphere = reinterpret_cast(shape); + auto* sphere = reinterpret_cast(shape); SHEditorWidgets::DragFloat ( "Radius", diff --git a/SHADE_Engine/src/Math/Geometry/SHBox.cpp b/SHADE_Engine/src/Math/Geometry/SHBox.cpp deleted file mode 100644 index bcdb7324..00000000 --- a/SHADE_Engine/src/Math/Geometry/SHBox.cpp +++ /dev/null @@ -1,154 +0,0 @@ -/**************************************************************************************** - * \file SHBox.cpp - * \author Diren D Bharwani, diren.dbharwani, 390002520 - * \brief Implementation for a 3-Dimensional Oriented Bounding Box - * - * \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 "SHBox.h" -// Project Headers -#include "Math/SHMathHelpers.h" -#include "Math/SHRay.h" -#include "Math/SHQuaternion.h" - -using namespace DirectX; - -namespace SHADE -{ - /*-----------------------------------------------------------------------------------*/ - /* Constructors & Destructor Definitions */ - /*-----------------------------------------------------------------------------------*/ - - SHBox::SHBox() noexcept - { - Extents = SHVec3::One * 0.5f; - } - - SHBox::SHBox(const SHVec3& c, const SHVec3& hE, const SHQuaternion& o) noexcept - { - Center = c; - Extents = hE; - Orientation = o; - } - - - SHBox::SHBox(const SHBox& rhs) noexcept - { - if (this == &rhs) - return; - - Center = rhs.Center; - Extents = rhs.Extents; - Orientation = rhs.Orientation; - } - - SHBox::SHBox(SHBox&& rhs) noexcept - { - Center = rhs.Center; - Extents = rhs.Extents; - Orientation = rhs.Orientation; - } - - /*-----------------------------------------------------------------------------------*/ - /* Operator Overload Definitions */ - /*-----------------------------------------------------------------------------------*/ - - SHBox& SHBox::operator=(const SHBox& rhs) noexcept - { - if (this != &rhs) - { - Center = rhs.Center; - Extents = rhs.Extents; - Orientation = rhs.Orientation; - } - - return *this; - } - - SHBox& SHBox::operator=(SHBox&& rhs) noexcept - { - Center = rhs.Center; - Extents = rhs.Extents; - Orientation = rhs.Orientation; - - return *this; - } - - - /*-----------------------------------------------------------------------------------*/ - /* Getter Function Definitions */ - /*-----------------------------------------------------------------------------------*/ - - std::vector SHBox::GetVertices() const noexcept - { - std::vector vertices; - vertices.resize(8); - GetCorners(vertices.data()); - return vertices; - } - - /*-----------------------------------------------------------------------------------*/ - /* Public Function Member Definitions */ - /*-----------------------------------------------------------------------------------*/ - - bool SHBox::TestPoint(const SHVec3& point) const noexcept - { - return BoundingOrientedBox::Contains(point); - } - - SHRaycastResult SHBox::Raycast(const SHRay& ray) const noexcept - { - 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); - - // TODO: Compute Normal - } - - return result; - } - - bool SHBox::Contains(const SHBox& rhs) const noexcept - { - return BoundingOrientedBox::Contains(rhs) == CONTAINS; - } - - float SHBox::Volume() const noexcept - { - return 8.0f * (Extents.x * Extents.y * Extents.z); - } - - float SHBox::SurfaceArea() const noexcept - { - return 8.0f * ((Extents.x * Extents.y) - + (Extents.x * Extents.z) - + (Extents.y * Extents.z)); - } - - /*-----------------------------------------------------------------------------------*/ - /* Static Function Member Definitions */ - /*-----------------------------------------------------------------------------------*/ - - bool SHBox::Intersect(const SHBox& lhs, const SHBox& rhs) noexcept - { - return lhs.Intersects(rhs); - } - - SHBox SHBox::BuildFromVertices(const SHVec3* vertices, size_t numVertices, size_t stride) noexcept - { - SHBox result; - CreateFromPoints(result, numVertices, vertices, stride); - return result; - } - -} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Geometry/SHBox.h b/SHADE_Engine/src/Math/Geometry/SHBox.h deleted file mode 100644 index 7815ce2c..00000000 --- a/SHADE_Engine/src/Math/Geometry/SHBox.h +++ /dev/null @@ -1,138 +0,0 @@ -/**************************************************************************************** - * \file SHBox.h - * \author Diren D Bharwani, diren.dbharwani, 390002520 - * \brief Interface for a 3-Dimensional Oriented Bounding Box - * - * \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. -****************************************************************************************/ - -#pragma once - -#include - -// Project Headers -#include "SHShape.h" - -namespace SHADE -{ - /*-----------------------------------------------------------------------------------*/ - /* Type Definitions */ - /*-----------------------------------------------------------------------------------*/ - - /** - * @brief - * Encapsulates a 3D-oriented bounding box. - */ - class SH_API SHBox : public SHShape, - public DirectX::BoundingOrientedBox - { - public: - /*---------------------------------------------------------------------------------*/ - /* Static Data Members */ - /*---------------------------------------------------------------------------------*/ - - static constexpr size_t NUM_VERTICES = 8; - - /*---------------------------------------------------------------------------------*/ - /* Constructors & Destructor */ - /*---------------------------------------------------------------------------------*/ - - ~SHBox () override = default; - - SHBox () noexcept; - SHBox (const SHVec3& center, const SHVec3& halfExtents, const SHQuaternion& orientation) noexcept; - SHBox (const SHBox& rhs) noexcept; - SHBox (SHBox&& rhs) noexcept; - - /*---------------------------------------------------------------------------------*/ - /* Operator Overloads */ - /*---------------------------------------------------------------------------------*/ - - SHBox& operator= (const SHBox& rhs) noexcept; - SHBox& operator= (SHBox&& rhs) noexcept; - - /*---------------------------------------------------------------------------------*/ - /* Getter Functions */ - /*---------------------------------------------------------------------------------*/ - - [[nodiscard]] std::vector GetVertices () const noexcept; - - /*---------------------------------------------------------------------------------*/ - /* Member Functions */ - /*---------------------------------------------------------------------------------*/ - - /** - * @brief - * Checks if a point is inside the box. - * @param point - * The point to check. - * @return - * True if the point is inside the box. - */ - [[nodiscard]] bool TestPoint (const SHVec3& point) const noexcept override; - - /** - * @brief - * Casts a ray against the box. - * @param ray - * The ray to cast. - * @return - * The result of the raycast.
- * See the corresponding header for the contents of the raycast result object. - */ - [[nodiscard]] SHRaycastResult Raycast (const SHRay& ray) const noexcept override; - - /** - * @brief - * Checks if an entire other box is contained by this box. - * @param rhs - * The box to check. - * @return - * True if the other sphere is completely contained by this box. - */ - [[nodiscard]] bool Contains (const SHBox& rhs) const noexcept; - - /** - * @brief - * Calculates the volume of the box. - */ - [[nodiscard]] float Volume () const noexcept; - - /** - * @brief - * Calculates the surface area of the box. - */ - [[nodiscard]] float SurfaceArea () const noexcept; - - /*---------------------------------------------------------------------------------*/ - /* Static Member Functions */ - /*---------------------------------------------------------------------------------*/ - - /** - * @brief - * Checks if two boxes are intersecting. - * @return - * True if they are intersecting. - */ - [[nodiscard]] static bool Intersect (const SHBox& lhs, const SHBox& rhs) noexcept; - - /** - * @brief - * Builds a box from a set of vertices. - * @param vertices - * The vertices to build a box from. - * @param numVertices - * The number of vertices in the set to build from. - * @param stride - * The stride between each vertex, in the instance there is data in between each - * vertex that does not define the geometry of the object. - * @return - * An box that contains all the vertices in the set. - */ - [[nodiscard]] static SHBox BuildFromVertices (const SHVec3* vertices, size_t numVertices, size_t stride = 0) noexcept; - }; - - -} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Geometry/SHSphere.cpp b/SHADE_Engine/src/Math/Geometry/SHSphere.cpp deleted file mode 100644 index 9d7a7c68..00000000 --- a/SHADE_Engine/src/Math/Geometry/SHSphere.cpp +++ /dev/null @@ -1,150 +0,0 @@ -/**************************************************************************************** - * \file SHSphere.cpp - * \author Diren D Bharwani, diren.dbharwani, 390002520 - * \brief Implementation for a Bounding Sphere - * - * \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 "SHSphere.h" -// Project Headers -#include "Math/SHMathHelpers.h" -#include "Math/SHRay.h" - -using namespace DirectX; - -namespace SHADE -{ - /*-----------------------------------------------------------------------------------*/ - /* Constructors & Destructor Definitions */ - /*-----------------------------------------------------------------------------------*/ - - SHSphere::SHSphere() noexcept - { - Radius = 0.5f; - } - - SHSphere::SHSphere(const SHVec3& center, float radius) noexcept - { - Center = center; - Radius = radius; - } - - SHSphere::SHSphere(const SHSphere& rhs) noexcept - { - if (this == &rhs) - return; - - Center = rhs.Center; - Radius = rhs.Radius; - } - - SHSphere::SHSphere(SHSphere&& rhs) noexcept - { - Center = rhs.Center; - Radius = rhs.Radius; - } - - /*-----------------------------------------------------------------------------------*/ - /* Operator Overload Definitions */ - /*-----------------------------------------------------------------------------------*/ - - SHSphere& SHSphere::operator=(const SHSphere& rhs) noexcept - { - if (this != &rhs) - { - Center = rhs.Center; - Radius = rhs.Radius; - } - - return *this; - } - - SHSphere& SHSphere::operator=(SHSphere&& rhs) noexcept - { - - Center = rhs.Center; - Radius = rhs.Radius; - - return *this; - } - - /*-----------------------------------------------------------------------------------*/ - /* Public Function Member Definitions */ - /*-----------------------------------------------------------------------------------*/ - - bool SHSphere::TestPoint(const SHVec3& point) const noexcept - { - return BoundingSphere::Contains(point); - } - - SHRaycastResult SHSphere::Raycast(const SHRay& ray) const noexcept - { - 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); - - // TODO: Compute Normal - } - - return result; - } - - bool SHSphere::Contains(const SHSphere& rhs) const noexcept - { - return BoundingSphere::Contains(rhs) == CONTAINS; - } - - float SHSphere::Volume() const noexcept - { - return (4.0f / 3.0f) * SHMath::PI * (Radius * Radius * Radius); - } - - float SHSphere::SurfaceArea() const noexcept - { - return 4.0f * SHMath::PI * (Radius * Radius); - } - - /*-----------------------------------------------------------------------------------*/ - /* Static Function Member Definitions */ - /*-----------------------------------------------------------------------------------*/ - - SHSphere SHSphere::Combine(const SHSphere& lhs, const SHSphere& rhs) noexcept - { - SHSphere result; - CreateMerged(result, lhs, rhs); - return result; - } - - bool SHSphere::Intersect(const SHSphere& lhs, const SHSphere& rhs) noexcept - { - return lhs.Intersects(rhs); - } - - SHSphere SHSphere::BuildFromSpheres(const SHSphere* spheres, size_t numSpheres) noexcept - { - SHSphere result; - - for (size_t i = 1; i < numSpheres; ++i) - CreateMerged(result, spheres[i - 1], spheres[i]); - - return result; - } - - SHSphere SHSphere::BuildFromVertices(const SHVec3* vertices, size_t numVertices, size_t stride) noexcept - { - SHSphere result; - CreateFromPoints(result, numVertices, vertices, stride); - return result; - } - -} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Geometry/SHSphere.h b/SHADE_Engine/src/Math/Geometry/SHSphere.h deleted file mode 100644 index 3c534ac4..00000000 --- a/SHADE_Engine/src/Math/Geometry/SHSphere.h +++ /dev/null @@ -1,146 +0,0 @@ -/**************************************************************************************** - * \file SHSphere.h - * \author Diren D Bharwani, diren.dbharwani, 390002520 - * \brief Interface for a Bounding Sphere. - * - * \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. -****************************************************************************************/ - -#pragma once - -#include - -// Project Headers -#include "SHShape.h" - -namespace SHADE -{ - /*-----------------------------------------------------------------------------------*/ - /* Type Definitions */ - /*-----------------------------------------------------------------------------------*/ - - /** - * @brief - * Encapsulates a 3D Sphere. - */ - class SH_API SHSphere : public SHShape, - public DirectX::BoundingSphere - { - public: - /*---------------------------------------------------------------------------------*/ - /* Constructors & Destructor */ - /*---------------------------------------------------------------------------------*/ - - SHSphere () noexcept; - SHSphere (const SHVec3& center, float radius) noexcept; - SHSphere (const SHSphere& rhs) noexcept; - SHSphere (SHSphere&& rhs) noexcept; - - ~SHSphere () override = default; - - /*---------------------------------------------------------------------------------*/ - /* Operator Overloads */ - /*---------------------------------------------------------------------------------*/ - - SHSphere& operator= (const SHSphere& rhs) noexcept; - SHSphere& operator= (SHSphere&& rhs) noexcept; - - /*---------------------------------------------------------------------------------*/ - /* Member Functions */ - /*---------------------------------------------------------------------------------*/ - - /** - * @brief - * Checks if a point is inside the sphere. - * @param point - * The point to check. - * @return - * True if the point is inside the sphere. - */ - [[nodiscard]] bool TestPoint (const SHVec3& point) const noexcept override; - - /** - * @brief - * Casts a ray against the sphere. - * @param ray - * The ray to cast. - * @return - * The result of the raycast.
- * See the corresponding header for the contents of the raycast result object. - */ - [[nodiscard]] SHRaycastResult Raycast (const SHRay& ray) const noexcept override; - - /** - * @brief - * Checks if an entire other sphere is contained by this sphere. - * @param rhs - * The sphere to check. - * @return - * True if the other sphere is completely contained by this sphere. - */ - [[nodiscard]] bool Contains (const SHSphere& rhs) const noexcept; - - /** - * @brief - * Calculates the volume of the sphere. - */ - [[nodiscard]] float Volume () const noexcept; - - /** - * @brief - * Calculates the surface area of the sphere. - */ - [[nodiscard]] float SurfaceArea () const noexcept; - - - /*---------------------------------------------------------------------------------*/ - /* Static Member Functions */ - /*---------------------------------------------------------------------------------*/ - - /** - * @brief - * Combines two spheres to form a larger sphere. - * If one sphere is completely contained by the other, the result is the larger sphere. - * @return - * The combined sphere. - */ - [[nodiscard]] static SHSphere Combine (const SHSphere& lhs, const SHSphere& rhs) noexcept; - - /** - * @brief - * Checks if two spheres are intersecting. - * @return - * True if they are intersecting. - */ - [[nodiscard]] static bool Intersect (const SHSphere& lhs, const SHSphere& rhs) noexcept; - - /** - * @brief - * Builds a single sphere from multiple spheres. - * @param spheres - * The set of spheres to build from. - * @param numSpheres - * The number of spheres in the set to build from. - * @return - * A sphere that contains all the spheres in the set. - */ - [[nodiscard]] static SHSphere BuildFromSpheres (const SHSphere* spheres, size_t numSpheres) noexcept; - - /** - * @brief - * Builds a sphere from a set of vertices. - * @param vertices - * The vertices to build a sphere from. - * @param numVertices - * The number of vertices in the set to build from. - * @param stride - * The stride between each vertex, in the instance there is data in between each - * vertex that does not define the geometry of the object. - * @return - * A sphere that contains all the vertices in the set. - */ - [[nodiscard]] static SHSphere BuildFromVertices (const SHVec3* vertices, size_t numVertices, size_t stride = 0) noexcept; - }; -} // namespace SHADE diff --git a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBoxCollisionShape.cpp b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBox.cpp similarity index 51% rename from SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBoxCollisionShape.cpp rename to SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBox.cpp index 7a6ab7d2..b46c92da 100644 --- a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBoxCollisionShape.cpp +++ b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBox.cpp @@ -11,12 +11,12 @@ #include // Primary Header -#include "SHBoxCollisionShape.h" +#include "SHBox.h" // Project Headers #include "Math/SHMathHelpers.h" #include "Math/SHMatrix.h" -#include "Physics/Collision/SHCollider.h" +#include "Physics/Collision/SHCompositeCollider.h" namespace SHADE { @@ -24,66 +24,46 @@ namespace SHADE /* Constructors & Destructor Definitions */ /*-----------------------------------------------------------------------------------*/ - SHBoxCollisionShape::SHBoxCollisionShape(SHCollisionShapeID id) noexcept - : SHConvexPolyhedronCollisionShape (id, SHCollisionShape::Type::BOX) - , SHBox () - , relativeExtents { SHVec3::One } - , scale { SHVec3::One } - {} - - SHBoxCollisionShape::SHBoxCollisionShape(const SHBoxCollisionShape& rhs) noexcept - : SHConvexPolyhedronCollisionShape (rhs.id, SHCollisionShape::Type::BOX) - , SHBox (rhs.Center, rhs.Extents, rhs.Orientation) - , relativeExtents { rhs.relativeExtents } - , scale { rhs.scale } + SHBox::SHBox(SHCollisionShapeID id) noexcept + : SHConvexPolyhedron (id, Type::BOX) + , relativeExtents { SHVec3::One } + , scale { SHVec3::One } { - halfEdgeStructure = rhs.halfEdgeStructure; - - material = rhs.material; - collider = rhs.collider; - transform = rhs.transform; - rotationOffset = rhs.rotationOffset; - flags = rhs.flags; - // Since all collision tags are taken from the matrix, we do not need to do a deep copy here. - collisionTag = rhs.collisionTag; + Extents = SHVec3::One * 0.5f; } - SHBoxCollisionShape::SHBoxCollisionShape(SHBoxCollisionShape&& rhs) noexcept - : SHConvexPolyhedronCollisionShape (rhs.id, SHCollisionShape::Type::BOX) - , SHBox (rhs.Center, rhs.Extents, rhs.Orientation) - , relativeExtents { rhs.relativeExtents } - , scale { rhs.scale } + SHBox::SHBox(const SHBox& rhs) noexcept + : SHConvexPolyhedron ( rhs ) + , relativeExtents { rhs.relativeExtents } + , scale { rhs.scale } { - halfEdgeStructure = rhs.halfEdgeStructure; + Center = rhs.Center; + Extents = rhs.Extents; + Orientation = rhs.Orientation; + } - material = rhs.material; - collider = rhs.collider; - transform = rhs.transform; - rotationOffset = rhs.rotationOffset; - flags = rhs.flags; - // Since all collision tags are taken from the matrix, we do not need to do a deep copy here. - collisionTag = rhs.collisionTag; + SHBox::SHBox(SHBox&& rhs) noexcept + : SHConvexPolyhedron ( rhs ) + , relativeExtents { rhs.relativeExtents } + , scale { rhs.scale } + { + Center = rhs.Center; + Extents = rhs.Extents; + Orientation = rhs.Orientation; } /*-----------------------------------------------------------------------------------*/ /* Operator Overload Definitions */ /*-----------------------------------------------------------------------------------*/ - SHBoxCollisionShape& SHBoxCollisionShape::operator=(const SHBoxCollisionShape& rhs) noexcept + SHBox& SHBox::operator=(const SHBox& rhs) noexcept { if (this == &rhs) return *this; // Collision Shape Properties - id = rhs.id; - material = rhs.material; - collider = rhs.collider; - transform = rhs.transform; - rotationOffset = rhs.rotationOffset; - flags = rhs.flags; - // Since all collision tags are taken from the matrix, we do not need to do a deep copy here. - collisionTag = rhs.collisionTag; + SHConvexPolyhedron::operator=(rhs); // Box Properties @@ -95,23 +75,15 @@ namespace SHADE relativeExtents = rhs.relativeExtents; scale = rhs.scale; - halfEdgeStructure = rhs.halfEdgeStructure; return *this; } - SHBoxCollisionShape& SHBoxCollisionShape::operator=(SHBoxCollisionShape&& rhs) noexcept + SHBox& SHBox::operator=(SHBox&& rhs) noexcept { // Collision Shape Properties - id = rhs.id; - material = rhs.material; - collider = rhs.collider; - transform = rhs.transform; - rotationOffset = rhs.rotationOffset; - flags = rhs.flags; - // Since all collision tags are taken from the matrix, we do not need to do a deep copy here. - collisionTag = rhs.collisionTag; + SHConvexPolyhedron::operator=(rhs); // Box Properties @@ -123,7 +95,6 @@ namespace SHADE relativeExtents = rhs.relativeExtents; scale = rhs.scale; - halfEdgeStructure = rhs.halfEdgeStructure; return *this; } @@ -132,32 +103,28 @@ namespace SHADE /* Getter Function Definitions */ /*-----------------------------------------------------------------------------------*/ - SHVec3 SHBoxCollisionShape::GetCenter() const noexcept - { - return Center; - } - - SHVec3 SHBoxCollisionShape::GetWorldExtents() const noexcept + SHVec3 SHBox::GetWorldExtents() const noexcept { return Extents; } - SHVec3 SHBoxCollisionShape::GetRelativeExtents() const noexcept + SHVec3 SHBox::GetRelativeExtents() const noexcept { return relativeExtents; } - SHVec3 SHBoxCollisionShape::GetVertex(int index) const + SHVec3 SHBox::GetVertex(int index) const { - static constexpr int NUM_VERTICES = 8; - if (index < 0 || index >= NUM_VERTICES) throw std::invalid_argument("Index out-of-range!"); - return GetVertices()[index]; + SHVec3 vertices[NUM_VERTICES]; + GetCorners(vertices); + + return vertices[index]; } - SHVec3 SHBoxCollisionShape::GetNormal(int faceIndex) const + SHVec3 SHBox::GetNormal(int faceIndex) const { // Get local normal const SHVec3& LOCAL_NORMAL = halfEdgeStructure->GetFace(faceIndex).normal; @@ -166,88 +133,47 @@ namespace SHADE return SHVec3::Rotate(LOCAL_NORMAL, Orientation); } - SHVec3 SHBoxCollisionShape::GetPosition() const noexcept + SHVec3 SHBox::GetWorldCentroid() const noexcept { return Center; } - SHQuaternion SHBoxCollisionShape::GetOrientation() const noexcept + SHVec3 SHBox::GetRelativeCentroid() const noexcept { - return Orientation; + if (collider) + return SHVec3{ Center } - collider->GetPosition(); + + return Center; } - float SHBoxCollisionShape::GetVolume() const noexcept - { - return Volume(); - } - - SHVec3 SHBoxCollisionShape::GetLocalCentroid() const noexcept + SHVec3 SHBox::GetLocalCentroid() const noexcept { return SHVec3::Zero; } - /*-----------------------------------------------------------------------------------*/ - /* Setter Function Definitions */ - /*-----------------------------------------------------------------------------------*/ - - void SHBoxCollisionShape::SetCenter(const SHVec3& newCenter) noexcept + SHQuaternion SHBox::GetWorldOrientation() const noexcept { - Center = newCenter; + return Orientation; } - void SHBoxCollisionShape::SetWorldExtents(const SHVec3& newWorldExtents) noexcept + SHQuaternion SHBox::GetRelativeOrientation() const noexcept { - Extents = newWorldExtents; - - // Recompute Relative radius - relativeExtents = 2.0f * Extents / scale; + return transform.orientation; } - void SHBoxCollisionShape::SetRelativeExtents(const SHVec3& newRelativeExtents) noexcept + float SHBox::GetVolume() const noexcept { - relativeExtents = newRelativeExtents; - - // Recompute world radius - Extents = relativeExtents * scale * 0.5f; + return 8.0f * (Extents.x * Extents.y * Extents.z); } - void SHBoxCollisionShape::SetScale(const SHVec3& newScale) noexcept + float SHBox::GetSurfaceArea() const noexcept { - scale = SHVec3::Abs(newScale); - - // Recompute world radius - Extents = relativeExtents * scale * 0.5f; + return 8.0f * (Extents.x * Extents.y + + Extents.x * Extents.z + + Extents.y * Extents.z); } - /*-----------------------------------------------------------------------------------*/ - /* Public Member Function Definitions */ - /*-----------------------------------------------------------------------------------*/ - - void SHBoxCollisionShape::ComputeTransforms() noexcept - { - const SHTransform& PARENT_TRANSFORM = collider->GetTransform(); - - SetScale(PARENT_TRANSFORM.scale); - - // Recompute center - const SHQuaternion FINAL_ROT = PARENT_TRANSFORM.orientation * transform.orientation; - const SHMatrix TRS = SHMatrix::Rotate(FINAL_ROT) * SHMatrix::Translate(PARENT_TRANSFORM.position); - - Orientation = FINAL_ROT; - Center = SHVec3::Transform(transform.position, TRS); - } - - bool SHBoxCollisionShape::TestPoint(const SHVec3& point) const noexcept - { - return SHBox::TestPoint(point); - } - - SHRaycastResult SHBoxCollisionShape::Raycast(const SHRay& ray) const noexcept - { - return SHBox::Raycast(ray); - } - - SHMatrix SHBoxCollisionShape::GetInertiaTensor(float mass) const noexcept + SHMatrix SHBox::GetInertiaTensor(float mass) const noexcept { static constexpr float ONE_OVER_TWELVE = (1.0f / 12.0f); @@ -270,30 +196,93 @@ namespace SHADE return result; } - SHMatrix SHBoxCollisionShape::ComputeWorldTransform() const noexcept - { - const SHTransform& PARENT_TRANSFORM = collider->GetTransform(); - const SHQuaternion ROTATION = PARENT_TRANSFORM.orientation * transform.orientation; - const SHVec3 SCALE = SHVec3{ Extents } *2.0f; + /*-----------------------------------------------------------------------------------*/ + /* Setter Function Definitions */ + /*-----------------------------------------------------------------------------------*/ - return SHMatrix::Transform - ( - Center - , ROTATION - , SCALE - ); + void SHBox::SetWorldExtents(const SHVec3& newWorldExtents) noexcept + { + Extents = newWorldExtents; + + // Recompute Relative radius + relativeExtents = 2.0f * Extents / scale; } - SHAABB SHBoxCollisionShape::ComputeAABB() const noexcept + void SHBox::SetRelativeExtents(const SHVec3& newRelativeExtents) noexcept + { + relativeExtents = newRelativeExtents; + + // Recompute world radius + Extents = relativeExtents * scale * 0.5f; + } + + void SHBox::SetScale(const SHVec3& newScale) noexcept + { + scale = SHVec3::Abs(newScale); + + // Recompute world radius + Extents = relativeExtents * scale * 0.5f; + } + + /*-----------------------------------------------------------------------------------*/ + /* Public Member Function Definitions */ + /*-----------------------------------------------------------------------------------*/ + + void SHBox::Update() noexcept + { + const SHTransform& PARENT_TRANSFORM = collider->GetTransform(); + + SetScale(PARENT_TRANSFORM.scale); + + // Recompute center + const SHQuaternion FINAL_ROT = PARENT_TRANSFORM.orientation * transform.orientation; + const SHMatrix TRS = SHMatrix::Rotate(FINAL_ROT) * SHMatrix::Translate(PARENT_TRANSFORM.position); + + Orientation = FINAL_ROT; + Center = SHVec3::Transform(transform.position, TRS); + } + + bool SHBox::TestPoint(const SHVec3& point) const noexcept + { + return Contains(point); + } + + SHRaycastResult SHBox::Raycast(const SHRay& ray) const noexcept + { + 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); + + // TODO: Compute Normal: Test which face the position belongs in. The normal is that face's normal. + } + + return result; + } + + SHMatrix SHBox::GetTRS() const noexcept + { + const SHQuaternion ROTATION = collider ? collider->GetTransform().orientation * transform.orientation : Orientation; + const SHVec3 SCALE = SHVec3{ Extents } *2.0f; + + return SHMatrix::Transform(Center, ROTATION, SCALE); + } + + SHAABB SHBox::ComputeAABB() const noexcept { SHVec3 min{ std::numeric_limits::max() }; SHVec3 max{ std::numeric_limits::lowest() }; - const auto& VERTICES = GetVertices(); - for (auto& vtx : VERTICES) + SHVec3 vertices[NUM_VERTICES]; + GetCorners(vertices); + + for (auto& vertex : vertices) { - min = SHVec3::Min({ vtx, min }); - max = SHVec3::Max({ vtx, max }); + min = SHVec3::Min({ vertex, min }); + max = SHVec3::Max({ vertex, max }); } const SHVec3 HALF_EXTENTS = (max - min) * 0.5f; diff --git a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBoxCollisionShape.h b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBox.h similarity index 53% rename from SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBoxCollisionShape.h rename to SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBox.h index a5be0d70..bf250726 100644 --- a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBoxCollisionShape.h +++ b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHBox.h @@ -1,5 +1,5 @@ /**************************************************************************************** - * \file SHBoxCollisionShape.h + * \file SHBox.h * \author Diren D Bharwani, diren.dbharwani, 390002520 * \brief Interface for a Box Collision Shape. * @@ -10,9 +10,10 @@ #pragma once +#include + // Project Headers -#include "Math/Geometry/SHBox.h" -#include "SHConvexPolyhedronCollisionShape.h" +#include "SHConvexPolyhedron.h" namespace SHADE { @@ -38,57 +39,67 @@ namespace SHADE * @brief * Encapsulate a Box Shape used for Physics Simulations. */ - class SH_API SHBoxCollisionShape final : public SHConvexPolyhedronCollisionShape - , private SHBox + class SH_API SHBox final : public SHConvexPolyhedron + , private DirectX::BoundingOrientedBox { private: /*---------------------------------------------------------------------------------*/ /* Friends */ /*---------------------------------------------------------------------------------*/ - friend class SHCollider; + friend class SHCompositeCollider; friend class SHCollision; friend class SHCollisionShapeLibrary; public: + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + + static constexpr int NUM_VERTICES = 8; + /*---------------------------------------------------------------------------------*/ /* Constructors & Destructor */ /*---------------------------------------------------------------------------------*/ - SHBoxCollisionShape (SHCollisionShapeID id) noexcept; - SHBoxCollisionShape (const SHBoxCollisionShape& rhs) noexcept; - SHBoxCollisionShape (SHBoxCollisionShape&& rhs) noexcept; + SHBox (SHCollisionShapeID id) noexcept; + SHBox (const SHBox& rhs) noexcept; + SHBox (SHBox&& rhs) noexcept; - ~SHBoxCollisionShape () override = default; + ~SHBox () override = default; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ /*---------------------------------------------------------------------------------*/ - SHBoxCollisionShape& operator= (const SHBoxCollisionShape& rhs) noexcept; - SHBoxCollisionShape& operator= (SHBoxCollisionShape&& rhs) noexcept; + SHBox& operator= (const SHBox& rhs) noexcept; + SHBox& operator= (SHBox&& rhs) noexcept; /*---------------------------------------------------------------------------------*/ /* Getter Functions */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] SHVec3 GetCenter () const noexcept; - [[nodiscard]] SHVec3 GetWorldExtents () const noexcept; - [[nodiscard]] SHVec3 GetRelativeExtents () const noexcept; + [[nodiscard]] SHVec3 GetWorldExtents () const noexcept; + [[nodiscard]] SHVec3 GetRelativeExtents () const noexcept; - [[nodiscard]] SHVec3 GetVertex (int index) const override; - [[nodiscard]] SHVec3 GetNormal (int faceIndex) const override; + // Overriden Methods - [[nodiscard]] SHVec3 GetPosition () const noexcept override; - [[nodiscard]] SHQuaternion GetOrientation () const noexcept override; - [[nodiscard]] float GetVolume () const noexcept override; - [[nodiscard]] SHVec3 GetLocalCentroid () const noexcept override; + [[nodiscard]] SHVec3 GetVertex (int index) const override; + [[nodiscard]] SHVec3 GetNormal (int faceIndex) const override; + + [[nodiscard]] SHVec3 GetWorldCentroid () const noexcept override; + [[nodiscard]] SHVec3 GetRelativeCentroid () const noexcept override; + [[nodiscard]] SHVec3 GetLocalCentroid () const noexcept override; + [[nodiscard]] SHQuaternion GetWorldOrientation () const noexcept override; + [[nodiscard]] SHQuaternion GetRelativeOrientation () const noexcept override; + [[nodiscard]] float GetVolume () const noexcept override; + [[nodiscard]] float GetSurfaceArea () const noexcept override; + [[nodiscard]] SHMatrix GetInertiaTensor (float mass) const noexcept override; /*---------------------------------------------------------------------------------*/ /* Setter Functions */ /*---------------------------------------------------------------------------------*/ - void SetCenter (const SHVec3& newCenter) noexcept; void SetWorldExtents (const SHVec3& newWorldExtents) noexcept; void SetRelativeExtents (const SHVec3& newRelativeExtents) noexcept; void SetScale (const SHVec3& newScale) noexcept; @@ -97,66 +108,19 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - /** - * @brief - * Recomputes the transform of this box. - */ - void ComputeTransforms () noexcept override; - - /** - * @brief - * Tests if a point is inside the box. - * @param point - * The point to test. - * @return - * True if the point is inside the box. - */ - [[nodiscard]] bool TestPoint (const SHVec3& point) const noexcept override; - - /** - * @brief - * Casts a ray against this box. - * @param ray - * The ray to cast. - * @return - * An object holding the results of the raycast.
- * See the corresponding header for the contents of the object. - */ - [[nodiscard]] SHRaycastResult Raycast (const SHRay& ray) const noexcept override; - - /** - * @brief - * Computes the inertia tensor of the box. - * @param mass - * The mass of the sphere. - * @return - * The inertia tensor of the box. - */ - [[nodiscard]] SHMatrix GetInertiaTensor (float mass) const noexcept override; - - /** - * @brief - * Computes the transformation matrix of the box. - * @return - * The transformation matrix of the box. - */ - [[nodiscard]] SHMatrix ComputeWorldTransform () const noexcept override; - - /** - * @brief - * Computes the a tight-fitting AABB that contains this box. - * @return - * A tight-fitting AABB that contains this box. - */ - [[nodiscard]] SHAABB ComputeAABB () const noexcept override; + void Update () noexcept override; + [[nodiscard]] bool TestPoint (const SHVec3& point) const noexcept override; + [[nodiscard]] SHRaycastResult Raycast (const SHRay& ray) const noexcept override; + [[nodiscard]] SHMatrix GetTRS () const noexcept override; + [[nodiscard]] SHAABB ComputeAABB () const noexcept override; private: /*---------------------------------------------------------------------------------*/ /* Data Members */ /*---------------------------------------------------------------------------------*/ - SHVec3 relativeExtents; - SHVec3 scale; // Intended to be passed in by the base collider. + SHVec3 relativeExtents; + SHVec3 scale; }; diff --git a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShape.cpp b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShape.cpp index b412a227..f089c02c 100644 --- a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShape.cpp +++ b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShape.cpp @@ -14,7 +14,7 @@ #include "SHCollisionShape.h" // Project Headers -#include "Physics/Collision/SHCollider.h" +#include "Physics/Collision/SHCompositeCollider.h" #include "Physics/Collision/CollisionTags/SHCollisionTagMatrix.h" #include "Reflection/SHReflectionMetadata.h" #include "Tools/Utilities/SHUtilities.h" @@ -112,25 +112,15 @@ namespace SHADE return *collisionTag; } - SHVec3 SHCollisionShape::GetPosition() const noexcept - { - const SHTransform& PARENT_TRANSFORM = collider->GetTransform(); - - const SHQuaternion FINAL_ROT = PARENT_TRANSFORM.orientation * transform.orientation; - const SHMatrix TRS = SHMatrix::Rotate(FINAL_ROT) * SHMatrix::Translate(PARENT_TRANSFORM.position); - - return SHVec3::Transform(transform.position, TRS); - } - - SHQuaternion SHCollisionShape::GetOrientation() const noexcept - { - return collider->GetOrientation() * transform.orientation; - } - /*-----------------------------------------------------------------------------------*/ /* Setter Function Definitions */ /*-----------------------------------------------------------------------------------*/ + void SHCollisionShape::SetCollisionTag(SHCollisionTag* newCollisionTag) noexcept + { + collisionTag = newCollisionTag; + } + void SHCollisionShape::SetFriction(float friction) noexcept { material.SetFriction(friction); @@ -155,7 +145,7 @@ namespace SHADE { transform.position = posOffset; - ComputeTransforms(); + Update(); } void SHCollisionShape::SetRotationOffset(const SHVec3& rotOffset) noexcept @@ -163,7 +153,7 @@ namespace SHADE rotationOffset = rotOffset; transform.orientation = SHQuaternion::FromEuler(rotationOffset); - ComputeTransforms(); + Update(); } void SHCollisionShape::SetIsTrigger(bool isTrigger) noexcept @@ -174,11 +164,6 @@ namespace SHADE isTrigger ? flags |= FLAG_VALUE : flags &= ~FLAG_VALUE; } - void SHCollisionShape::SetCollisionTag(SHCollisionTag* newCollisionTag) noexcept - { - collisionTag = newCollisionTag; - } - } // namespace SHADE RTTR_REGISTRATION diff --git a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShape.h b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShape.h index 42e9fb0e..b4ce9529 100644 --- a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShape.h +++ b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShape.h @@ -41,7 +41,7 @@ namespace SHADE /* Friends */ /*---------------------------------------------------------------------------------*/ - friend class SHCollider; + friend class SHCompositeCollider; friend class SHColliderComponent; friend class SHCollisionShapeLibrary; friend class SHCollisionSpace; @@ -57,6 +57,7 @@ namespace SHADE SPHERE , BOX , CAPSULE + , CONVEX_HULL , COUNT , INVALID = -1 @@ -83,44 +84,47 @@ namespace SHADE /* Getter Functions */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] EntityID GetEntityID () const noexcept; - [[nodiscard]] uint32_t GetIndex () const noexcept; + [[nodiscard]] EntityID GetEntityID () const noexcept; + [[nodiscard]] uint32_t GetIndex () const noexcept; // Material Properties // TODO: Remove individual setters once instanced materials are supported - [[nodiscard]] float GetFriction () const noexcept; - [[nodiscard]] float GetBounciness () const noexcept; - [[nodiscard]] float GetDensity () const noexcept; - [[nodiscard]] const SHPhysicsMaterial& GetMaterial () const noexcept; - - + [[nodiscard]] float GetFriction () const noexcept; + [[nodiscard]] float GetBounciness () const noexcept; + [[nodiscard]] float GetDensity () const noexcept; + [[nodiscard]] const SHPhysicsMaterial& GetMaterial () const noexcept; // Offsets - [[nodiscard]] const SHVec3& GetPositionOffset () const noexcept; - [[nodiscard]] const SHVec3& GetRotationOffset () const noexcept; + [[nodiscard]] const SHVec3& GetPositionOffset () const noexcept; + [[nodiscard]] const SHVec3& GetRotationOffset () const noexcept; // Flags - [[nodiscard]] Type GetType () const noexcept; - [[nodiscard]] bool IsTrigger () const noexcept; - [[nodiscard]] bool IsColliding () const noexcept; + [[nodiscard]] Type GetType () const noexcept; + [[nodiscard]] bool IsTrigger () const noexcept; + [[nodiscard]] bool IsColliding () const noexcept; - [[nodiscard]] const SHCollisionTag& GetCollisionTag () const noexcept; + [[nodiscard]] const SHCollisionTag& GetCollisionTag () const noexcept; // Virtual methods - [[nodiscard]] virtual SHVec3 GetPosition () const noexcept; - [[nodiscard]] virtual SHQuaternion GetOrientation () const noexcept; - [[nodiscard]] virtual float GetVolume () const noexcept = 0; - [[nodiscard]] virtual SHMatrix GetInertiaTensor (float mass) const noexcept = 0; - [[nodiscard]] virtual SHVec3 GetLocalCentroid () const noexcept = 0; + [[nodiscard]] virtual SHVec3 GetWorldCentroid () const noexcept = 0; + [[nodiscard]] virtual SHVec3 GetRelativeCentroid () const noexcept = 0; + [[nodiscard]] virtual SHVec3 GetLocalCentroid () const noexcept = 0; + [[nodiscard]] virtual SHQuaternion GetWorldOrientation () const noexcept = 0; + [[nodiscard]] virtual SHQuaternion GetRelativeOrientation () const noexcept = 0; + [[nodiscard]] virtual float GetVolume () const noexcept = 0; + [[nodiscard]] virtual float GetSurfaceArea () const noexcept = 0; + [[nodiscard]] virtual SHMatrix GetInertiaTensor (float mass) const noexcept = 0; /*---------------------------------------------------------------------------------*/ /* Setter Functions */ /*---------------------------------------------------------------------------------*/ + void SetCollisionTag (SHCollisionTag* newCollisionTag) noexcept; + void SetFriction (float friction) noexcept; void SetBounciness (float bounciness) noexcept; void SetDensity (float density) noexcept; @@ -130,18 +134,55 @@ namespace SHADE void SetRotationOffset (const SHVec3& rotOffset) noexcept; // Flags - - void SetIsTrigger (bool isTrigger) noexcept; - void SetCollisionTag (SHCollisionTag* newCollisionTag) noexcept; + // Forces rigidbody to recompute mass if one exists + void SetIsTrigger (bool isTrigger) noexcept; /*---------------------------------------------------------------------------------*/ /* Member Functions */ /*---------------------------------------------------------------------------------*/ - virtual void ComputeTransforms () noexcept = 0; - [[nodiscard]] virtual SHMatrix ComputeWorldTransform () const noexcept = 0; - [[nodiscard]] virtual SHAABB ComputeAABB () const noexcept = 0; + /** + * @brief + * Computes the transform of the shape. + */ + virtual void Update () noexcept = 0; + + /** + * @brief + * Tests if a point is inside this shape. + * @param point + * The point to test against the shape. + * @return + * True if the point is inside the shape. False otherwise. + */ + [[nodiscard]] virtual bool TestPoint (const SHVec3& point) const noexcept = 0; + + /** + * @brief + * Casts a ray at this shape. + * @param ray + * The ray to cast at the shape. + * @return + * The result of the ray cast. See the corresponding struct for it's contents. + */ + [[nodiscard]] virtual SHRaycastResult Raycast (const SHRay& ray) const noexcept = 0; + + /** + * @brief + * Computes the TRS matrix for rendering the shape. + * @return + * The model-to-world matrix for rendering the shape. + */ + [[nodiscard]] virtual SHMatrix GetTRS () const noexcept = 0; + + /** + * @brief + * Computes a tight-fitting AABB around this shape. + * @return + * An AABB. + */ + [[nodiscard]] virtual SHAABB ComputeAABB () const noexcept = 0; protected: /*---------------------------------------------------------------------------------*/ @@ -150,16 +191,17 @@ namespace SHADE SHCollisionShapeID id; - SHPhysicsMaterial material; + SHCompositeCollider* collider; // The collider it belongs to. + SHCollisionTag* collisionTag; + SHPhysicsMaterial material; // TODO: Change to pointer once instancing is supported - SHCollider* collider; // The collider it belongs to. - SHTransform transform; + SHTransform transform; // Stores the local position and rotation. // Needed for conversion to euler angles SHVec3 rotationOffset; - uint8_t flags; // 0 0 0 isColliding trigger capsule sphere box - SHCollisionTag* collisionTag; + uint8_t flags; // 0 0 0 trigger convexHull capsule sphere box + RTTR_ENABLE() }; diff --git a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShapeLibrary.cpp b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShapeLibrary.cpp index 46adb47b..e0202d67 100644 --- a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShapeLibrary.cpp +++ b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShapeLibrary.cpp @@ -39,12 +39,12 @@ namespace SHADE /* Public Member Function Definitions */ /*-----------------------------------------------------------------------------------*/ - SHSphereCollisionShape* SHCollisionShapeLibrary::CreateSphere(SHCollisionShapeID id, const SHSphereCreateInfo& createInfo) + SHSphere* SHCollisionShapeLibrary::CreateSphere(SHCollisionShapeID id, const SHSphereCreateInfo& createInfo) { - const auto RESULT = spheres.emplace(id, new SHSphereCollisionShape{ id }); + const auto RESULT = spheres.emplace(id, new SHSphere{ id }); if (RESULT.second) { - SHSphereCollisionShape* sphere = RESULT.first->second; + SHSphere* sphere = RESULT.first->second; sphere->Center = createInfo.Center; sphere->Radius = createInfo.Radius; @@ -57,12 +57,12 @@ namespace SHADE return spheres.find(id)->second; } - SHBoxCollisionShape* SHCollisionShapeLibrary::CreateBox(SHCollisionShapeID id, const SHBoxCreateInfo& createInfo) + SHBox* SHCollisionShapeLibrary::CreateBox(SHCollisionShapeID id, const SHBoxCreateInfo& createInfo) { - const auto RESULT = boxes.emplace(id, new SHBoxCollisionShape{ id }); + const auto RESULT = boxes.emplace(id, new SHBox{ id }); if (RESULT.second) { - SHBoxCollisionShape* box = RESULT.first->second; + SHBox* box = RESULT.first->second; box->Center = createInfo.Center; box->Extents = createInfo.Extents; @@ -90,7 +90,7 @@ namespace SHADE } case SHCollisionShape::Type::SPHERE: { - SHSphereCollisionShape* sphere = spheres.find(shape->id)->second; + SHSphere* sphere = spheres.find(shape->id)->second; spheres.erase(shape->id); delete sphere; diff --git a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShapeLibrary.h b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShapeLibrary.h index dbfc4fc7..462b8dd8 100644 --- a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShapeLibrary.h +++ b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHCollisionShapeLibrary.h @@ -13,8 +13,8 @@ #include // Project Header -#include "SHSphereCollisionShape.h" -#include "SHBoxCollisionShape.h" +#include "SHSphere.h" +#include "SHBox.h" namespace SHADE { @@ -51,7 +51,7 @@ namespace SHADE * @return * A new sphere collision shape. */ - SHSphereCollisionShape* CreateSphere (SHCollisionShapeID id, const SHSphereCreateInfo& createInfo); + SHSphere* CreateSphere (SHCollisionShapeID id, const SHSphereCreateInfo& createInfo); /** * @brief @@ -63,7 +63,7 @@ namespace SHADE * @return * A new box collision shape. */ - SHBoxCollisionShape* CreateBox (SHCollisionShapeID id, const SHBoxCreateInfo& createInfo); + SHBox* CreateBox (SHCollisionShapeID id, const SHBoxCreateInfo& createInfo); /** * @brief @@ -85,8 +85,8 @@ namespace SHADE // We use unordered maps for fast lookup when deleting. // Since we are not instancing shapes (yet?), I'd rather not iterate through an entire vector to find the shape. - using Spheres = std::unordered_map; - using Boxes = std::unordered_map; + using Spheres = std::unordered_map; + using Boxes = std::unordered_map; /*---------------------------------------------------------------------------------*/ /* Data Members */ diff --git a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHConvexPolyhedronCollisionShape.cpp b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHConvexPolyhedron.cpp similarity index 76% rename from SHADE_Engine/src/Physics/Collision/CollisionShapes/SHConvexPolyhedronCollisionShape.cpp rename to SHADE_Engine/src/Physics/Collision/CollisionShapes/SHConvexPolyhedron.cpp index 51d4e684..a2aef745 100644 --- a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHConvexPolyhedronCollisionShape.cpp +++ b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHConvexPolyhedron.cpp @@ -11,7 +11,7 @@ #include // Primary Header -#include "SHConvexPolyhedronCollisionShape.h" +#include "SHConvexPolyhedron.h" namespace SHADE { @@ -19,12 +19,12 @@ namespace SHADE /* Constructors & Destructor Definitions */ /*-----------------------------------------------------------------------------------*/ - SHConvexPolyhedronCollisionShape::SHConvexPolyhedronCollisionShape(SHCollisionShapeID id,Type polyhedronType) noexcept + SHConvexPolyhedron::SHConvexPolyhedron(SHCollisionShapeID id,Type polyhedronType) noexcept : SHCollisionShape (id, polyhedronType) , halfEdgeStructure { nullptr } {} - SHConvexPolyhedronCollisionShape::SHConvexPolyhedronCollisionShape(const SHConvexPolyhedronCollisionShape& rhs) noexcept + SHConvexPolyhedron::SHConvexPolyhedron(const SHConvexPolyhedron& rhs) noexcept : SHCollisionShape (rhs.id, rhs.GetType()) , halfEdgeStructure { nullptr } { @@ -37,7 +37,7 @@ namespace SHADE collisionTag = rhs.collisionTag; } - SHConvexPolyhedronCollisionShape::SHConvexPolyhedronCollisionShape(SHConvexPolyhedronCollisionShape&& rhs) noexcept + SHConvexPolyhedron::SHConvexPolyhedron(SHConvexPolyhedron&& rhs) noexcept : SHCollisionShape (rhs.id, rhs.GetType()) , halfEdgeStructure { nullptr } { @@ -54,7 +54,7 @@ namespace SHADE /* Operator Overload Definitions */ /*-----------------------------------------------------------------------------------*/ - SHConvexPolyhedronCollisionShape& SHConvexPolyhedronCollisionShape::operator=(const SHConvexPolyhedronCollisionShape& rhs) noexcept + SHConvexPolyhedron& SHConvexPolyhedron::operator=(const SHConvexPolyhedron& rhs) noexcept { if (this == &rhs) return *this; @@ -73,7 +73,7 @@ namespace SHADE return *this; } - SHConvexPolyhedronCollisionShape& SHConvexPolyhedronCollisionShape::operator=(SHConvexPolyhedronCollisionShape&& rhs) noexcept + SHConvexPolyhedron& SHConvexPolyhedron::operator=(SHConvexPolyhedron&& rhs) noexcept { material = rhs.material; collider = rhs.collider; @@ -93,28 +93,28 @@ namespace SHADE /* Getter Function Definitions */ /*-----------------------------------------------------------------------------------*/ - const SHHalfEdgeStructure* SHConvexPolyhedronCollisionShape::GetHalfEdgeStructure() const noexcept + const SHHalfEdgeStructure* SHConvexPolyhedron::GetHalfEdgeStructure() const noexcept { return halfEdgeStructure; } - int32_t SHConvexPolyhedronCollisionShape::GetFaceCount() const noexcept + int32_t SHConvexPolyhedron::GetFaceCount() const noexcept { return halfEdgeStructure->GetFaceCount(); } - int32_t SHConvexPolyhedronCollisionShape::GetHalfEdgeCount() const noexcept + int32_t SHConvexPolyhedron::GetHalfEdgeCount() const noexcept { return halfEdgeStructure->GetHalfEdgeCount(); } - const SHHalfEdgeStructure::Face& SHConvexPolyhedronCollisionShape::GetFace(int index) const + const SHHalfEdgeStructure::Face& SHConvexPolyhedron::GetFace(int index) const { // Assume it has already been initialised return halfEdgeStructure->GetFace(index); } - const SHHalfEdgeStructure::HalfEdge& SHConvexPolyhedronCollisionShape::GetHalfEdge(int index) const + const SHHalfEdgeStructure::HalfEdge& SHConvexPolyhedron::GetHalfEdge(int index) const { // Assume it has already been initialised return halfEdgeStructure->GetHalfEdge(index); diff --git a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHConvexPolyhedronCollisionShape.h b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHConvexPolyhedron.h similarity index 83% rename from SHADE_Engine/src/Physics/Collision/CollisionShapes/SHConvexPolyhedronCollisionShape.h rename to SHADE_Engine/src/Physics/Collision/CollisionShapes/SHConvexPolyhedron.h index 6095cbca..cdc7f45f 100644 --- a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHConvexPolyhedronCollisionShape.h +++ b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHConvexPolyhedron.h @@ -24,14 +24,14 @@ namespace SHADE * @brief * Encapsulates a convex polyhedron shape used for Physics Simulations.. */ - class SH_API SHConvexPolyhedronCollisionShape : public SHCollisionShape + class SH_API SHConvexPolyhedron : public SHCollisionShape { private: /*---------------------------------------------------------------------------------*/ /* Friends */ /*---------------------------------------------------------------------------------*/ - friend class SHCollider; + friend class SHCompositeCollider; friend class SHCollision; friend class SHCollisionShapeLibrary; @@ -40,18 +40,18 @@ namespace SHADE /* Constructors & Destructor */ /*---------------------------------------------------------------------------------*/ - SHConvexPolyhedronCollisionShape (SHCollisionShapeID id, Type polyhedronType) noexcept; - SHConvexPolyhedronCollisionShape (const SHConvexPolyhedronCollisionShape& rhs) noexcept; - SHConvexPolyhedronCollisionShape (SHConvexPolyhedronCollisionShape&& rhs) noexcept; + SHConvexPolyhedron (SHCollisionShapeID id, Type polyhedronType) noexcept; + SHConvexPolyhedron (const SHConvexPolyhedron& rhs) noexcept; + SHConvexPolyhedron (SHConvexPolyhedron&& rhs) noexcept; - ~SHConvexPolyhedronCollisionShape () override = default; + ~SHConvexPolyhedron () override = default; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ /*---------------------------------------------------------------------------------*/ - SHConvexPolyhedronCollisionShape& operator=(const SHConvexPolyhedronCollisionShape& rhs) noexcept; - SHConvexPolyhedronCollisionShape& operator=(SHConvexPolyhedronCollisionShape&& rhs) noexcept; + SHConvexPolyhedron& operator=(const SHConvexPolyhedron& rhs) noexcept; + SHConvexPolyhedron& operator=(SHConvexPolyhedron&& rhs) noexcept; /*---------------------------------------------------------------------------------*/ /* Getter Functions */ diff --git a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHSphereCollisionShape.cpp b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHSphere.cpp similarity index 51% rename from SHADE_Engine/src/Physics/Collision/CollisionShapes/SHSphereCollisionShape.cpp rename to SHADE_Engine/src/Physics/Collision/CollisionShapes/SHSphere.cpp index a5c82017..04f8718e 100644 --- a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHSphereCollisionShape.cpp +++ b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHSphere.cpp @@ -11,12 +11,12 @@ #include // Primary Header -#include "SHSphereCollisionShape.h" +#include "SHSphere.h" // Project Headers #include "Math/SHMathHelpers.h" #include "Math/SHMatrix.h" -#include "Physics/Collision/SHCollider.h" +#include "Physics/Collision/SHCompositeCollider.h" namespace SHADE { @@ -24,64 +24,42 @@ namespace SHADE /* Constructors & Destructor Definitions */ /*-----------------------------------------------------------------------------------*/ - SHSphereCollisionShape::SHSphereCollisionShape(SHCollisionShapeID id) noexcept - : SHCollisionShape (id, SHCollisionShape::Type::SPHERE) - , SHSphere () + SHSphere::SHSphere(SHCollisionShapeID id) noexcept + : SHCollisionShape (id, Type::SPHERE) , relativeRadius { 1.0f } , scale { 1.0f } - {} - - SHSphereCollisionShape::SHSphereCollisionShape(const SHSphereCollisionShape& rhs) noexcept - : SHCollisionShape (rhs.id, SHCollisionShape::Type::SPHERE) - , SHSphere (rhs.Center, rhs.Radius) - , relativeRadius { rhs.relativeRadius } - , scale { rhs.scale } { - - material = rhs.material; - collider = rhs.collider; - transform = rhs.transform; - rotationOffset = rhs.rotationOffset; - flags = rhs.flags; - // Since all collision tags are taken from the matrix, we do not need to do a deep copy here. - collisionTag = rhs.collisionTag; + Radius = 0.5f; } - SHSphereCollisionShape::SHSphereCollisionShape(SHSphereCollisionShape&& rhs) noexcept - : SHCollisionShape (rhs.id, SHCollisionShape::Type::SPHERE) - , SHSphere (rhs.Center, rhs.Radius) + SHSphere::SHSphere(const SHSphere& rhs) noexcept + : SHCollisionShape ( rhs ) , relativeRadius { rhs.relativeRadius } , scale { rhs.scale } { + Center = rhs.Center; + Radius = rhs.Radius; + } - material = rhs.material; - collider = rhs.collider; - transform = rhs.transform; - rotationOffset = rhs.rotationOffset; - flags = rhs.flags; - // Since all collision tags are taken from the matrix, we do not need to do a deep copy here. - collisionTag = rhs.collisionTag; + SHSphere::SHSphere(SHSphere&& rhs) noexcept + : SHCollisionShape ( rhs ) + , relativeRadius { rhs.relativeRadius } + , scale { rhs.scale } + { + Center = rhs.Center; + Radius = rhs.Radius; } /*-----------------------------------------------------------------------------------*/ /* Operator Overload Definitions */ /*-----------------------------------------------------------------------------------*/ - SHSphereCollisionShape& SHSphereCollisionShape::operator=(const SHSphereCollisionShape& rhs) noexcept + SHSphere& SHSphere::operator=(const SHSphere& rhs) noexcept { if (this == &rhs) return *this; - // Collision Shape Properties - - id = rhs.id; - material = rhs.material; - collider = rhs.collider; - transform = rhs.transform; - rotationOffset = rhs.rotationOffset; - flags = rhs.flags; - // Since all collision tags are taken from the matrix, we do not need to do a deep copy here. - collisionTag = rhs.collisionTag; + SHCollisionShape::operator=(rhs); // Sphere Properties @@ -96,18 +74,9 @@ namespace SHADE return *this; } - SHSphereCollisionShape& SHSphereCollisionShape::operator=(SHSphereCollisionShape&& rhs) noexcept + SHSphere& SHSphere::operator=(SHSphere&& rhs) noexcept { - // Collision Shape Properties - - id = rhs.id; - material = rhs.material; - collider = rhs.collider; - transform = rhs.transform; - rotationOffset = rhs.rotationOffset; - flags = rhs.flags; - // Since all collision tags are taken from the matrix, we do not need to do a deep copy here. - collisionTag = rhs.collisionTag; + SHCollisionShape::operator=(rhs); // Sphere Properties @@ -126,51 +95,73 @@ namespace SHADE /* Getter Function Definitions */ /*-----------------------------------------------------------------------------------*/ - SHVec3 SHSphereCollisionShape::GetCenter() const noexcept - { - return Center; - } - - float SHSphereCollisionShape::GetWorldRadius() const noexcept + float SHSphere::GetWorldRadius() const noexcept { return Radius; } - float SHSphereCollisionShape::GetRelativeRadius() const noexcept + float SHSphere::GetRelativeRadius() const noexcept { return relativeRadius; } - SHVec3 SHSphereCollisionShape::GetPosition() const noexcept + SHVec3 SHSphere::GetWorldCentroid() const noexcept { return Center; } - SHQuaternion SHSphereCollisionShape::GetOrientation() const noexcept + SHVec3 SHSphere::GetRelativeCentroid() const noexcept { - return collider->GetTransform().orientation * transform.orientation; + if (collider) + return SHVec3{ Center } - collider->GetPosition(); + + return Center; } - float SHSphereCollisionShape::GetVolume() const noexcept - { - return Volume(); - } - - SHVec3 SHSphereCollisionShape::GetLocalCentroid() const noexcept + SHVec3 SHSphere::GetLocalCentroid() const noexcept { return SHVec3::Zero; } + SHQuaternion SHSphere::GetWorldOrientation() const noexcept + { + if (collider) + return collider->GetOrientation() * transform.orientation; + + return transform.orientation; + } + + SHQuaternion SHSphere::GetRelativeOrientation() const noexcept + { + return transform.orientation; + } + + float SHSphere::GetVolume() const noexcept + { + return (4.0f / 3.0f) * SHMath::PI * (Radius * Radius * Radius); + } + + float SHSphere::GetSurfaceArea() const noexcept + { + return 4.0f * SHMath::PI * (Radius * Radius); + } + + SHMatrix SHSphere::GetInertiaTensor(float mass) const noexcept + { + static constexpr float TWO_OVER_FIVE = 2.0f / 5.0f; + + const float DIAGONAL = TWO_OVER_FIVE * mass * (Radius * Radius); + + SHMatrix result; + result.m[0][0] = result.m[1][1] = result.m[2][2] = DIAGONAL; + return result; + } + /*-----------------------------------------------------------------------------------*/ /* Setter Function Definitions */ /*-----------------------------------------------------------------------------------*/ - void SHSphereCollisionShape::SetCenter(const SHVec3& newCenter) noexcept - { - Center = newCenter; - } - - void SHSphereCollisionShape::SetWorldRadius(float newWorldRadius) noexcept + void SHSphere::SetWorldRadius(float newWorldRadius) noexcept { Radius = newWorldRadius; @@ -178,7 +169,7 @@ namespace SHADE relativeRadius = 2.0f * Radius / scale; } - void SHSphereCollisionShape::SetRelativeRadius(float newRelativeRadius) noexcept + void SHSphere::SetRelativeRadius(float newRelativeRadius) noexcept { relativeRadius = newRelativeRadius; @@ -186,7 +177,7 @@ namespace SHADE Radius = relativeRadius * scale * 0.5f; } - void SHSphereCollisionShape::SetScale(float maxScale) noexcept + void SHSphere::SetScale(float maxScale) noexcept { scale = std::fabs(maxScale); @@ -198,7 +189,7 @@ namespace SHADE /* Public Member Function Definitions */ /*-----------------------------------------------------------------------------------*/ - void SHSphereCollisionShape::ComputeTransforms() noexcept + void SHSphere::Update() noexcept { const SHTransform& PARENT_TRANSFORM = collider->GetTransform(); @@ -212,42 +203,35 @@ namespace SHADE Center = SHVec3::Transform(transform.position, TRS); } - bool SHSphereCollisionShape::TestPoint(const SHVec3& point) const noexcept + bool SHSphere::TestPoint(const SHVec3& point) const noexcept { - return SHSphere::TestPoint(point); + return Contains(point); } - SHRaycastResult SHSphereCollisionShape::Raycast(const SHRay& ray) const noexcept + SHRaycastResult SHSphere::Raycast(const SHRay& ray) const noexcept { - return SHSphere::Raycast(ray); - } + SHRaycastResult result; - SHMatrix SHSphereCollisionShape::GetInertiaTensor(float mass) const noexcept - { - static constexpr float TWO_OVER_FIVE = 2.0f / 5.0f; + 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); + result.normal = SHVec3::Normalise(result.position - Center); + } - const float DIAGONAL = TWO_OVER_FIVE * mass * (Radius * Radius); - - SHMatrix result; - result.m[0][0] = result.m[1][1] = result.m[2][2] = DIAGONAL; return result; } - SHMatrix SHSphereCollisionShape::ComputeWorldTransform() const noexcept + SHMatrix SHSphere::GetTRS() const noexcept { - const SHTransform& PARENT_TRANSFORM = collider->GetTransform(); - const SHQuaternion ROTATION = PARENT_TRANSFORM.orientation * transform.orientation; - const SHVec3 SCALE{ Radius }; + const SHQuaternion ROTATION = collider ? collider->GetTransform().orientation * transform.orientation : transform.orientation; + const SHVec3 SCALE { Radius }; - return SHMatrix::Transform - ( - Center - , ROTATION - , SCALE - ); + return SHMatrix::Transform(Center, ROTATION, SCALE); } - SHAABB SHSphereCollisionShape::ComputeAABB() const noexcept + SHAABB SHSphere::ComputeAABB() const noexcept { return SHAABB{ Center, SHVec3{ Radius } }; } diff --git a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHSphereCollisionShape.h b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHSphere.h similarity index 55% rename from SHADE_Engine/src/Physics/Collision/CollisionShapes/SHSphereCollisionShape.h rename to SHADE_Engine/src/Physics/Collision/CollisionShapes/SHSphere.h index 7dbdf13d..c4e97fe0 100644 --- a/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHSphereCollisionShape.h +++ b/SHADE_Engine/src/Physics/Collision/CollisionShapes/SHSphere.h @@ -10,8 +10,9 @@ #pragma once +#include + // Project Headers -#include "Math/Geometry/SHSphere.h" #include "SHCollisionShape.h" namespace SHADE @@ -37,15 +38,15 @@ namespace SHADE * @brief * Encapsulate a Sphere Shape used for Physics Simulations. */ - class SH_API SHSphereCollisionShape final : public SHCollisionShape - , private SHSphere + class SH_API SHSphere final : public SHCollisionShape + , private DirectX::BoundingSphere { private: /*---------------------------------------------------------------------------------*/ /* Friends */ /*---------------------------------------------------------------------------------*/ - friend class SHCollider; + friend class SHCompositeCollider; friend class SHCollision; friend class SHCollisionShapeLibrary; @@ -54,37 +55,39 @@ namespace SHADE /* Constructors & Destructor */ /*---------------------------------------------------------------------------------*/ - SHSphereCollisionShape (SHCollisionShapeID id) noexcept; - SHSphereCollisionShape (const SHSphereCollisionShape& rhs) noexcept; - SHSphereCollisionShape (SHSphereCollisionShape&& rhs) noexcept; + SHSphere (SHCollisionShapeID id) noexcept; + SHSphere (const SHSphere& rhs) noexcept; + SHSphere (SHSphere&& rhs) noexcept; - ~SHSphereCollisionShape () override = default; + ~SHSphere () override = default; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ /*---------------------------------------------------------------------------------*/ - SHSphereCollisionShape& operator= (const SHSphereCollisionShape& rhs) noexcept; - SHSphereCollisionShape& operator= (SHSphereCollisionShape&& rhs) noexcept; + SHSphere& operator= (const SHSphere& rhs) noexcept; + SHSphere& operator= (SHSphere&& rhs) noexcept; /*---------------------------------------------------------------------------------*/ /* Getter Functions */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] SHVec3 GetCenter () const noexcept; - [[nodiscard]] float GetWorldRadius () const noexcept; - [[nodiscard]] float GetRelativeRadius () const noexcept; + [[nodiscard]] float GetWorldRadius () const noexcept; + [[nodiscard]] float GetRelativeRadius () const noexcept; - [[nodiscard]] SHVec3 GetPosition () const noexcept override; - [[nodiscard]] SHQuaternion GetOrientation () const noexcept override; - [[nodiscard]] float GetVolume () const noexcept override; - [[nodiscard]] SHVec3 GetLocalCentroid () const noexcept override; + [[nodiscard]] SHVec3 GetWorldCentroid () const noexcept override; + [[nodiscard]] SHVec3 GetRelativeCentroid () const noexcept override; + [[nodiscard]] SHVec3 GetLocalCentroid () const noexcept override; + [[nodiscard]] SHQuaternion GetWorldOrientation () const noexcept override; + [[nodiscard]] SHQuaternion GetRelativeOrientation () const noexcept override; + [[nodiscard]] float GetVolume () const noexcept override; + [[nodiscard]] float GetSurfaceArea () const noexcept override; + [[nodiscard]] SHMatrix GetInertiaTensor (float mass) const noexcept override; /*---------------------------------------------------------------------------------*/ /* Setter Functions */ /*---------------------------------------------------------------------------------*/ - void SetCenter (const SHVec3& newCenter) noexcept; void SetWorldRadius (float newWorldRadius) noexcept; void SetRelativeRadius (float newRelativeRadius) noexcept; void SetScale (float maxScale) noexcept; @@ -93,58 +96,11 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - /** - * @brief - * Recomputes the transform of this sphere. - */ - void ComputeTransforms () noexcept override; - - /** - * @brief - * Tests if a point is inside the sphere. - * @param point - * The point to test. - * @return - * True if the point is inside the sphere. - */ - [[nodiscard]] bool TestPoint (const SHVec3& point) const noexcept override; - - /** - * @brief - * Casts a ray against this sphere. - * @param ray - * The ray to cast. - * @return - * An object holding the results of the raycast.
- * See the corresponding header for the contents of the object. - */ - [[nodiscard]] SHRaycastResult Raycast (const SHRay& ray) const noexcept override; - - /** - * @brief - * Computes the inertia tensor of the sphere. - * @param mass - * The mass of the sphere. - * @return - * The inertia tensor of the sphere. - */ - [[nodiscard]] SHMatrix GetInertiaTensor (float mass) const noexcept override; - - /** - * @brief - * Computes the transformation matrix of the sphere. - * @return - * The transformation matrix of the sphere. - */ - [[nodiscard]] SHMatrix ComputeWorldTransform () const noexcept override; - - /** - * @brief - * Computes the a tight-fitting AABB that contains this sphere. - * @return - * A tight-fitting AABB that contains this sphere. - */ - [[nodiscard]] SHAABB ComputeAABB () const noexcept override; + void Update () noexcept override; + [[nodiscard]] bool TestPoint (const SHVec3& point) const noexcept override; + [[nodiscard]] SHRaycastResult Raycast (const SHRay& ray) const noexcept override; + [[nodiscard]] SHMatrix GetTRS () const noexcept override; + [[nodiscard]] SHAABB ComputeAABB () const noexcept override; private: /*---------------------------------------------------------------------------------*/ @@ -152,7 +108,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ float relativeRadius; - float scale; // Intended to be passed in by the base collider. + float scale; }; } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/Collision/Contacts/SHCollisionKey.cpp b/SHADE_Engine/src/Physics/Collision/Contacts/SHCollisionKey.cpp index 4bb22697..8bd26e91 100644 --- a/SHADE_Engine/src/Physics/Collision/Contacts/SHCollisionKey.cpp +++ b/SHADE_Engine/src/Physics/Collision/Contacts/SHCollisionKey.cpp @@ -14,7 +14,7 @@ #include "SHCollisionKey.h" // Project Headers -#include "Physics/Collision/SHCollider.h" +#include "Physics/Collision/SHCompositeCollider.h" #include "Physics/Interface/SHColliderComponent.h" diff --git a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCapsuleVsConvex.cpp b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCapsuleVsConvex.cpp index 1b03feab..e7da5f8b 100644 --- a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCapsuleVsConvex.cpp +++ b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCapsuleVsConvex.cpp @@ -16,7 +16,7 @@ // Project Headers #include "Math/SHMathHelpers.h" -#include "Physics/Collision/CollisionShapes/SHBoxCollisionShape.h" +#include "Physics/Collision/CollisionShapes/SHBox.h" // TODO diff --git a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCollision.h b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCollision.h index 0c65a94f..e98983b4 100644 --- a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCollision.h +++ b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHCollision.h @@ -84,27 +84,9 @@ namespace SHADE // Sphere VS Convex - static FaceQuery findClosestFace - ( - const SHSphereCollisionShape& sphere - , const SHConvexPolyhedronCollisionShape& polyhedron - ) noexcept; - - static int32_t findClosestPoint - ( - const SHSphereCollisionShape& sphere - , const SHConvexPolyhedronCollisionShape& polyhedron - , int32_t faceIndex - ) noexcept; - - static int32_t findVoronoiRegion - ( - const SHSphereCollisionShape& sphere - , const SHVec3& faceVertex - , const SHVec3& faceNormal - , const SHVec3& tangent1 - , const SHVec3& tangent2 - ) noexcept; + static FaceQuery findClosestFace (const SHSphere& sphere, const SHConvexPolyhedron& polyhedron) noexcept; + static int32_t findClosestPoint (const SHSphere& sphere, const SHConvexPolyhedron& polyhedron, int32_t faceIndex) noexcept; + static int32_t findVoronoiRegion (const SHSphere& sphere, const SHVec3& faceVertex, const SHVec3& faceNormal, const SHVec3& tangent1, const SHVec3& tangent2) noexcept; // Capsule VS Convex diff --git a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsCapsule.cpp b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsCapsule.cpp index 3e308aed..d7eb02fc 100644 --- a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsCapsule.cpp +++ b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsCapsule.cpp @@ -15,7 +15,7 @@ // Project Headers #include "Math/SHMathHelpers.h" -#include "Physics/Collision/CollisionShapes/SHSphereCollisionShape.h" +#include "Physics/Collision/CollisionShapes/SHSphere.h" // TODO diff --git a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsConvex.cpp b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsConvex.cpp index 8245d672..1b97a7c6 100644 --- a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsConvex.cpp +++ b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsConvex.cpp @@ -18,7 +18,7 @@ #include "Math/Geometry/SHPlane.h" #include "Math/SHMathHelpers.h" #include "Physics/Collision/CollisionShapes/SHCollisionShape.h" -#include "Physics/Collision/CollisionShapes/SHBoxCollisionShape.h" +#include "Physics/Collision/CollisionShapes/SHBox.h" namespace SHADE { @@ -28,10 +28,10 @@ namespace SHADE bool SHCollision::SphereVsConvex(const SHCollisionShape& A, const SHCollisionShape& B) noexcept { - const SHSphereCollisionShape& SPHERE = dynamic_cast(A); - const SHConvexPolyhedronCollisionShape& POLYHEDRON = dynamic_cast(B); + const SHSphere& SPHERE = dynamic_cast(A); + const SHConvexPolyhedron& POLYHEDRON = dynamic_cast(B); - const SHVec3 CENTER = SPHERE.GetCenter(); + const SHVec3 CENTER = SPHERE.Center; const float RADIUS = SPHERE.GetWorldRadius(); // Find closest face @@ -76,10 +76,10 @@ namespace SHADE { // Convert to underlying types // For the convex, we only need the convex polyhedron shape since the get vertex is pure virtual. - const SHSphereCollisionShape& SPHERE = dynamic_cast(A); - const SHConvexPolyhedronCollisionShape& POLYHEDRON = dynamic_cast(B); + const SHSphere& SPHERE = dynamic_cast(A); + const SHConvexPolyhedron& POLYHEDRON = dynamic_cast(B); - const SHVec3 CENTER = SPHERE.GetCenter(); + const SHVec3 CENTER = SPHERE.Center; const float RADIUS = SPHERE.GetWorldRadius(); const FaceQuery FACE_QUERY = findClosestFace(SPHERE, POLYHEDRON); @@ -98,7 +98,7 @@ namespace SHADE manifold.normal = -POLYHEDRON.GetNormal(FACE_QUERY.closestFace); contact.penetration = PENETRATION; - contact.position = SPHERE.GetCenter(); + contact.position = CENTER; manifold.contacts[numContacts++] = contact; manifold.numContacts = numContacts; @@ -187,13 +187,13 @@ namespace SHADE SHCollision::FaceQuery SHCollision::findClosestFace ( - const SHSphereCollisionShape& sphere - , const SHConvexPolyhedronCollisionShape& polyhedron + const SHSphere& sphere + , const SHConvexPolyhedron& polyhedron ) noexcept { FaceQuery faceQuery; - const SHVec3 CENTER = sphere.GetCenter(); + const SHVec3 CENTER = sphere.Center; const float RADIUS = sphere.GetWorldRadius(); /* @@ -233,15 +233,15 @@ namespace SHADE int32_t SHCollision::findClosestPoint ( - const SHSphereCollisionShape& sphere - , const SHConvexPolyhedronCollisionShape& polyhedron - , int32_t faceIndex + const SHSphere& sphere + , const SHConvexPolyhedron& polyhedron + , int32_t faceIndex ) noexcept { // Find closest point on face int32_t closestPointIndex = -1; - const SHVec3 CENTER = sphere.GetCenter(); + const SHVec3 CENTER = sphere.Center; const SHHalfEdgeStructure::Face& FACE = polyhedron.GetFace(faceIndex); const int32_t NUM_VERITICES = static_cast(FACE.vertexIndices.size()); @@ -264,11 +264,11 @@ namespace SHADE int32_t SHCollision::findVoronoiRegion ( - const SHSphereCollisionShape& sphere - , const SHVec3& faceVertex - , const SHVec3& faceNormal - , const SHVec3& tangent1 - , const SHVec3& tangent2 + const SHSphere& sphere + , const SHVec3& faceVertex + , const SHVec3& faceNormal + , const SHVec3& tangent1 + , const SHVec3& tangent2 ) noexcept { static constexpr int NUM_TANGENTS = 2; @@ -288,7 +288,7 @@ namespace SHADE * */ - const SHVec3& CENTER = sphere.GetCenter(); + const SHVec3 CENTER = sphere.Center; const float RADIUS = sphere.GetWorldRadius(); const SHVec3 TANGENTS [NUM_TANGENTS] { tangent1, tangent2 }; diff --git a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsSphere.cpp b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsSphere.cpp index 07b6c9a6..aa270b16 100644 --- a/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsSphere.cpp +++ b/SHADE_Engine/src/Physics/Collision/Narrowphase/SHSphereVsSphere.cpp @@ -15,7 +15,7 @@ // Project Headers #include "Math/SHMathHelpers.h" -#include "Physics/Collision/CollisionShapes/SHSphereCollisionShape.h" +#include "Physics/Collision/CollisionShapes/SHSphere.h" namespace SHADE { @@ -25,22 +25,41 @@ namespace SHADE bool SHCollision::SphereVsSphere(const SHCollisionShape& A, const SHCollisionShape& B) noexcept { - const SHSphereCollisionShape& SPHERE_A = dynamic_cast(A); - const SHSphereCollisionShape& SPHERE_B = dynamic_cast(B); + const SHSphere& SPHERE_A = dynamic_cast(A); + const SHSphere& SPHERE_B = dynamic_cast(B); - return SHSphere::Intersect(SPHERE_A, SPHERE_B); + const SHVec3 CENTER_A = SPHERE_A.Center; + const float RADIUS_A = SPHERE_A.Radius; + const SHVec3 CENTER_B = SPHERE_B.Center; + const float RADIUS_B = SPHERE_B.Radius; + + + const SHVec3 A_TO_B = CENTER_B - CENTER_A; + const float DISTANCE_BETWEEN_CENTERS_SQUARED = A_TO_B.LengthSquared(); + + const float COMBINED_RADIUS = RADIUS_B + RADIUS_A; + const float COMBINED_RADIUS_SQUARED = COMBINED_RADIUS * COMBINED_RADIUS; + + if (DISTANCE_BETWEEN_CENTERS_SQUARED > COMBINED_RADIUS_SQUARED) + return false; } bool SHCollision::SphereVsSphere(SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept { // Convert to underlying types - const SHSphereCollisionShape& SPHERE_A = dynamic_cast(A); - const SHSphereCollisionShape& SPHERE_B = dynamic_cast(B); + const SHSphere& SPHERE_A = dynamic_cast(A); + const SHSphere& SPHERE_B = dynamic_cast(B); - const SHVec3 A_TO_B = SPHERE_B.GetCenter() - SPHERE_A.GetCenter(); + const SHVec3 CENTER_A = SPHERE_A.Center; + const float RADIUS_A = SPHERE_A.Radius; + const SHVec3 CENTER_B = SPHERE_B.Center; + const float RADIUS_B = SPHERE_B.Radius; + + + const SHVec3 A_TO_B = CENTER_B - CENTER_A; const float DISTANCE_BETWEEN_CENTERS_SQUARED = A_TO_B.LengthSquared(); - const float COMBINED_RADIUS = (SPHERE_A.GetWorldRadius() + SPHERE_B.GetWorldRadius()); + const float COMBINED_RADIUS = RADIUS_B + RADIUS_A; const float COMBINED_RADIUS_SQUARED = COMBINED_RADIUS * COMBINED_RADIUS; if (DISTANCE_BETWEEN_CENTERS_SQUARED > COMBINED_RADIUS_SQUARED) @@ -56,15 +75,15 @@ namespace SHADE if (SHMath::CompareFloat(DISTANCE_BETWEEN_CENTERS_SQUARED, 0.0f)) { manifold.normal = SHVec3::UnitY; - contact.position = SPHERE_A.GetCenter(); - contact.penetration = SPHERE_B.GetWorldRadius(); + contact.position = CENTER_A; + contact.penetration = RADIUS_B; manifold.contacts[numContacts++] = contact; } else { manifold.normal = SHVec3::Normalise(A_TO_B); - contact.position = SPHERE_B.GetCenter() - (manifold.normal * SPHERE_B.GetWorldRadius()); + contact.position = CENTER_B - manifold.normal * RADIUS_B; contact.penetration = COMBINED_RADIUS - A_TO_B.Length(); manifold.contacts[numContacts++] = contact; diff --git a/SHADE_Engine/src/Physics/Collision/SHCollisionSpace.cpp b/SHADE_Engine/src/Physics/Collision/SHCollisionSpace.cpp index 88fdac40..e410ac36 100644 --- a/SHADE_Engine/src/Physics/Collision/SHCollisionSpace.cpp +++ b/SHADE_Engine/src/Physics/Collision/SHCollisionSpace.cpp @@ -40,7 +40,7 @@ namespace SHADE /* Public Member Functions Definitions */ /*-----------------------------------------------------------------------------------*/ - void SHCollisionSpace::AddCollider(SHCollider* collider) noexcept + void SHCollisionSpace::AddCollider(SHCompositeCollider* collider) noexcept { const bool INSERTED = colliders.emplace(collider->entityID, collider).second; if (!INSERTED) @@ -56,7 +56,7 @@ namespace SHADE broadphase.Insert(shape->id, shape->ComputeAABB()); } - void SHCollisionSpace::RemoveCollider(SHCollider* collider) noexcept + void SHCollisionSpace::RemoveCollider(SHCompositeCollider* collider) noexcept { colliders.erase(collider->entityID); @@ -209,12 +209,12 @@ namespace SHADE { case SHCollisionShape::Type::SPHERE: { - baseResult = dynamic_cast(SHAPE)->Raycast(info.ray); + baseResult = dynamic_cast(SHAPE)->Raycast(info.ray); break; } case SHCollisionShape::Type::BOX: { - baseResult = dynamic_cast(SHAPE)->Raycast(info.ray); + baseResult = dynamic_cast(SHAPE)->Raycast(info.ray); break; } case SHCollisionShape::Type::CAPSULE: @@ -246,7 +246,7 @@ namespace SHADE /* Private Member Functions Definitions */ /*-----------------------------------------------------------------------------------*/ - void SHCollisionSpace::broadphaseQuery(SHRigidBody::Type rigidBodyType, SHCollider* collider) noexcept + void SHCollisionSpace::broadphaseQuery(SHRigidBody::Type rigidBodyType, SHCompositeCollider* collider) noexcept { for (auto* shape : collider->shapes) { diff --git a/SHADE_Engine/src/Physics/Collision/SHCollisionSpace.h b/SHADE_Engine/src/Physics/Collision/SHCollisionSpace.h index 8607cf45..fa1f0d16 100644 --- a/SHADE_Engine/src/Physics/Collision/SHCollisionSpace.h +++ b/SHADE_Engine/src/Physics/Collision/SHCollisionSpace.h @@ -18,7 +18,7 @@ // Project Headers #include "Broadphase/SHDynamicAABBTree.h" #include "Physics/Dynamics/SHContactManager.h" -#include "SHCollider.h" +#include "SHCompositeCollider.h" #include "SHPhysicsRaycastResult.h" #include "CollisionTags/SHCollisionTags.h" #include "CollisionTags/SHCollisionTags.h" @@ -118,7 +118,7 @@ namespace SHADE * @param collider * A collider to add. Duplicates will be ignored. */ - void AddCollider (SHCollider* collider) noexcept; + void AddCollider (SHCompositeCollider* collider) noexcept; /** * @brief @@ -127,7 +127,7 @@ namespace SHADE * @param collider * A collider to remove. If a reference to it doesn't exist, it will be ignored. */ - void RemoveCollider (SHCollider* collider) noexcept; + void RemoveCollider (SHCompositeCollider* collider) noexcept; /** * @brief @@ -165,7 +165,7 @@ namespace SHADE SHCollisionShape* B = nullptr; }; - using Colliders = std::unordered_map; + using Colliders = std::unordered_map; using NarrowphaseBatch = std::unordered_map; /*---------------------------------------------------------------------------------*/ @@ -185,7 +185,7 @@ namespace SHADE // Broadphase helpers - void broadphaseQuery (SHRigidBody::Type rigidBodyType, SHCollider* collider) noexcept; + void broadphaseQuery (SHRigidBody::Type rigidBodyType, SHCompositeCollider* collider) noexcept; // Narrowphase helpers diff --git a/SHADE_Engine/src/Physics/Collision/SHCollider.cpp b/SHADE_Engine/src/Physics/Collision/SHCompositeCollider.cpp similarity index 81% rename from SHADE_Engine/src/Physics/Collision/SHCollider.cpp rename to SHADE_Engine/src/Physics/Collision/SHCompositeCollider.cpp index 021605c7..97fef331 100644 --- a/SHADE_Engine/src/Physics/Collision/SHCollider.cpp +++ b/SHADE_Engine/src/Physics/Collision/SHCompositeCollider.cpp @@ -11,7 +11,7 @@ #include // Primary Header -#include "SHCollider.h" +#include "SHCompositeCollider.h" // Project Headers #include "Broadphase/SHDynamicAABBTree.h" @@ -27,7 +27,7 @@ namespace SHADE /* Constructors & Destructor Definitions */ /*-----------------------------------------------------------------------------------*/ - SHCollider::SHCollider(EntityID eid, const SHTransform& worldTransform) noexcept + SHCompositeCollider::SHCompositeCollider(EntityID eid, const SHTransform& worldTransform) noexcept : entityID { eid } , active { true } , debugDraw { false } @@ -38,7 +38,7 @@ namespace SHADE , transform { worldTransform } {} - SHCollider::SHCollider(const SHCollider& rhs) noexcept + SHCompositeCollider::SHCompositeCollider(const SHCompositeCollider& rhs) noexcept : entityID { rhs.entityID } , active { rhs.active } , debugDraw { rhs.debugDraw } @@ -57,7 +57,7 @@ namespace SHADE copyShapes(rhs); } - SHCollider::SHCollider(SHCollider&& rhs) noexcept + SHCompositeCollider::SHCompositeCollider(SHCompositeCollider&& rhs) noexcept : entityID { rhs.entityID } , active { rhs.active } , debugDraw { rhs.debugDraw } @@ -76,7 +76,7 @@ namespace SHADE copyShapes(rhs); } - SHCollider::~SHCollider() noexcept + SHCompositeCollider::~SHCompositeCollider() noexcept { if (!shapeLibrary) { @@ -92,7 +92,7 @@ namespace SHADE /* Operator Overload Definitions */ /*-----------------------------------------------------------------------------------*/ - SHCollider& SHCollider::operator=(const SHCollider& rhs) noexcept + SHCompositeCollider& SHCompositeCollider::operator=(const SHCompositeCollider& rhs) noexcept { if (this == &rhs) return *this; @@ -117,7 +117,7 @@ namespace SHADE return *this; } - SHCollider& SHCollider::operator=(SHCollider&& rhs) noexcept + SHCompositeCollider& SHCompositeCollider::operator=(SHCompositeCollider&& rhs) noexcept { if (!shapeLibrary) { @@ -143,47 +143,47 @@ namespace SHADE /* Getter Function Definitions */ /*-----------------------------------------------------------------------------------*/ - EntityID SHCollider::GetEntityID() const noexcept + EntityID SHCompositeCollider::GetEntityID() const noexcept { return entityID; } - bool SHCollider::IsActive() const noexcept + bool SHCompositeCollider::IsActive() const noexcept { return active; } - bool SHCollider::GetDebugDrawState() const noexcept + bool SHCompositeCollider::GetDebugDrawState() const noexcept { return debugDraw; } - const SHTransform& SHCollider::GetTransform() const noexcept + const SHTransform& SHCompositeCollider::GetTransform() const noexcept { return transform; } - const SHVec3& SHCollider::GetPosition() const noexcept + const SHVec3& SHCompositeCollider::GetPosition() const noexcept { return transform.position; } - const SHQuaternion& SHCollider::GetOrientation() const noexcept + const SHQuaternion& SHCompositeCollider::GetOrientation() const noexcept { return transform.orientation; } - const SHVec3& SHCollider::GetScale() const noexcept + const SHVec3& SHCompositeCollider::GetScale() const noexcept { return transform.scale; } - const SHCollider::CollisionShapes& SHCollider::GetCollisionShapes() const noexcept + const SHCompositeCollider::CollisionShapes& SHCompositeCollider::GetCollisionShapes() const noexcept { return shapes; } - SHCollisionShape* SHCollider::GetCollisionShape(int index) const + SHCollisionShape* SHCompositeCollider::GetCollisionShape(int index) const { const int NUM_SHAPES = static_cast(shapes.size()); @@ -197,7 +197,7 @@ namespace SHADE /* Setter Function Definitions */ /*-----------------------------------------------------------------------------------*/ - void SHCollider::SetIsActive(bool state) noexcept + void SHCompositeCollider::SetIsActive(bool state) noexcept { if (active == state) return; @@ -216,7 +216,7 @@ namespace SHADE } } - void SHCollider::SetDebugDrawState(bool state) noexcept + void SHCompositeCollider::SetDebugDrawState(bool state) noexcept { debugDraw = state; @@ -234,36 +234,36 @@ namespace SHADE #endif } - void SHCollider::SetRigidBody(SHRigidBody* rb) noexcept + void SHCompositeCollider::SetRigidBody(SHRigidBody* rb) noexcept { rigidBody = rb; } - void SHCollider::SetTransform(const SHTransform& newTransform) noexcept + void SHCompositeCollider::SetTransform(const SHTransform& newTransform) noexcept { hasMoved = true; transform = newTransform; } - void SHCollider::SetPosition(const SHVec3& newPosition) noexcept + void SHCompositeCollider::SetPosition(const SHVec3& newPosition) noexcept { hasMoved = true; transform.position = newPosition; } - void SHCollider::SetOrientation(const SHQuaternion& newOrientation) noexcept + void SHCompositeCollider::SetOrientation(const SHQuaternion& newOrientation) noexcept { hasMoved = true; transform.orientation = newOrientation; } - void SHCollider::SetScale(const SHVec3& newScale) noexcept + void SHCompositeCollider::SetScale(const SHVec3& newScale) noexcept { hasMoved = true; transform.scale = newScale; } - void SHCollider::SetFactory(SHCollisionShapeLibrary* factory) noexcept + void SHCompositeCollider::SetFactory(SHCollisionShapeLibrary* factory) noexcept { shapeLibrary = factory; } @@ -272,12 +272,12 @@ namespace SHADE /* Public Member Function Definitions */ /*-----------------------------------------------------------------------------------*/ - const SHMatrix& SHCollider::ComputeTransformTRS() noexcept + const SHMatrix& SHCompositeCollider::ComputeTransformTRS() noexcept { return transform.ComputeTRS(); } - int SHCollider::AddSphereCollisionShape(float relativeRadius, const SHVec3& posOffset, const SHVec3& rotOffset) + int SHCompositeCollider::AddSphereCollisionShape(float relativeRadius, const SHVec3& posOffset, const SHVec3& rotOffset) { if (!shapeLibrary) { @@ -304,7 +304,7 @@ namespace SHADE const uint32_t NEW_INDEX = static_cast(shapes.size()); const SHCollisionShapeID NEW_SHAPE_ID{ entityID, NEW_INDEX }; - SHSphereCollisionShape* sphere = shapeLibrary->CreateSphere(NEW_SHAPE_ID, SPHERE_CREATE_INFO); + SHSphere* sphere = shapeLibrary->CreateSphere(NEW_SHAPE_ID, SPHERE_CREATE_INFO); // Set offsets sphere->collider = this; @@ -332,7 +332,7 @@ namespace SHADE return static_cast(NEW_INDEX); } - int SHCollider::AddBoxCollisionShape(const SHVec3& relativeExtents, const SHVec3& posOffset, const SHVec3& rotOffset) + int SHCompositeCollider::AddBoxCollisionShape(const SHVec3& relativeExtents, const SHVec3& posOffset, const SHVec3& rotOffset) { if (!shapeLibrary) { @@ -357,7 +357,7 @@ namespace SHADE const uint32_t NEW_INDEX = static_cast(shapes.size()); const SHCollisionShapeID NEW_SHAPE_ID{ entityID, NEW_INDEX }; - SHBoxCollisionShape* box = shapeLibrary->CreateBox(NEW_SHAPE_ID, BOX_CREATE_INFO); + SHBox* box = shapeLibrary->CreateBox(NEW_SHAPE_ID, BOX_CREATE_INFO); // Set offsets box->collider = this; @@ -386,7 +386,7 @@ namespace SHADE } - void SHCollider::RemoveCollisionShape(int index) + void SHCompositeCollider::RemoveCollisionShape(int index) { if (!shapeLibrary) { @@ -434,23 +434,23 @@ namespace SHADE SHLOG_INFO_D("Removing Collision Shape {} from Entity {}", index, entityID) } - void SHCollider::RecomputeShapes() noexcept + void SHCompositeCollider::RecomputeShapes() noexcept { for (auto* shape : shapes) - shape->ComputeTransforms(); + shape->Update(); } /*-----------------------------------------------------------------------------------*/ /* Private Member Function Definitions */ /*-----------------------------------------------------------------------------------*/ - void SHCollider::copyShapes(const SHCollider& rhsCollider) + void SHCompositeCollider::copyShapes(const SHCompositeCollider& rhsCollider) { for (const auto* shape : rhsCollider.shapes) copyShape(shape); } - void SHCollider::copyShape(const SHCollisionShape* rhsShape) + void SHCompositeCollider::copyShape(const SHCollisionShape* rhsShape) { switch (rhsShape->GetType()) { @@ -460,7 +460,7 @@ namespace SHADE } case SHCollisionShape::Type::SPHERE: { - const SHSphereCollisionShape* RHS_SPHERE = dynamic_cast(rhsShape); + const SHSphere* RHS_SPHERE = dynamic_cast(rhsShape); const SHSphereCreateInfo SPHERE_CREATE_INFO { @@ -473,7 +473,7 @@ namespace SHADE const uint32_t NEW_INDEX = static_cast(shapes.size()); const SHCollisionShapeID NEW_SHAPE_ID{ entityID, NEW_INDEX }; - SHSphereCollisionShape* sphere = shapeLibrary->CreateSphere(NEW_SHAPE_ID, SPHERE_CREATE_INFO); + SHSphere* sphere = shapeLibrary->CreateSphere(NEW_SHAPE_ID, SPHERE_CREATE_INFO); *sphere = *RHS_SPHERE; shapes.emplace_back(sphere); diff --git a/SHADE_Engine/src/Physics/Collision/SHCollider.h b/SHADE_Engine/src/Physics/Collision/SHCompositeCollider.h similarity index 92% rename from SHADE_Engine/src/Physics/Collision/SHCollider.h rename to SHADE_Engine/src/Physics/Collision/SHCompositeCollider.h index c4a4ad17..e5ce8609 100644 --- a/SHADE_Engine/src/Physics/Collision/SHCollider.h +++ b/SHADE_Engine/src/Physics/Collision/SHCompositeCollider.h @@ -33,7 +33,7 @@ namespace SHADE * Base class for a collider. * There are only two collider types supported by SHADE Engine: Composite & Hull */ - class SH_API SHCollider + class SH_API SHCompositeCollider { private: /*---------------------------------------------------------------------------------*/ @@ -64,17 +64,17 @@ namespace SHADE * This is particularly important for composite colliders for offsets & relative sizes. * @return */ - SHCollider (EntityID eid, const SHTransform& worldTransform = SHTransform::Identity) noexcept; - SHCollider (const SHCollider& rhs) noexcept; - SHCollider (SHCollider&& rhs) noexcept; - ~SHCollider () noexcept; + SHCompositeCollider (EntityID eid, const SHTransform& worldTransform = SHTransform::Identity) noexcept; + SHCompositeCollider (const SHCompositeCollider& rhs) noexcept; + SHCompositeCollider (SHCompositeCollider&& rhs) noexcept; + ~SHCompositeCollider () noexcept; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ /*---------------------------------------------------------------------------------*/ - SHCollider& operator=(const SHCollider& rhs) noexcept; - SHCollider& operator=(SHCollider&& rhs) noexcept; + SHCompositeCollider& operator=(const SHCompositeCollider& rhs) noexcept; + SHCompositeCollider& operator=(SHCompositeCollider&& rhs) noexcept; /*---------------------------------------------------------------------------------*/ /* Getter Functions */ @@ -192,7 +192,7 @@ namespace SHADE /* Member Functions */ /*---------------------------------------------------------------------------------*/ - void copyShapes (const SHCollider& rhsCollider); + void copyShapes (const SHCompositeCollider& rhsCollider); void copyShape (const SHCollisionShape* rhsShape); }; diff --git a/SHADE_Engine/src/Physics/Dynamics/SHRigidBody.cpp b/SHADE_Engine/src/Physics/Dynamics/SHRigidBody.cpp index 3b2de659..9688caa2 100644 --- a/SHADE_Engine/src/Physics/Dynamics/SHRigidBody.cpp +++ b/SHADE_Engine/src/Physics/Dynamics/SHRigidBody.cpp @@ -17,7 +17,7 @@ #include #include -#include "Physics/Collision/SHCollider.h" +#include "Physics/Collision/SHCompositeCollider.h" #include "Tools/Logger/SHLogger.h" namespace SHADE @@ -296,7 +296,7 @@ namespace SHADE /* Setter Functions Definitions */ /*-----------------------------------------------------------------------------------*/ - void SHRigidBody::SetCollider(SHCollider* c) noexcept + void SHRigidBody::SetCollider(SHCompositeCollider* c) noexcept { collider = c; } @@ -690,7 +690,7 @@ namespace SHADE trueMass.emplace_back(MASS); // Weighted sum of masses contribute to the centroid's location using the collider's local position. - localCentroid += MASS * (shape->GetPosition() - collider->GetPosition()); + localCentroid += MASS * shape->GetRelativeCentroid(); } if (totalMass > 0.0f) @@ -726,8 +726,9 @@ namespace SHADE I = R * I; // Parallel Axis Theorem + // https://en.wikipedia.org/wiki/Parallel_axis_theorem // J = I + m((R /dot R)E_3 - R /outerProduct R) - const SHVec3 R = SHAPE->GetPosition() - worldCentroid; + const SHVec3 R = SHAPE->GetWorldCentroid() - worldCentroid; const float R_MAG2 = R.LengthSquared(); const SHMatrix R_OX_R = SHVec3::OuterProduct(R, R); diff --git a/SHADE_Engine/src/Physics/Dynamics/SHRigidBody.h b/SHADE_Engine/src/Physics/Dynamics/SHRigidBody.h index 3c869ad2..44ae3f48 100644 --- a/SHADE_Engine/src/Physics/Dynamics/SHRigidBody.h +++ b/SHADE_Engine/src/Physics/Dynamics/SHRigidBody.h @@ -22,7 +22,7 @@ namespace SHADE /* Forward Declarations */ /*-------------------------------------------------------------------------------------*/ - class SHCollider; + class SHCompositeCollider; /*-------------------------------------------------------------------------------------*/ /* Type Definitions */ @@ -113,7 +113,7 @@ namespace SHADE /* Setter Functions */ /*-----------------------------------------------------------------------------------*/ - void SetCollider (SHCollider* c) noexcept; + void SetCollider (SHCompositeCollider* c) noexcept; /** * @brief @@ -229,7 +229,7 @@ namespace SHADE // The entityID here is only meant for linking with the actual component in the engine. EntityID entityID; - SHCollider* collider; + SHCompositeCollider* collider; Type bodyType; diff --git a/SHADE_Engine/src/Physics/Interface/PhysicsObject/SHPhysicsObject.cpp b/SHADE_Engine/src/Physics/Interface/PhysicsObject/SHPhysicsObject.cpp index 56c2ac7b..07e08b78 100644 --- a/SHADE_Engine/src/Physics/Interface/PhysicsObject/SHPhysicsObject.cpp +++ b/SHADE_Engine/src/Physics/Interface/PhysicsObject/SHPhysicsObject.cpp @@ -111,7 +111,7 @@ namespace SHADE collider->SetRigidBody(nullptr); } - SHCollider* SHPhysicsObject::CreateCollider(const SHTransform& transform) + SHCompositeCollider* SHPhysicsObject::CreateCollider(const SHTransform& transform) { if (collider) { @@ -119,7 +119,7 @@ namespace SHADE return collider; } - collider = new SHCollider{ entityID, transform }; + collider = new SHCompositeCollider{ entityID, transform }; // Link with rigidBody if it exists if (rigidBody) @@ -148,13 +148,13 @@ namespace SHADE /* Private Member Function Definitions */ /*-----------------------------------------------------------------------------------*/ - void SHPhysicsObject::deepCopyComponents(const SHRigidBody* rhsRigidBody, const SHCollider* rhsCollider) + void SHPhysicsObject::deepCopyComponents(const SHRigidBody* rhsRigidBody, const SHCompositeCollider* rhsCollider) { if (rhsRigidBody) rigidBody = new SHRigidBody{ *rhsRigidBody }; if (rhsCollider) - collider = new SHCollider { *rhsCollider }; + collider = new SHCompositeCollider { *rhsCollider }; } } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/Interface/PhysicsObject/SHPhysicsObject.h b/SHADE_Engine/src/Physics/Interface/PhysicsObject/SHPhysicsObject.h index 2550543b..f1ee042d 100644 --- a/SHADE_Engine/src/Physics/Interface/PhysicsObject/SHPhysicsObject.h +++ b/SHADE_Engine/src/Physics/Interface/PhysicsObject/SHPhysicsObject.h @@ -11,7 +11,7 @@ #pragma once // Project Headers -#include "Physics/Collision/SHCollider.h" +#include "Physics/Collision/SHCompositeCollider.h" #include "Physics/Dynamics/SHRigidBody.h" namespace SHADE @@ -33,7 +33,7 @@ namespace SHADE EntityID entityID = MAX_EID; SHRigidBody* rigidBody = nullptr; - SHCollider* collider = nullptr; + SHCompositeCollider* collider = nullptr; /*-----------------------------------------------------------------------------------*/ /* Constructors & Destructor */ @@ -91,7 +91,7 @@ namespace SHADE * Pointer to the collider that was created. The memory of this collider is managed * by the physics object itself. */ - SHCollider* CreateCollider (const SHTransform& transform = SHTransform::Identity); + SHCompositeCollider* CreateCollider (const SHTransform& transform = SHTransform::Identity); /** * @brief @@ -104,6 +104,6 @@ namespace SHADE /* Member Functions */ /*-----------------------------------------------------------------------------------*/ - void deepCopyComponents(const SHRigidBody* rhsRigidBody, const SHCollider* rhsCollider); + void deepCopyComponents(const SHRigidBody* rhsRigidBody, const SHCompositeCollider* rhsCollider); }; } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/Interface/SHColliderComponent.cpp b/SHADE_Engine/src/Physics/Interface/SHColliderComponent.cpp index 7425f418..5569c713 100644 --- a/SHADE_Engine/src/Physics/Interface/SHColliderComponent.cpp +++ b/SHADE_Engine/src/Physics/Interface/SHColliderComponent.cpp @@ -32,12 +32,12 @@ namespace SHADE /* Getter Function Definitions */ /*-----------------------------------------------------------------------------------*/ - SHCollider* const SHColliderComponent::GetCollider() const noexcept + SHCompositeCollider* const SHColliderComponent::GetCollider() const noexcept { return collider; } - const SHCollider::CollisionShapes* const SHColliderComponent::GetCollisionShapes() const noexcept + const SHCompositeCollider::CollisionShapes* const SHColliderComponent::GetCollisionShapes() const noexcept { if (!collider) return nullptr; @@ -66,7 +66,7 @@ namespace SHADE /* Setter Function Definitions */ /*-----------------------------------------------------------------------------------*/ - void SHColliderComponent::SetCollider(SHCollider* c) noexcept + void SHColliderComponent::SetCollider(SHCompositeCollider* c) noexcept { collider = c; } diff --git a/SHADE_Engine/src/Physics/Interface/SHColliderComponent.h b/SHADE_Engine/src/Physics/Interface/SHColliderComponent.h index d0cc7064..3c8e6342 100644 --- a/SHADE_Engine/src/Physics/Interface/SHColliderComponent.h +++ b/SHADE_Engine/src/Physics/Interface/SHColliderComponent.h @@ -14,7 +14,7 @@ // Project Headers #include "ECS_Base/Components/SHComponent.h" -#include "Physics/Collision/SHCollider.h" +#include "Physics/Collision/SHCompositeCollider.h" namespace SHADE { @@ -54,8 +54,8 @@ namespace SHADE /* Getter Functions */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] SHCollider* const GetCollider () const noexcept; - [[nodiscard]] const SHCollider::CollisionShapes* const GetCollisionShapes() const noexcept; + [[nodiscard]] SHCompositeCollider* const GetCollider () const noexcept; + [[nodiscard]] const SHCompositeCollider::CollisionShapes* const GetCollisionShapes() const noexcept; [[nodiscard]] SHCollisionShape* const GetCollisionShape (int index) const; // Required for serialisation @@ -66,7 +66,7 @@ namespace SHADE /* Setter Functions */ /*---------------------------------------------------------------------------------*/ - void SetCollider (SHCollider* c) noexcept; + void SetCollider (SHCompositeCollider* c) noexcept; // Required for serialisation @@ -77,7 +77,7 @@ namespace SHADE /* Data Members */ /*---------------------------------------------------------------------------------*/ - SHCollider* collider; + SHCompositeCollider* collider; RTTR_ENABLE() }; diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp index 3bd26925..1b1055be 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.cpp @@ -122,7 +122,7 @@ namespace SHADE return onColliderDrawEvent.get()->handle; } - void SHPhysicsDebugDrawSystem::drawCollider(SHDebugDrawSystem* debugDrawSystem, const SHCollider& collider) noexcept + void SHPhysicsDebugDrawSystem::drawCollider(SHDebugDrawSystem* debugDrawSystem, const SHCompositeCollider& collider) noexcept { for (const auto* SHAPE : collider.GetCollisionShapes()) { @@ -132,12 +132,12 @@ namespace SHADE { case SHCollisionShape::Type::SPHERE: { - debugDrawSystem->DrawWireSphere(SHAPE->ComputeWorldTransform(), DRAW_COLOUR, true); + debugDrawSystem->DrawWireSphere(SHAPE->GetTRS(), DRAW_COLOUR, true); break; } case SHCollisionShape::Type::BOX: { - debugDrawSystem->DrawWireCube(SHAPE->ComputeWorldTransform(), DRAW_COLOUR, true); + debugDrawSystem->DrawWireCube(SHAPE->GetTRS(), DRAW_COLOUR, true); break; } case SHCollisionShape::Type::CAPSULE: diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h index efc0738e..c284792f 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h +++ b/SHADE_Engine/src/Physics/System/SHPhysicsDebugDrawSystem.h @@ -18,9 +18,9 @@ #include "ECS_Base/System/SHSystemRoutine.h" #include "Events/SHEvent.h" #include "Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h" -#include "Physics/Collision/SHCollider.h" +#include "Physics/Collision/SHCompositeCollider.h" #include "Physics/Collision/SHPhysicsRaycastResult.h" -#include "Physics/Collision/CollisionShapes/SHSphereCollisionShape.h" +#include "Physics/Collision/CollisionShapes/SHSphere.h" namespace SHADE @@ -138,7 +138,7 @@ namespace SHADE SHEventHandle onColliderDraw(SHEventPtr onColliderDrawEvent); - static void drawCollider (SHDebugDrawSystem* debugDrawSystem, const SHCollider& collider) noexcept; + static void drawCollider (SHDebugDrawSystem* debugDrawSystem, const SHCompositeCollider& collider) noexcept; static void drawRaycast (SHDebugDrawSystem* debugDrawSystem, const DebugDrawInfo::Raycast& raycastInfo) noexcept; }; diff --git a/SHADE_Engine/src/Serialization/SHYAMLConverters.h b/SHADE_Engine/src/Serialization/SHYAMLConverters.h index a746df1b..18d8d63f 100644 --- a/SHADE_Engine/src/Serialization/SHYAMLConverters.h +++ b/SHADE_Engine/src/Serialization/SHYAMLConverters.h @@ -1,7 +1,7 @@ #pragma once #include "Graphics/MiddleEnd/Interface/SHRenderable.h" #include "Graphics/MiddleEnd/Materials/SHMaterialSpec.h" -#include "Physics/Collision/CollisionShapes/SHSphereCollisionShape.h" +#include "Physics/Collision/CollisionShapes/SHSphere.h" #include "Resource/SHResourceManager.h" #include "Math/Vector/SHVec2.h" #include "Math/Vector/SHVec3.h" @@ -144,13 +144,13 @@ namespace YAML { case SHCollisionShape::Type::BOX: { - const auto& BOX = dynamic_cast(rhs); + const auto& BOX = dynamic_cast(rhs); node[HalfExtents] = BOX.GetRelativeExtents(); } break; case SHCollisionShape::Type::SPHERE: { - const auto& SPHERE = dynamic_cast(rhs); + const auto& SPHERE = dynamic_cast(rhs); node[Radius] = SPHERE.GetRelativeRadius(); } break; @@ -188,7 +188,7 @@ namespace YAML { if (node[HalfExtents].IsDefined()) { - auto* box = dynamic_cast(&rhs); + auto* box = dynamic_cast(&rhs); box->SetRelativeExtents(node[HalfExtents].as()); } } @@ -197,7 +197,7 @@ namespace YAML { if (node[Radius].IsDefined()) { - auto* sphere = dynamic_cast(&rhs); + auto* sphere = dynamic_cast(&rhs); sphere->SetRelativeRadius(node[Radius].as()); } } diff --git a/SHADE_Managed/src/Components/Collider.cxx b/SHADE_Managed/src/Components/Collider.cxx index 9ed46289..f4b4f09d 100644 --- a/SHADE_Managed/src/Components/Collider.cxx +++ b/SHADE_Managed/src/Components/Collider.cxx @@ -128,19 +128,19 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ Vector3 BoxCollider::Center::get() { - return Convert::ToCLI(getNativeCollisionShape().GetCenter()); + return Convert::ToCLI(getNativeCollisionShape().GetWorldCentroid()); } Vector3 BoxCollider::HalfExtents::get() { - return Convert::ToCLI(getNativeCollisionShape().GetWorldExtents()); + return Convert::ToCLI(getNativeCollisionShape().GetWorldExtents()); } void BoxCollider::HalfExtents::set(Vector3 value) { - getNativeCollisionShape().SetWorldExtents(Convert::ToNative(value)); + getNativeCollisionShape().SetWorldExtents(Convert::ToNative(value)); } Quaternion BoxCollider::Orientation::get() { - return Convert::ToCLI(getNativeCollisionShape().GetOrientation()); + return Convert::ToCLI(getNativeCollisionShape().GetWorldOrientation()); } /*---------------------------------------------------------------------------------*/ @@ -162,15 +162,15 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ Vector3 SphereCollider::Center::get() { - return Convert::ToCLI(getNativeCollisionShape().GetCenter()); + return Convert::ToCLI(getNativeCollisionShape().GetWorldCentroid()); } float SphereCollider::Radius::get() { - return getNativeCollisionShape().GetWorldRadius(); + return getNativeCollisionShape().GetWorldRadius(); } void SphereCollider::Radius::set(float value) { - getNativeCollisionShape().SetWorldRadius(value); + getNativeCollisionShape().SetWorldRadius(value); } /*---------------------------------------------------------------------------------*/ @@ -178,11 +178,11 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ bool SphereCollider::TestPoint(Vector3 point) { - return getNativeCollisionShape().TestPoint(Convert::ToNative(point)); + return getNativeCollisionShape().TestPoint(Convert::ToNative(point)); } bool SphereCollider::Raycast(Ray ray, float maxDistance) { - return getNativeCollisionShape().Raycast(Convert::ToNative(ray)); + return getNativeCollisionShape().Raycast(Convert::ToNative(ray)); } /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Managed/src/Physics/Physics.cxx b/SHADE_Managed/src/Physics/Physics.cxx index bfb79b75..60aba58d 100644 --- a/SHADE_Managed/src/Physics/Physics.cxx +++ b/SHADE_Managed/src/Physics/Physics.cxx @@ -179,20 +179,7 @@ namespace SHADE throw gcnew System::InvalidOperationException("Attempted to retrieve invalid CollisionShape."); const auto& NATIVE_SHAPE = managedShape->getNativeCollisionShape(); - switch (NATIVE_SHAPE.GetType()) - { - case SHCollisionShape::Type::SPHERE: - { - shapePos = Convert::ToCLI(dynamic_cast(NATIVE_SHAPE).GetCenter()); - break; - } - case SHCollisionShape::Type::BOX: - { - shapePos = Convert::ToCLI(dynamic_cast(NATIVE_SHAPE).GetCenter()); - break; - } - default: break; - } + shapePos = Convert::ToCLI(NATIVE_SHAPE.GetWorldCentroid()); ray.Position += shapePos; @@ -226,20 +213,7 @@ namespace SHADE throw gcnew System::InvalidOperationException("Attempted to retrieve invalid CollisionShape."); const auto& NATIVE_SHAPE = managedShape->getNativeCollisionShape(); - switch (NATIVE_SHAPE.GetType()) - { - case SHCollisionShape::Type::SPHERE: - { - shapePos = Convert::ToCLI(dynamic_cast(NATIVE_SHAPE).GetCenter()); - break; - } - case SHCollisionShape::Type::BOX: - { - shapePos = Convert::ToCLI(dynamic_cast(NATIVE_SHAPE).GetCenter()); - break; - } - default: break; - } + shapePos = Convert::ToCLI(NATIVE_SHAPE.GetWorldCentroid()); ray.Position += shapePos; @@ -306,20 +280,7 @@ namespace SHADE throw gcnew System::InvalidOperationException("Attempted to retrieve invalid CollisionShape."); const auto& NATIVE_SHAPE = managedShape->getNativeCollisionShape(); - switch (NATIVE_SHAPE.GetType()) - { - case SHCollisionShape::Type::SPHERE: - { - shapePos = Convert::ToCLI(dynamic_cast(NATIVE_SHAPE).GetCenter()); - break; - } - case SHCollisionShape::Type::BOX: - { - shapePos = Convert::ToCLI(dynamic_cast(NATIVE_SHAPE).GetCenter()); - break; - } - default: break; - } + shapePos = Convert::ToCLI(NATIVE_SHAPE.GetWorldCentroid()); start += shapePos;