From e1fb92e3f1ae9d0d760462bbe0b2432b749cac68 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Thu, 13 Oct 2022 17:32:53 +0800 Subject: [PATCH] Adding Bounding Spheres --- .../src/Math/Geometry/SHBoundingBox.cpp | 17 +- .../src/Math/Geometry/SHBoundingBox.h | 4 +- .../src/Math/Geometry/SHBoundingSphere.cpp | 183 ++++++++++++++++++ .../src/Math/Geometry/SHBoundingSphere.h | 83 ++++++++ SHADE_Engine/src/Math/Geometry/SHShape.h | 3 +- SHADE_Engine/src/Physics/SHPhysicsSystem.cpp | 2 +- 6 files changed, 277 insertions(+), 15 deletions(-) create mode 100644 SHADE_Engine/src/Math/Geometry/SHBoundingSphere.cpp create mode 100644 SHADE_Engine/src/Math/Geometry/SHBoundingSphere.h diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp index 11a14eed..0dd02caa 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp @@ -26,12 +26,12 @@ namespace SHADE SHBoundingBox::SHBoundingBox() noexcept { - type = Type::BOUNDING_BOX; + type = Type::BOX; } SHBoundingBox::SHBoundingBox(const SHVec3& c, const SHVec3& hE) noexcept { - type = Type::BOUNDING_BOX; + type = Type::BOX; Center = c; Extents = hE; @@ -43,7 +43,7 @@ namespace SHADE if (this == &rhs) return; - type = Type::BOUNDING_BOX; + type = Type::BOX; Center = rhs.Center; Extents = rhs.Extents; @@ -51,7 +51,7 @@ namespace SHADE SHBoundingBox::SHBoundingBox(SHBoundingBox&& rhs) noexcept { - type = Type::BOUNDING_BOX; + type = Type::BOX; Center = rhs.Center; Extents = rhs.Extents; @@ -63,14 +63,11 @@ namespace SHADE SHBoundingBox& SHBoundingBox::operator=(const SHBoundingBox& rhs) noexcept { - if (this == &rhs) - return *this; - - if (rhs.type != Type::BOUNDING_BOX) + if (rhs.type != Type::BOX) { SHLOG_WARNING("Cannot assign a non-bounding box to a bounding box!") } - else + else if (this != &rhs) { Center = rhs.Center; Extents = rhs.Extents; @@ -81,7 +78,7 @@ namespace SHADE SHBoundingBox& SHBoundingBox::operator=(SHBoundingBox&& rhs) noexcept { - if (rhs.type != Type::BOUNDING_BOX) + if (rhs.type != Type::BOX) { SHLOG_WARNING("Cannot assign a non-bounding box to a bounding box!") } diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h index 5c38e1b8..2fdc6634 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h @@ -36,7 +36,7 @@ namespace SHADE /* Constructors & Destructor */ /*---------------------------------------------------------------------------------*/ - ~SHBoundingBox () noexcept = default; + ~SHBoundingBox () override = default; SHBoundingBox () noexcept; SHBoundingBox (const SHVec3& center, const SHVec3& halfExtents) noexcept; @@ -44,7 +44,7 @@ namespace SHADE SHBoundingBox (SHBoundingBox&& rhs) noexcept; /*---------------------------------------------------------------------------------*/ - /* Operator Overloads */ + /* Operator Overloads */ /*---------------------------------------------------------------------------------*/ SHBoundingBox& operator= (const SHBoundingBox& rhs) noexcept; diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.cpp b/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.cpp new file mode 100644 index 00000000..c9bdcfe2 --- /dev/null +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.cpp @@ -0,0 +1,183 @@ +/**************************************************************************************** + * \file SHBoundingSphere.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 "SHBoundingSphere.h" +// Project Headers +#include "Math/SHMathHelpers.h" +#include "Math/SHRay.h" + +using namespace DirectX; + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Constructors & Destructor Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHBoundingSphere::SHBoundingSphere() noexcept + { + type = Type::SPHERE; + } + + SHBoundingSphere::SHBoundingSphere(const SHVec3& center, float radius) noexcept + { + type = Type::SPHERE; + + Center = center; + Radius = radius; + } + + SHBoundingSphere::SHBoundingSphere(const SHBoundingSphere& rhs) noexcept + { + if (this == &rhs) + return; + + type = Type::SPHERE; + + Center = rhs.Center; + Radius = rhs.Radius; + } + + SHBoundingSphere::SHBoundingSphere(SHBoundingSphere&& rhs) noexcept + { + type = Type::SPHERE; + + Center = rhs.Center; + Radius = rhs.Radius; + } + + /*-----------------------------------------------------------------------------------*/ + /* Operator Overload Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHBoundingSphere& SHBoundingSphere::operator=(const SHBoundingSphere& rhs) noexcept + { + if (rhs.type != Type::SPHERE) + { + SHLOG_WARNING("Cannot assign a non-sphere to a sphere!") + } + else if (this != &rhs) + { + Center = rhs.Center; + Radius = rhs.Radius; + } + + return *this; + } + + SHBoundingSphere& SHBoundingSphere::operator=(SHBoundingSphere&& rhs) noexcept + { + if (rhs.type != Type::SPHERE) + { + SHLOG_WARNING("Cannot assign a non-sphere to a sphere!") + } + else + { + Center = rhs.Center; + Radius = rhs.Radius; + } + + return *this; + } + + /*-----------------------------------------------------------------------------------*/ + /* Getter Function Definitions */ + /*-----------------------------------------------------------------------------------*/ + + const SHVec3& SHBoundingSphere::GetCenter() const noexcept + { + return Center; + } + + float SHBoundingSphere::GetRadius() const noexcept + { + return Radius; + } + + /*-----------------------------------------------------------------------------------*/ + /* Setter Function Definitions */ + /*-----------------------------------------------------------------------------------*/ + + void SHBoundingSphere::SetCenter(const SHVec3& center) noexcept + { + Center = center; + } + + void SHBoundingSphere::SetRadius(float radius) noexcept + { + Radius = radius; + } + + /*-----------------------------------------------------------------------------------*/ + /* Public Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + bool SHBoundingSphere::TestPoint(const SHVec3& point) noexcept + { + return BoundingSphere::Contains(point); + } + + bool SHBoundingSphere::Raycast(const SHRay& ray, float& distance) noexcept + { + return Intersects(ray.position, ray.direction, distance); + } + + bool SHBoundingSphere::Contains(const SHBoundingSphere& rhs) const noexcept + { + return BoundingSphere::Contains(rhs); + } + + float SHBoundingSphere::Volume() const noexcept + { + return (4.0f / 3.0f) * SHMath::PI * (Radius * Radius * Radius); + } + + float SHBoundingSphere::SurfaceArea() const noexcept + { + return 4.0f * SHMath::PI * (Radius * Radius); + } + + /*-----------------------------------------------------------------------------------*/ + /* Static Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHBoundingSphere SHBoundingSphere::Combine(const SHBoundingSphere& lhs, const SHBoundingSphere& rhs) noexcept + { + SHBoundingSphere result; + CreateMerged(result, lhs, rhs); + return result; + } + + bool SHBoundingSphere::Intersect(const SHBoundingSphere& lhs, const SHBoundingSphere& rhs) noexcept + { + return lhs.Intersects(rhs); + } + + SHBoundingSphere SHBoundingSphere::BuildFromSpheres(const SHBoundingSphere* spheres, size_t numSpheres) noexcept + { + SHBoundingSphere result; + + for (size_t i = 1; i < numSpheres; ++i) + CreateMerged(result, spheres[i - 1], spheres[i]); + + return result; + } + + SHBoundingSphere SHBoundingSphere::BuildFromVertices(const SHVec3* vertices, size_t numVertices, size_t stride) noexcept + { + SHBoundingSphere result; + CreateFromPoints(result, numVertices, vertices, stride); + return result; + } + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.h b/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.h new file mode 100644 index 00000000..00ce3e57 --- /dev/null +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.h @@ -0,0 +1,83 @@ +/**************************************************************************************** + * \file SHBoundingSphere.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" +#include "SH_API.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + + class SH_API SHBoundingSphere : public SHShape, + private DirectX::BoundingSphere + { + public: + /*---------------------------------------------------------------------------------*/ + /* Constructors & Destructor */ + /*---------------------------------------------------------------------------------*/ + + SHBoundingSphere () noexcept; + SHBoundingSphere (const SHVec3& center, float radius) noexcept; + SHBoundingSphere (const SHBoundingSphere& rhs) noexcept; + SHBoundingSphere (SHBoundingSphere&& rhs) noexcept; + + ~SHBoundingSphere () override = default; + + /*---------------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*---------------------------------------------------------------------------------*/ + + SHBoundingSphere& operator= (const SHBoundingSphere& rhs) noexcept; + SHBoundingSphere& operator= (SHBoundingSphere&& rhs) noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Getter Functions */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] const SHVec3& GetCenter () const noexcept; + [[nodiscard]] float GetRadius () const noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Setter Functions */ + /*---------------------------------------------------------------------------------*/ + + void SetCenter (const SHVec3& center) noexcept; + void SetRadius (float radius) noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Function Members */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] bool TestPoint (const SHVec3& point) noexcept override; + [[nodiscard]] bool Raycast (const SHRay& ray, float& distance) noexcept override; + + [[nodiscard]] bool Contains (const SHBoundingSphere& rhs) const noexcept; + [[nodiscard]] float Volume () const noexcept; + [[nodiscard]] float SurfaceArea () const noexcept; + + + /*---------------------------------------------------------------------------------*/ + /* Static Function Members */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] static SHBoundingSphere Combine (const SHBoundingSphere& lhs, const SHBoundingSphere& rhs) noexcept; + [[nodiscard]] static bool Intersect (const SHBoundingSphere& lhs, const SHBoundingSphere& rhs) noexcept; + [[nodiscard]] static SHBoundingSphere BuildFromSpheres (const SHBoundingSphere* spheres, size_t numSpheres) noexcept; + [[nodiscard]] static SHBoundingSphere BuildFromVertices (const SHVec3* vertices, size_t numVertices, size_t stride = 0) noexcept; + + }; +} // namespace SHADE diff --git a/SHADE_Engine/src/Math/Geometry/SHShape.h b/SHADE_Engine/src/Math/Geometry/SHShape.h index 69578cb3..ab2db558 100644 --- a/SHADE_Engine/src/Math/Geometry/SHShape.h +++ b/SHADE_Engine/src/Math/Geometry/SHShape.h @@ -31,11 +31,10 @@ namespace SHADE enum class Type { - BOUNDING_BOX + BOX , SPHERE , CAPSULE , CONVEX_HULL - , TRIANGLE , COUNT , NONE = -1 diff --git a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp index f3a4c276..6f3cadb6 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp @@ -398,7 +398,7 @@ namespace SHADE switch (shape->GetType()) { - case SHShape::Type::BOUNDING_BOX: + case SHShape::Type::BOX: { auto* box = reinterpret_cast(shape); const SHVec3& SHADE_EXTENTS = box->GetHalfExtents();