Removed Redundant Geometry

This commit is contained in:
Diren D Bharwani 2023-01-04 17:48:08 +08:00
parent a49c674c2b
commit dd2fc934a2
36 changed files with 556 additions and 1261 deletions

View File

@ -358,7 +358,7 @@ namespace SHADE
{ {
SHEditorWidgets::BeginPanel(std::format("{} Box #{}", ICON_FA_CUBE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y }); SHEditorWidgets::BeginPanel(std::format("{} Box #{}", ICON_FA_CUBE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y });
auto* box = reinterpret_cast<SHBoxCollisionShape*>(shape); auto* box = reinterpret_cast<SHBox*>(shape);
SHEditorWidgets::DragVec3 SHEditorWidgets::DragVec3
( (
"Half Extents", { "X", "Y", "Z" }, "Half Extents", { "X", "Y", "Z" },
@ -368,7 +368,7 @@ namespace SHADE
else if (shape->GetType() == SHCollisionShape::Type::SPHERE) else if (shape->GetType() == SHCollisionShape::Type::SPHERE)
{ {
SHEditorWidgets::BeginPanel(std::format("{} Sphere #{}", ICON_MD_CIRCLE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y }); SHEditorWidgets::BeginPanel(std::format("{} Sphere #{}", ICON_MD_CIRCLE, i).data(), { ImGui::GetContentRegionAvail().x, ImGui::GetContentRegionAvail().y });
auto* sphere = reinterpret_cast<SHSphereCollisionShape*>(shape); auto* sphere = reinterpret_cast<SHSphere*>(shape);
SHEditorWidgets::DragFloat SHEditorWidgets::DragFloat
( (
"Radius", "Radius",

View File

@ -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 <SHpch.h>
// 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<SHVec3> SHBox::GetVertices() const noexcept
{
std::vector<SHVec3> 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

View File

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

View File

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

View File

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

View File

@ -11,12 +11,12 @@
#include <SHpch.h> #include <SHpch.h>
// Primary Header // Primary Header
#include "SHBoxCollisionShape.h" #include "SHBox.h"
// Project Headers // Project Headers
#include "Math/SHMathHelpers.h" #include "Math/SHMathHelpers.h"
#include "Math/SHMatrix.h" #include "Math/SHMatrix.h"
#include "Physics/Collision/SHCollider.h" #include "Physics/Collision/SHCompositeCollider.h"
namespace SHADE namespace SHADE
{ {
@ -24,66 +24,46 @@ namespace SHADE
/* Constructors & Destructor Definitions */ /* Constructors & Destructor Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHBoxCollisionShape::SHBoxCollisionShape(SHCollisionShapeID id) noexcept SHBox::SHBox(SHCollisionShapeID id) noexcept
: SHConvexPolyhedronCollisionShape (id, SHCollisionShape::Type::BOX) : SHConvexPolyhedron (id, Type::BOX)
, SHBox ()
, relativeExtents { SHVec3::One } , relativeExtents { SHVec3::One }
, scale { 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 }
{ {
halfEdgeStructure = rhs.halfEdgeStructure; Extents = SHVec3::One * 0.5f;
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;
} }
SHBoxCollisionShape::SHBoxCollisionShape(SHBoxCollisionShape&& rhs) noexcept SHBox::SHBox(const SHBox& rhs) noexcept
: SHConvexPolyhedronCollisionShape (rhs.id, SHCollisionShape::Type::BOX) : SHConvexPolyhedron ( rhs )
, SHBox (rhs.Center, rhs.Extents, rhs.Orientation)
, relativeExtents { rhs.relativeExtents } , relativeExtents { rhs.relativeExtents }
, scale { rhs.scale } , scale { rhs.scale }
{ {
halfEdgeStructure = rhs.halfEdgeStructure; Center = rhs.Center;
Extents = rhs.Extents;
Orientation = rhs.Orientation;
}
material = rhs.material; SHBox::SHBox(SHBox&& rhs) noexcept
collider = rhs.collider; : SHConvexPolyhedron ( rhs )
transform = rhs.transform; , relativeExtents { rhs.relativeExtents }
rotationOffset = rhs.rotationOffset; , scale { rhs.scale }
flags = rhs.flags; {
// Since all collision tags are taken from the matrix, we do not need to do a deep copy here. Center = rhs.Center;
collisionTag = rhs.collisionTag; Extents = rhs.Extents;
Orientation = rhs.Orientation;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Operator Overload Definitions */ /* Operator Overload Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHBoxCollisionShape& SHBoxCollisionShape::operator=(const SHBoxCollisionShape& rhs) noexcept SHBox& SHBox::operator=(const SHBox& rhs) noexcept
{ {
if (this == &rhs) if (this == &rhs)
return *this; return *this;
// Collision Shape Properties // Collision Shape Properties
id = rhs.id; SHConvexPolyhedron::operator=(rhs);
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;
// Box Properties // Box Properties
@ -95,23 +75,15 @@ namespace SHADE
relativeExtents = rhs.relativeExtents; relativeExtents = rhs.relativeExtents;
scale = rhs.scale; scale = rhs.scale;
halfEdgeStructure = rhs.halfEdgeStructure;
return *this; return *this;
} }
SHBoxCollisionShape& SHBoxCollisionShape::operator=(SHBoxCollisionShape&& rhs) noexcept SHBox& SHBox::operator=(SHBox&& rhs) noexcept
{ {
// Collision Shape Properties // Collision Shape Properties
id = rhs.id; SHConvexPolyhedron::operator=(rhs);
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;
// Box Properties // Box Properties
@ -123,7 +95,6 @@ namespace SHADE
relativeExtents = rhs.relativeExtents; relativeExtents = rhs.relativeExtents;
scale = rhs.scale; scale = rhs.scale;
halfEdgeStructure = rhs.halfEdgeStructure;
return *this; return *this;
} }
@ -132,32 +103,28 @@ namespace SHADE
/* Getter Function Definitions */ /* Getter Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHVec3 SHBoxCollisionShape::GetCenter() const noexcept SHVec3 SHBox::GetWorldExtents() const noexcept
{
return Center;
}
SHVec3 SHBoxCollisionShape::GetWorldExtents() const noexcept
{ {
return Extents; return Extents;
} }
SHVec3 SHBoxCollisionShape::GetRelativeExtents() const noexcept SHVec3 SHBox::GetRelativeExtents() const noexcept
{ {
return relativeExtents; 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) if (index < 0 || index >= NUM_VERTICES)
throw std::invalid_argument("Index out-of-range!"); 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 // Get local normal
const SHVec3& LOCAL_NORMAL = halfEdgeStructure->GetFace(faceIndex).normal; const SHVec3& LOCAL_NORMAL = halfEdgeStructure->GetFace(faceIndex).normal;
@ -166,88 +133,47 @@ namespace SHADE
return SHVec3::Rotate(LOCAL_NORMAL, Orientation); return SHVec3::Rotate(LOCAL_NORMAL, Orientation);
} }
SHVec3 SHBoxCollisionShape::GetPosition() const noexcept SHVec3 SHBox::GetWorldCentroid() const noexcept
{ {
return Center; 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 SHVec3 SHBox::GetLocalCentroid() const noexcept
{
return Volume();
}
SHVec3 SHBoxCollisionShape::GetLocalCentroid() const noexcept
{ {
return SHVec3::Zero; return SHVec3::Zero;
} }
/*-----------------------------------------------------------------------------------*/ SHQuaternion SHBox::GetWorldOrientation() const noexcept
/* Setter Function Definitions */
/*-----------------------------------------------------------------------------------*/
void SHBoxCollisionShape::SetCenter(const SHVec3& newCenter) noexcept
{ {
Center = newCenter; return Orientation;
} }
void SHBoxCollisionShape::SetWorldExtents(const SHVec3& newWorldExtents) noexcept SHQuaternion SHBox::GetRelativeOrientation() const noexcept
{ {
Extents = newWorldExtents; return transform.orientation;
// Recompute Relative radius
relativeExtents = 2.0f * Extents / scale;
} }
void SHBoxCollisionShape::SetRelativeExtents(const SHVec3& newRelativeExtents) noexcept float SHBox::GetVolume() const noexcept
{ {
relativeExtents = newRelativeExtents; return 8.0f * (Extents.x * Extents.y * Extents.z);
// Recompute world radius
Extents = relativeExtents * scale * 0.5f;
} }
void SHBoxCollisionShape::SetScale(const SHVec3& newScale) noexcept float SHBox::GetSurfaceArea() const noexcept
{ {
scale = SHVec3::Abs(newScale); return 8.0f * (Extents.x * Extents.y
+ Extents.x * Extents.z
// Recompute world radius + Extents.y * Extents.z);
Extents = relativeExtents * scale * 0.5f;
} }
/*-----------------------------------------------------------------------------------*/ SHMatrix SHBox::GetInertiaTensor(float mass) const noexcept
/* 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
{ {
static constexpr float ONE_OVER_TWELVE = (1.0f / 12.0f); static constexpr float ONE_OVER_TWELVE = (1.0f / 12.0f);
@ -270,30 +196,93 @@ namespace SHADE
return result; return result;
} }
SHMatrix SHBoxCollisionShape::ComputeWorldTransform() const noexcept /*-----------------------------------------------------------------------------------*/
{ /* Setter Function Definitions */
const SHTransform& PARENT_TRANSFORM = collider->GetTransform(); /*-----------------------------------------------------------------------------------*/
const SHQuaternion ROTATION = PARENT_TRANSFORM.orientation * transform.orientation;
const SHVec3 SCALE = SHVec3{ Extents } *2.0f;
return SHMatrix::Transform void SHBox::SetWorldExtents(const SHVec3& newWorldExtents) noexcept
( {
Center Extents = newWorldExtents;
, ROTATION
, SCALE // 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<float>::max() }; SHVec3 min{ std::numeric_limits<float>::max() };
SHVec3 max{ std::numeric_limits<float>::lowest() }; SHVec3 max{ std::numeric_limits<float>::lowest() };
const auto& VERTICES = GetVertices(); SHVec3 vertices[NUM_VERTICES];
for (auto& vtx : VERTICES) GetCorners(vertices);
for (auto& vertex : vertices)
{ {
min = SHVec3::Min({ vtx, min }); min = SHVec3::Min({ vertex, min });
max = SHVec3::Max({ vtx, max }); max = SHVec3::Max({ vertex, max });
} }
const SHVec3 HALF_EXTENTS = (max - min) * 0.5f; const SHVec3 HALF_EXTENTS = (max - min) * 0.5f;

View File

@ -1,5 +1,5 @@
/**************************************************************************************** /****************************************************************************************
* \file SHBoxCollisionShape.h * \file SHBox.h
* \author Diren D Bharwani, diren.dbharwani, 390002520 * \author Diren D Bharwani, diren.dbharwani, 390002520
* \brief Interface for a Box Collision Shape. * \brief Interface for a Box Collision Shape.
* *
@ -10,9 +10,10 @@
#pragma once #pragma once
#include <DirectXCollision.h>
// Project Headers // Project Headers
#include "Math/Geometry/SHBox.h" #include "SHConvexPolyhedron.h"
#include "SHConvexPolyhedronCollisionShape.h"
namespace SHADE namespace SHADE
{ {
@ -38,57 +39,67 @@ namespace SHADE
* @brief * @brief
* Encapsulate a Box Shape used for Physics Simulations. * Encapsulate a Box Shape used for Physics Simulations.
*/ */
class SH_API SHBoxCollisionShape final : public SHConvexPolyhedronCollisionShape class SH_API SHBox final : public SHConvexPolyhedron
, private SHBox , private DirectX::BoundingOrientedBox
{ {
private: private:
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Friends */ /* Friends */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
friend class SHCollider; friend class SHCompositeCollider;
friend class SHCollision; friend class SHCollision;
friend class SHCollisionShapeLibrary; friend class SHCollisionShapeLibrary;
public: public:
/*---------------------------------------------------------------------------------*/
/* Data Members */
/*---------------------------------------------------------------------------------*/
static constexpr int NUM_VERTICES = 8;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Constructors & Destructor */ /* Constructors & Destructor */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
SHBoxCollisionShape (SHCollisionShapeID id) noexcept; SHBox (SHCollisionShapeID id) noexcept;
SHBoxCollisionShape (const SHBoxCollisionShape& rhs) noexcept; SHBox (const SHBox& rhs) noexcept;
SHBoxCollisionShape (SHBoxCollisionShape&& rhs) noexcept; SHBox (SHBox&& rhs) noexcept;
~SHBoxCollisionShape () override = default; ~SHBox () override = default;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Operator Overloads */ /* Operator Overloads */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
SHBoxCollisionShape& operator= (const SHBoxCollisionShape& rhs) noexcept; SHBox& operator= (const SHBox& rhs) noexcept;
SHBoxCollisionShape& operator= (SHBoxCollisionShape&& rhs) noexcept; SHBox& operator= (SHBox&& rhs) noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Getter Functions */ /* Getter Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] SHVec3 GetCenter () const noexcept;
[[nodiscard]] SHVec3 GetWorldExtents () const noexcept; [[nodiscard]] SHVec3 GetWorldExtents () const noexcept;
[[nodiscard]] SHVec3 GetRelativeExtents () const noexcept; [[nodiscard]] SHVec3 GetRelativeExtents () const noexcept;
// Overriden Methods
[[nodiscard]] SHVec3 GetVertex (int index) const override; [[nodiscard]] SHVec3 GetVertex (int index) const override;
[[nodiscard]] SHVec3 GetNormal (int faceIndex) const override; [[nodiscard]] SHVec3 GetNormal (int faceIndex) const override;
[[nodiscard]] SHVec3 GetPosition () const noexcept override; [[nodiscard]] SHVec3 GetWorldCentroid () const noexcept override;
[[nodiscard]] SHQuaternion GetOrientation () const noexcept override; [[nodiscard]] SHVec3 GetRelativeCentroid () const noexcept override;
[[nodiscard]] float GetVolume () const noexcept override;
[[nodiscard]] SHVec3 GetLocalCentroid () 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 */ /* Setter Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
void SetCenter (const SHVec3& newCenter) noexcept;
void SetWorldExtents (const SHVec3& newWorldExtents) noexcept; void SetWorldExtents (const SHVec3& newWorldExtents) noexcept;
void SetRelativeExtents (const SHVec3& newRelativeExtents) noexcept; void SetRelativeExtents (const SHVec3& newRelativeExtents) noexcept;
void SetScale (const SHVec3& newScale) noexcept; void SetScale (const SHVec3& newScale) noexcept;
@ -97,57 +108,10 @@ namespace SHADE
/* Function Members */ /* Function Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/** void Update () noexcept override;
* @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; [[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. <br/>
* See the corresponding header for the contents of the object.
*/
[[nodiscard]] SHRaycastResult Raycast (const SHRay& ray) const noexcept override; [[nodiscard]] SHRaycastResult Raycast (const SHRay& ray) const noexcept override;
[[nodiscard]] SHMatrix GetTRS () 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; [[nodiscard]] SHAABB ComputeAABB () const noexcept override;
private: private:
@ -156,7 +120,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
SHVec3 relativeExtents; SHVec3 relativeExtents;
SHVec3 scale; // Intended to be passed in by the base collider. SHVec3 scale;
}; };

View File

@ -14,7 +14,7 @@
#include "SHCollisionShape.h" #include "SHCollisionShape.h"
// Project Headers // Project Headers
#include "Physics/Collision/SHCollider.h" #include "Physics/Collision/SHCompositeCollider.h"
#include "Physics/Collision/CollisionTags/SHCollisionTagMatrix.h" #include "Physics/Collision/CollisionTags/SHCollisionTagMatrix.h"
#include "Reflection/SHReflectionMetadata.h" #include "Reflection/SHReflectionMetadata.h"
#include "Tools/Utilities/SHUtilities.h" #include "Tools/Utilities/SHUtilities.h"
@ -112,25 +112,15 @@ namespace SHADE
return *collisionTag; 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 */ /* Setter Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void SHCollisionShape::SetCollisionTag(SHCollisionTag* newCollisionTag) noexcept
{
collisionTag = newCollisionTag;
}
void SHCollisionShape::SetFriction(float friction) noexcept void SHCollisionShape::SetFriction(float friction) noexcept
{ {
material.SetFriction(friction); material.SetFriction(friction);
@ -155,7 +145,7 @@ namespace SHADE
{ {
transform.position = posOffset; transform.position = posOffset;
ComputeTransforms(); Update();
} }
void SHCollisionShape::SetRotationOffset(const SHVec3& rotOffset) noexcept void SHCollisionShape::SetRotationOffset(const SHVec3& rotOffset) noexcept
@ -163,7 +153,7 @@ namespace SHADE
rotationOffset = rotOffset; rotationOffset = rotOffset;
transform.orientation = SHQuaternion::FromEuler(rotationOffset); transform.orientation = SHQuaternion::FromEuler(rotationOffset);
ComputeTransforms(); Update();
} }
void SHCollisionShape::SetIsTrigger(bool isTrigger) noexcept void SHCollisionShape::SetIsTrigger(bool isTrigger) noexcept
@ -174,11 +164,6 @@ namespace SHADE
isTrigger ? flags |= FLAG_VALUE : flags &= ~FLAG_VALUE; isTrigger ? flags |= FLAG_VALUE : flags &= ~FLAG_VALUE;
} }
void SHCollisionShape::SetCollisionTag(SHCollisionTag* newCollisionTag) noexcept
{
collisionTag = newCollisionTag;
}
} // namespace SHADE } // namespace SHADE
RTTR_REGISTRATION RTTR_REGISTRATION

View File

@ -41,7 +41,7 @@ namespace SHADE
/* Friends */ /* Friends */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
friend class SHCollider; friend class SHCompositeCollider;
friend class SHColliderComponent; friend class SHColliderComponent;
friend class SHCollisionShapeLibrary; friend class SHCollisionShapeLibrary;
friend class SHCollisionSpace; friend class SHCollisionSpace;
@ -57,6 +57,7 @@ namespace SHADE
SPHERE SPHERE
, BOX , BOX
, CAPSULE , CAPSULE
, CONVEX_HULL
, COUNT , COUNT
, INVALID = -1 , INVALID = -1
@ -94,8 +95,6 @@ namespace SHADE
[[nodiscard]] float GetDensity () const noexcept; [[nodiscard]] float GetDensity () const noexcept;
[[nodiscard]] const SHPhysicsMaterial& GetMaterial () const noexcept; [[nodiscard]] const SHPhysicsMaterial& GetMaterial () const noexcept;
// Offsets // Offsets
[[nodiscard]] const SHVec3& GetPositionOffset () const noexcept; [[nodiscard]] const SHVec3& GetPositionOffset () const noexcept;
@ -111,16 +110,21 @@ namespace SHADE
// Virtual methods // Virtual methods
[[nodiscard]] virtual SHVec3 GetPosition () const noexcept; [[nodiscard]] virtual SHVec3 GetWorldCentroid () const noexcept = 0;
[[nodiscard]] virtual SHQuaternion GetOrientation () const noexcept; [[nodiscard]] virtual SHVec3 GetRelativeCentroid () const noexcept = 0;
[[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 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 */ /* Setter Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
void SetCollisionTag (SHCollisionTag* newCollisionTag) noexcept;
void SetFriction (float friction) noexcept; void SetFriction (float friction) noexcept;
void SetBounciness (float bounciness) noexcept; void SetBounciness (float bounciness) noexcept;
void SetDensity (float density) noexcept; void SetDensity (float density) noexcept;
@ -131,16 +135,53 @@ namespace SHADE
// Flags // Flags
// Forces rigidbody to recompute mass if one exists
void SetIsTrigger (bool isTrigger) noexcept; void SetIsTrigger (bool isTrigger) noexcept;
void SetCollisionTag (SHCollisionTag* newCollisionTag) noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Member Functions */ /* Member Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
virtual void ComputeTransforms () noexcept = 0; /**
[[nodiscard]] virtual SHMatrix ComputeWorldTransform () 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; [[nodiscard]] virtual SHAABB ComputeAABB () const noexcept = 0;
protected: protected:
@ -150,16 +191,17 @@ namespace SHADE
SHCollisionShapeID id; 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; // Stores the local position and rotation.
SHTransform transform;
// Needed for conversion to euler angles // Needed for conversion to euler angles
SHVec3 rotationOffset; SHVec3 rotationOffset;
uint8_t flags; // 0 0 0 isColliding trigger capsule sphere box uint8_t flags; // 0 0 0 trigger convexHull capsule sphere box
SHCollisionTag* collisionTag;
RTTR_ENABLE() RTTR_ENABLE()
}; };

View File

@ -39,12 +39,12 @@ namespace SHADE
/* Public Member Function Definitions */ /* 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) if (RESULT.second)
{ {
SHSphereCollisionShape* sphere = RESULT.first->second; SHSphere* sphere = RESULT.first->second;
sphere->Center = createInfo.Center; sphere->Center = createInfo.Center;
sphere->Radius = createInfo.Radius; sphere->Radius = createInfo.Radius;
@ -57,12 +57,12 @@ namespace SHADE
return spheres.find(id)->second; 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) if (RESULT.second)
{ {
SHBoxCollisionShape* box = RESULT.first->second; SHBox* box = RESULT.first->second;
box->Center = createInfo.Center; box->Center = createInfo.Center;
box->Extents = createInfo.Extents; box->Extents = createInfo.Extents;
@ -90,7 +90,7 @@ namespace SHADE
} }
case SHCollisionShape::Type::SPHERE: case SHCollisionShape::Type::SPHERE:
{ {
SHSphereCollisionShape* sphere = spheres.find(shape->id)->second; SHSphere* sphere = spheres.find(shape->id)->second;
spheres.erase(shape->id); spheres.erase(shape->id);
delete sphere; delete sphere;

View File

@ -13,8 +13,8 @@
#include <unordered_map> #include <unordered_map>
// Project Header // Project Header
#include "SHSphereCollisionShape.h" #include "SHSphere.h"
#include "SHBoxCollisionShape.h" #include "SHBox.h"
namespace SHADE namespace SHADE
{ {
@ -51,7 +51,7 @@ namespace SHADE
* @return * @return
* A new sphere collision shape. * A new sphere collision shape.
*/ */
SHSphereCollisionShape* CreateSphere (SHCollisionShapeID id, const SHSphereCreateInfo& createInfo); SHSphere* CreateSphere (SHCollisionShapeID id, const SHSphereCreateInfo& createInfo);
/** /**
* @brief * @brief
@ -63,7 +63,7 @@ namespace SHADE
* @return * @return
* A new box collision shape. * A new box collision shape.
*/ */
SHBoxCollisionShape* CreateBox (SHCollisionShapeID id, const SHBoxCreateInfo& createInfo); SHBox* CreateBox (SHCollisionShapeID id, const SHBoxCreateInfo& createInfo);
/** /**
* @brief * @brief
@ -85,8 +85,8 @@ namespace SHADE
// We use unordered maps for fast lookup when deleting. // 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. // 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<SHCollisionShapeID, SHSphereCollisionShape*, SHCollisionShapeIDHash>; using Spheres = std::unordered_map<SHCollisionShapeID, SHSphere*, SHCollisionShapeIDHash>;
using Boxes = std::unordered_map<SHCollisionShapeID, SHBoxCollisionShape*, SHCollisionShapeIDHash>; using Boxes = std::unordered_map<SHCollisionShapeID, SHBox*, SHCollisionShapeIDHash>;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Data Members */ /* Data Members */

View File

@ -11,7 +11,7 @@
#include <SHpch.h> #include <SHpch.h>
// Primary Header // Primary Header
#include "SHConvexPolyhedronCollisionShape.h" #include "SHConvexPolyhedron.h"
namespace SHADE namespace SHADE
{ {
@ -19,12 +19,12 @@ namespace SHADE
/* Constructors & Destructor Definitions */ /* Constructors & Destructor Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHConvexPolyhedronCollisionShape::SHConvexPolyhedronCollisionShape(SHCollisionShapeID id,Type polyhedronType) noexcept SHConvexPolyhedron::SHConvexPolyhedron(SHCollisionShapeID id,Type polyhedronType) noexcept
: SHCollisionShape (id, polyhedronType) : SHCollisionShape (id, polyhedronType)
, halfEdgeStructure { nullptr } , halfEdgeStructure { nullptr }
{} {}
SHConvexPolyhedronCollisionShape::SHConvexPolyhedronCollisionShape(const SHConvexPolyhedronCollisionShape& rhs) noexcept SHConvexPolyhedron::SHConvexPolyhedron(const SHConvexPolyhedron& rhs) noexcept
: SHCollisionShape (rhs.id, rhs.GetType()) : SHCollisionShape (rhs.id, rhs.GetType())
, halfEdgeStructure { nullptr } , halfEdgeStructure { nullptr }
{ {
@ -37,7 +37,7 @@ namespace SHADE
collisionTag = rhs.collisionTag; collisionTag = rhs.collisionTag;
} }
SHConvexPolyhedronCollisionShape::SHConvexPolyhedronCollisionShape(SHConvexPolyhedronCollisionShape&& rhs) noexcept SHConvexPolyhedron::SHConvexPolyhedron(SHConvexPolyhedron&& rhs) noexcept
: SHCollisionShape (rhs.id, rhs.GetType()) : SHCollisionShape (rhs.id, rhs.GetType())
, halfEdgeStructure { nullptr } , halfEdgeStructure { nullptr }
{ {
@ -54,7 +54,7 @@ namespace SHADE
/* Operator Overload Definitions */ /* Operator Overload Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHConvexPolyhedronCollisionShape& SHConvexPolyhedronCollisionShape::operator=(const SHConvexPolyhedronCollisionShape& rhs) noexcept SHConvexPolyhedron& SHConvexPolyhedron::operator=(const SHConvexPolyhedron& rhs) noexcept
{ {
if (this == &rhs) if (this == &rhs)
return *this; return *this;
@ -73,7 +73,7 @@ namespace SHADE
return *this; return *this;
} }
SHConvexPolyhedronCollisionShape& SHConvexPolyhedronCollisionShape::operator=(SHConvexPolyhedronCollisionShape&& rhs) noexcept SHConvexPolyhedron& SHConvexPolyhedron::operator=(SHConvexPolyhedron&& rhs) noexcept
{ {
material = rhs.material; material = rhs.material;
collider = rhs.collider; collider = rhs.collider;
@ -93,28 +93,28 @@ namespace SHADE
/* Getter Function Definitions */ /* Getter Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
const SHHalfEdgeStructure* SHConvexPolyhedronCollisionShape::GetHalfEdgeStructure() const noexcept const SHHalfEdgeStructure* SHConvexPolyhedron::GetHalfEdgeStructure() const noexcept
{ {
return halfEdgeStructure; return halfEdgeStructure;
} }
int32_t SHConvexPolyhedronCollisionShape::GetFaceCount() const noexcept int32_t SHConvexPolyhedron::GetFaceCount() const noexcept
{ {
return halfEdgeStructure->GetFaceCount(); return halfEdgeStructure->GetFaceCount();
} }
int32_t SHConvexPolyhedronCollisionShape::GetHalfEdgeCount() const noexcept int32_t SHConvexPolyhedron::GetHalfEdgeCount() const noexcept
{ {
return halfEdgeStructure->GetHalfEdgeCount(); 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 // Assume it has already been initialised
return halfEdgeStructure->GetFace(index); 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 // Assume it has already been initialised
return halfEdgeStructure->GetHalfEdge(index); return halfEdgeStructure->GetHalfEdge(index);

View File

@ -24,14 +24,14 @@ namespace SHADE
* @brief * @brief
* Encapsulates a convex polyhedron shape used for Physics Simulations.. * Encapsulates a convex polyhedron shape used for Physics Simulations..
*/ */
class SH_API SHConvexPolyhedronCollisionShape : public SHCollisionShape class SH_API SHConvexPolyhedron : public SHCollisionShape
{ {
private: private:
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Friends */ /* Friends */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
friend class SHCollider; friend class SHCompositeCollider;
friend class SHCollision; friend class SHCollision;
friend class SHCollisionShapeLibrary; friend class SHCollisionShapeLibrary;
@ -40,18 +40,18 @@ namespace SHADE
/* Constructors & Destructor */ /* Constructors & Destructor */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
SHConvexPolyhedronCollisionShape (SHCollisionShapeID id, Type polyhedronType) noexcept; SHConvexPolyhedron (SHCollisionShapeID id, Type polyhedronType) noexcept;
SHConvexPolyhedronCollisionShape (const SHConvexPolyhedronCollisionShape& rhs) noexcept; SHConvexPolyhedron (const SHConvexPolyhedron& rhs) noexcept;
SHConvexPolyhedronCollisionShape (SHConvexPolyhedronCollisionShape&& rhs) noexcept; SHConvexPolyhedron (SHConvexPolyhedron&& rhs) noexcept;
~SHConvexPolyhedronCollisionShape () override = default; ~SHConvexPolyhedron () override = default;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Operator Overloads */ /* Operator Overloads */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
SHConvexPolyhedronCollisionShape& operator=(const SHConvexPolyhedronCollisionShape& rhs) noexcept; SHConvexPolyhedron& operator=(const SHConvexPolyhedron& rhs) noexcept;
SHConvexPolyhedronCollisionShape& operator=(SHConvexPolyhedronCollisionShape&& rhs) noexcept; SHConvexPolyhedron& operator=(SHConvexPolyhedron&& rhs) noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Getter Functions */ /* Getter Functions */

View File

@ -11,12 +11,12 @@
#include <SHpch.h> #include <SHpch.h>
// Primary Header // Primary Header
#include "SHSphereCollisionShape.h" #include "SHSphere.h"
// Project Headers // Project Headers
#include "Math/SHMathHelpers.h" #include "Math/SHMathHelpers.h"
#include "Math/SHMatrix.h" #include "Math/SHMatrix.h"
#include "Physics/Collision/SHCollider.h" #include "Physics/Collision/SHCompositeCollider.h"
namespace SHADE namespace SHADE
{ {
@ -24,64 +24,42 @@ namespace SHADE
/* Constructors & Destructor Definitions */ /* Constructors & Destructor Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHSphereCollisionShape::SHSphereCollisionShape(SHCollisionShapeID id) noexcept SHSphere::SHSphere(SHCollisionShapeID id) noexcept
: SHCollisionShape (id, SHCollisionShape::Type::SPHERE) : SHCollisionShape (id, Type::SPHERE)
, SHSphere ()
, relativeRadius { 1.0f } , relativeRadius { 1.0f }
, scale { 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 }
{ {
Radius = 0.5f;
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;
} }
SHSphereCollisionShape::SHSphereCollisionShape(SHSphereCollisionShape&& rhs) noexcept SHSphere::SHSphere(const SHSphere& rhs) noexcept
: SHCollisionShape (rhs.id, SHCollisionShape::Type::SPHERE) : SHCollisionShape ( rhs )
, SHSphere (rhs.Center, rhs.Radius)
, relativeRadius { rhs.relativeRadius } , relativeRadius { rhs.relativeRadius }
, scale { rhs.scale } , scale { rhs.scale }
{ {
Center = rhs.Center;
Radius = rhs.Radius;
}
material = rhs.material; SHSphere::SHSphere(SHSphere&& rhs) noexcept
collider = rhs.collider; : SHCollisionShape ( rhs )
transform = rhs.transform; , relativeRadius { rhs.relativeRadius }
rotationOffset = rhs.rotationOffset; , scale { rhs.scale }
flags = rhs.flags; {
// Since all collision tags are taken from the matrix, we do not need to do a deep copy here. Center = rhs.Center;
collisionTag = rhs.collisionTag; Radius = rhs.Radius;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Operator Overload Definitions */ /* Operator Overload Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHSphereCollisionShape& SHSphereCollisionShape::operator=(const SHSphereCollisionShape& rhs) noexcept SHSphere& SHSphere::operator=(const SHSphere& rhs) noexcept
{ {
if (this == &rhs) if (this == &rhs)
return *this; return *this;
// Collision Shape Properties SHCollisionShape::operator=(rhs);
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;
// Sphere Properties // Sphere Properties
@ -96,18 +74,9 @@ namespace SHADE
return *this; return *this;
} }
SHSphereCollisionShape& SHSphereCollisionShape::operator=(SHSphereCollisionShape&& rhs) noexcept SHSphere& SHSphere::operator=(SHSphere&& rhs) noexcept
{ {
// Collision Shape Properties SHCollisionShape::operator=(rhs);
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;
// Sphere Properties // Sphere Properties
@ -126,51 +95,73 @@ namespace SHADE
/* Getter Function Definitions */ /* Getter Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHVec3 SHSphereCollisionShape::GetCenter() const noexcept float SHSphere::GetWorldRadius() const noexcept
{
return Center;
}
float SHSphereCollisionShape::GetWorldRadius() const noexcept
{ {
return Radius; return Radius;
} }
float SHSphereCollisionShape::GetRelativeRadius() const noexcept float SHSphere::GetRelativeRadius() const noexcept
{ {
return relativeRadius; return relativeRadius;
} }
SHVec3 SHSphereCollisionShape::GetPosition() const noexcept SHVec3 SHSphere::GetWorldCentroid() const noexcept
{ {
return Center; 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 SHVec3 SHSphere::GetLocalCentroid() const noexcept
{
return Volume();
}
SHVec3 SHSphereCollisionShape::GetLocalCentroid() const noexcept
{ {
return SHVec3::Zero; 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 */ /* Setter Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void SHSphereCollisionShape::SetCenter(const SHVec3& newCenter) noexcept void SHSphere::SetWorldRadius(float newWorldRadius) noexcept
{
Center = newCenter;
}
void SHSphereCollisionShape::SetWorldRadius(float newWorldRadius) noexcept
{ {
Radius = newWorldRadius; Radius = newWorldRadius;
@ -178,7 +169,7 @@ namespace SHADE
relativeRadius = 2.0f * Radius / scale; relativeRadius = 2.0f * Radius / scale;
} }
void SHSphereCollisionShape::SetRelativeRadius(float newRelativeRadius) noexcept void SHSphere::SetRelativeRadius(float newRelativeRadius) noexcept
{ {
relativeRadius = newRelativeRadius; relativeRadius = newRelativeRadius;
@ -186,7 +177,7 @@ namespace SHADE
Radius = relativeRadius * scale * 0.5f; Radius = relativeRadius * scale * 0.5f;
} }
void SHSphereCollisionShape::SetScale(float maxScale) noexcept void SHSphere::SetScale(float maxScale) noexcept
{ {
scale = std::fabs(maxScale); scale = std::fabs(maxScale);
@ -198,7 +189,7 @@ namespace SHADE
/* Public Member Function Definitions */ /* Public Member Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void SHSphereCollisionShape::ComputeTransforms() noexcept void SHSphere::Update() noexcept
{ {
const SHTransform& PARENT_TRANSFORM = collider->GetTransform(); const SHTransform& PARENT_TRANSFORM = collider->GetTransform();
@ -212,42 +203,35 @@ namespace SHADE
Center = SHVec3::Transform(transform.position, TRS); 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;
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);
} }
SHMatrix SHSphereCollisionShape::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; return result;
} }
SHMatrix SHSphereCollisionShape::ComputeWorldTransform() const noexcept SHMatrix SHSphere::GetTRS() const noexcept
{ {
const SHTransform& PARENT_TRANSFORM = collider->GetTransform(); const SHQuaternion ROTATION = collider ? collider->GetTransform().orientation * transform.orientation : transform.orientation;
const SHQuaternion ROTATION = PARENT_TRANSFORM.orientation * transform.orientation;
const SHVec3 SCALE { Radius }; const SHVec3 SCALE { Radius };
return SHMatrix::Transform return SHMatrix::Transform(Center, ROTATION, SCALE);
(
Center
, ROTATION
, SCALE
);
} }
SHAABB SHSphereCollisionShape::ComputeAABB() const noexcept SHAABB SHSphere::ComputeAABB() const noexcept
{ {
return SHAABB{ Center, SHVec3{ Radius } }; return SHAABB{ Center, SHVec3{ Radius } };
} }

View File

@ -10,8 +10,9 @@
#pragma once #pragma once
#include <DirectXCollision.h>
// Project Headers // Project Headers
#include "Math/Geometry/SHSphere.h"
#include "SHCollisionShape.h" #include "SHCollisionShape.h"
namespace SHADE namespace SHADE
@ -37,15 +38,15 @@ namespace SHADE
* @brief * @brief
* Encapsulate a Sphere Shape used for Physics Simulations. * Encapsulate a Sphere Shape used for Physics Simulations.
*/ */
class SH_API SHSphereCollisionShape final : public SHCollisionShape class SH_API SHSphere final : public SHCollisionShape
, private SHSphere , private DirectX::BoundingSphere
{ {
private: private:
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Friends */ /* Friends */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
friend class SHCollider; friend class SHCompositeCollider;
friend class SHCollision; friend class SHCollision;
friend class SHCollisionShapeLibrary; friend class SHCollisionShapeLibrary;
@ -54,37 +55,39 @@ namespace SHADE
/* Constructors & Destructor */ /* Constructors & Destructor */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
SHSphereCollisionShape (SHCollisionShapeID id) noexcept; SHSphere (SHCollisionShapeID id) noexcept;
SHSphereCollisionShape (const SHSphereCollisionShape& rhs) noexcept; SHSphere (const SHSphere& rhs) noexcept;
SHSphereCollisionShape (SHSphereCollisionShape&& rhs) noexcept; SHSphere (SHSphere&& rhs) noexcept;
~SHSphereCollisionShape () override = default; ~SHSphere () override = default;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Operator Overloads */ /* Operator Overloads */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
SHSphereCollisionShape& operator= (const SHSphereCollisionShape& rhs) noexcept; SHSphere& operator= (const SHSphere& rhs) noexcept;
SHSphereCollisionShape& operator= (SHSphereCollisionShape&& rhs) noexcept; SHSphere& operator= (SHSphere&& rhs) noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Getter Functions */ /* Getter Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] SHVec3 GetCenter () const noexcept;
[[nodiscard]] float GetWorldRadius () const noexcept; [[nodiscard]] float GetWorldRadius () const noexcept;
[[nodiscard]] float GetRelativeRadius () const noexcept; [[nodiscard]] float GetRelativeRadius () const noexcept;
[[nodiscard]] SHVec3 GetPosition () const noexcept override; [[nodiscard]] SHVec3 GetWorldCentroid () const noexcept override;
[[nodiscard]] SHQuaternion GetOrientation () const noexcept override; [[nodiscard]] SHVec3 GetRelativeCentroid () const noexcept override;
[[nodiscard]] float GetVolume () const noexcept override;
[[nodiscard]] SHVec3 GetLocalCentroid () 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 */ /* Setter Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
void SetCenter (const SHVec3& newCenter) noexcept;
void SetWorldRadius (float newWorldRadius) noexcept; void SetWorldRadius (float newWorldRadius) noexcept;
void SetRelativeRadius (float newRelativeRadius) noexcept; void SetRelativeRadius (float newRelativeRadius) noexcept;
void SetScale (float maxScale) noexcept; void SetScale (float maxScale) noexcept;
@ -93,57 +96,10 @@ namespace SHADE
/* Function Members */ /* Function Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/** void Update () noexcept override;
* @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; [[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. <br/>
* See the corresponding header for the contents of the object.
*/
[[nodiscard]] SHRaycastResult Raycast (const SHRay& ray) const noexcept override; [[nodiscard]] SHRaycastResult Raycast (const SHRay& ray) const noexcept override;
[[nodiscard]] SHMatrix GetTRS () 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; [[nodiscard]] SHAABB ComputeAABB () const noexcept override;
private: private:
@ -152,7 +108,7 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
float relativeRadius; float relativeRadius;
float scale; // Intended to be passed in by the base collider. float scale;
}; };
} // namespace SHADE } // namespace SHADE

View File

@ -14,7 +14,7 @@
#include "SHCollisionKey.h" #include "SHCollisionKey.h"
// Project Headers // Project Headers
#include "Physics/Collision/SHCollider.h" #include "Physics/Collision/SHCompositeCollider.h"
#include "Physics/Interface/SHColliderComponent.h" #include "Physics/Interface/SHColliderComponent.h"

View File

@ -16,7 +16,7 @@
// Project Headers // Project Headers
#include "Math/SHMathHelpers.h" #include "Math/SHMathHelpers.h"
#include "Physics/Collision/CollisionShapes/SHBoxCollisionShape.h" #include "Physics/Collision/CollisionShapes/SHBox.h"
// TODO // TODO

View File

@ -84,27 +84,9 @@ namespace SHADE
// Sphere VS Convex // Sphere VS Convex
static FaceQuery findClosestFace static FaceQuery findClosestFace (const SHSphere& sphere, const SHConvexPolyhedron& polyhedron) noexcept;
( static int32_t findClosestPoint (const SHSphere& sphere, const SHConvexPolyhedron& polyhedron, int32_t faceIndex) noexcept;
const SHSphereCollisionShape& sphere static int32_t findVoronoiRegion (const SHSphere& sphere, const SHVec3& faceVertex, const SHVec3& faceNormal, const SHVec3& tangent1, const SHVec3& tangent2) noexcept;
, 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;
// Capsule VS Convex // Capsule VS Convex

View File

@ -15,7 +15,7 @@
// Project Headers // Project Headers
#include "Math/SHMathHelpers.h" #include "Math/SHMathHelpers.h"
#include "Physics/Collision/CollisionShapes/SHSphereCollisionShape.h" #include "Physics/Collision/CollisionShapes/SHSphere.h"
// TODO // TODO

View File

@ -18,7 +18,7 @@
#include "Math/Geometry/SHPlane.h" #include "Math/Geometry/SHPlane.h"
#include "Math/SHMathHelpers.h" #include "Math/SHMathHelpers.h"
#include "Physics/Collision/CollisionShapes/SHCollisionShape.h" #include "Physics/Collision/CollisionShapes/SHCollisionShape.h"
#include "Physics/Collision/CollisionShapes/SHBoxCollisionShape.h" #include "Physics/Collision/CollisionShapes/SHBox.h"
namespace SHADE namespace SHADE
{ {
@ -28,10 +28,10 @@ namespace SHADE
bool SHCollision::SphereVsConvex(const SHCollisionShape& A, const SHCollisionShape& B) noexcept bool SHCollision::SphereVsConvex(const SHCollisionShape& A, const SHCollisionShape& B) noexcept
{ {
const SHSphereCollisionShape& SPHERE = dynamic_cast<const SHSphereCollisionShape&>(A); const SHSphere& SPHERE = dynamic_cast<const SHSphere&>(A);
const SHConvexPolyhedronCollisionShape& POLYHEDRON = dynamic_cast<const SHConvexPolyhedronCollisionShape&>(B); const SHConvexPolyhedron& POLYHEDRON = dynamic_cast<const SHConvexPolyhedron&>(B);
const SHVec3 CENTER = SPHERE.GetCenter(); const SHVec3 CENTER = SPHERE.Center;
const float RADIUS = SPHERE.GetWorldRadius(); const float RADIUS = SPHERE.GetWorldRadius();
// Find closest face // Find closest face
@ -76,10 +76,10 @@ namespace SHADE
{ {
// Convert to underlying types // Convert to underlying types
// For the convex, we only need the convex polyhedron shape since the get vertex is pure virtual. // For the convex, we only need the convex polyhedron shape since the get vertex is pure virtual.
const SHSphereCollisionShape& SPHERE = dynamic_cast<const SHSphereCollisionShape&>(A); const SHSphere& SPHERE = dynamic_cast<const SHSphere&>(A);
const SHConvexPolyhedronCollisionShape& POLYHEDRON = dynamic_cast<const SHConvexPolyhedronCollisionShape&>(B); const SHConvexPolyhedron& POLYHEDRON = dynamic_cast<const SHConvexPolyhedron&>(B);
const SHVec3 CENTER = SPHERE.GetCenter(); const SHVec3 CENTER = SPHERE.Center;
const float RADIUS = SPHERE.GetWorldRadius(); const float RADIUS = SPHERE.GetWorldRadius();
const FaceQuery FACE_QUERY = findClosestFace(SPHERE, POLYHEDRON); const FaceQuery FACE_QUERY = findClosestFace(SPHERE, POLYHEDRON);
@ -98,7 +98,7 @@ namespace SHADE
manifold.normal = -POLYHEDRON.GetNormal(FACE_QUERY.closestFace); manifold.normal = -POLYHEDRON.GetNormal(FACE_QUERY.closestFace);
contact.penetration = PENETRATION; contact.penetration = PENETRATION;
contact.position = SPHERE.GetCenter(); contact.position = CENTER;
manifold.contacts[numContacts++] = contact; manifold.contacts[numContacts++] = contact;
manifold.numContacts = numContacts; manifold.numContacts = numContacts;
@ -187,13 +187,13 @@ namespace SHADE
SHCollision::FaceQuery SHCollision::findClosestFace SHCollision::FaceQuery SHCollision::findClosestFace
( (
const SHSphereCollisionShape& sphere const SHSphere& sphere
, const SHConvexPolyhedronCollisionShape& polyhedron , const SHConvexPolyhedron& polyhedron
) noexcept ) noexcept
{ {
FaceQuery faceQuery; FaceQuery faceQuery;
const SHVec3 CENTER = sphere.GetCenter(); const SHVec3 CENTER = sphere.Center;
const float RADIUS = sphere.GetWorldRadius(); const float RADIUS = sphere.GetWorldRadius();
/* /*
@ -233,15 +233,15 @@ namespace SHADE
int32_t SHCollision::findClosestPoint int32_t SHCollision::findClosestPoint
( (
const SHSphereCollisionShape& sphere const SHSphere& sphere
, const SHConvexPolyhedronCollisionShape& polyhedron , const SHConvexPolyhedron& polyhedron
, int32_t faceIndex , int32_t faceIndex
) noexcept ) noexcept
{ {
// Find closest point on face // Find closest point on face
int32_t closestPointIndex = -1; int32_t closestPointIndex = -1;
const SHVec3 CENTER = sphere.GetCenter(); const SHVec3 CENTER = sphere.Center;
const SHHalfEdgeStructure::Face& FACE = polyhedron.GetFace(faceIndex); const SHHalfEdgeStructure::Face& FACE = polyhedron.GetFace(faceIndex);
const int32_t NUM_VERITICES = static_cast<int32_t>(FACE.vertexIndices.size()); const int32_t NUM_VERITICES = static_cast<int32_t>(FACE.vertexIndices.size());
@ -264,7 +264,7 @@ namespace SHADE
int32_t SHCollision::findVoronoiRegion int32_t SHCollision::findVoronoiRegion
( (
const SHSphereCollisionShape& sphere const SHSphere& sphere
, const SHVec3& faceVertex , const SHVec3& faceVertex
, const SHVec3& faceNormal , const SHVec3& faceNormal
, const SHVec3& tangent1 , const SHVec3& tangent1
@ -288,7 +288,7 @@ namespace SHADE
* *
*/ */
const SHVec3& CENTER = sphere.GetCenter(); const SHVec3 CENTER = sphere.Center;
const float RADIUS = sphere.GetWorldRadius(); const float RADIUS = sphere.GetWorldRadius();
const SHVec3 TANGENTS [NUM_TANGENTS] { tangent1, tangent2 }; const SHVec3 TANGENTS [NUM_TANGENTS] { tangent1, tangent2 };

View File

@ -15,7 +15,7 @@
// Project Headers // Project Headers
#include "Math/SHMathHelpers.h" #include "Math/SHMathHelpers.h"
#include "Physics/Collision/CollisionShapes/SHSphereCollisionShape.h" #include "Physics/Collision/CollisionShapes/SHSphere.h"
namespace SHADE namespace SHADE
{ {
@ -25,22 +25,41 @@ namespace SHADE
bool SHCollision::SphereVsSphere(const SHCollisionShape& A, const SHCollisionShape& B) noexcept bool SHCollision::SphereVsSphere(const SHCollisionShape& A, const SHCollisionShape& B) noexcept
{ {
const SHSphereCollisionShape& SPHERE_A = dynamic_cast<const SHSphereCollisionShape&>(A); const SHSphere& SPHERE_A = dynamic_cast<const SHSphere&>(A);
const SHSphereCollisionShape& SPHERE_B = dynamic_cast<const SHSphereCollisionShape&>(B); const SHSphere& SPHERE_B = dynamic_cast<const SHSphere&>(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 bool SHCollision::SphereVsSphere(SHManifold& manifold, const SHCollisionShape& A, const SHCollisionShape& B) noexcept
{ {
// Convert to underlying types // Convert to underlying types
const SHSphereCollisionShape& SPHERE_A = dynamic_cast<const SHSphereCollisionShape&>(A); const SHSphere& SPHERE_A = dynamic_cast<const SHSphere&>(A);
const SHSphereCollisionShape& SPHERE_B = dynamic_cast<const SHSphereCollisionShape&>(B); const SHSphere& SPHERE_B = dynamic_cast<const SHSphere&>(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 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; const float COMBINED_RADIUS_SQUARED = COMBINED_RADIUS * COMBINED_RADIUS;
if (DISTANCE_BETWEEN_CENTERS_SQUARED > COMBINED_RADIUS_SQUARED) if (DISTANCE_BETWEEN_CENTERS_SQUARED > COMBINED_RADIUS_SQUARED)
@ -56,15 +75,15 @@ namespace SHADE
if (SHMath::CompareFloat(DISTANCE_BETWEEN_CENTERS_SQUARED, 0.0f)) if (SHMath::CompareFloat(DISTANCE_BETWEEN_CENTERS_SQUARED, 0.0f))
{ {
manifold.normal = SHVec3::UnitY; manifold.normal = SHVec3::UnitY;
contact.position = SPHERE_A.GetCenter(); contact.position = CENTER_A;
contact.penetration = SPHERE_B.GetWorldRadius(); contact.penetration = RADIUS_B;
manifold.contacts[numContacts++] = contact; manifold.contacts[numContacts++] = contact;
} }
else else
{ {
manifold.normal = SHVec3::Normalise(A_TO_B); 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(); contact.penetration = COMBINED_RADIUS - A_TO_B.Length();
manifold.contacts[numContacts++] = contact; manifold.contacts[numContacts++] = contact;

View File

@ -40,7 +40,7 @@ namespace SHADE
/* Public Member Functions Definitions */ /* 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; const bool INSERTED = colliders.emplace(collider->entityID, collider).second;
if (!INSERTED) if (!INSERTED)
@ -56,7 +56,7 @@ namespace SHADE
broadphase.Insert(shape->id, shape->ComputeAABB()); broadphase.Insert(shape->id, shape->ComputeAABB());
} }
void SHCollisionSpace::RemoveCollider(SHCollider* collider) noexcept void SHCollisionSpace::RemoveCollider(SHCompositeCollider* collider) noexcept
{ {
colliders.erase(collider->entityID); colliders.erase(collider->entityID);
@ -209,12 +209,12 @@ namespace SHADE
{ {
case SHCollisionShape::Type::SPHERE: case SHCollisionShape::Type::SPHERE:
{ {
baseResult = dynamic_cast<const SHSphereCollisionShape*>(SHAPE)->Raycast(info.ray); baseResult = dynamic_cast<const SHSphere*>(SHAPE)->Raycast(info.ray);
break; break;
} }
case SHCollisionShape::Type::BOX: case SHCollisionShape::Type::BOX:
{ {
baseResult = dynamic_cast<const SHBoxCollisionShape*>(SHAPE)->Raycast(info.ray); baseResult = dynamic_cast<const SHBox*>(SHAPE)->Raycast(info.ray);
break; break;
} }
case SHCollisionShape::Type::CAPSULE: case SHCollisionShape::Type::CAPSULE:
@ -246,7 +246,7 @@ namespace SHADE
/* Private Member Functions Definitions */ /* 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) for (auto* shape : collider->shapes)
{ {

View File

@ -18,7 +18,7 @@
// Project Headers // Project Headers
#include "Broadphase/SHDynamicAABBTree.h" #include "Broadphase/SHDynamicAABBTree.h"
#include "Physics/Dynamics/SHContactManager.h" #include "Physics/Dynamics/SHContactManager.h"
#include "SHCollider.h" #include "SHCompositeCollider.h"
#include "SHPhysicsRaycastResult.h" #include "SHPhysicsRaycastResult.h"
#include "CollisionTags/SHCollisionTags.h" #include "CollisionTags/SHCollisionTags.h"
#include "CollisionTags/SHCollisionTags.h" #include "CollisionTags/SHCollisionTags.h"
@ -118,7 +118,7 @@ namespace SHADE
* @param collider * @param collider
* A collider to add. Duplicates will be ignored. * A collider to add. Duplicates will be ignored.
*/ */
void AddCollider (SHCollider* collider) noexcept; void AddCollider (SHCompositeCollider* collider) noexcept;
/** /**
* @brief * @brief
@ -127,7 +127,7 @@ namespace SHADE
* @param collider * @param collider
* A collider to remove. If a reference to it doesn't exist, it will be ignored. * 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 * @brief
@ -165,7 +165,7 @@ namespace SHADE
SHCollisionShape* B = nullptr; SHCollisionShape* B = nullptr;
}; };
using Colliders = std::unordered_map<EntityID, SHCollider*>; using Colliders = std::unordered_map<EntityID, SHCompositeCollider*>;
using NarrowphaseBatch = std::unordered_map<SHCollisionKey, NarrowphasePair, SHCollisionKeyHash>; using NarrowphaseBatch = std::unordered_map<SHCollisionKey, NarrowphasePair, SHCollisionKeyHash>;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -185,7 +185,7 @@ namespace SHADE
// Broadphase helpers // Broadphase helpers
void broadphaseQuery (SHRigidBody::Type rigidBodyType, SHCollider* collider) noexcept; void broadphaseQuery (SHRigidBody::Type rigidBodyType, SHCompositeCollider* collider) noexcept;
// Narrowphase helpers // Narrowphase helpers

View File

@ -11,7 +11,7 @@
#include <SHpch.h> #include <SHpch.h>
// Primary Header // Primary Header
#include "SHCollider.h" #include "SHCompositeCollider.h"
// Project Headers // Project Headers
#include "Broadphase/SHDynamicAABBTree.h" #include "Broadphase/SHDynamicAABBTree.h"
@ -27,7 +27,7 @@ namespace SHADE
/* Constructors & Destructor Definitions */ /* Constructors & Destructor Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHCollider::SHCollider(EntityID eid, const SHTransform& worldTransform) noexcept SHCompositeCollider::SHCompositeCollider(EntityID eid, const SHTransform& worldTransform) noexcept
: entityID { eid } : entityID { eid }
, active { true } , active { true }
, debugDraw { false } , debugDraw { false }
@ -38,7 +38,7 @@ namespace SHADE
, transform { worldTransform } , transform { worldTransform }
{} {}
SHCollider::SHCollider(const SHCollider& rhs) noexcept SHCompositeCollider::SHCompositeCollider(const SHCompositeCollider& rhs) noexcept
: entityID { rhs.entityID } : entityID { rhs.entityID }
, active { rhs.active } , active { rhs.active }
, debugDraw { rhs.debugDraw } , debugDraw { rhs.debugDraw }
@ -57,7 +57,7 @@ namespace SHADE
copyShapes(rhs); copyShapes(rhs);
} }
SHCollider::SHCollider(SHCollider&& rhs) noexcept SHCompositeCollider::SHCompositeCollider(SHCompositeCollider&& rhs) noexcept
: entityID { rhs.entityID } : entityID { rhs.entityID }
, active { rhs.active } , active { rhs.active }
, debugDraw { rhs.debugDraw } , debugDraw { rhs.debugDraw }
@ -76,7 +76,7 @@ namespace SHADE
copyShapes(rhs); copyShapes(rhs);
} }
SHCollider::~SHCollider() noexcept SHCompositeCollider::~SHCompositeCollider() noexcept
{ {
if (!shapeLibrary) if (!shapeLibrary)
{ {
@ -92,7 +92,7 @@ namespace SHADE
/* Operator Overload Definitions */ /* Operator Overload Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHCollider& SHCollider::operator=(const SHCollider& rhs) noexcept SHCompositeCollider& SHCompositeCollider::operator=(const SHCompositeCollider& rhs) noexcept
{ {
if (this == &rhs) if (this == &rhs)
return *this; return *this;
@ -117,7 +117,7 @@ namespace SHADE
return *this; return *this;
} }
SHCollider& SHCollider::operator=(SHCollider&& rhs) noexcept SHCompositeCollider& SHCompositeCollider::operator=(SHCompositeCollider&& rhs) noexcept
{ {
if (!shapeLibrary) if (!shapeLibrary)
{ {
@ -143,47 +143,47 @@ namespace SHADE
/* Getter Function Definitions */ /* Getter Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
EntityID SHCollider::GetEntityID() const noexcept EntityID SHCompositeCollider::GetEntityID() const noexcept
{ {
return entityID; return entityID;
} }
bool SHCollider::IsActive() const noexcept bool SHCompositeCollider::IsActive() const noexcept
{ {
return active; return active;
} }
bool SHCollider::GetDebugDrawState() const noexcept bool SHCompositeCollider::GetDebugDrawState() const noexcept
{ {
return debugDraw; return debugDraw;
} }
const SHTransform& SHCollider::GetTransform() const noexcept const SHTransform& SHCompositeCollider::GetTransform() const noexcept
{ {
return transform; return transform;
} }
const SHVec3& SHCollider::GetPosition() const noexcept const SHVec3& SHCompositeCollider::GetPosition() const noexcept
{ {
return transform.position; return transform.position;
} }
const SHQuaternion& SHCollider::GetOrientation() const noexcept const SHQuaternion& SHCompositeCollider::GetOrientation() const noexcept
{ {
return transform.orientation; return transform.orientation;
} }
const SHVec3& SHCollider::GetScale() const noexcept const SHVec3& SHCompositeCollider::GetScale() const noexcept
{ {
return transform.scale; return transform.scale;
} }
const SHCollider::CollisionShapes& SHCollider::GetCollisionShapes() const noexcept const SHCompositeCollider::CollisionShapes& SHCompositeCollider::GetCollisionShapes() const noexcept
{ {
return shapes; return shapes;
} }
SHCollisionShape* SHCollider::GetCollisionShape(int index) const SHCollisionShape* SHCompositeCollider::GetCollisionShape(int index) const
{ {
const int NUM_SHAPES = static_cast<int>(shapes.size()); const int NUM_SHAPES = static_cast<int>(shapes.size());
@ -197,7 +197,7 @@ namespace SHADE
/* Setter Function Definitions */ /* Setter Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void SHCollider::SetIsActive(bool state) noexcept void SHCompositeCollider::SetIsActive(bool state) noexcept
{ {
if (active == state) if (active == state)
return; return;
@ -216,7 +216,7 @@ namespace SHADE
} }
} }
void SHCollider::SetDebugDrawState(bool state) noexcept void SHCompositeCollider::SetDebugDrawState(bool state) noexcept
{ {
debugDraw = state; debugDraw = state;
@ -234,36 +234,36 @@ namespace SHADE
#endif #endif
} }
void SHCollider::SetRigidBody(SHRigidBody* rb) noexcept void SHCompositeCollider::SetRigidBody(SHRigidBody* rb) noexcept
{ {
rigidBody = rb; rigidBody = rb;
} }
void SHCollider::SetTransform(const SHTransform& newTransform) noexcept void SHCompositeCollider::SetTransform(const SHTransform& newTransform) noexcept
{ {
hasMoved = true; hasMoved = true;
transform = newTransform; transform = newTransform;
} }
void SHCollider::SetPosition(const SHVec3& newPosition) noexcept void SHCompositeCollider::SetPosition(const SHVec3& newPosition) noexcept
{ {
hasMoved = true; hasMoved = true;
transform.position = newPosition; transform.position = newPosition;
} }
void SHCollider::SetOrientation(const SHQuaternion& newOrientation) noexcept void SHCompositeCollider::SetOrientation(const SHQuaternion& newOrientation) noexcept
{ {
hasMoved = true; hasMoved = true;
transform.orientation = newOrientation; transform.orientation = newOrientation;
} }
void SHCollider::SetScale(const SHVec3& newScale) noexcept void SHCompositeCollider::SetScale(const SHVec3& newScale) noexcept
{ {
hasMoved = true; hasMoved = true;
transform.scale = newScale; transform.scale = newScale;
} }
void SHCollider::SetFactory(SHCollisionShapeLibrary* factory) noexcept void SHCompositeCollider::SetFactory(SHCollisionShapeLibrary* factory) noexcept
{ {
shapeLibrary = factory; shapeLibrary = factory;
} }
@ -272,12 +272,12 @@ namespace SHADE
/* Public Member Function Definitions */ /* Public Member Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
const SHMatrix& SHCollider::ComputeTransformTRS() noexcept const SHMatrix& SHCompositeCollider::ComputeTransformTRS() noexcept
{ {
return transform.ComputeTRS(); 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) if (!shapeLibrary)
{ {
@ -304,7 +304,7 @@ namespace SHADE
const uint32_t NEW_INDEX = static_cast<uint32_t>(shapes.size()); const uint32_t NEW_INDEX = static_cast<uint32_t>(shapes.size());
const SHCollisionShapeID NEW_SHAPE_ID{ entityID, NEW_INDEX }; 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 // Set offsets
sphere->collider = this; sphere->collider = this;
@ -332,7 +332,7 @@ namespace SHADE
return static_cast<int>(NEW_INDEX); return static_cast<int>(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) if (!shapeLibrary)
{ {
@ -357,7 +357,7 @@ namespace SHADE
const uint32_t NEW_INDEX = static_cast<uint32_t>(shapes.size()); const uint32_t NEW_INDEX = static_cast<uint32_t>(shapes.size());
const SHCollisionShapeID NEW_SHAPE_ID{ entityID, NEW_INDEX }; 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 // Set offsets
box->collider = this; box->collider = this;
@ -386,7 +386,7 @@ namespace SHADE
} }
void SHCollider::RemoveCollisionShape(int index) void SHCompositeCollider::RemoveCollisionShape(int index)
{ {
if (!shapeLibrary) if (!shapeLibrary)
{ {
@ -434,23 +434,23 @@ namespace SHADE
SHLOG_INFO_D("Removing Collision Shape {} from Entity {}", index, entityID) SHLOG_INFO_D("Removing Collision Shape {} from Entity {}", index, entityID)
} }
void SHCollider::RecomputeShapes() noexcept void SHCompositeCollider::RecomputeShapes() noexcept
{ {
for (auto* shape : shapes) for (auto* shape : shapes)
shape->ComputeTransforms(); shape->Update();
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Private Member Function Definitions */ /* Private Member Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void SHCollider::copyShapes(const SHCollider& rhsCollider) void SHCompositeCollider::copyShapes(const SHCompositeCollider& rhsCollider)
{ {
for (const auto* shape : rhsCollider.shapes) for (const auto* shape : rhsCollider.shapes)
copyShape(shape); copyShape(shape);
} }
void SHCollider::copyShape(const SHCollisionShape* rhsShape) void SHCompositeCollider::copyShape(const SHCollisionShape* rhsShape)
{ {
switch (rhsShape->GetType()) switch (rhsShape->GetType())
{ {
@ -460,7 +460,7 @@ namespace SHADE
} }
case SHCollisionShape::Type::SPHERE: case SHCollisionShape::Type::SPHERE:
{ {
const SHSphereCollisionShape* RHS_SPHERE = dynamic_cast<const SHSphereCollisionShape*>(rhsShape); const SHSphere* RHS_SPHERE = dynamic_cast<const SHSphere*>(rhsShape);
const SHSphereCreateInfo SPHERE_CREATE_INFO const SHSphereCreateInfo SPHERE_CREATE_INFO
{ {
@ -473,7 +473,7 @@ namespace SHADE
const uint32_t NEW_INDEX = static_cast<uint32_t>(shapes.size()); const uint32_t NEW_INDEX = static_cast<uint32_t>(shapes.size());
const SHCollisionShapeID NEW_SHAPE_ID{ entityID, NEW_INDEX }; 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; *sphere = *RHS_SPHERE;
shapes.emplace_back(sphere); shapes.emplace_back(sphere);

View File

@ -33,7 +33,7 @@ namespace SHADE
* Base class for a collider. * Base class for a collider.
* There are only two collider types supported by SHADE Engine: Composite & Hull * There are only two collider types supported by SHADE Engine: Composite & Hull
*/ */
class SH_API SHCollider class SH_API SHCompositeCollider
{ {
private: private:
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -64,17 +64,17 @@ namespace SHADE
* This is particularly important for composite colliders for offsets & relative sizes. * This is particularly important for composite colliders for offsets & relative sizes.
* @return * @return
*/ */
SHCollider (EntityID eid, const SHTransform& worldTransform = SHTransform::Identity) noexcept; SHCompositeCollider (EntityID eid, const SHTransform& worldTransform = SHTransform::Identity) noexcept;
SHCollider (const SHCollider& rhs) noexcept; SHCompositeCollider (const SHCompositeCollider& rhs) noexcept;
SHCollider (SHCollider&& rhs) noexcept; SHCompositeCollider (SHCompositeCollider&& rhs) noexcept;
~SHCollider () noexcept; ~SHCompositeCollider () noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Operator Overloads */ /* Operator Overloads */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
SHCollider& operator=(const SHCollider& rhs) noexcept; SHCompositeCollider& operator=(const SHCompositeCollider& rhs) noexcept;
SHCollider& operator=(SHCollider&& rhs) noexcept; SHCompositeCollider& operator=(SHCompositeCollider&& rhs) noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Getter Functions */ /* Getter Functions */
@ -192,7 +192,7 @@ namespace SHADE
/* Member Functions */ /* Member Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
void copyShapes (const SHCollider& rhsCollider); void copyShapes (const SHCompositeCollider& rhsCollider);
void copyShape (const SHCollisionShape* rhsShape); void copyShape (const SHCollisionShape* rhsShape);
}; };

View File

@ -17,7 +17,7 @@
#include <complex.h> #include <complex.h>
#include <numeric> #include <numeric>
#include "Physics/Collision/SHCollider.h" #include "Physics/Collision/SHCompositeCollider.h"
#include "Tools/Logger/SHLogger.h" #include "Tools/Logger/SHLogger.h"
namespace SHADE namespace SHADE
@ -296,7 +296,7 @@ namespace SHADE
/* Setter Functions Definitions */ /* Setter Functions Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void SHRigidBody::SetCollider(SHCollider* c) noexcept void SHRigidBody::SetCollider(SHCompositeCollider* c) noexcept
{ {
collider = c; collider = c;
} }
@ -690,7 +690,7 @@ namespace SHADE
trueMass.emplace_back(MASS); trueMass.emplace_back(MASS);
// Weighted sum of masses contribute to the centroid's location using the collider's local position. // 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) if (totalMass > 0.0f)
@ -726,8 +726,9 @@ namespace SHADE
I = R * I; I = R * I;
// Parallel Axis Theorem // Parallel Axis Theorem
// https://en.wikipedia.org/wiki/Parallel_axis_theorem
// J = I + m((R /dot R)E_3 - R /outerProduct R) // 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 float R_MAG2 = R.LengthSquared();
const SHMatrix R_OX_R = SHVec3::OuterProduct(R, R); const SHMatrix R_OX_R = SHVec3::OuterProduct(R, R);

View File

@ -22,7 +22,7 @@ namespace SHADE
/* Forward Declarations */ /* Forward Declarations */
/*-------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------*/
class SHCollider; class SHCompositeCollider;
/*-------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------*/
/* Type Definitions */ /* Type Definitions */
@ -113,7 +113,7 @@ namespace SHADE
/* Setter Functions */ /* Setter Functions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void SetCollider (SHCollider* c) noexcept; void SetCollider (SHCompositeCollider* c) noexcept;
/** /**
* @brief * @brief
@ -229,7 +229,7 @@ namespace SHADE
// The entityID here is only meant for linking with the actual component in the engine. // The entityID here is only meant for linking with the actual component in the engine.
EntityID entityID; EntityID entityID;
SHCollider* collider; SHCompositeCollider* collider;
Type bodyType; Type bodyType;

View File

@ -111,7 +111,7 @@ namespace SHADE
collider->SetRigidBody(nullptr); collider->SetRigidBody(nullptr);
} }
SHCollider* SHPhysicsObject::CreateCollider(const SHTransform& transform) SHCompositeCollider* SHPhysicsObject::CreateCollider(const SHTransform& transform)
{ {
if (collider) if (collider)
{ {
@ -119,7 +119,7 @@ namespace SHADE
return collider; return collider;
} }
collider = new SHCollider{ entityID, transform }; collider = new SHCompositeCollider{ entityID, transform };
// Link with rigidBody if it exists // Link with rigidBody if it exists
if (rigidBody) if (rigidBody)
@ -148,13 +148,13 @@ namespace SHADE
/* Private Member Function Definitions */ /* Private Member Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void SHPhysicsObject::deepCopyComponents(const SHRigidBody* rhsRigidBody, const SHCollider* rhsCollider) void SHPhysicsObject::deepCopyComponents(const SHRigidBody* rhsRigidBody, const SHCompositeCollider* rhsCollider)
{ {
if (rhsRigidBody) if (rhsRigidBody)
rigidBody = new SHRigidBody{ *rhsRigidBody }; rigidBody = new SHRigidBody{ *rhsRigidBody };
if (rhsCollider) if (rhsCollider)
collider = new SHCollider { *rhsCollider }; collider = new SHCompositeCollider { *rhsCollider };
} }
} // namespace SHADE } // namespace SHADE

View File

@ -11,7 +11,7 @@
#pragma once #pragma once
// Project Headers // Project Headers
#include "Physics/Collision/SHCollider.h" #include "Physics/Collision/SHCompositeCollider.h"
#include "Physics/Dynamics/SHRigidBody.h" #include "Physics/Dynamics/SHRigidBody.h"
namespace SHADE namespace SHADE
@ -33,7 +33,7 @@ namespace SHADE
EntityID entityID = MAX_EID; EntityID entityID = MAX_EID;
SHRigidBody* rigidBody = nullptr; SHRigidBody* rigidBody = nullptr;
SHCollider* collider = nullptr; SHCompositeCollider* collider = nullptr;
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Constructors & Destructor */ /* Constructors & Destructor */
@ -91,7 +91,7 @@ namespace SHADE
* Pointer to the collider that was created. The memory of this collider is managed * Pointer to the collider that was created. The memory of this collider is managed
* by the physics object itself. * by the physics object itself.
*/ */
SHCollider* CreateCollider (const SHTransform& transform = SHTransform::Identity); SHCompositeCollider* CreateCollider (const SHTransform& transform = SHTransform::Identity);
/** /**
* @brief * @brief
@ -104,6 +104,6 @@ namespace SHADE
/* Member Functions */ /* Member Functions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void deepCopyComponents(const SHRigidBody* rhsRigidBody, const SHCollider* rhsCollider); void deepCopyComponents(const SHRigidBody* rhsRigidBody, const SHCompositeCollider* rhsCollider);
}; };
} // namespace SHADE } // namespace SHADE

View File

@ -32,12 +32,12 @@ namespace SHADE
/* Getter Function Definitions */ /* Getter Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHCollider* const SHColliderComponent::GetCollider() const noexcept SHCompositeCollider* const SHColliderComponent::GetCollider() const noexcept
{ {
return collider; return collider;
} }
const SHCollider::CollisionShapes* const SHColliderComponent::GetCollisionShapes() const noexcept const SHCompositeCollider::CollisionShapes* const SHColliderComponent::GetCollisionShapes() const noexcept
{ {
if (!collider) if (!collider)
return nullptr; return nullptr;
@ -66,7 +66,7 @@ namespace SHADE
/* Setter Function Definitions */ /* Setter Function Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
void SHColliderComponent::SetCollider(SHCollider* c) noexcept void SHColliderComponent::SetCollider(SHCompositeCollider* c) noexcept
{ {
collider = c; collider = c;
} }

View File

@ -14,7 +14,7 @@
// Project Headers // Project Headers
#include "ECS_Base/Components/SHComponent.h" #include "ECS_Base/Components/SHComponent.h"
#include "Physics/Collision/SHCollider.h" #include "Physics/Collision/SHCompositeCollider.h"
namespace SHADE namespace SHADE
{ {
@ -54,8 +54,8 @@ namespace SHADE
/* Getter Functions */ /* Getter Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] SHCollider* const GetCollider () const noexcept; [[nodiscard]] SHCompositeCollider* const GetCollider () const noexcept;
[[nodiscard]] const SHCollider::CollisionShapes* const GetCollisionShapes() const noexcept; [[nodiscard]] const SHCompositeCollider::CollisionShapes* const GetCollisionShapes() const noexcept;
[[nodiscard]] SHCollisionShape* const GetCollisionShape (int index) const; [[nodiscard]] SHCollisionShape* const GetCollisionShape (int index) const;
// Required for serialisation // Required for serialisation
@ -66,7 +66,7 @@ namespace SHADE
/* Setter Functions */ /* Setter Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
void SetCollider (SHCollider* c) noexcept; void SetCollider (SHCompositeCollider* c) noexcept;
// Required for serialisation // Required for serialisation
@ -77,7 +77,7 @@ namespace SHADE
/* Data Members */ /* Data Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
SHCollider* collider; SHCompositeCollider* collider;
RTTR_ENABLE() RTTR_ENABLE()
}; };

View File

@ -122,7 +122,7 @@ namespace SHADE
return onColliderDrawEvent.get()->handle; 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()) for (const auto* SHAPE : collider.GetCollisionShapes())
{ {
@ -132,12 +132,12 @@ namespace SHADE
{ {
case SHCollisionShape::Type::SPHERE: case SHCollisionShape::Type::SPHERE:
{ {
debugDrawSystem->DrawWireSphere(SHAPE->ComputeWorldTransform(), DRAW_COLOUR, true); debugDrawSystem->DrawWireSphere(SHAPE->GetTRS(), DRAW_COLOUR, true);
break; break;
} }
case SHCollisionShape::Type::BOX: case SHCollisionShape::Type::BOX:
{ {
debugDrawSystem->DrawWireCube(SHAPE->ComputeWorldTransform(), DRAW_COLOUR, true); debugDrawSystem->DrawWireCube(SHAPE->GetTRS(), DRAW_COLOUR, true);
break; break;
} }
case SHCollisionShape::Type::CAPSULE: case SHCollisionShape::Type::CAPSULE:

View File

@ -18,9 +18,9 @@
#include "ECS_Base/System/SHSystemRoutine.h" #include "ECS_Base/System/SHSystemRoutine.h"
#include "Events/SHEvent.h" #include "Events/SHEvent.h"
#include "Graphics/MiddleEnd/Interface/SHDebugDrawSystem.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/SHPhysicsRaycastResult.h"
#include "Physics/Collision/CollisionShapes/SHSphereCollisionShape.h" #include "Physics/Collision/CollisionShapes/SHSphere.h"
namespace SHADE namespace SHADE
@ -138,7 +138,7 @@ namespace SHADE
SHEventHandle onColliderDraw(SHEventPtr onColliderDrawEvent); 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; static void drawRaycast (SHDebugDrawSystem* debugDrawSystem, const DebugDrawInfo::Raycast& raycastInfo) noexcept;
}; };

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "Graphics/MiddleEnd/Interface/SHRenderable.h" #include "Graphics/MiddleEnd/Interface/SHRenderable.h"
#include "Graphics/MiddleEnd/Materials/SHMaterialSpec.h" #include "Graphics/MiddleEnd/Materials/SHMaterialSpec.h"
#include "Physics/Collision/CollisionShapes/SHSphereCollisionShape.h" #include "Physics/Collision/CollisionShapes/SHSphere.h"
#include "Resource/SHResourceManager.h" #include "Resource/SHResourceManager.h"
#include "Math/Vector/SHVec2.h" #include "Math/Vector/SHVec2.h"
#include "Math/Vector/SHVec3.h" #include "Math/Vector/SHVec3.h"
@ -144,13 +144,13 @@ namespace YAML
{ {
case SHCollisionShape::Type::BOX: case SHCollisionShape::Type::BOX:
{ {
const auto& BOX = dynamic_cast<const SHBoxCollisionShape&>(rhs); const auto& BOX = dynamic_cast<const SHBox&>(rhs);
node[HalfExtents] = BOX.GetRelativeExtents(); node[HalfExtents] = BOX.GetRelativeExtents();
} }
break; break;
case SHCollisionShape::Type::SPHERE: case SHCollisionShape::Type::SPHERE:
{ {
const auto& SPHERE = dynamic_cast<const SHSphereCollisionShape&>(rhs); const auto& SPHERE = dynamic_cast<const SHSphere&>(rhs);
node[Radius] = SPHERE.GetRelativeRadius(); node[Radius] = SPHERE.GetRelativeRadius();
} }
break; break;
@ -188,7 +188,7 @@ namespace YAML
{ {
if (node[HalfExtents].IsDefined()) if (node[HalfExtents].IsDefined())
{ {
auto* box = dynamic_cast<SHBoxCollisionShape*>(&rhs); auto* box = dynamic_cast<SHBox*>(&rhs);
box->SetRelativeExtents(node[HalfExtents].as<SHVec3>()); box->SetRelativeExtents(node[HalfExtents].as<SHVec3>());
} }
} }
@ -197,7 +197,7 @@ namespace YAML
{ {
if (node[Radius].IsDefined()) if (node[Radius].IsDefined())
{ {
auto* sphere = dynamic_cast<SHSphereCollisionShape*>(&rhs); auto* sphere = dynamic_cast<SHSphere*>(&rhs);
sphere->SetRelativeRadius(node[Radius].as<float>()); sphere->SetRelativeRadius(node[Radius].as<float>());
} }
} }

View File

@ -128,19 +128,19 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
Vector3 BoxCollider::Center::get() Vector3 BoxCollider::Center::get()
{ {
return Convert::ToCLI(getNativeCollisionShape<SHBoxCollisionShape>().GetCenter()); return Convert::ToCLI(getNativeCollisionShape<SHBox>().GetWorldCentroid());
} }
Vector3 BoxCollider::HalfExtents::get() Vector3 BoxCollider::HalfExtents::get()
{ {
return Convert::ToCLI(getNativeCollisionShape<SHBoxCollisionShape>().GetWorldExtents()); return Convert::ToCLI(getNativeCollisionShape<SHBox>().GetWorldExtents());
} }
void BoxCollider::HalfExtents::set(Vector3 value) void BoxCollider::HalfExtents::set(Vector3 value)
{ {
getNativeCollisionShape<SHBoxCollisionShape>().SetWorldExtents(Convert::ToNative(value)); getNativeCollisionShape<SHBox>().SetWorldExtents(Convert::ToNative(value));
} }
Quaternion BoxCollider::Orientation::get() Quaternion BoxCollider::Orientation::get()
{ {
return Convert::ToCLI(getNativeCollisionShape<SHBoxCollisionShape>().GetOrientation()); return Convert::ToCLI(getNativeCollisionShape<SHBox>().GetWorldOrientation());
} }
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -162,15 +162,15 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
Vector3 SphereCollider::Center::get() Vector3 SphereCollider::Center::get()
{ {
return Convert::ToCLI(getNativeCollisionShape<SHSphereCollisionShape>().GetCenter()); return Convert::ToCLI(getNativeCollisionShape<SHSphere>().GetWorldCentroid());
} }
float SphereCollider::Radius::get() float SphereCollider::Radius::get()
{ {
return getNativeCollisionShape<SHSphereCollisionShape>().GetWorldRadius(); return getNativeCollisionShape<SHSphere>().GetWorldRadius();
} }
void SphereCollider::Radius::set(float value) void SphereCollider::Radius::set(float value)
{ {
getNativeCollisionShape<SHSphereCollisionShape>().SetWorldRadius(value); getNativeCollisionShape<SHSphere>().SetWorldRadius(value);
} }
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
@ -178,11 +178,11 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
bool SphereCollider::TestPoint(Vector3 point) bool SphereCollider::TestPoint(Vector3 point)
{ {
return getNativeCollisionShape<SHSphereCollisionShape>().TestPoint(Convert::ToNative(point)); return getNativeCollisionShape<SHSphere>().TestPoint(Convert::ToNative(point));
} }
bool SphereCollider::Raycast(Ray ray, float maxDistance) bool SphereCollider::Raycast(Ray ray, float maxDistance)
{ {
return getNativeCollisionShape<SHSphereCollisionShape>().Raycast(Convert::ToNative(ray)); return getNativeCollisionShape<SHSphere>().Raycast(Convert::ToNative(ray));
} }
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/

View File

@ -179,20 +179,7 @@ namespace SHADE
throw gcnew System::InvalidOperationException("Attempted to retrieve invalid CollisionShape."); throw gcnew System::InvalidOperationException("Attempted to retrieve invalid CollisionShape.");
const auto& NATIVE_SHAPE = managedShape->getNativeCollisionShape(); const auto& NATIVE_SHAPE = managedShape->getNativeCollisionShape();
switch (NATIVE_SHAPE.GetType()) shapePos = Convert::ToCLI(NATIVE_SHAPE.GetWorldCentroid());
{
case SHCollisionShape::Type::SPHERE:
{
shapePos = Convert::ToCLI(dynamic_cast<const SHSphereCollisionShape&>(NATIVE_SHAPE).GetCenter());
break;
}
case SHCollisionShape::Type::BOX:
{
shapePos = Convert::ToCLI(dynamic_cast<const SHBoxCollisionShape&>(NATIVE_SHAPE).GetCenter());
break;
}
default: break;
}
ray.Position += shapePos; ray.Position += shapePos;
@ -226,20 +213,7 @@ namespace SHADE
throw gcnew System::InvalidOperationException("Attempted to retrieve invalid CollisionShape."); throw gcnew System::InvalidOperationException("Attempted to retrieve invalid CollisionShape.");
const auto& NATIVE_SHAPE = managedShape->getNativeCollisionShape(); const auto& NATIVE_SHAPE = managedShape->getNativeCollisionShape();
switch (NATIVE_SHAPE.GetType()) shapePos = Convert::ToCLI(NATIVE_SHAPE.GetWorldCentroid());
{
case SHCollisionShape::Type::SPHERE:
{
shapePos = Convert::ToCLI(dynamic_cast<const SHSphereCollisionShape&>(NATIVE_SHAPE).GetCenter());
break;
}
case SHCollisionShape::Type::BOX:
{
shapePos = Convert::ToCLI(dynamic_cast<const SHBoxCollisionShape&>(NATIVE_SHAPE).GetCenter());
break;
}
default: break;
}
ray.Position += shapePos; ray.Position += shapePos;
@ -306,20 +280,7 @@ namespace SHADE
throw gcnew System::InvalidOperationException("Attempted to retrieve invalid CollisionShape."); throw gcnew System::InvalidOperationException("Attempted to retrieve invalid CollisionShape.");
const auto& NATIVE_SHAPE = managedShape->getNativeCollisionShape(); const auto& NATIVE_SHAPE = managedShape->getNativeCollisionShape();
switch (NATIVE_SHAPE.GetType()) shapePos = Convert::ToCLI(NATIVE_SHAPE.GetWorldCentroid());
{
case SHCollisionShape::Type::SPHERE:
{
shapePos = Convert::ToCLI(dynamic_cast<const SHSphereCollisionShape&>(NATIVE_SHAPE).GetCenter());
break;
}
case SHCollisionShape::Type::BOX:
{
shapePos = Convert::ToCLI(dynamic_cast<const SHBoxCollisionShape&>(NATIVE_SHAPE).GetCenter());
break;
}
default: break;
}
start += shapePos; start += shapePos;