SP3-2 Cleaned Up Physics System #85

Merged
direnbharwani merged 11 commits from SP3-2-Physics into main 2022-10-13 18:32:25 +08:00
6 changed files with 277 additions and 15 deletions
Showing only changes of commit e1fb92e3f1 - Show all commits

View File

@ -26,12 +26,12 @@ namespace SHADE
SHBoundingBox::SHBoundingBox() noexcept SHBoundingBox::SHBoundingBox() noexcept
{ {
type = Type::BOUNDING_BOX; type = Type::BOX;
} }
SHBoundingBox::SHBoundingBox(const SHVec3& c, const SHVec3& hE) noexcept SHBoundingBox::SHBoundingBox(const SHVec3& c, const SHVec3& hE) noexcept
{ {
type = Type::BOUNDING_BOX; type = Type::BOX;
Center = c; Center = c;
Extents = hE; Extents = hE;
@ -43,7 +43,7 @@ namespace SHADE
if (this == &rhs) if (this == &rhs)
return; return;
type = Type::BOUNDING_BOX; type = Type::BOX;
Center = rhs.Center; Center = rhs.Center;
Extents = rhs.Extents; Extents = rhs.Extents;
@ -51,7 +51,7 @@ namespace SHADE
SHBoundingBox::SHBoundingBox(SHBoundingBox&& rhs) noexcept SHBoundingBox::SHBoundingBox(SHBoundingBox&& rhs) noexcept
{ {
type = Type::BOUNDING_BOX; type = Type::BOX;
Center = rhs.Center; Center = rhs.Center;
Extents = rhs.Extents; Extents = rhs.Extents;
@ -63,14 +63,11 @@ namespace SHADE
SHBoundingBox& SHBoundingBox::operator=(const SHBoundingBox& rhs) noexcept SHBoundingBox& SHBoundingBox::operator=(const SHBoundingBox& rhs) noexcept
{ {
if (this == &rhs) if (rhs.type != Type::BOX)
return *this;
if (rhs.type != Type::BOUNDING_BOX)
{ {
SHLOG_WARNING("Cannot assign a non-bounding box to a bounding box!") SHLOG_WARNING("Cannot assign a non-bounding box to a bounding box!")
} }
else else if (this != &rhs)
{ {
Center = rhs.Center; Center = rhs.Center;
Extents = rhs.Extents; Extents = rhs.Extents;
@ -81,7 +78,7 @@ namespace SHADE
SHBoundingBox& SHBoundingBox::operator=(SHBoundingBox&& rhs) noexcept 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!") SHLOG_WARNING("Cannot assign a non-bounding box to a bounding box!")
} }

View File

@ -36,7 +36,7 @@ namespace SHADE
/* Constructors & Destructor */ /* Constructors & Destructor */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
~SHBoundingBox () noexcept = default; ~SHBoundingBox () override = default;
SHBoundingBox () noexcept; SHBoundingBox () noexcept;
SHBoundingBox (const SHVec3& center, const SHVec3& halfExtents) noexcept; SHBoundingBox (const SHVec3& center, const SHVec3& halfExtents) noexcept;
@ -44,7 +44,7 @@ namespace SHADE
SHBoundingBox (SHBoundingBox&& rhs) noexcept; SHBoundingBox (SHBoundingBox&& rhs) noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Operator Overloads */ /* Operator Overloads */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
SHBoundingBox& operator= (const SHBoundingBox& rhs) noexcept; SHBoundingBox& operator= (const SHBoundingBox& rhs) noexcept;

View File

@ -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 <SHpch.h>
// 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

View File

@ -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 <DirectXCollision.h>
// 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

View File

@ -31,11 +31,10 @@ namespace SHADE
enum class Type enum class Type
{ {
BOUNDING_BOX BOX
, SPHERE , SPHERE
, CAPSULE , CAPSULE
, CONVEX_HULL , CONVEX_HULL
, TRIANGLE
, COUNT , COUNT
, NONE = -1 , NONE = -1

View File

@ -398,7 +398,7 @@ namespace SHADE
switch (shape->GetType()) switch (shape->GetType())
{ {
case SHShape::Type::BOUNDING_BOX: case SHShape::Type::BOX:
{ {
auto* box = reinterpret_cast<SHBoundingBox*>(shape); auto* box = reinterpret_cast<SHBoundingBox*>(shape);
const SHVec3& SHADE_EXTENTS = box->GetHalfExtents(); const SHVec3& SHADE_EXTENTS = box->GetHalfExtents();