From 7c4a9ca004ad951d9bd0c81a0075d296e6754373 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Mon, 10 Oct 2022 18:07:21 +0800 Subject: [PATCH 1/8] Added conversions for shade math to reactphysics math --- SHADE_Engine/src/Math/SHQuaternion.cpp | 18 ++++++ SHADE_Engine/src/Math/SHQuaternion.h | 23 +++++-- SHADE_Engine/src/Math/Vector/SHVec2.cpp | 9 +++ SHADE_Engine/src/Math/Vector/SHVec2.h | 16 ++++- SHADE_Engine/src/Math/Vector/SHVec3.cpp | 14 +++++ SHADE_Engine/src/Math/Vector/SHVec3.h | 59 ++++++++++-------- SHADE_Engine/src/Physics/SHPhysicsObject.cpp | 23 ++----- SHADE_Engine/src/Physics/SHPhysicsObject.h | 3 - SHADE_Engine/src/Physics/SHPhysicsSystem.cpp | 64 ++++---------------- SHADE_Engine/src/Physics/SHPhysicsSystem.h | 14 ++--- SHADE_Managed/premake5.lua | 3 +- 11 files changed, 133 insertions(+), 113 deletions(-) diff --git a/SHADE_Engine/src/Math/SHQuaternion.cpp b/SHADE_Engine/src/Math/SHQuaternion.cpp index a6428d30..8463a7c2 100644 --- a/SHADE_Engine/src/Math/SHQuaternion.cpp +++ b/SHADE_Engine/src/Math/SHQuaternion.cpp @@ -67,6 +67,19 @@ namespace SHADE XMStoreFloat4(this, XMQuaternionRotationMatrix(M)); } + SHQuaternion::SHQuaternion(const reactphysics3d::Vector3& rp3dEuler) noexcept + : XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f ) + { + const SHVec3& SHADE_VEC{ rp3dEuler }; + + const XMVECTOR V = XMLoadFloat3(&SHADE_VEC); + XMStoreFloat4(this, XMQuaternionRotationRollPitchYawFromVector(V)); + } + + SHQuaternion::SHQuaternion(const reactphysics3d::Quaternion& rp3dQuat) noexcept + : XMFLOAT4( rp3dQuat.x, rp3dQuat.y, rp3dQuat.z, rp3dQuat.w ) + {} + /*-----------------------------------------------------------------------------------*/ /* Operator Overload Definitions */ /*-----------------------------------------------------------------------------------*/ @@ -176,6 +189,11 @@ namespace SHADE return XMQuaternionNotEqual(Q1, Q2); } + SHQuaternion::operator reactphysics3d::Quaternion() const noexcept + { + return reactphysics3d::Quaternion{ x, y, z, w }; + } + SHQuaternion operator*(float lhs, const SHQuaternion& rhs) noexcept { return rhs * lhs; diff --git a/SHADE_Engine/src/Math/SHQuaternion.h b/SHADE_Engine/src/Math/SHQuaternion.h index 61b55ef9..01452cef 100644 --- a/SHADE_Engine/src/Math/SHQuaternion.h +++ b/SHADE_Engine/src/Math/SHQuaternion.h @@ -11,6 +11,8 @@ #pragma once #include +#include + #include // Project Headers @@ -46,12 +48,17 @@ namespace SHADE SHQuaternion (const SHQuaternion& rhs) = default; SHQuaternion (SHQuaternion&& rhs) = default; - SHQuaternion () noexcept; - SHQuaternion (float x, float y, float z, float w) noexcept; - SHQuaternion (float yaw, float pitch, float roll) noexcept; - SHQuaternion (const SHVec3& eulerAngles) noexcept; - SHQuaternion (const SHVec3& axis, float angleInRad) noexcept; - SHQuaternion (const SHMatrix& rotationMatrix) noexcept; + SHQuaternion () noexcept; + SHQuaternion (float x, float y, float z, float w) noexcept; + SHQuaternion (float yaw, float pitch, float roll) noexcept; + SHQuaternion (const SHVec3& eulerAngles) noexcept; + SHQuaternion (const SHVec3& axis, float angleInRad) noexcept; + SHQuaternion (const SHMatrix& rotationMatrix) noexcept; + + // Conversion from other math types + + SHQuaternion (const reactphysics3d::Vector3& rp3dEuler) noexcept; + SHQuaternion (const reactphysics3d::Quaternion& rp3dQuat) noexcept; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ @@ -76,6 +83,10 @@ namespace SHADE [[nodiscard]] bool operator== (const SHQuaternion& rhs) const noexcept; [[nodiscard]] bool operator!= (const SHQuaternion& rhs) const noexcept; + // Conversion to other math types used by SHADE + + operator reactphysics3d::Quaternion () const noexcept; + /*---------------------------------------------------------------------------------*/ /* Getter Functions */ /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/Vector/SHVec2.cpp b/SHADE_Engine/src/Math/Vector/SHVec2.cpp index 545492cb..c417a1b2 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec2.cpp +++ b/SHADE_Engine/src/Math/Vector/SHVec2.cpp @@ -46,6 +46,10 @@ namespace SHADE : XMFLOAT2( _x, _y ) {} + SHVec2::SHVec2(const reactphysics3d::Vector2& rp3dVec2) noexcept + : XMFLOAT2( rp3dVec2.x, rp3dVec2.y ) + {} + /*-----------------------------------------------------------------------------------*/ /* Operator Overload Definitions */ /*-----------------------------------------------------------------------------------*/ @@ -213,6 +217,11 @@ namespace SHADE } } + SHVec2::operator reactphysics3d::Vector2() const noexcept + { + return reactphysics3d::Vector2{ x, y }; + } + SHVec2 operator* (float lhs, const SHVec2& rhs) noexcept { SHVec2 result; diff --git a/SHADE_Engine/src/Math/Vector/SHVec2.h b/SHADE_Engine/src/Math/Vector/SHVec2.h index 17642126..5f2f50e2 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec2.h +++ b/SHADE_Engine/src/Math/Vector/SHVec2.h @@ -11,6 +11,8 @@ #pragma once #include +#include + #include #include @@ -52,9 +54,13 @@ namespace SHADE SHVec2 (SHVec2&& rhs) = default; ~SHVec2 () = default; - SHVec2 () noexcept; - SHVec2 (float n) noexcept; - SHVec2 (float x, float y) noexcept; + SHVec2 () noexcept; + SHVec2 (float n) noexcept; + SHVec2 (float x, float y) noexcept; + + // Conversion from other math types to SHADE + + SHVec2 (const reactphysics3d::Vector2& rp3dVec2) noexcept; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ @@ -86,6 +92,10 @@ namespace SHADE [[nodiscard]] float operator[] (int index) const; [[nodiscard]] float operator[] (size_t index) const; + // Conversion to other math types used by SHADE + + operator reactphysics3d::Vector2 () const noexcept; + /*---------------------------------------------------------------------------------*/ /* Function Members */ /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/Vector/SHVec3.cpp b/SHADE_Engine/src/Math/Vector/SHVec3.cpp index 1bcb47b3..2d86276b 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec3.cpp +++ b/SHADE_Engine/src/Math/Vector/SHVec3.cpp @@ -51,6 +51,10 @@ namespace SHADE : XMFLOAT3( _x, _y, _z ) {} + SHVec3::SHVec3(const reactphysics3d::Vector3& rp3dVec3) noexcept + : XMFLOAT3( rp3dVec3.x, rp3dVec3.y, rp3dVec3.z ) + {} + /*-----------------------------------------------------------------------------------*/ /* Operator Overload Definitions */ /*-----------------------------------------------------------------------------------*/ @@ -223,6 +227,16 @@ namespace SHADE } } + SHVec3::operator reactphysics3d::Vector3() const noexcept + { + return reactphysics3d::Vector3{ x, y , z }; + } + + SHVec3::operator reactphysics3d::Quaternion() const noexcept + { + return reactphysics3d::Quaternion::fromEulerAngles(x, y, z); + } + SHVec3 operator* (float lhs, const SHVec3& rhs) noexcept { SHVec3 result; diff --git a/SHADE_Engine/src/Math/Vector/SHVec3.h b/SHADE_Engine/src/Math/Vector/SHVec3.h index cc0e043e..03a3578f 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec3.h +++ b/SHADE_Engine/src/Math/Vector/SHVec3.h @@ -11,6 +11,9 @@ #pragma once #include +#include +#include + #include #include @@ -57,40 +60,48 @@ namespace SHADE SHVec3 (SHVec3&& rhs) = default; ~SHVec3 () = default; - SHVec3 () noexcept; - SHVec3 (float n) noexcept; - SHVec3 (float x, float y, float z) noexcept; + SHVec3 () noexcept; + SHVec3 (float n) noexcept; + SHVec3 (float x, float y, float z) noexcept; + + // Conversion from other math types to SHADE + + SHVec3 (const reactphysics3d::Vector3& rp3dVec3) noexcept; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ /*---------------------------------------------------------------------------------*/ - SHVec3& operator= (const SHVec3& rhs) = default; - SHVec3& operator= (SHVec3&& rhs) = default; + SHVec3& operator= (const SHVec3& rhs) = default; + SHVec3& operator= (SHVec3&& rhs) = default; - SHVec3& operator+= (const SHVec3& rhs) noexcept; - SHVec3& operator-= (const SHVec3& rhs) noexcept; - SHVec3& operator*= (const SHVec3& rhs) noexcept; - SHVec3& operator*= (float rhs) noexcept; - SHVec3& operator/= (const SHVec3& rhs) noexcept; - SHVec3& operator/= (float rhs) noexcept; + SHVec3& operator+= (const SHVec3& rhs) noexcept; + SHVec3& operator-= (const SHVec3& rhs) noexcept; + SHVec3& operator*= (const SHVec3& rhs) noexcept; + SHVec3& operator*= (float rhs) noexcept; + SHVec3& operator/= (const SHVec3& rhs) noexcept; + SHVec3& operator/= (float rhs) noexcept; - [[nodiscard]] SHVec3 operator+ (const SHVec3& rhs) const noexcept; - [[nodiscard]] SHVec3 operator- (const SHVec3& rhs) const noexcept; - [[nodiscard]] SHVec3 operator- () const noexcept; - [[nodiscard]] SHVec3 operator* (const SHVec3& rhs) const noexcept; - [[nodiscard]] SHVec3 operator* (float rhs) const noexcept; - [[nodiscard]] SHVec3 operator/ (const SHVec3& rhs) const noexcept; - [[nodiscard]] SHVec3 operator/ (float rhs) const noexcept; + [[nodiscard]] SHVec3 operator+ (const SHVec3& rhs) const noexcept; + [[nodiscard]] SHVec3 operator- (const SHVec3& rhs) const noexcept; + [[nodiscard]] SHVec3 operator- () const noexcept; + [[nodiscard]] SHVec3 operator* (const SHVec3& rhs) const noexcept; + [[nodiscard]] SHVec3 operator* (float rhs) const noexcept; + [[nodiscard]] SHVec3 operator/ (const SHVec3& rhs) const noexcept; + [[nodiscard]] SHVec3 operator/ (float rhs) const noexcept; - [[nodiscard]] bool operator== (const SHVec3& rhs) const noexcept; - [[nodiscard]] bool operator!= (const SHVec3& rhs) const noexcept; + [[nodiscard]] bool operator== (const SHVec3& rhs) const noexcept; + [[nodiscard]] bool operator!= (const SHVec3& rhs) const noexcept; - [[nodiscard]] float& operator[] (int index); - [[nodiscard]] float& operator[] (size_t index); - [[nodiscard]] float operator[] (int index) const; - [[nodiscard]] float operator[] (size_t index) const; + [[nodiscard]] float& operator[] (int index); + [[nodiscard]] float& operator[] (size_t index); + [[nodiscard]] float operator[] (int index) const; + [[nodiscard]] float operator[] (size_t index) const; + // Conversion to other math types used by SHADE + + operator reactphysics3d::Vector3 () const noexcept; + operator reactphysics3d::Quaternion () const noexcept; /*---------------------------------------------------------------------------------*/ /* Function Members */ diff --git a/SHADE_Engine/src/Physics/SHPhysicsObject.cpp b/SHADE_Engine/src/Physics/SHPhysicsObject.cpp index 844803b1..1188ae40 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsObject.cpp +++ b/SHADE_Engine/src/Physics/SHPhysicsObject.cpp @@ -44,10 +44,7 @@ namespace SHADE SHVec3 result; if (rp3dBody) - { - const auto& RP3D_RESULT = rp3dBody->getTransform().getPosition(); - result = SHVec3{ RP3D_RESULT.x, RP3D_RESULT.y, RP3D_RESULT.z }; - } + result = SHVec3{ rp3dBody->getTransform().getPosition() }; return result; } @@ -57,10 +54,7 @@ namespace SHADE SHQuaternion result; if (rp3dBody) - { - const auto& RP3D_RESULT = rp3dBody->getTransform().getOrientation(); - result = SHQuaternion{ RP3D_RESULT.x, RP3D_RESULT.y, RP3D_RESULT.z, RP3D_RESULT.w }; - } + result = SHQuaternion{ rp3dBody->getTransform().getOrientation() }; return result; } @@ -70,10 +64,7 @@ namespace SHADE SHVec3 result; if (rp3dBody) - { - const auto& RP3D_RESULT = rp3dBody->getTransform().getOrientation(); - result = SHQuaternion{ RP3D_RESULT.x, RP3D_RESULT.y, RP3D_RESULT.z, RP3D_RESULT.w }.ToEuler(); - } + result = SHQuaternion{ rp3dBody->getTransform().getOrientation() }.ToEuler(); return result; } @@ -84,10 +75,8 @@ namespace SHADE void SHPhysicsObject::SetPosition(const SHVec3& position) noexcept { - const rp3d::Vector3 RP3D_POS { position.x, position.y, position.z }; - rp3d::Transform rp3dTF; - rp3dTF.setPosition(RP3D_POS); + rp3dTF.setPosition(position); rp3dTF.setOrientation(rp3dBody->getTransform().getOrientation()); rp3dBody->setTransform(rp3dTF); @@ -96,11 +85,9 @@ namespace SHADE void SHPhysicsObject::SetOrientation(const SHQuaternion& orientation) noexcept { - const rp3d::Quaternion RP3D_ORIENTATION { orientation.x, orientation.y, orientation.z, orientation.w }; - rp3d::Transform rp3dTF; rp3dTF.setPosition(rp3dBody->getTransform().getPosition()); - rp3dTF.setOrientation(RP3D_ORIENTATION); + rp3dTF.setOrientation(orientation); rp3dBody->setTransform(rp3dTF); prevTransform = rp3dTF; diff --git a/SHADE_Engine/src/Physics/SHPhysicsObject.h b/SHADE_Engine/src/Physics/SHPhysicsObject.h index cdff2f5b..6002d697 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsObject.h +++ b/SHADE_Engine/src/Physics/SHPhysicsObject.h @@ -82,8 +82,5 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Function Members */ /*---------------------------------------------------------------------------------*/ - - - }; } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp index f3a4c276..1fb8232e 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp @@ -68,10 +68,7 @@ namespace SHADE if (world) { - const auto RP3D_GRAVITY = world->getGravity(); - result.x = RP3D_GRAVITY.x; - result.y = RP3D_GRAVITY.y; - result.z = RP3D_GRAVITY.z; + result = world->getGravity(); } else { @@ -112,8 +109,7 @@ namespace SHADE { if (world) { - const rp3d::Vector3 G { gravity.x, gravity.y, gravity.z }; - world->setGravity(G); + world->setGravity(gravity); } else { @@ -161,8 +157,7 @@ namespace SHADE { if (world) { - const rp3d::Vector3 G { settings.gravity.x, settings.gravity.y, settings.gravity.z }; - world->setGravity(G); + world->setGravity(settings.gravity); world->setNbIterationsVelocitySolver(settings.numVelocitySolverIterations); world->setNbIterationsPositionSolver(settings.numPositionSolverIterations); world->enableSleeping(settings.sleepingEnabled); @@ -225,13 +220,7 @@ namespace SHADE SHADE_TF = SHComponentManager::GetComponent(entityID); } - const SHVec3& SHADE_POS = SHADE_TF->GetWorldPosition(); - const SHVec3& SHADE_ROT = SHADE_TF->GetWorldRotation(); - - const rp3d::Vector3 RP3D_POS { SHADE_POS.x, SHADE_POS.y, SHADE_POS.z }; - const rp3d::Quaternion RP3D_ROT = rp3d::Quaternion::fromEulerAngles( SHADE_ROT.x, SHADE_ROT.y, SHADE_ROT.z ); - - const rp3d::Transform RP3D_TF { RP3D_POS, RP3D_ROT }; + const rp3d::Transform RP3D_TF { SHADE_TF->GetWorldPosition(), SHADE_TF->GetWorldRotation() }; // If collider already exists if (physicsObject->hasColliders) @@ -251,10 +240,7 @@ namespace SHADE case SHCollider::Type::BOX: { SHBoundingBox* box = reinterpret_cast(collider.GetShape()); - const SHVec3& SHADE_EXTENTS = box->GetHalfExtents(); - - rp3d::Vector3 RP3D_EXTENTS { SHADE_EXTENTS.x, SHADE_EXTENTS.y, SHADE_EXTENTS.z }; - rp3d::BoxShape* newBox = factory.createBoxShape(RP3D_EXTENTS); + rp3d::BoxShape* newBox = factory.createBoxShape(box->GetHalfExtents()); // TODO(Diren): Handle offsets physicsObject->rp3dBody->addCollider(newBox, RP3D_TF); @@ -296,13 +282,7 @@ namespace SHADE SHADE_TF = SHComponentManager::GetComponent(entityID); } - const SHVec3& SHADE_POS = SHADE_TF->GetWorldPosition(); - const SHVec3& SHADE_ROT = SHADE_TF->GetWorldRotation(); - - const rp3d::Vector3 RP3D_POS { SHADE_POS.x, SHADE_POS.y, SHADE_POS.z }; - const rp3d::Quaternion RP3D_ROT = rp3d::Quaternion::fromEulerAngles( SHADE_ROT.x, SHADE_ROT.y, SHADE_ROT.z ); - - const rp3d::Transform RP3D_TF { RP3D_POS, RP3D_ROT }; + const rp3d::Transform RP3D_TF { SHADE_TF->GetWorldPosition(), SHADE_TF->GetWorldRotation() }; // No rb if (!physicsObject->isRigidBody) @@ -316,10 +296,7 @@ namespace SHADE case SHCollider::Type::BOX: { SHBoundingBox* box = reinterpret_cast(collider.GetShape()); - const SHVec3& SHADE_EXTENTS = box->GetHalfExtents(); - - rp3d::Vector3 RP3D_EXTENTS { SHADE_EXTENTS.x, SHADE_EXTENTS.y, SHADE_EXTENTS.z }; - rp3d::BoxShape* newBox = factory.createBoxShape(RP3D_EXTENTS); + rp3d::BoxShape* newBox = factory.createBoxShape(box->GetHalfExtents()); // TODO(Diren): Handle offsets physicsObject->rp3dBody->addCollider(newBox, RP3D_TF); @@ -401,10 +378,7 @@ namespace SHADE case SHShape::Type::BOUNDING_BOX: { auto* box = reinterpret_cast(shape); - const SHVec3& SHADE_EXTENTS = box->GetHalfExtents(); - - rp3d::Vector3 RP3D_EXTENTS { SHADE_EXTENTS.x, SHADE_EXTENTS.y, SHADE_EXTENTS.z }; - rp3d::BoxShape* newBox = factory.createBoxShape(RP3D_EXTENTS); + rp3d::BoxShape* newBox = factory.createBoxShape(box->GetHalfExtents()); // TODO(Diren): Handle offsets @@ -593,12 +567,9 @@ namespace SHADE } // Convert RP3D Transform to SHADE - const SHVec3 SHADE_POS = SHVec3{ rp3dPos.x, rp3dPos.y, rp3dPos.z }; - const SHVec3 SHADE_ROT = SHQuaternion{ rp3dRot.x, rp3dRot.y, rp3dRot.z, rp3dRot.w }.ToEuler(); - auto* tfComponent = SHComponentManager::GetComponent(ENTITY_ID); - tfComponent->SetWorldPosition(SHADE_POS); - tfComponent->SetWorldRotation(SHADE_ROT); + tfComponent->SetWorldPosition(rp3dPos); + tfComponent->SetWorldRotation(SHQuaternion{ rp3dRot }.ToEuler()); // Cache transforms physicsObject.prevTransform = CURRENT_TF; @@ -660,18 +631,12 @@ namespace SHADE } case 8: // Linear Velocity { - const SHVec3& SHADE_VEL = comp->GetLinearVelocity(); - rp3d::Vector3 RP3D_VEL { SHADE_VEL.x, SHADE_VEL.y, SHADE_VEL.z }; - - rigidBody->setLinearVelocity(RP3D_VEL); + rigidBody->setLinearVelocity(comp->GetLinearVelocity()); break; } case 9: // Angular Velocity { - const SHVec3& SHADE_VEL = comp->GetAngularVelocity(); - rp3d::Vector3 RP3D_VEL { SHADE_VEL.x, SHADE_VEL.y, SHADE_VEL.z }; - - rigidBody->setAngularVelocity(RP3D_VEL); + rigidBody->setAngularVelocity(comp->GetAngularVelocity()); break; } default: break; @@ -718,12 +683,9 @@ namespace SHADE case SHCollider::Type::BOX: { SHBoundingBox* box = reinterpret_cast(collider.GetShape()); - const SHVec3& SHADE_EXTENTS = box->GetHalfExtents(); - - rp3d::Vector3 RP3D_EXTENTS { SHADE_EXTENTS.x, SHADE_EXTENTS.y, SHADE_EXTENTS.z }; auto* rp3dBoxShape = reinterpret_cast(physicsObject->rp3dBody->getCollider(index)->getCollisionShape()); - rp3dBoxShape->setHalfExtents(RP3D_EXTENTS); + rp3dBoxShape->setHalfExtents(box->GetHalfExtents()); if (rp3dBoxShape) { diff --git a/SHADE_Engine/src/Physics/SHPhysicsSystem.h b/SHADE_Engine/src/Physics/SHPhysicsSystem.h index 4eaf4a89..92a4730d 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsSystem.h +++ b/SHADE_Engine/src/Physics/SHPhysicsSystem.h @@ -185,18 +185,18 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Function Members */ /*---------------------------------------------------------------------------------*/ - SHPhysicsObject* GetPhysicsObject (EntityID entityID) noexcept; + SHPhysicsObject* GetPhysicsObject (EntityID entityID) noexcept; - void SyncRigidBodyComponents (std::vector& denseArray) noexcept; - void SyncColliderComponents (std::vector& denseArray) noexcept; - void SyncTransforms () noexcept; + void SyncRigidBodyComponents (std::vector& denseArray) noexcept; + void SyncColliderComponents (std::vector& denseArray) noexcept; + void SyncTransforms () noexcept; // TODO(Diren): Trigger handling static void SyncRigidBody (SHPhysicsObject const* physicsObject, const SHRigidBodyComponent* comp) noexcept; - static void SetRP3DLinearConstraints (rp3d::RigidBody const* rp3dRigidBody, uint8_t rbFlags) noexcept; - static void SetRP3DAngularConstraints (rp3d::RigidBody const* rp3dRigidBody, uint8_t rbFlags) noexcept; + static void SetRP3DLinearConstraints (rp3d::RigidBody const* rp3dRigidBody, uint8_t rbFlags) noexcept; + static void SetRP3DAngularConstraints (rp3d::RigidBody const* rp3dRigidBody, uint8_t rbFlags) noexcept; - static void SyncCollider (SHPhysicsObject const* physicsObject, SHColliderComponent* comp) noexcept; + static void SyncCollider (SHPhysicsObject const* physicsObject, SHColliderComponent* comp) noexcept; }; diff --git a/SHADE_Managed/premake5.lua b/SHADE_Managed/premake5.lua index 052be24a..6d37122c 100644 --- a/SHADE_Managed/premake5.lua +++ b/SHADE_Managed/premake5.lua @@ -30,8 +30,9 @@ project "SHADE_Managed" "%{IncludeDir.imguizmo}", "%{IncludeDir.imnodes}", "%{IncludeDir.yamlcpp}", - "%{IncludeDir.RTTR}/include", + "%{IncludeDir.RTTR}/include", "%{IncludeDir.dotnet}\\include", + "%{IncludeDir.reactphysics3d}\\include", "%{wks.location}/SHADE_Engine/src" } From 46a082b62efab28def786201e204ac07b2c5492b Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Mon, 10 Oct 2022 21:13:14 +0800 Subject: [PATCH 2/8] Moved rigidbody creation into physics object --- .../src/Math/Geometry/SHBoundingBox.cpp | 14 +-- SHADE_Engine/src/Math/Geometry/SHShape.h | 2 +- SHADE_Engine/src/Math/SHQuaternion.cpp | 5 ++ SHADE_Engine/src/Math/SHQuaternion.h | 1 + SHADE_Engine/src/Math/Vector/SHVec3.cpp | 5 ++ SHADE_Engine/src/Math/Vector/SHVec3.h | 9 +- .../Physics/Components/SHColliderComponent.h | 6 ++ .../Components/SHRigidBodyComponent.cpp | 6 +- .../Physics/Components/SHRigidBodyComponent.h | 9 +- SHADE_Engine/src/Physics/SHPhysicsObject.cpp | 85 ++++++++++++++++-- SHADE_Engine/src/Physics/SHPhysicsObject.h | 37 +++++--- SHADE_Engine/src/Physics/SHPhysicsSystem.cpp | 90 +++++++++---------- SHADE_Engine/src/Physics/SHPhysicsSystem.h | 14 ++- 13 files changed, 198 insertions(+), 85 deletions(-) diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp index 3abcc315..9068fe42 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp @@ -26,13 +26,13 @@ namespace SHADE , center { c } , halfExtents { hE } { - type = Type::BOUNDING_BOX; + type = Type::BOX; } SHBoundingBox::SHBoundingBox(const SHVec3* vertices, size_t numVertices) noexcept : SHShape {} { - type = Type::BOUNDING_BOX; + type = Type::BOX; if (vertices == nullptr || numVertices < 2) { @@ -63,7 +63,7 @@ namespace SHADE SHBoundingBox::SHBoundingBox(const SHBoundingBox* boxes, size_t numBoxes) noexcept : SHShape {} { - type = Type::BOUNDING_BOX; + type = Type::BOX; if (boxes == nullptr || numBoxes == 0) { @@ -83,7 +83,7 @@ namespace SHADE , center { rhs.center } , halfExtents { rhs.halfExtents } { - type = Type::BOUNDING_BOX; + type = Type::BOX; } SHBoundingBox::SHBoundingBox(SHBoundingBox&& rhs) noexcept @@ -91,12 +91,12 @@ namespace SHADE , center { rhs.center } , halfExtents { rhs.halfExtents } { - type = Type::BOUNDING_BOX; + type = Type::BOX; } SHBoundingBox& SHBoundingBox::operator=(const SHBoundingBox& rhs) noexcept { - if (rhs.type != Type::BOUNDING_BOX) + if (rhs.type != Type::BOX) { SHLOG_WARNING("Cannot assign a non-bounding box to a bounding box!") } @@ -111,7 +111,7 @@ namespace SHADE SHBoundingBox& SHBoundingBox::operator=(SHBoundingBox&& rhs) noexcept { - if (rhs.type != Type::BOUNDING_BOX) + if (rhs.type != Type::BOX) { SHLOG_WARNING("Cannot assign a non-bounding box to a bounding box!") } diff --git a/SHADE_Engine/src/Math/Geometry/SHShape.h b/SHADE_Engine/src/Math/Geometry/SHShape.h index e33ca583..8efece09 100644 --- a/SHADE_Engine/src/Math/Geometry/SHShape.h +++ b/SHADE_Engine/src/Math/Geometry/SHShape.h @@ -29,7 +29,7 @@ namespace SHADE enum class Type { - BOUNDING_BOX + BOX , SPHERE , CAPSULE , CONVEX_HULL diff --git a/SHADE_Engine/src/Math/SHQuaternion.cpp b/SHADE_Engine/src/Math/SHQuaternion.cpp index 8463a7c2..33c568a5 100644 --- a/SHADE_Engine/src/Math/SHQuaternion.cpp +++ b/SHADE_Engine/src/Math/SHQuaternion.cpp @@ -194,6 +194,11 @@ namespace SHADE return reactphysics3d::Quaternion{ x, y, z, w }; } + SHQuaternion::operator reactphysics3d::Vector3() const noexcept + { + return reactphysics3d::Vector3{ ToEuler() }; + } + SHQuaternion operator*(float lhs, const SHQuaternion& rhs) noexcept { return rhs * lhs; diff --git a/SHADE_Engine/src/Math/SHQuaternion.h b/SHADE_Engine/src/Math/SHQuaternion.h index 01452cef..c94907b5 100644 --- a/SHADE_Engine/src/Math/SHQuaternion.h +++ b/SHADE_Engine/src/Math/SHQuaternion.h @@ -86,6 +86,7 @@ namespace SHADE // Conversion to other math types used by SHADE operator reactphysics3d::Quaternion () const noexcept; + operator reactphysics3d::Vector3 () const noexcept; /*---------------------------------------------------------------------------------*/ /* Getter Functions */ diff --git a/SHADE_Engine/src/Math/Vector/SHVec3.cpp b/SHADE_Engine/src/Math/Vector/SHVec3.cpp index 2d86276b..8ec91fb8 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec3.cpp +++ b/SHADE_Engine/src/Math/Vector/SHVec3.cpp @@ -14,6 +14,7 @@ #include "SHVec3.h" // Project Headers #include "Math/SHMatrix.h" +#include "Math/SHQuaternion.h" #include "Tools/SHLogger.h" using namespace DirectX; @@ -55,6 +56,10 @@ namespace SHADE : XMFLOAT3( rp3dVec3.x, rp3dVec3.y, rp3dVec3.z ) {} + SHVec3::SHVec3(const reactphysics3d::Quaternion& rp3dVec3) noexcept + : XMFLOAT3( SHQuaternion{rp3dVec3}.ToEuler() ) + {} + /*-----------------------------------------------------------------------------------*/ /* Operator Overload Definitions */ /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/Vector/SHVec3.h b/SHADE_Engine/src/Math/Vector/SHVec3.h index 03a3578f..7badf6cd 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec3.h +++ b/SHADE_Engine/src/Math/Vector/SHVec3.h @@ -60,13 +60,14 @@ namespace SHADE SHVec3 (SHVec3&& rhs) = default; ~SHVec3 () = default; - SHVec3 () noexcept; - SHVec3 (float n) noexcept; - SHVec3 (float x, float y, float z) noexcept; + SHVec3 () noexcept; + SHVec3 (float n) noexcept; + SHVec3 (float x, float y, float z) noexcept; // Conversion from other math types to SHADE - SHVec3 (const reactphysics3d::Vector3& rp3dVec3) noexcept; + SHVec3 (const reactphysics3d::Vector3& rp3dVec3) noexcept; + SHVec3 (const reactphysics3d::Quaternion& rp3dVec3) noexcept; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ diff --git a/SHADE_Engine/src/Physics/Components/SHColliderComponent.h b/SHADE_Engine/src/Physics/Components/SHColliderComponent.h index cb9696d9..fd4bcb9a 100644 --- a/SHADE_Engine/src/Physics/Components/SHColliderComponent.h +++ b/SHADE_Engine/src/Physics/Components/SHColliderComponent.h @@ -16,6 +16,11 @@ #include "ECS_Base/Components/SHComponent.h" #include "Physics/SHCollider.h" +//namespace SHADE +//{ +// class SHPhysicsSystem; +//} + namespace SHADE { /*-----------------------------------------------------------------------------------*/ @@ -30,6 +35,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ friend class SHPhysicsSystem; + friend class SHPhysicsObject; /*---------------------------------------------------------------------------------*/ /* Type Definitions */ diff --git a/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.cpp b/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.cpp index 5f961de4..e5938717 100644 --- a/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.cpp +++ b/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.cpp @@ -392,9 +392,9 @@ RTTR_REGISTRATION registration::enumeration("RigidBody Type") ( - value("Static", SHRigidBodyComponent::Type::STATIC), - value("Dynamic", SHRigidBodyComponent::Type::DYNAMIC), - value("Kinematic", SHRigidBodyComponent::Type::KINEMATIC) + value("Static", SHRigidBodyComponent::Type::STATIC), + value("Kinematic", SHRigidBodyComponent::Type::KINEMATIC), + value("Dynamic", SHRigidBodyComponent::Type::DYNAMIC) ); registration::class_("RigidBody Component") diff --git a/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.h b/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.h index 0793b2ff..285df810 100644 --- a/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.h +++ b/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.h @@ -14,7 +14,13 @@ // Project Headers #include "ECS_Base/Components/SHComponent.h" -#include "Physics/SHPhysicsObject.h" +#include "Math/Vector/SHVec3.h" +#include "Math/SHQuaternion.h" + +//namespace SHADE +//{ +// class SHPhysicsSystem; +//} namespace SHADE { @@ -30,6 +36,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ friend class SHPhysicsSystem; + friend class SHPhysicsObject; public: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Physics/SHPhysicsObject.cpp b/SHADE_Engine/src/Physics/SHPhysicsObject.cpp index 1188ae40..a007c5fe 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsObject.cpp +++ b/SHADE_Engine/src/Physics/SHPhysicsObject.cpp @@ -15,7 +15,8 @@ // Project Headers #include "ECS_Base/Managers/SHSystemManager.h" -#include "SHPhysicsSystem.h" +#include "ECS_Base/Managers/SHComponentManager.h" + namespace SHADE { @@ -23,16 +24,20 @@ namespace SHADE /* Constructors & Destructor Definitions */ /*-----------------------------------------------------------------------------------*/ - SHPhysicsObject::SHPhysicsObject() noexcept - : entityID { MAX_EID } + SHPhysicsObject::SHPhysicsObject(EntityID eid, rp3d::PhysicsCommon* physicsFactory, rp3d::PhysicsWorld* physicsWorld) noexcept + : entityID { eid } , isRigidBody { false } , hasColliders{ false } + , factory { physicsFactory } + , world { physicsWorld } , rp3dBody { nullptr } {} SHPhysicsObject::~SHPhysicsObject() noexcept { - rp3dBody = nullptr; + factory = nullptr; + world = nullptr; + rp3dBody = nullptr; } /*-----------------------------------------------------------------------------------*/ @@ -75,6 +80,12 @@ namespace SHADE void SHPhysicsObject::SetPosition(const SHVec3& position) noexcept { + if (!rp3dBody) + { + SHLOG_ERROR("Cannot set position of a non-existent physics body for Entity {}", entityID) + return; + } + rp3d::Transform rp3dTF; rp3dTF.setPosition(position); rp3dTF.setOrientation(rp3dBody->getTransform().getOrientation()); @@ -85,6 +96,12 @@ namespace SHADE void SHPhysicsObject::SetOrientation(const SHQuaternion& orientation) noexcept { + if (!rp3dBody) + { + SHLOG_ERROR("Cannot set orientation of a non-existent physics body for Entity {}", entityID) + return; + } + rp3d::Transform rp3dTF; rp3dTF.setPosition(rp3dBody->getTransform().getPosition()); rp3dTF.setOrientation(orientation); @@ -95,14 +112,70 @@ namespace SHADE void SHPhysicsObject::SetRotation(const SHVec3& rotation) noexcept { - const rp3d::Quaternion RP3D_ORIENTATION = rp3d::Quaternion::fromEulerAngles( rotation.x, rotation.y, rotation.z ); + if (!rp3dBody) + { + SHLOG_ERROR("Cannot set rotation of a non-existent physics body for Entity {}", entityID) + return; + } rp3d::Transform rp3dTF; rp3dTF.setPosition(rp3dBody->getTransform().getPosition()); - rp3dTF.setOrientation(RP3D_ORIENTATION); + rp3dTF.setOrientation(rotation); rp3dBody->setTransform(rp3dTF); prevTransform = rp3dTF; } + /*-----------------------------------------------------------------------------------*/ + /* Public Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + void SHPhysicsObject::CreateRigidBody(const SHTransformComponent* tf, SHRigidBodyComponent* rb, SHColliderComponent* c) + { + // If collider already exists + if (hasColliders) + world->destroyCollisionBody(rp3dBody); + + rp3dBody = world->createRigidBody(rp3d::Transform{ tf->GetWorldPosition(), tf->GetWorldRotation() }); + isRigidBody = true; + + rb->position = tf->GetWorldPosition(); + rb->orientation = tf->GetWorldRotation(); + } + + void SHPhysicsObject::DestroyRigidBody() noexcept + { + + } + + void SHPhysicsObject::CreateCollisionBody(const SHTransformComponent* tf, SHColliderComponent* c) + { + + } + + void SHPhysicsObject::DestroyCollisionBody() noexcept + { + + } + + int SHPhysicsObject::AddCollider(SHCollider* collider) + { + return 0; + } + + void SHPhysicsObject::RemoveCollider(int index) noexcept + { + + } + + void SHPhysicsObject::SyncRigidBody() noexcept + { + + } + + void SHPhysicsObject::SyncColliders() noexcept + { + + } + } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/SHPhysicsObject.h b/SHADE_Engine/src/Physics/SHPhysicsObject.h index 6002d697..c6ecd250 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsObject.h +++ b/SHADE_Engine/src/Physics/SHPhysicsObject.h @@ -13,9 +13,9 @@ #include // Project Headers -#include "Math/Vector/SHVec3.h" -#include "Math/SHQuaternion.h" -#include "ECS_Base/Entity/SHEntity.h" +#include "Math/Transform/SHTransformComponent.h" +#include "Components/SHRigidBodyComponent.h" +#include "Components/SHColliderComponent.h" namespace SHADE { @@ -31,15 +31,13 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ friend class SHPhysicsSystem; - friend class SHRigidBodyComponent; - friend class SHColliderComponent; public: /*---------------------------------------------------------------------------------*/ /* Constructors & Destructor */ /*---------------------------------------------------------------------------------*/ - SHPhysicsObject () noexcept; + SHPhysicsObject (EntityID eid, rp3d::PhysicsCommon* physicsFactory, rp3d::PhysicsWorld* physicsWorld) noexcept; SHPhysicsObject (const SHPhysicsObject& rhs) noexcept = default; SHPhysicsObject (SHPhysicsObject&& rhs) noexcept = default; virtual ~SHPhysicsObject () noexcept; @@ -63,9 +61,24 @@ namespace SHADE /* Setter Functions */ /*---------------------------------------------------------------------------------*/ - void SetPosition (const SHVec3& position) noexcept; - void SetOrientation (const SHQuaternion& orientation) noexcept; - void SetRotation (const SHVec3& rotation) noexcept; + void SetPosition (const SHVec3& position) noexcept; + void SetOrientation (const SHQuaternion& orientation) noexcept; + void SetRotation (const SHVec3& rotation) noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Function Members */ + /*---------------------------------------------------------------------------------*/ + + void CreateRigidBody (const SHTransformComponent* tf, SHRigidBodyComponent* rb, SHColliderComponent* c); + void DestroyRigidBody () noexcept; + + void CreateCollisionBody (const SHTransformComponent* tf, SHColliderComponent* c); + int AddCollider (SHCollider* collider); + void RemoveCollider (int index) noexcept; + void DestroyCollisionBody () noexcept; + + void SyncRigidBody () noexcept; + void SyncColliders () noexcept; private: /*---------------------------------------------------------------------------------*/ @@ -76,11 +89,9 @@ namespace SHADE bool isRigidBody; bool hasColliders; + rp3d::PhysicsCommon* factory; + rp3d::PhysicsWorld* world; rp3d::CollisionBody* rp3dBody; // Can be either a collision body or a rigid body rp3d::Transform prevTransform; // Cached transform for interpolation - - /*---------------------------------------------------------------------------------*/ - /* Function Members */ - /*---------------------------------------------------------------------------------*/ }; } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp index 1fb8232e..38c7e19c 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp @@ -204,32 +204,18 @@ namespace SHADE // Check if entity is already a physics object auto* physicsObject = GetPhysicsObject(entityID); + if (!physicsObject) - { - physicsObject = &(map.emplace(entityID, SHPhysicsObject{}).first->second); - physicsObject->entityID = entityID; - } + physicsObject = CreatePhysicsObject(entityID); - // Get entity transform - auto const* SHADE_TF = SHComponentManager::GetComponent_s(entityID); + // Get components transform + auto* transformComponent = EnsureTransform(entityID); + auto* rigidBodyComponent = SHComponentManager::GetComponent(entityID); - // Possibly redundant - if (!SHADE_TF) - { - SHComponentManager::AddComponent(entityID); - SHADE_TF = SHComponentManager::GetComponent(entityID); - } - - const rp3d::Transform RP3D_TF { SHADE_TF->GetWorldPosition(), SHADE_TF->GetWorldRotation() }; - - // If collider already exists - if (physicsObject->hasColliders) - world->destroyCollisionBody(physicsObject->rp3dBody); - - physicsObject->rp3dBody = world->createRigidBody(RP3D_TF); - physicsObject->isRigidBody = true; + physicsObject->CreateRigidBody(transformComponent, rigidBodyComponent, nullptr); // Recreate colliders + const rp3d::Transform RP3D_TF { transformComponent->GetWorldPosition(), transformComponent->GetWorldRotation() }; if (physicsObject->hasColliders) { const auto& COLLIDERS = SHComponentManager::GetComponent(entityID)->GetColliders(); @@ -267,22 +253,11 @@ namespace SHADE // Check if entity is already a physics object auto* physicsObject = GetPhysicsObject(entityID); if (!physicsObject) - { - physicsObject = &(map.emplace(entityID, SHPhysicsObject{}).first->second); - physicsObject->entityID = entityID; - } + physicsObject = CreatePhysicsObject(entityID); // Get entity transform - auto const* SHADE_TF = SHComponentManager::GetComponent_s(entityID); - - // Possibly redundant - if (!SHADE_TF) - { - SHComponentManager::AddComponent(entityID); - SHADE_TF = SHComponentManager::GetComponent(entityID); - } - - const rp3d::Transform RP3D_TF { SHADE_TF->GetWorldPosition(), SHADE_TF->GetWorldRotation() }; + auto const* TF = EnsureTransform(entityID); + const rp3d::Transform RP3D_TF { TF->GetWorldPosition(), TF->GetWorldRotation() }; // No rb if (!physicsObject->isRigidBody) @@ -375,7 +350,7 @@ namespace SHADE switch (shape->GetType()) { - case SHShape::Type::BOUNDING_BOX: + case SHShape::Type::BOX: { auto* box = reinterpret_cast(shape); rp3d::BoxShape* newBox = factory.createBoxShape(box->GetHalfExtents()); @@ -457,6 +432,12 @@ namespace SHADE /* Private Function Member Definitions */ /*-----------------------------------------------------------------------------------*/ + SHPhysicsObject* SHPhysicsSystem::CreatePhysicsObject(EntityID entityID) noexcept + { + auto* newPhysicsObject = &map.emplace(entityID, SHPhysicsObject{entityID, &factory, world}).first->second; + return newPhysicsObject; + } + SHPhysicsObject* SHPhysicsSystem::GetPhysicsObject(EntityID entityID) noexcept { const auto it = map.find(entityID); @@ -469,6 +450,14 @@ namespace SHADE return &(it->second); } + void SHPhysicsSystem::SyncActiveStates(SHPhysicsObject* physicsObject, bool componentActive) noexcept + { + const bool RP3D_ACTIVE = physicsObject->rp3dBody->isActive(); + if (RP3D_ACTIVE != componentActive) + physicsObject->rp3dBody->setIsActive(componentActive); + } + + void SHPhysicsSystem::SyncRigidBodyComponents(std::vector& denseArray) noexcept { if (denseArray.empty()) @@ -479,14 +468,11 @@ namespace SHADE const EntityID ENTITY_ID = comp.GetEID(); // Get physicsObject - auto const* physicsObject = GetPhysicsObject(ENTITY_ID); + auto* physicsObject = GetPhysicsObject(ENTITY_ID); - const bool RP3D_ACTIVE = physicsObject->rp3dBody->isActive(); // TODO(Diren): Check if active in hierarchy const bool COMPONENT_ACTIVE = comp.isActive; - - if (RP3D_ACTIVE != COMPONENT_ACTIVE) - physicsObject->rp3dBody->setIsActive(COMPONENT_ACTIVE); + SyncActiveStates(physicsObject, COMPONENT_ACTIVE); if (!COMPONENT_ACTIVE) continue; @@ -509,14 +495,11 @@ namespace SHADE const EntityID ENTITY_ID = comp.GetEID(); // Get physicsObject - auto const* physicsObject = GetPhysicsObject(ENTITY_ID); + auto* physicsObject = GetPhysicsObject(ENTITY_ID); - const bool RP3D_ACTIVE = physicsObject->rp3dBody->isActive(); // TODO(Diren): Check if active in hierarchy const bool COMPONENT_ACTIVE = comp.isActive; - - if (RP3D_ACTIVE != COMPONENT_ACTIVE) - physicsObject->rp3dBody->setIsActive(COMPONENT_ACTIVE); + SyncActiveStates(physicsObject, COMPONENT_ACTIVE); if (!COMPONENT_ACTIVE) continue; @@ -706,4 +689,19 @@ namespace SHADE } } + SHTransformComponent* SHPhysicsSystem::EnsureTransform(EntityID entityID) + { + auto* tf = SHComponentManager::GetComponent_s(entityID); + + // Possibly redundant + if (!tf) + { + SHComponentManager::AddComponent(entityID); + tf = SHComponentManager::GetComponent(entityID); + } + + return tf; + } + + } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/SHPhysicsSystem.h b/SHADE_Engine/src/Physics/SHPhysicsSystem.h index 92a4730d..b5ab36bc 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsSystem.h +++ b/SHADE_Engine/src/Physics/SHPhysicsSystem.h @@ -19,6 +19,7 @@ #include "SHPhysicsObject.h" #include "Components/SHRigidBodyComponent.h" #include "Components/SHColliderComponent.h" +#include "Math/Transform/SHTransformComponent.h" #include "Scene/SHSceneGraph.h" #include "ECS_Base/System/SHSystemRoutine.h" @@ -185,11 +186,13 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Function Members */ /*---------------------------------------------------------------------------------*/ - SHPhysicsObject* GetPhysicsObject (EntityID entityID) noexcept; + SHPhysicsObject* CreatePhysicsObject (EntityID entityID) noexcept; + SHPhysicsObject* GetPhysicsObject (EntityID entityID) noexcept; - void SyncRigidBodyComponents (std::vector& denseArray) noexcept; - void SyncColliderComponents (std::vector& denseArray) noexcept; - void SyncTransforms () noexcept; + void SyncActiveStates (SHPhysicsObject* physicsObject, bool componentActive) noexcept; + void SyncRigidBodyComponents (std::vector& denseArray) noexcept; + void SyncColliderComponents (std::vector& denseArray) noexcept; + void SyncTransforms () noexcept; // TODO(Diren): Trigger handling static void SyncRigidBody (SHPhysicsObject const* physicsObject, const SHRigidBodyComponent* comp) noexcept; @@ -197,6 +200,9 @@ namespace SHADE static void SetRP3DAngularConstraints (rp3d::RigidBody const* rp3dRigidBody, uint8_t rbFlags) noexcept; static void SyncCollider (SHPhysicsObject const* physicsObject, SHColliderComponent* comp) noexcept; + + // TODO(Diren): Remove when responsibility shifted to editor + SHTransformComponent* EnsureTransform (EntityID entityID); }; From e68ef89c00f9411501ed92682becde42ac7fe193 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Mon, 10 Oct 2022 21:21:20 +0800 Subject: [PATCH 3/8] synced rigidbodies through physics object --- .../Physics/Components/SHRigidBodyComponent.h | 3 + SHADE_Engine/src/Physics/SHPhysicsObject.cpp | 109 ++++++++++++++++-- SHADE_Engine/src/Physics/SHPhysicsObject.h | 12 +- SHADE_Engine/src/Physics/SHPhysicsSystem.cpp | 6 +- 4 files changed, 107 insertions(+), 23 deletions(-) diff --git a/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.h b/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.h index 285df810..8b62b7ff 100644 --- a/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.h +++ b/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.h @@ -144,6 +144,9 @@ namespace SHADE /* Data Members */ /*---------------------------------------------------------------------------------*/ + static constexpr size_t NUM_FLAGS = 8; + static constexpr size_t NUM_DIRTY_FLAGS = 16; + Type type; // rX rY rZ pX pY pZ slp g diff --git a/SHADE_Engine/src/Physics/SHPhysicsObject.cpp b/SHADE_Engine/src/Physics/SHPhysicsObject.cpp index a007c5fe..bbe8c726 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsObject.cpp +++ b/SHADE_Engine/src/Physics/SHPhysicsObject.cpp @@ -143,37 +143,122 @@ namespace SHADE rb->orientation = tf->GetWorldRotation(); } - void SHPhysicsObject::DestroyRigidBody() noexcept - { - - } - void SHPhysicsObject::CreateCollisionBody(const SHTransformComponent* tf, SHColliderComponent* c) { } - void SHPhysicsObject::DestroyCollisionBody() noexcept - { - - } - int SHPhysicsObject::AddCollider(SHCollider* collider) { return 0; } + void SHPhysicsObject::DestroyRigidBody() noexcept + { + + } + + void SHPhysicsObject::DestroyCollisionBody() noexcept + { + + } + void SHPhysicsObject::RemoveCollider(int index) noexcept { } - void SHPhysicsObject::SyncRigidBody() noexcept + void SHPhysicsObject::SyncRigidBody(SHRigidBodyComponent* rb) const noexcept { + SHASSERT(rp3dBody != nullptr, "ReactPhysics body does not exist!") + + if (rb->dirtyFlags == 0) + return; + + auto* rigidBody = reinterpret_cast(rp3dBody); + + const uint16_t RB_FLAGS = rb->dirtyFlags; + for (size_t i = 0; i < SHRigidBodyComponent::NUM_DIRTY_FLAGS; ++i) + { + // Check if current dirty flag has been set to true + if (RB_FLAGS & 1U << i) + { + switch (i) + { + case 0: // Gravity + { + rigidBody->enableGravity(rb->IsGravityEnabled()); + break; + } + case 1: // Sleeping + { + rigidBody->setIsAllowedToSleep(rb->IsAllowedToSleep()); + break; + } + case 2: // Linear Constraints + { + const rp3d::Vector3 CONSTRAINTS + { + rb->flags & 1U << 2 ? 0.0f : 1.0f, + rb->flags & 1U << 3 ? 0.0f : 1.0f, + rb->flags & 1U << 4 ? 0.0f : 1.0f + }; + + rigidBody->setLinearLockAxisFactor(CONSTRAINTS); + break; + } + case 3: // Angular Constraints + { + const rp3d::Vector3 CONSTRAINTS + { + rb->flags & 1U << 5 ? 0.0f : 1.0f, + rb->flags & 1U << 6 ? 0.0f : 1.0f, + rb->flags & 1U << 7 ? 0.0f : 1.0f + }; + + rigidBody->setAngularLockAxisFactor(CONSTRAINTS); + break; + } + case 4: // Type + { + rigidBody->setType(static_cast(rb->GetType())); + break; + } + case 5: // Mass + { + rigidBody->setMass(rb->GetMass()); + break; + } + case 6: // Drag + { + rigidBody->setLinearDamping(rb->GetDrag()); + break; + } + case 7: // Angular Drag + { + rigidBody->setAngularDamping(rb->GetAngularDrag()); + break; + } + case 8: // Linear Velocity + { + rigidBody->setLinearVelocity(rb->GetLinearVelocity()); + break; + } + case 9: // Angular Velocity + { + rigidBody->setAngularVelocity(rb->GetAngularVelocity()); + break; + } + default: break; + } + } + } + + rb->dirtyFlags = 0; } - void SHPhysicsObject::SyncColliders() noexcept + void SHPhysicsObject::SyncColliders(SHColliderComponent* c) const noexcept { } diff --git a/SHADE_Engine/src/Physics/SHPhysicsObject.h b/SHADE_Engine/src/Physics/SHPhysicsObject.h index c6ecd250..8562a0a1 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsObject.h +++ b/SHADE_Engine/src/Physics/SHPhysicsObject.h @@ -70,15 +70,15 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ void CreateRigidBody (const SHTransformComponent* tf, SHRigidBodyComponent* rb, SHColliderComponent* c); - void DestroyRigidBody () noexcept; - void CreateCollisionBody (const SHTransformComponent* tf, SHColliderComponent* c); int AddCollider (SHCollider* collider); - void RemoveCollider (int index) noexcept; - void DestroyCollisionBody () noexcept; - void SyncRigidBody () noexcept; - void SyncColliders () noexcept; + void DestroyRigidBody () noexcept; + void RemoveCollider (int index) noexcept; + void DestroyCollisionBody () noexcept; + + void SyncRigidBody (SHRigidBodyComponent* rb) const noexcept; + void SyncColliders (SHColliderComponent* c) const noexcept; private: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp index 38c7e19c..b922cdd7 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp @@ -477,11 +477,7 @@ namespace SHADE if (!COMPONENT_ACTIVE) continue; - if (comp.dirtyFlags > 0) - { - SyncRigidBody(physicsObject, &comp); - comp.dirtyFlags = 0; - } + physicsObject->SyncRigidBody(&comp); } } From ff9b504bc5224089726983f7098b19bfd8b226fb Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Tue, 11 Oct 2022 01:26:12 +0800 Subject: [PATCH 4/8] Moved collider interface into physics object. Added support for removing rigidbodies --- SHADE_Application/src/Scenes/SBTestScene.cpp | 3 +- .../Components/SHColliderComponent.cpp | 16 +- .../Physics/Components/SHColliderComponent.h | 2 +- SHADE_Engine/src/Physics/SHCollider.cpp | 106 +++---- SHADE_Engine/src/Physics/SHCollider.h | 22 +- SHADE_Engine/src/Physics/SHPhysicsObject.cpp | 108 ++++++- SHADE_Engine/src/Physics/SHPhysicsObject.h | 4 +- SHADE_Engine/src/Physics/SHPhysicsSystem.cpp | 287 ++++-------------- SHADE_Engine/src/Physics/SHPhysicsSystem.h | 13 +- 9 files changed, 227 insertions(+), 334 deletions(-) diff --git a/SHADE_Application/src/Scenes/SBTestScene.cpp b/SHADE_Application/src/Scenes/SBTestScene.cpp index 3b277e6c..36c69386 100644 --- a/SHADE_Application/src/Scenes/SBTestScene.cpp +++ b/SHADE_Application/src/Scenes/SBTestScene.cpp @@ -104,8 +104,7 @@ namespace Sandbox transform.SetWorldRotation(SHMath::GenerateRandomNumber(), SHMath::GenerateRandomNumber(), SHMath::GenerateRandomNumber()); transform.SetWorldScale(TEST_OBJ_SCALE); - auto* box = collider.AddBoundingBox(); - box->SetHalfExtents(transform.GetWorldScale() * 0.5f); + collider.AddBoundingBox(SHVec3::One * 0.5f, SHVec3::Zero); stressTestObjects.emplace_back(entity); } diff --git a/SHADE_Engine/src/Physics/Components/SHColliderComponent.cpp b/SHADE_Engine/src/Physics/Components/SHColliderComponent.cpp index 73e7ccbd..ada90eaa 100644 --- a/SHADE_Engine/src/Physics/Components/SHColliderComponent.cpp +++ b/SHADE_Engine/src/Physics/Components/SHColliderComponent.cpp @@ -24,7 +24,8 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ SHColliderComponent::SHColliderComponent() noexcept - : system { nullptr } + : system { nullptr } + , colliders {} {} /*-----------------------------------------------------------------------------------*/ @@ -87,12 +88,17 @@ namespace SHADE system->RemoveCollider(GetEID()); } - SHBoundingBox* SHColliderComponent::AddBoundingBox() noexcept + SHBoundingBox* SHColliderComponent::AddBoundingBox(const SHVec3& halfExtents, const SHVec3& posOffset) noexcept { const auto TYPE = SHCollider::Type::BOX; - const auto BOX_PAIR = std::make_pair(SHCollider{TYPE}, true); - auto& collider = colliders.emplace_back(BOX_PAIR).first; + auto boxPair = std::make_pair(SHCollider{TYPE}, true); + auto& collider = colliders.emplace_back(boxPair).first; + + const auto* tf = SHComponentManager::GetComponent(GetEID()); + + collider.SetPositionOffset(posOffset); + collider.SetAsBoundingBox(tf->GetWorldScale() * halfExtents); if (!system) { @@ -101,7 +107,7 @@ namespace SHADE } // Notify Physics System - system->AddCollisionShape(GetEID(), collider.GetShape()); + system->AddCollisionShape(GetEID(), &collider); return reinterpret_cast(collider.GetShape()); } diff --git a/SHADE_Engine/src/Physics/Components/SHColliderComponent.h b/SHADE_Engine/src/Physics/Components/SHColliderComponent.h index fd4bcb9a..db4cd612 100644 --- a/SHADE_Engine/src/Physics/Components/SHColliderComponent.h +++ b/SHADE_Engine/src/Physics/Components/SHColliderComponent.h @@ -82,7 +82,7 @@ namespace SHADE void OnCreate () override; void OnDestroy () override; - SHBoundingBox* AddBoundingBox () noexcept; + SHBoundingBox* AddBoundingBox (const SHVec3& halfExtents = SHVec3::One, const SHVec3& posOffset = SHVec3::Zero) noexcept; void RemoveCollider (int index); diff --git a/SHADE_Engine/src/Physics/SHCollider.cpp b/SHADE_Engine/src/Physics/SHCollider.cpp index 724db19c..5a3a6ac2 100644 --- a/SHADE_Engine/src/Physics/SHCollider.cpp +++ b/SHADE_Engine/src/Physics/SHCollider.cpp @@ -29,25 +29,23 @@ namespace SHADE } SHCollider::SHCollider(const SHCollider& rhs) noexcept - : type { rhs.type} - , isTrigger { rhs.isTrigger } - , dirty { true } - , shape { nullptr } + : type { rhs.type} + , isTrigger { rhs.isTrigger } + , dirty { true } + , shape { nullptr } + , positionOffset { rhs.positionOffset } { CreateShape(); - - // TODO(Diren): Copy transform data over } SHCollider::SHCollider(SHCollider&& rhs) noexcept - : type { rhs.type} - , isTrigger { rhs.isTrigger } - , dirty { true } - , shape { nullptr } + : type { rhs.type} + , isTrigger { rhs.isTrigger } + , dirty { true } + , shape { nullptr } + , positionOffset { rhs.positionOffset } { CreateShape(); - - // TODO(Diren): Copy transform data over } SHCollider::~SHCollider() noexcept @@ -61,27 +59,26 @@ namespace SHADE SHCollider& SHCollider::operator=(const SHCollider& rhs) noexcept { - type = rhs.type; - isTrigger = rhs.isTrigger; - dirty = true; + if (this == &rhs) + return *this; + + type = rhs.type; + isTrigger = rhs.isTrigger; + dirty = true; + positionOffset = rhs.positionOffset; CreateShape(); - - // TODO(Diren): Copy transform data over - return *this; } SHCollider& SHCollider::operator=(SHCollider&& rhs) noexcept { - type = rhs.type; - isTrigger = rhs.isTrigger; - dirty = true; + type = rhs.type; + isTrigger = rhs.isTrigger; + dirty = true; + positionOffset = rhs.positionOffset; CreateShape(); - - // TODO(Diren): Copy transform data over - return *this; } @@ -104,11 +101,6 @@ namespace SHADE return type; } - SHShape* SHCollider::GetShape() const noexcept - { - return shape; - } - float SHCollider::GetFriction() const noexcept { // TODO(Diren): Fix after implementing materials @@ -126,38 +118,41 @@ namespace SHADE return 0.0f; } - SHVec3 SHCollider::GetPosition() const noexcept - { - // TODO(Diren): Fix after linking transform data - return SHVec3::Zero; - } - const SHVec3& SHCollider::GetPositionOffset() const noexcept { return positionOffset; } - SHQuaternion SHCollider::GetOrientation() const noexcept + SHShape* SHCollider::GetShape() noexcept { - // TODO(Diren): Fix after linking transform data - return SHQuaternion::Identity; + dirty = true; + return shape; } /*-----------------------------------------------------------------------------------*/ /* Setter Function Definitions */ /*-----------------------------------------------------------------------------------*/ - void SHCollider::SetType(Type newType) noexcept + void SHCollider::SetAsBoundingBox(const SHVec3& halfExtents) { - if (type == newType) - return; - dirty = true; + type = Type::BOX; - type = newType; - CreateShape(); + delete shape; + shape = new SHBoundingBox{ positionOffset, halfExtents }; } + void SHCollider::SetAsBoundingSphere(float radius) + { + dirty = true; + type = Type::SPHERE; + + // TODO(Diren): Add Sphere + delete shape; + } + + + void SHCollider::SetIsTrigger(bool trigger) noexcept { dirty = true; @@ -191,25 +186,22 @@ namespace SHADE void SHCollider::CreateShape() { - // Remove current shape delete shape; switch (type) { - case Type::BOX: CreateBoundingBox(); break; - case Type::SPHERE: CreateSphere(); break; + case Type::BOX: + { + SetAsBoundingBox(SHVec3::One); + break; + } + case Type::SPHERE: + { + SetAsBoundingSphere(1.0f); + break; + } default: break; } } - void SHCollider::CreateBoundingBox() - { - shape = new SHBoundingBox{ SHVec3::Zero, SHVec3::One }; - } - - void SHCollider::CreateSphere() - { - - } - } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/SHCollider.h b/SHADE_Engine/src/Physics/SHCollider.h index fce4a5b2..b53ce58a 100644 --- a/SHADE_Engine/src/Physics/SHCollider.h +++ b/SHADE_Engine/src/Physics/SHCollider.h @@ -32,8 +32,6 @@ namespace SHADE BOX , SPHERE , CAPSULE - , CONVEX_HULL - , CONVEX_MESH }; /*---------------------------------------------------------------------------------*/ @@ -62,28 +60,28 @@ namespace SHADE [[nodiscard]] bool IsTrigger () const noexcept; [[nodiscard]] Type GetType () const noexcept; - [[nodiscard]] SHShape* GetShape () const noexcept; [[nodiscard]] float GetFriction () const noexcept; [[nodiscard]] float GetBounciness () const noexcept; [[nodiscard]] float GetDensity () const noexcept; - [[nodiscard]] SHVec3 GetPosition () const noexcept; [[nodiscard]] const SHVec3& GetPositionOffset () const noexcept; - [[nodiscard]] SHQuaternion GetOrientation () const noexcept; + + [[nodiscard]] SHShape* GetShape () noexcept; /*---------------------------------------------------------------------------------*/ /* Setter Functions */ /*---------------------------------------------------------------------------------*/ - void SetType (Type newType) noexcept; + void SetAsBoundingBox (const SHVec3& halfExtents); + void SetAsBoundingSphere (float radius); - void SetIsTrigger (bool isTrigger) noexcept; - void SetFriction (float friction) noexcept; - void SetBounciness (float bounciness) noexcept; - void SetDensity (float density) noexcept; + void SetIsTrigger (bool isTrigger) noexcept; + void SetFriction (float friction) noexcept; + void SetBounciness (float bounciness) noexcept; + void SetDensity (float density) noexcept; - void SetPositionOffset (const SHVec3& positionOffset) noexcept; + void SetPositionOffset (const SHVec3& positionOffset) noexcept; private: /*---------------------------------------------------------------------------------*/ @@ -101,8 +99,6 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ void CreateShape (); - void CreateBoundingBox (); - void CreateSphere (); }; } // namespace SHADE diff --git a/SHADE_Engine/src/Physics/SHPhysicsObject.cpp b/SHADE_Engine/src/Physics/SHPhysicsObject.cpp index bbe8c726..eaecc9c0 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsObject.cpp +++ b/SHADE_Engine/src/Physics/SHPhysicsObject.cpp @@ -132,7 +132,7 @@ namespace SHADE void SHPhysicsObject::CreateRigidBody(const SHTransformComponent* tf, SHRigidBodyComponent* rb, SHColliderComponent* c) { - // If collider already exists + // If collider already exists, recreate the collision body as a rigid body if (hasColliders) world->destroyCollisionBody(rp3dBody); @@ -141,31 +141,91 @@ namespace SHADE rb->position = tf->GetWorldPosition(); rb->orientation = tf->GetWorldRotation(); + + if (hasColliders) + { + c->position = tf->GetWorldPosition(); + c->orientation = tf->GetWorldRotation(); + // Get array of colliders and add them back into the rigidbody + for (auto& collider : c->colliders | std::views::keys) + AddCollider(&collider); + } } void SHPhysicsObject::CreateCollisionBody(const SHTransformComponent* tf, SHColliderComponent* c) { - + if (rp3dBody == nullptr) + rp3dBody = world->createCollisionBody(rp3d::Transform{ tf->GetWorldPosition(), tf->GetWorldRotation() }); + + hasColliders = true; + + c->position = tf->GetWorldPosition(); + c->orientation = tf->GetWorldRotation(); + + for (auto& collider : c->colliders | std::views::keys) + AddCollider(&collider); } int SHPhysicsObject::AddCollider(SHCollider* collider) { - return 0; + switch (collider->GetType()) + { + case SHCollider::Type::BOX: + { + const auto* box = reinterpret_cast(collider->GetShape()); + rp3d::BoxShape* newBox = factory->createBoxShape(box->GetHalfExtents()); + + // TODO(Diren): Handle offsets + rp3dBody->addCollider(newBox, rp3d::Transform{ collider->GetPositionOffset(), SHQuaternion::Identity }); + break; + } + case SHCollider::Type::SPHERE: + { + break; + } + // TODO(Diren): Add more collider shapes + default: break; + } + + return static_cast(rp3dBody->getNbColliders()) - 1; } - void SHPhysicsObject::DestroyRigidBody() noexcept + void SHPhysicsObject::DestroyRigidBody(SHColliderComponent* c) noexcept { - + world->destroyRigidBody(reinterpret_cast(rp3dBody)); + + if (hasColliders) + { + // Preserve colliders as a collision body + rp3dBody = world->createCollisionBody(rp3d::Transform{ c->position, c->orientation }); + for (auto& collider : c->colliders | std::views::keys) + AddCollider(&collider); + } + + isRigidBody = false; } void SHPhysicsObject::DestroyCollisionBody() noexcept { - + // Remove all colliders + for (uint32_t i = 0; i < rp3dBody->getNbColliders(); ++i) + { + auto* collider = rp3dBody->getCollider(i); + rp3dBody->removeCollider(collider); + } } - void SHPhysicsObject::RemoveCollider(int index) noexcept + void SHPhysicsObject::RemoveCollider(int index) { - + const int NUM_COLLIDERS = static_cast(rp3dBody->getNbColliders()); + if (NUM_COLLIDERS == 0) + return; + + if (index < 0 || index >= NUM_COLLIDERS) + throw std::invalid_argument("Index out of range!"); + + auto* collider = rp3dBody->getCollider(index); + rp3dBody->removeCollider(collider); } void SHPhysicsObject::SyncRigidBody(SHRigidBodyComponent* rb) const noexcept @@ -260,7 +320,37 @@ namespace SHADE void SHPhysicsObject::SyncColliders(SHColliderComponent* c) const noexcept { - + int index = 0; + for (auto& [collider, dirty] : c->colliders) + { + if (!dirty) + continue; + + // Update offsets + auto* rp3dCollider = rp3dBody->getCollider(index); + rp3dCollider->setLocalToBodyTransform(rp3d::Transform(collider.GetPositionOffset(), SHQuaternion::Identity)); + + switch (collider.GetType()) + { + case SHCollider::Type::BOX: + { + const auto* box = reinterpret_cast(collider.GetShape()); + + auto* rp3dBoxShape = reinterpret_cast(rp3dCollider->getCollisionShape()); + rp3dBoxShape->setHalfExtents(box->GetHalfExtents()); + + break; + } + case SHCollider::Type::SPHERE: + { + break; + } + default: break; + } + + dirty = false; + ++index; + } } } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Physics/SHPhysicsObject.h b/SHADE_Engine/src/Physics/SHPhysicsObject.h index 8562a0a1..39a85421 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsObject.h +++ b/SHADE_Engine/src/Physics/SHPhysicsObject.h @@ -73,8 +73,8 @@ namespace SHADE void CreateCollisionBody (const SHTransformComponent* tf, SHColliderComponent* c); int AddCollider (SHCollider* collider); - void DestroyRigidBody () noexcept; - void RemoveCollider (int index) noexcept; + void DestroyRigidBody (SHColliderComponent* c) noexcept; + void RemoveCollider (int index); void DestroyCollisionBody () noexcept; void SyncRigidBody (SHRigidBodyComponent* rb) const noexcept; diff --git a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp index b922cdd7..c5a7aa57 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp @@ -202,46 +202,14 @@ namespace SHADE SHLOG_INFO("Adding a Rigidbody to the Physics World.") #endif - // Check if entity is already a physics object - auto* physicsObject = GetPhysicsObject(entityID); + auto* physicsObject = CreatePhysicsObject(entityID); - if (!physicsObject) - physicsObject = CreatePhysicsObject(entityID); - - // Get components transform - auto* transformComponent = EnsureTransform(entityID); - auto* rigidBodyComponent = SHComponentManager::GetComponent(entityID); - - physicsObject->CreateRigidBody(transformComponent, rigidBodyComponent, nullptr); - - // Recreate colliders - const rp3d::Transform RP3D_TF { transformComponent->GetWorldPosition(), transformComponent->GetWorldRotation() }; - if (physicsObject->hasColliders) - { - const auto& COLLIDERS = SHComponentManager::GetComponent(entityID)->GetColliders(); - for (const auto& collider : COLLIDERS | std::views::keys) - { - switch (collider.GetType()) - { - case SHCollider::Type::BOX: - { - SHBoundingBox* box = reinterpret_cast(collider.GetShape()); - rp3d::BoxShape* newBox = factory.createBoxShape(box->GetHalfExtents()); - - // TODO(Diren): Handle offsets - physicsObject->rp3dBody->addCollider(newBox, RP3D_TF); - break; - } - case SHCollider::Type::SPHERE: - { - break; - } - // TODO(Diren): Add more collider shapes - default: break; - } - } - } - + physicsObject->CreateRigidBody + ( + EnsureTransform(entityID), + SHComponentManager::GetComponent(entityID), + SHComponentManager::GetComponent_s(entityID) + ); } void SHPhysicsSystem::AddCollider(EntityID entityID) noexcept @@ -250,43 +218,13 @@ namespace SHADE SHLOG_INFO("Adding a Collider to the Physics World.") #endif - // Check if entity is already a physics object - auto* physicsObject = GetPhysicsObject(entityID); - if (!physicsObject) - physicsObject = CreatePhysicsObject(entityID); + auto* physicsObject = CreatePhysicsObject(entityID); - // Get entity transform - auto const* TF = EnsureTransform(entityID); - const rp3d::Transform RP3D_TF { TF->GetWorldPosition(), TF->GetWorldRotation() }; - - // No rb - if (!physicsObject->isRigidBody) - physicsObject->rp3dBody = world->createCollisionBody(RP3D_TF); - - const auto& COLLIDERS = SHComponentManager::GetComponent(entityID)->GetColliders(); - for (const auto& collider : COLLIDERS | std::views::keys) - { - switch (collider.GetType()) - { - case SHCollider::Type::BOX: - { - SHBoundingBox* box = reinterpret_cast(collider.GetShape()); - rp3d::BoxShape* newBox = factory.createBoxShape(box->GetHalfExtents()); - - // TODO(Diren): Handle offsets - physicsObject->rp3dBody->addCollider(newBox, RP3D_TF); - break; - } - case SHCollider::Type::SPHERE: - { - break; - } - // TODO(Diren): Add more collider shapes - default: break; - } - } - - physicsObject->hasColliders = true; + physicsObject->CreateCollisionBody + ( + EnsureTransform(entityID), + SHComponentManager::GetComponent(entityID) + ); } void SHPhysicsSystem::RemoveRigidBody(EntityID entityID) noexcept @@ -295,6 +233,13 @@ namespace SHADE SHLOG_INFO("Removing a Rigidbody from the Physics World.") #endif + auto* physicsObject = GetPhysicsObject(entityID); + SHASSERT(physicsObject != nullptr, "Physics object has been lost from the world!") + + physicsObject->DestroyRigidBody(SHComponentManager::GetComponent_s(entityID)); + + if (physicsObject->rp3dBody == nullptr) + DestroyPhysicsObject(entityID); } void SHPhysicsSystem::RemoveCollider(EntityID entityID) noexcept @@ -344,30 +289,10 @@ namespace SHADE } - void SHPhysicsSystem::AddCollisionShape(EntityID entityID, SHShape* shape) + void SHPhysicsSystem::AddCollisionShape(EntityID entityID, SHCollider* collider) { auto* physicsObject = GetPhysicsObject(entityID); - - switch (shape->GetType()) - { - case SHShape::Type::BOX: - { - auto* box = reinterpret_cast(shape); - rp3d::BoxShape* newBox = factory.createBoxShape(box->GetHalfExtents()); - - // TODO(Diren): Handle offsets - - rp3d::Transform tf = rp3d::Transform::identity(); - physicsObject->rp3dBody->addCollider(newBox, tf); - break; - } - case SHShape::Type::SPHERE: - { - break; - } - // TODO(Diren): Add more collider shapes - default: break; - } + physicsObject->AddCollider(collider); } void SHPhysicsSystem::RemoveCollisionShape(EntityID entityID, int index) @@ -424,6 +349,7 @@ namespace SHADE if (system->worldUpdated) { system->SyncTransforms(); + // TODO(Diren): Handle trigger messages for scripting } } @@ -434,8 +360,14 @@ namespace SHADE SHPhysicsObject* SHPhysicsSystem::CreatePhysicsObject(EntityID entityID) noexcept { - auto* newPhysicsObject = &map.emplace(entityID, SHPhysicsObject{entityID, &factory, world}).first->second; - return newPhysicsObject; + const auto it = map.find(entityID); + if (it == map.end()) + { + auto* newPhysicsObject = &map.emplace(entityID, SHPhysicsObject{entityID, &factory, world}).first->second; + return newPhysicsObject; + } + + return &(it->second); } SHPhysicsObject* SHPhysicsSystem::GetPhysicsObject(EntityID entityID) noexcept @@ -450,6 +382,11 @@ namespace SHADE return &(it->second); } + void SHPhysicsSystem::DestroyPhysicsObject(EntityID entityID) noexcept + { + map.erase(entityID); + } + void SHPhysicsSystem::SyncActiveStates(SHPhysicsObject* physicsObject, bool componentActive) noexcept { const bool RP3D_ACTIVE = physicsObject->rp3dBody->isActive(); @@ -500,17 +437,14 @@ namespace SHADE if (!COMPONENT_ACTIVE) continue; - SyncCollider(physicsObject, &comp); + physicsObject->SyncColliders(&comp); } } void SHPhysicsSystem::SyncTransforms() noexcept { - for (auto& pair : map) + for (auto& [entityID, physicsObject] : map) { - const EntityID ENTITY_ID = pair.first; - SHPhysicsObject& physicsObject = pair.second; - rp3d::Vector3 rp3dPos; rp3d::Quaternion rp3dRot; @@ -520,7 +454,7 @@ namespace SHADE if (physicsObject.isRigidBody) { - auto const* rbComponent = SHComponentManager::GetComponent(ENTITY_ID); + auto* rbComponent = SHComponentManager::GetComponent(entityID); if (rbComponent->GetType() == SHRigidBodyComponent::Type::STATIC) continue; @@ -538,6 +472,16 @@ namespace SHADE rp3dPos = CURRENT_TF.getPosition(); rp3dRot = CURRENT_TF.getOrientation(); } + + rbComponent->position = CURRENT_TF.getPosition(); + rbComponent->orientation = CURRENT_TF.getOrientation(); + + if (physicsObject.hasColliders) + { + auto* colliderComponent = SHComponentManager::GetComponent(entityID); + colliderComponent->position = CURRENT_TF.getPosition(); + colliderComponent->orientation = CURRENT_TF.getOrientation(); + } } else { @@ -546,145 +490,16 @@ namespace SHADE } // Convert RP3D Transform to SHADE - auto* tfComponent = SHComponentManager::GetComponent(ENTITY_ID); + auto* tfComponent = SHComponentManager::GetComponent(entityID); tfComponent->SetWorldPosition(rp3dPos); tfComponent->SetWorldRotation(SHQuaternion{ rp3dRot }.ToEuler()); + // Cache transforms physicsObject.prevTransform = CURRENT_TF; } } - void SHPhysicsSystem::SyncRigidBody(SHPhysicsObject const* physicsObject, const SHRigidBodyComponent* comp) noexcept - { - auto* rigidBody = reinterpret_cast(physicsObject->rp3dBody); - - const uint16_t RB_FLAGS = comp->dirtyFlags; - const size_t NUM_FLAGS = sizeof(RB_FLAGS) * 8U; - for (size_t i = 0; i < NUM_FLAGS; ++i) - { - // Check if current dirty flag has been set to true - if (RB_FLAGS & 1U << i) - { - switch (i) - { - case 0: // Gravity - { - rigidBody->enableGravity(comp->IsGravityEnabled()); - break; - } - case 1: // Sleeping - { - rigidBody->setIsAllowedToSleep(comp->IsAllowedToSleep()); - break; - } - case 2: // Linear Constraints - { - SetRP3DLinearConstraints(rigidBody, comp->flags); - break; - } - case 3: // Angular Constraints - { - SetRP3DAngularConstraints(rigidBody, comp->flags); - break; - } - case 4: // Type - { - rigidBody->setType(static_cast(comp->GetType())); - break; - } - case 5: // Mass - { - rigidBody->setMass(comp->GetMass()); - break; - } - case 6: // Drag - { - rigidBody->setLinearDamping(comp->GetDrag()); - break; - } - case 7: // Angular Drag - { - rigidBody->setAngularDamping(comp->GetAngularDrag()); - break; - } - case 8: // Linear Velocity - { - rigidBody->setLinearVelocity(comp->GetLinearVelocity()); - break; - } - case 9: // Angular Velocity - { - rigidBody->setAngularVelocity(comp->GetAngularVelocity()); - break; - } - default: break; - } - } - } - } - - void SHPhysicsSystem::SetRP3DLinearConstraints(rp3d::RigidBody const* rp3dRigidBody, uint8_t rbFlags) noexcept - { - const rp3d::Vector3 CONSTRAINTS - { - rbFlags & 1U << 2 ? 0.0f : 1.0f, - rbFlags & 1U << 3 ? 0.0f : 1.0f, - rbFlags & 1U << 4 ? 0.0f : 1.0f - }; - - - rp3dRigidBody->setLinearLockAxisFactor(CONSTRAINTS); - } - - void SHPhysicsSystem::SetRP3DAngularConstraints(rp3d::RigidBody const* rp3dRigidBody, uint8_t rbFlags) noexcept - { - const rp3d::Vector3 CONSTRAINTS - { - rbFlags & 1U << 5 ? 0.0f : 1.0f, - rbFlags & 1U << 6 ? 0.0f : 1.0f, - rbFlags & 1U << 7 ? 0.0f : 1.0f - }; - - rp3dRigidBody->setAngularLockAxisFactor(CONSTRAINTS); - } - - void SHPhysicsSystem::SyncCollider(SHPhysicsObject const* physicsObject, SHColliderComponent* comp) noexcept - { - int index = 0; - for (auto& [collider, dirty] : comp->colliders) - { - if (!dirty) - continue; - - switch (collider.GetType()) - { - case SHCollider::Type::BOX: - { - SHBoundingBox* box = reinterpret_cast(collider.GetShape()); - - auto* rp3dBoxShape = reinterpret_cast(physicsObject->rp3dBody->getCollider(index)->getCollisionShape()); - rp3dBoxShape->setHalfExtents(box->GetHalfExtents()); - - if (rp3dBoxShape) - { - SHLOG_INFO("Updating box things") - } - - break; - } - case SHCollider::Type::SPHERE: - { - break; - } - default: break; - } - - dirty = false; - ++index; - } - } - SHTransformComponent* SHPhysicsSystem::EnsureTransform(EntityID entityID) { auto* tf = SHComponentManager::GetComponent_s(entityID); diff --git a/SHADE_Engine/src/Physics/SHPhysicsSystem.h b/SHADE_Engine/src/Physics/SHPhysicsSystem.h index b5ab36bc..bc6a1ba2 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsSystem.h +++ b/SHADE_Engine/src/Physics/SHPhysicsSystem.h @@ -82,8 +82,8 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - void Init () override; - void Exit () override; + void Init () override; + void Exit () override; void AddRigidBody (EntityID entityID) noexcept; void AddCollider (EntityID entityID) noexcept; @@ -101,7 +101,7 @@ namespace SHADE void AddTorque (EntityID entityID, const SHVec3& torque) const noexcept; void AddRelativeTorque (EntityID entityID, const SHVec3& relativeTorque) const noexcept; - void AddCollisionShape (EntityID entityID, SHShape* shape); + void AddCollisionShape (EntityID entityID, SHCollider* collider); void RemoveCollisionShape (EntityID entityID, int index); /*---------------------------------------------------------------------------------*/ @@ -188,6 +188,7 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ SHPhysicsObject* CreatePhysicsObject (EntityID entityID) noexcept; SHPhysicsObject* GetPhysicsObject (EntityID entityID) noexcept; + void DestroyPhysicsObject (EntityID entityID) noexcept; void SyncActiveStates (SHPhysicsObject* physicsObject, bool componentActive) noexcept; void SyncRigidBodyComponents (std::vector& denseArray) noexcept; @@ -195,12 +196,6 @@ namespace SHADE void SyncTransforms () noexcept; // TODO(Diren): Trigger handling - static void SyncRigidBody (SHPhysicsObject const* physicsObject, const SHRigidBodyComponent* comp) noexcept; - static void SetRP3DLinearConstraints (rp3d::RigidBody const* rp3dRigidBody, uint8_t rbFlags) noexcept; - static void SetRP3DAngularConstraints (rp3d::RigidBody const* rp3dRigidBody, uint8_t rbFlags) noexcept; - - static void SyncCollider (SHPhysicsObject const* physicsObject, SHColliderComponent* comp) noexcept; - // TODO(Diren): Remove when responsibility shifted to editor SHTransformComponent* EnsureTransform (EntityID entityID); }; From 2fa71f0fd973db9494ce1ef7a91f29c1d675aedc Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Wed, 12 Oct 2022 19:12:34 +0800 Subject: [PATCH 5/8] Added Ray --- .../src/Math/Geometry/SHBoundingBox.cpp | 5 ++ .../src/Math/Geometry/SHBoundingBox.h | 3 +- SHADE_Engine/src/Math/Geometry/SHShape.h | 7 ++- SHADE_Engine/src/Math/SHMatrix.cpp | 33 ++++++++++ SHADE_Engine/src/Math/SHMatrix.h | 20 ++++++- SHADE_Engine/src/Math/SHRay.cpp | 60 +++++++++++++++++++ SHADE_Engine/src/Math/SHRay.h | 54 +++++++++++++++++ 7 files changed, 177 insertions(+), 5 deletions(-) create mode 100644 SHADE_Engine/src/Math/SHRay.cpp create mode 100644 SHADE_Engine/src/Math/SHRay.h diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp index 3abcc315..72b46819 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp @@ -201,6 +201,11 @@ namespace SHADE return true; } + bool SHBoundingBox::Raycast(const SHVec3& SHRay, float& distance) noexcept + { + return false; + } + bool SHBoundingBox::Contains(const SHBoundingBox& rhs) const noexcept { const SHVec3 V = SHVec3::Abs(rhs.center - center); diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h index a89c5965..2ad5f1ed 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h @@ -64,7 +64,8 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] bool TestPoint (const SHVec3& point) noexcept override; + [[nodiscard]] bool TestPoint (const SHVec3& point) noexcept override; + [[nodiscard]] bool Raycast (const SHVec3& SHRay, float& distance) noexcept override; [[nodiscard]] bool Contains (const SHBoundingBox& rhs) const noexcept; [[nodiscard]] float Volume () const noexcept; diff --git a/SHADE_Engine/src/Math/Geometry/SHShape.h b/SHADE_Engine/src/Math/Geometry/SHShape.h index e33ca583..204d4e0c 100644 --- a/SHADE_Engine/src/Math/Geometry/SHShape.h +++ b/SHADE_Engine/src/Math/Geometry/SHShape.h @@ -11,8 +11,10 @@ #pragma once // Project Headers -#include "Math/Transform/SHTransform.h" #include "SH_API.h" +#include "Math/Transform/SHTransform.h" +#include "Math/SHRay.h" + namespace SHADE { @@ -69,7 +71,8 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] virtual bool TestPoint (const SHVec3& point) noexcept = 0; + [[nodiscard]] virtual bool TestPoint (const SHVec3& point) noexcept = 0; + [[nodiscard]] virtual bool Raycast (const SHVec3& SHRay, float& distance) noexcept = 0; protected: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/SHMatrix.cpp b/SHADE_Engine/src/Math/SHMatrix.cpp index 94c17914..571fa4e0 100644 --- a/SHADE_Engine/src/Math/SHMatrix.cpp +++ b/SHADE_Engine/src/Math/SHMatrix.cpp @@ -334,6 +334,39 @@ namespace SHADE return ss.str(); } + bool SHMatrix::Decompose(SHVec3& translation, SHVec3& rotation, SHVec3& scale) const noexcept + { + XMVECTOR s, r, t; + const XMMATRIX M = XMLoadFloat4x4(this); + + if (!XMMatrixDecompose(&s, &r, &t, M)) + return false; + + SHQuaternion orientation; + + XMStoreFloat3(&scale, s); + XMStoreFloat4(&orientation, r); + XMStoreFloat3(&translation, t); + + rotation = orientation.ToEuler(); + + return true; + } + + bool SHMatrix::Decompose(SHVec3& translation, SHQuaternion& orientation, SHVec3& scale) const noexcept + { + XMVECTOR s, r, t; + const XMMATRIX M = XMLoadFloat4x4(this); + + if (!XMMatrixDecompose(&s, &r, &t, M)) + return false; + + XMStoreFloat3(&scale, s); + XMStoreFloat4(&orientation, r); + XMStoreFloat3(&translation, t); + + return true; + } /*-----------------------------------------------------------------------------------*/ /* Static Function Member Definitions */ diff --git a/SHADE_Engine/src/Math/SHMatrix.h b/SHADE_Engine/src/Math/SHMatrix.h index 3666fbe6..7a662478 100644 --- a/SHADE_Engine/src/Math/SHMatrix.h +++ b/SHADE_Engine/src/Math/SHMatrix.h @@ -16,7 +16,6 @@ // Project Headers #include "SH_API.h" #include "Vector/SHVec4.h" -#include "SH_API.h" namespace SHADE { @@ -25,7 +24,6 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ class SHVec2; class SHVec3; - class SHVec4; class SHQuaternion; /*-----------------------------------------------------------------------------------*/ @@ -109,6 +107,24 @@ namespace SHADE [[nodiscard]] float Determinant () const noexcept; [[nodiscard]] std::string ToString () const noexcept; + /** + * @brief Decomposes a transformation matrix into translation, euler angles and scale. + * @param[out] scale The scaling factor of the matrix. + * @param[out] rotation The euler angles of the matrix. + * @param[out] translation The translation of the matrix. + * @return True if decomposition was successful. + */ + bool Decompose (SHVec3& translation, SHVec3& rotation, SHVec3& scale) const noexcept; + + /** + * @brief Decomposes a transformation matrix into translation, orientation and scale. + * @param[out] scale The scaling factor of the matrix. + * @param[out] orientation The orientation of the matrix. + * @param[out] translation The translation of the matrix. + * @return True if decomposition was successful. + */ + bool Decompose (SHVec3& translation, SHQuaternion& orientation, SHVec3& scale) const noexcept; + /*---------------------------------------------------------------------------------*/ /* Static Function Members */ /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/SHRay.cpp b/SHADE_Engine/src/Math/SHRay.cpp new file mode 100644 index 00000000..87f12b81 --- /dev/null +++ b/SHADE_Engine/src/Math/SHRay.cpp @@ -0,0 +1,60 @@ +/**************************************************************************************** + * \file SHRay.cpp + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for a Ray. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#include + +// Primary Header +#include "SHRay.h" + +using namespace DirectX; + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Constructors & Destructor Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHRay::SHRay() noexcept + : direction { 0.0f, 0.0f, 1.0f } + {} + + SHRay::SHRay(const SHVec3& pos, const SHVec3& dir) noexcept + : position { pos } + , direction { dir } + {} + + /*-----------------------------------------------------------------------------------*/ + /* Operator Overload Definitions */ + /*-----------------------------------------------------------------------------------*/ + + bool SHRay::operator==(const SHRay& rhs) noexcept + { + const XMVECTOR LHS_POS = XMLoadFloat3(&position); + const XMVECTOR RHS_POS = XMLoadFloat3(&rhs.position); + + const XMVECTOR LHS_DIR = XMLoadFloat3(&direction); + const XMVECTOR RHS_DIR = XMLoadFloat3(&rhs.direction); + + return XMVector3Equal(LHS_POS, RHS_POS) && XMVector3NotEqual(LHS_DIR, RHS_DIR); + } + + bool SHRay::operator!=(const SHRay& rhs) noexcept + { + const XMVECTOR LHS_POS = XMLoadFloat3(&position); + const XMVECTOR RHS_POS = XMLoadFloat3(&rhs.position); + + const XMVECTOR LHS_DIR = XMLoadFloat3(&direction); + const XMVECTOR RHS_DIR = XMLoadFloat3(&rhs.direction); + + return XMVector3NotEqual(LHS_POS, RHS_POS) || XMVector3NotEqual(LHS_DIR, RHS_DIR); + } + + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/SHRay.h b/SHADE_Engine/src/Math/SHRay.h new file mode 100644 index 00000000..29d55b16 --- /dev/null +++ b/SHADE_Engine/src/Math/SHRay.h @@ -0,0 +1,54 @@ +/**************************************************************************************** + * \file SHRay.h + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Interface for a Ray. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#pragma once + +#include + +// Project Headers +#include "SH_API.h" +#include "Vector/SHVec3.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + + struct SH_API SHRay + { + public: + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + + SHVec3 position; + SHVec3 direction; + + /*---------------------------------------------------------------------------------*/ + /* Constructors & Destructor */ + /*---------------------------------------------------------------------------------*/ + + SHRay() noexcept; + SHRay(const SHVec3& pos, const SHVec3& dir) noexcept; + SHRay(const SHRay& rhs) noexcept = default; + SHRay(SHRay&& rhs) noexcept = default; + + /*---------------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*---------------------------------------------------------------------------------*/ + + SHRay& operator= (const SHRay& rhs) noexcept = default; + SHRay& operator= (SHRay&& rhs) noexcept = default; + + [[nodiscard]] bool operator==(const SHRay& rhs) noexcept; + [[nodiscard]] bool operator!=(const SHRay& rhs) noexcept; + }; +} // namespace SHADE \ No newline at end of file From d95dbd5ce6a420f42ddb4458a9f8b6228399d07d Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Wed, 12 Oct 2022 21:59:34 +0800 Subject: [PATCH 6/8] Reworked Bounding Boxes --- .../src/Math/Geometry/SHBoundingBox.cpp | 196 +++++++----------- .../src/Math/Geometry/SHBoundingBox.h | 62 +++--- SHADE_Engine/src/Math/Geometry/SHShape.h | 4 +- SHADE_Engine/src/Math/Vector/SHVec2.cpp | 110 +++------- SHADE_Engine/src/Math/Vector/SHVec2.h | 9 +- SHADE_Engine/src/Math/Vector/SHVec3.cpp | 128 ++++-------- SHADE_Engine/src/Math/Vector/SHVec3.h | 3 + SHADE_Engine/src/Math/Vector/SHVec4.cpp | 144 ++++--------- SHADE_Engine/src/Math/Vector/SHVec4.h | 7 +- 9 files changed, 242 insertions(+), 421 deletions(-) diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp index 72b46819..11a14eed 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp @@ -14,6 +14,9 @@ #include "SHBoundingBox.h" // Project Headers #include "Math/SHMathHelpers.h" +#include "Math/SHRay.h" + +using namespace DirectX; namespace SHADE { @@ -21,89 +24,56 @@ namespace SHADE /* Constructors & Destructor Definitions */ /*-----------------------------------------------------------------------------------*/ + SHBoundingBox::SHBoundingBox() noexcept + { + type = Type::BOUNDING_BOX; + } + SHBoundingBox::SHBoundingBox(const SHVec3& c, const SHVec3& hE) noexcept - : SHShape {} - , center { c } - , halfExtents { hE } - { - type = Type::BOUNDING_BOX; - } - - SHBoundingBox::SHBoundingBox(const SHVec3* vertices, size_t numVertices) noexcept - : SHShape {} { type = Type::BOUNDING_BOX; - if (vertices == nullptr || numVertices < 2) - { - SHLOG_ERROR("Insufficient number of vertices passed into bounding box constructor!") - return; - } - - SHVec3 min { std::numeric_limits::max() }; - SHVec3 max { std::numeric_limits::min() }; - - for (size_t i = 0; i < numVertices; ++i) - { - const SHVec3& v = vertices[i]; - - min.x = SHMath::Min(min.x, v.x); - min.y = SHMath::Min(min.y, v.y); - min.z = SHMath::Min(min.z, v.z); - - max.x = SHMath::Max(max.x, v.x); - max.y = SHMath::Max(max.y, v.y); - max.z = SHMath::Max(max.z, v.z); - } - - center = SHVec3::Lerp(min, max, 0.5f); - halfExtents = SHVec3::Abs((max - min) * 0.5f); + Center = c; + Extents = hE; } - SHBoundingBox::SHBoundingBox(const SHBoundingBox* boxes, size_t numBoxes) noexcept - : SHShape {} - { - type = Type::BOUNDING_BOX; - - if (boxes == nullptr || numBoxes == 0) - { - SHLOG_ERROR("Insufficient number of boxes passed into bounding box constructor!") - return; - } - - center = boxes->center; - halfExtents = boxes->halfExtents; - - for (size_t i = 1; i < numBoxes; ++i) - *this = Combine(*this, boxes[i]); - } SHBoundingBox::SHBoundingBox(const SHBoundingBox& rhs) noexcept - : SHShape {} - , center { rhs.center } - , halfExtents { rhs.halfExtents } { + if (this == &rhs) + return; + type = Type::BOUNDING_BOX; + + Center = rhs.Center; + Extents = rhs.Extents; } SHBoundingBox::SHBoundingBox(SHBoundingBox&& rhs) noexcept - : SHShape {} - , center { rhs.center } - , halfExtents { rhs.halfExtents } { type = Type::BOUNDING_BOX; + + Center = rhs.Center; + Extents = rhs.Extents; } + /*-----------------------------------------------------------------------------------*/ + /* Operator Overload Definitions */ + /*-----------------------------------------------------------------------------------*/ + SHBoundingBox& SHBoundingBox::operator=(const SHBoundingBox& rhs) noexcept { + if (this == &rhs) + return *this; + if (rhs.type != Type::BOUNDING_BOX) { SHLOG_WARNING("Cannot assign a non-bounding box to a bounding box!") } else { - center = rhs.center; - halfExtents = rhs.halfExtents; + Center = rhs.Center; + Extents = rhs.Extents; } return *this; @@ -117,8 +87,8 @@ namespace SHADE } else { - center = rhs.center; - halfExtents = rhs.halfExtents; + Center = rhs.Center; + Extents = rhs.Extents; } return *this; @@ -130,22 +100,22 @@ namespace SHADE const SHVec3& SHBoundingBox::GetCenter() const noexcept { - return center; + return Center; } const SHVec3& SHBoundingBox::GetHalfExtents() const noexcept { - return halfExtents; + return Extents; } SHVec3 SHBoundingBox::GetMin() const noexcept { - return center - halfExtents; + return SHVec3{ Center.x - Extents.x, Center.y - Extents.y, Center.z - Extents.z }; } SHVec3 SHBoundingBox::GetMax() const noexcept { - return center + halfExtents; + return SHVec3{ Center.x + Extents.x, Center.y + Extents.y, Center.z + Extents.z }; } /*-----------------------------------------------------------------------------------*/ @@ -154,36 +124,42 @@ namespace SHADE void SHBoundingBox::SetCenter(const SHVec3& newCenter) noexcept { - center = newCenter; + Center = newCenter; } void SHBoundingBox::SetHalfExtents(const SHVec3& newHalfExtents) noexcept { - halfExtents = newHalfExtents; + Extents = newHalfExtents; } void SHBoundingBox::SetMin(const SHVec3& min) noexcept { - const SHVec3 MAX = center + halfExtents; + const SHVec3 MAX = GetMax(); - center = SHVec3::Lerp(min, MAX, 0.5f); - halfExtents = SHVec3::Abs((MAX - min) * 0.5f); + Center = SHVec3::Lerp(min, MAX, 0.5f); + Extents = SHVec3::Abs((MAX - min) * 0.5f); } void SHBoundingBox::SetMax(const SHVec3& max) noexcept { - const SHVec3 MIN = center - halfExtents; + const SHVec3 MIN = GetMin(); - center = SHVec3::Lerp(MIN, max, 0.5f); - halfExtents = SHVec3::Abs((max - MIN) * 0.5f); + Center = SHVec3::Lerp(MIN, max, 0.5f); + Extents = SHVec3::Abs((max - MIN) * 0.5f); } void SHBoundingBox::SetMinMax(const SHVec3& min, const SHVec3& max) noexcept { - center = SHVec3::Lerp(min, max, 0.5f); - halfExtents = SHVec3::Abs((max - min) * 0.5f); + Center = SHVec3::Lerp(min, max, 0.5f); + Extents = SHVec3::Abs((max - min) * 0.5f); } + std::vector SHBoundingBox::GetVertices() const noexcept + { + std::vector vertices{ 8 }; + GetCorners(vertices.data()); + return vertices; + } /*-----------------------------------------------------------------------------------*/ /* Public Function Member Definitions */ @@ -191,43 +167,29 @@ namespace SHADE bool SHBoundingBox::TestPoint(const SHVec3& point) noexcept { - const SHVec3 V = SHVec3::Abs(point - center); - for (size_t i = 0; i < SHVec3::SIZE; ++i) - { - if (V[i] > halfExtents[i]) - return false; - } - - return true; + return BoundingBox::Contains(point); } - bool SHBoundingBox::Raycast(const SHVec3& SHRay, float& distance) noexcept + bool SHBoundingBox::Raycast(const SHRay& ray, float& distance) noexcept { - return false; + return BoundingBox::Intersects(ray.position, ray.direction, distance); } bool SHBoundingBox::Contains(const SHBoundingBox& rhs) const noexcept { - const SHVec3 V = SHVec3::Abs(rhs.center - center); - for (size_t i = 0; i < SHVec3::SIZE; ++i) - { - if (V[i] > rhs.halfExtents[i]) - return false; - } - - return true; + return BoundingBox::Contains(rhs); } float SHBoundingBox::Volume() const noexcept { - return 8.0f * (halfExtents.x * halfExtents.y * halfExtents.z); + return 8.0f * (Extents.x * Extents.y * Extents.z); } float SHBoundingBox::SurfaceArea() const noexcept { - return 8.0f * ((halfExtents.x * halfExtents.y) - + (halfExtents.x * halfExtents.z) - + (halfExtents.y * halfExtents.z)); + return 8.0f * ((Extents.x * Extents.y) + + (Extents.x * Extents.z) + + (Extents.y * Extents.z)); } /*-----------------------------------------------------------------------------------*/ @@ -236,37 +198,31 @@ namespace SHADE SHBoundingBox SHBoundingBox::Combine(const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept { - if (lhs.Contains(rhs)) - return lhs; - - if (rhs.Contains(lhs)) - return rhs; - - const SHVec3 LHS_MIN = lhs.GetMin(); - const SHVec3 LHS_MAX = lhs.GetMax(); - const SHVec3 RHS_MIN = rhs.GetMin(); - const SHVec3 RHS_MAX = rhs.GetMax(); - - SHVec3 min = SHVec3::Min({ LHS_MIN, RHS_MIN }); - SHVec3 max = SHVec3::Max({ LHS_MAX, RHS_MAX }); - - SHBoundingBox result{ lhs }; - result.SetMinMax(min, max); + SHBoundingBox result; + CreateMerged(result, lhs, rhs); return result; } bool SHBoundingBox::Intersect(const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept { - const SHVec3 V = SHVec3::Abs(lhs.center - rhs.center); - const SHVec3 D = lhs.halfExtents + rhs.halfExtents; + return lhs.Intersects(rhs); + } - for (size_t i = 0; i < SHVec3::SIZE; ++i) - { - if (V[i] > D[i]) - return false; - } + SHBoundingBox SHBoundingBox::BuildFromBoxes(const SHBoundingBox* boxes, size_t numBoxes) noexcept + { + SHBoundingBox result; - return true; + for (size_t i = 1; i < numBoxes; ++i) + CreateMerged(result, boxes[i - 1], boxes[i]); + + return result; + } + + SHBoundingBox SHBoundingBox::BuildFromVertices(const SHVec3* vertices, size_t numVertices, size_t stride) noexcept + { + SHBoundingBox result; + CreateFromPoints(result, numVertices, vertices, stride); + return result; } } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h index 2ad5f1ed..5c38e1b8 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h @@ -22,32 +22,43 @@ namespace SHADE /* Type Definitions */ /*-----------------------------------------------------------------------------------*/ - // TODO(Diren): Use DirectX BoundingBox instead of custom - class SH_API SHBoundingBox : public SHShape + class SH_API SHBoundingBox : public SHShape, + private DirectX::BoundingBox { public: + /*---------------------------------------------------------------------------------*/ + /* Static Data Members */ + /*---------------------------------------------------------------------------------*/ + + static constexpr size_t NUM_VERTICES = 8; + /*---------------------------------------------------------------------------------*/ /* Constructors & Destructor */ /*---------------------------------------------------------------------------------*/ - SHBoundingBox (const SHVec3& center, const SHVec3& halfExtents) noexcept; - SHBoundingBox (const SHVec3* vertices, size_t numVertices) noexcept; - SHBoundingBox (const SHBoundingBox* boxes, size_t numBoxes) noexcept; + ~SHBoundingBox () noexcept = default; - SHBoundingBox (const SHBoundingBox& rhs) noexcept; - SHBoundingBox (SHBoundingBox&& rhs) noexcept; + SHBoundingBox () noexcept; + SHBoundingBox (const SHVec3& center, const SHVec3& halfExtents) noexcept; + SHBoundingBox (const SHBoundingBox& rhs) noexcept; + SHBoundingBox (SHBoundingBox&& rhs) noexcept; - SHBoundingBox& operator= (const SHBoundingBox& rhs) noexcept; - SHBoundingBox& operator= (SHBoundingBox&& rhs) noexcept; + /*---------------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*---------------------------------------------------------------------------------*/ + + SHBoundingBox& operator= (const SHBoundingBox& rhs) noexcept; + SHBoundingBox& operator= (SHBoundingBox&& rhs) noexcept; /*---------------------------------------------------------------------------------*/ /* Getter Functions */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] const SHVec3& GetCenter () const noexcept; - [[nodiscard]] const SHVec3& GetHalfExtents () const noexcept; - [[nodiscard]] SHVec3 GetMin () const noexcept; - [[nodiscard]] SHVec3 GetMax () const noexcept; + [[nodiscard]] const SHVec3& GetCenter () const noexcept; + [[nodsicard]] const SHVec3& GetHalfExtents() const noexcept; + [[nodiscard]] SHVec3 GetMin () const noexcept; + [[nodiscard]] SHVec3 GetMax () const noexcept; + [[nodiscard]] std::vector GetVertices () const noexcept; /*---------------------------------------------------------------------------------*/ /* Setter Functions */ @@ -59,32 +70,25 @@ namespace SHADE void SetMax (const SHVec3& max) noexcept; void SetMinMax (const SHVec3& min, const SHVec3& max) noexcept; - /*---------------------------------------------------------------------------------*/ /* Function Members */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] bool TestPoint (const SHVec3& point) noexcept override; - [[nodiscard]] bool Raycast (const SHVec3& SHRay, float& distance) noexcept override; + [[nodiscard]] bool TestPoint (const SHVec3& point) noexcept override; + [[nodiscard]] bool Raycast (const SHRay& ray, float& distance) noexcept override; - [[nodiscard]] bool Contains (const SHBoundingBox& rhs) const noexcept; - [[nodiscard]] float Volume () const noexcept; - [[nodiscard]] float SurfaceArea () const noexcept; + [[nodiscard]] bool Contains (const SHBoundingBox& rhs) const noexcept; + [[nodiscard]] float Volume () const noexcept; + [[nodiscard]] float SurfaceArea () const noexcept; /*---------------------------------------------------------------------------------*/ /* Static Function Members */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] static SHBoundingBox Combine (const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept; - [[nodiscard]] static bool Intersect (const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept; - - private: - /*---------------------------------------------------------------------------------*/ - /* Data Members */ - /*---------------------------------------------------------------------------------*/ - - SHVec3 center; - SHVec3 halfExtents; + [[nodiscard]] static SHBoundingBox Combine (const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept; + [[nodiscard]] static bool Intersect (const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept; + [[nodiscard]] static SHBoundingBox BuildFromBoxes (const SHBoundingBox* boxes, size_t numBoxes) noexcept; + [[nodiscard]] static SHBoundingBox BuildFromVertices (const SHVec3* vertices, size_t numVertices, size_t stride = 0) noexcept; }; diff --git a/SHADE_Engine/src/Math/Geometry/SHShape.h b/SHADE_Engine/src/Math/Geometry/SHShape.h index 204d4e0c..69578cb3 100644 --- a/SHADE_Engine/src/Math/Geometry/SHShape.h +++ b/SHADE_Engine/src/Math/Geometry/SHShape.h @@ -71,8 +71,8 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - [[nodiscard]] virtual bool TestPoint (const SHVec3& point) noexcept = 0; - [[nodiscard]] virtual bool Raycast (const SHVec3& SHRay, float& distance) noexcept = 0; + [[nodiscard]] virtual bool TestPoint (const SHVec3& point) noexcept = 0; + [[nodiscard]] virtual bool Raycast (const SHRay& ray, float& distance) noexcept = 0; protected: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Math/Vector/SHVec2.cpp b/SHADE_Engine/src/Math/Vector/SHVec2.cpp index 545492cb..a71ad0ea 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec2.cpp +++ b/SHADE_Engine/src/Math/Vector/SHVec2.cpp @@ -38,6 +38,10 @@ namespace SHADE : XMFLOAT2( 0.0f, 0.0f ) {} + SHVec2::SHVec2(const XMFLOAT2& xmfloat2) noexcept + : XMFLOAT2 ( xmfloat2.x, xmfloat2.y ) + {} + SHVec2::SHVec2(float n) noexcept : XMFLOAT2( n, n ) {} @@ -50,6 +54,11 @@ namespace SHADE /* Operator Overload Definitions */ /*-----------------------------------------------------------------------------------*/ + SHVec2::operator XMVECTOR() const noexcept + { + return XMLoadFloat2(this); + } + SHVec2& SHVec2::operator+=(const SHVec2& rhs) noexcept { return *this = *this + rhs; @@ -83,22 +92,16 @@ namespace SHADE SHVec2 SHVec2::operator+(const SHVec2& rhs) const noexcept { SHVec2 result; - - const XMVECTOR V1 = XMLoadFloat2(this); - const XMVECTOR V2 = XMLoadFloat2(&rhs); - XMStoreFloat2(&result, XMVectorAdd(V1, V2)); + XMStoreFloat2(&result, XMVectorAdd(*this, rhs)); return result; } SHVec2 SHVec2::operator-(const SHVec2& rhs) const noexcept { SHVec2 result; - - const XMVECTOR V1 = XMLoadFloat2(this); - const XMVECTOR V2 = XMLoadFloat2(&rhs); - XMStoreFloat2(&result, XMVectorSubtract(V1, V2)); + XMStoreFloat2(&result, XMVectorSubtract(*this, rhs)); return result; } @@ -110,59 +113,43 @@ namespace SHADE SHVec2 SHVec2::operator*(const SHVec2& rhs) const noexcept { SHVec2 result; - - const XMVECTOR V1 = XMLoadFloat2(this); - const XMVECTOR V2 = XMLoadFloat2(&rhs); - XMStoreFloat2(&result, XMVectorMultiply(V1, V2)); + XMStoreFloat2(&result, XMVectorMultiply(*this, rhs)); return result; } SHVec2 SHVec2::operator*(float rhs) const noexcept { SHVec2 result; - - const XMVECTOR V = XMLoadFloat2(this); - XMStoreFloat2(&result, XMVectorScale(V, rhs)); + XMStoreFloat2(&result, XMVectorScale(*this, rhs)); return result; } SHVec2 SHVec2::operator/(const SHVec2& rhs) const noexcept { SHVec2 result; - - const XMVECTOR V1 = XMLoadFloat2(this); - const XMVECTOR V2 = XMLoadFloat2(&rhs); - XMStoreFloat2(&result, XMVectorDivide(V1, V2)); + XMStoreFloat2(&result, XMVectorDivide(*this, rhs)); return result; } SHVec2 SHVec2::operator/(float rhs) const noexcept { SHVec2 result; - - const XMVECTOR V = XMLoadFloat2(this); - XMStoreFloat2(&result, XMVectorScale(V, 1.0f / rhs)); + XMStoreFloat2(&result, XMVectorScale(*this, 1.0f / rhs)); return result; } bool SHVec2::operator==(const SHVec2& rhs) const noexcept { - const XMVECTOR V1 = XMLoadFloat2(this); - const XMVECTOR V2 = XMLoadFloat2(&rhs); - - return XMVector2Equal(V1, V2); + return XMVector2Equal(*this, rhs); } bool SHVec2::operator!=(const SHVec2& rhs) const noexcept { - const XMVECTOR V1 = XMLoadFloat2(this); - const XMVECTOR V2 = XMLoadFloat2(&rhs); - - return XMVector2NotEqual(V1, V2); + return XMVector2NotEqual(*this, rhs); } float& SHVec2::operator[](int index) @@ -216,10 +203,8 @@ namespace SHADE SHVec2 operator* (float lhs, const SHVec2& rhs) noexcept { SHVec2 result; - - const XMVECTOR V = XMLoadFloat2(&rhs); - XMStoreFloat2(&result, XMVectorScale(V, lhs)); + XMStoreFloat2(&result, XMVectorScale(rhs, lhs)); return result; } @@ -229,16 +214,12 @@ namespace SHADE float SHVec2::Length() const noexcept { - const XMVECTOR V = XMLoadFloat2(this); - - return XMVectorGetX(XMVector2Length(V)); + return XMVectorGetX(XMVector2Length(*this)); } float SHVec2::LengthSquared() const noexcept { - const XMVECTOR V = XMLoadFloat2(this); - - return XMVectorGetX(XMVector2LengthSq(V)); + return XMVectorGetX(XMVector2LengthSq(*this)); } std::string SHVec2::ToString() const noexcept @@ -251,20 +232,14 @@ namespace SHADE float SHVec2::Dot(const SHVec2& rhs) const noexcept { - const XMVECTOR V1 = XMLoadFloat2(this); - const XMVECTOR V2 = XMLoadFloat2(&rhs); - - return XMVectorGetX(XMVector2Dot(V1, V2)); + return XMVectorGetX(XMVector2Dot(*this, rhs)); } SHVec2 SHVec2::Cross(const SHVec2& rhs) const noexcept { SHVec2 result; - const XMVECTOR V1 = XMLoadFloat2(this); - const XMVECTOR V2 = XMLoadFloat2(&rhs); - - XMStoreFloat2(&result, XMVector2Cross(V1, V2)); + XMStoreFloat2(&result, XMVector2Cross(*this, rhs)); return result; } @@ -276,9 +251,7 @@ namespace SHADE { SHVec2 result; - const XMVECTOR V = XMLoadFloat2(&vec2); - - XMStoreFloat2(&result, XMVector2Normalize(V)); + XMStoreFloat2(&result, XMVector2Normalize(vec2)); return result; } @@ -297,10 +270,10 @@ namespace SHADE SHVec2 result; - XMVECTOR min = XMLoadFloat2(&(*vec2s.begin())); + XMVECTOR min = *vec2s.begin(); for (auto it = vec2s.begin() + 1; it != vec2s.end(); ++it) { - const XMVECTOR tmp = XMLoadFloat2(&(*it)); + const XMVECTOR tmp = *it; min = XMVectorMin(min, tmp); } @@ -318,10 +291,10 @@ namespace SHADE SHVec2 result; - XMVECTOR max = XMLoadFloat2(&(*vec2s.begin())); + XMVECTOR max = *vec2s.begin(); for (auto it = vec2s.begin() + 1; it != vec2s.end(); ++it) { - const XMVECTOR tmp = XMLoadFloat2(&(*it)); + const XMVECTOR tmp = *it; max = XMVectorMax(max, tmp); } @@ -333,11 +306,7 @@ namespace SHADE { SHVec2 result; - const XMVECTOR V = XMLoadFloat2(&v); - const XMVECTOR MIN = XMLoadFloat2(&vMin); - const XMVECTOR MAX = XMLoadFloat2(&vMax); - - XMStoreFloat2(&result, XMVectorClamp(V, MIN, MAX)); + XMStoreFloat2(&result, XMVectorClamp(v, vMin, vMax)); return result; } @@ -345,10 +314,7 @@ namespace SHADE { SHVec2 result; - const XMVECTOR V1 = XMLoadFloat2(&a); - const XMVECTOR V2 = XMLoadFloat2(&b); - - XMStoreFloat2(&result, XMVectorLerp(V1, V2, t)); + XMStoreFloat2(&result, XMVectorLerp(a, b, t)); return result; } @@ -369,10 +335,7 @@ namespace SHADE float SHVec2::Angle(const SHVec2& lhs, const SHVec2& rhs) noexcept { - const XMVECTOR V1 = XMLoadFloat2(&lhs); - const XMVECTOR V2 = XMLoadFloat2(&rhs); - - return XMVectorGetX(XMVector2AngleBetweenVectors(V1, V2)); + return XMVectorGetX(XMVector2AngleBetweenVectors(lhs, rhs)); } float SHVec2::Dot(const SHVec2& lhs, const SHVec2& rhs) noexcept @@ -384,11 +347,10 @@ namespace SHADE { SHVec2 result; - const XMVECTOR U = XMLoadFloat2(&u); const float V_DOT_U = Dot(v, u); const float U_LENSQ = u.LengthSquared(); - XMStoreFloat2(&result, XMVectorScale(U, V_DOT_U / U_LENSQ)); + XMStoreFloat2(&result, XMVectorScale(u, V_DOT_U / U_LENSQ)); return result; } @@ -396,10 +358,8 @@ namespace SHADE { SHVec2 result; - const XMVECTOR V = XMLoadFloat2(&v); - const XMVECTOR N = XMLoadFloat2(&normal); - XMStoreFloat2(&result, XMVector2Reflect(V, N)); + XMStoreFloat2(&result, XMVector2Reflect(v, normal)); return result; } @@ -407,10 +367,9 @@ namespace SHADE { SHVec2 result; - const XMVECTOR V = XMLoadFloat2(&v); const XMMATRIX R = XMMatrixRotationZ(angleInRad); - XMStoreFloat2(&result, XMVector2Transform(V, R)); + XMStoreFloat2(&result, XMVector2Transform(v, R)); return result; } @@ -418,10 +377,9 @@ namespace SHADE { SHVec2 result; - const XMVECTOR V = XMLoadFloat2(&v); const XMMATRIX TF = XMLoadFloat4x4(&transformMtx); - XMStoreFloat2(&result, XMVector2TransformCoord(V, TF)); + XMStoreFloat2(&result, XMVector2TransformCoord(v, TF)); return result; } diff --git a/SHADE_Engine/src/Math/Vector/SHVec2.h b/SHADE_Engine/src/Math/Vector/SHVec2.h index 17642126..7fed19b4 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec2.h +++ b/SHADE_Engine/src/Math/Vector/SHVec2.h @@ -52,9 +52,10 @@ namespace SHADE SHVec2 (SHVec2&& rhs) = default; ~SHVec2 () = default; - SHVec2 () noexcept; - SHVec2 (float n) noexcept; - SHVec2 (float x, float y) noexcept; + SHVec2 () noexcept; + SHVec2 (const XMFLOAT2& xmfloat2) noexcept; + SHVec2 (float n) noexcept; + SHVec2 (float x, float y) noexcept; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ @@ -63,6 +64,8 @@ namespace SHADE SHVec2& operator= (const SHVec2& rhs) = default; SHVec2& operator= (SHVec2&& rhs) = default; + operator DirectX::XMVECTOR () const noexcept; + SHVec2& operator+= (const SHVec2& rhs) noexcept; SHVec2& operator-= (const SHVec2& rhs) noexcept; SHVec2& operator*= (const SHVec2& rhs) noexcept; diff --git a/SHADE_Engine/src/Math/Vector/SHVec3.cpp b/SHADE_Engine/src/Math/Vector/SHVec3.cpp index 1bcb47b3..45fe4f61 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec3.cpp +++ b/SHADE_Engine/src/Math/Vector/SHVec3.cpp @@ -43,6 +43,10 @@ namespace SHADE : XMFLOAT3( 0.0f, 0.0f, 0.0f ) {} + SHVec3::SHVec3(const XMFLOAT3& xmfloat3) noexcept + : XMFLOAT3 ( xmfloat3.x, xmfloat3.y, xmfloat3.z ) + {} + SHVec3::SHVec3(float n) noexcept : XMFLOAT3( n, n, n ) {} @@ -55,6 +59,12 @@ namespace SHADE /* Operator Overload Definitions */ /*-----------------------------------------------------------------------------------*/ + SHVec3::operator XMVECTOR() const noexcept + { + return XMLoadFloat3(this); + } + + SHVec3& SHVec3::operator+=(const SHVec3& rhs) noexcept { return *this = *this + rhs; @@ -88,22 +98,16 @@ namespace SHADE SHVec3 SHVec3::operator+(const SHVec3& rhs) const noexcept { SHVec3 result; - - const XMVECTOR V1 = XMLoadFloat3(this); - const XMVECTOR V2 = XMLoadFloat3(&rhs); - XMStoreFloat3(&result, XMVectorAdd(V1, V2)); + XMStoreFloat3(&result, XMVectorAdd(*this, rhs)); return result; } SHVec3 SHVec3::operator-(const SHVec3& rhs) const noexcept { SHVec3 result; - - const XMVECTOR V1 = XMLoadFloat3(this); - const XMVECTOR V2 = XMLoadFloat3(&rhs); - XMStoreFloat3(&result, XMVectorSubtract(V1, V2)); + XMStoreFloat3(&result, XMVectorSubtract(*this, rhs)); return result; } @@ -116,59 +120,43 @@ namespace SHADE SHVec3 SHVec3::operator*(const SHVec3& rhs) const noexcept { SHVec3 result; - - const XMVECTOR V1 = XMLoadFloat3(this); - const XMVECTOR V2 = XMLoadFloat3(&rhs); - XMStoreFloat3(&result, XMVectorMultiply(V1, V2)); + XMStoreFloat3(&result, XMVectorMultiply(*this, rhs)); return result; } SHVec3 SHVec3::operator*(float rhs) const noexcept { SHVec3 result; - - const XMVECTOR V = XMLoadFloat3(this); - XMStoreFloat3(&result, XMVectorScale(V, rhs)); + XMStoreFloat3(&result, XMVectorScale(*this, rhs)); return result; } SHVec3 SHVec3::operator/(const SHVec3& rhs) const noexcept { SHVec3 result; - - const XMVECTOR V1 = XMLoadFloat3(this); - const XMVECTOR V2 = XMLoadFloat3(&rhs); - XMStoreFloat3(&result, XMVectorDivide(V1, V2)); + XMStoreFloat3(&result, XMVectorDivide(*this, rhs)); return result; } SHVec3 SHVec3::operator/(float rhs) const noexcept { SHVec3 result; - - const XMVECTOR V = XMLoadFloat3(this); - XMStoreFloat3(&result, XMVectorScale(V, 1.0f / rhs)); + XMStoreFloat3(&result, XMVectorScale(*this, 1.0f / rhs)); return result; } bool SHVec3::operator==(const SHVec3& rhs) const noexcept { - const XMVECTOR V1 = XMLoadFloat3(this); - const XMVECTOR V2 = XMLoadFloat3(&rhs); - - return XMVector3Equal(V1, V2); + return XMVector3Equal(*this, rhs); } bool SHVec3::operator!=(const SHVec3& rhs) const noexcept { - const XMVECTOR V1 = XMLoadFloat3(this); - const XMVECTOR V2 = XMLoadFloat3(&rhs); - - return XMVector3NotEqual(V1, V2); + return XMVector3NotEqual(*this, rhs); } float& SHVec3::operator[](int index) @@ -226,10 +214,8 @@ namespace SHADE SHVec3 operator* (float lhs, const SHVec3& rhs) noexcept { SHVec3 result; - - const XMVECTOR V = XMLoadFloat3(&rhs); - XMStoreFloat3(&result, XMVectorScale(V, lhs)); + XMStoreFloat3(&result, XMVectorScale(rhs, lhs)); return result; } @@ -239,16 +225,12 @@ namespace SHADE float SHVec3::Length() const noexcept { - const XMVECTOR V = XMLoadFloat3(this); - - return XMVectorGetX(XMVector3Length(V)); + return XMVectorGetX(XMVector3Length(*this)); } float SHVec3::LengthSquared() const noexcept { - const XMVECTOR V = XMLoadFloat3(this); - - return XMVectorGetX(XMVector3LengthSq(V)); + return XMVectorGetX(XMVector3LengthSq(*this)); } std::string SHVec3::ToString() const noexcept @@ -261,20 +243,14 @@ namespace SHADE float SHVec3::Dot(const SHVec3& rhs) const noexcept { - const XMVECTOR V1 = XMLoadFloat3(this); - const XMVECTOR V2 = XMLoadFloat3(&rhs); - - return XMVectorGetX(XMVector3Dot(V1, V2)); + return XMVectorGetX(XMVector3Dot(*this, rhs)); } SHVec3 SHVec3::Cross(const SHVec3& rhs) const noexcept { SHVec3 result; - const XMVECTOR V1 = XMLoadFloat3(this); - const XMVECTOR V2 = XMLoadFloat3(&rhs); - - XMStoreFloat3(&result, XMVector3Cross(V1, V2)); + XMStoreFloat3(&result, XMVector3Cross(*this, rhs)); return result; } @@ -286,9 +262,7 @@ namespace SHADE { SHVec3 result; - const XMVECTOR V = XMLoadFloat3(&v); - - XMStoreFloat3(&result, XMVector3Normalize(V)); + XMStoreFloat3(&result, XMVector3Normalize(v)); return result; } @@ -307,10 +281,10 @@ namespace SHADE SHVec3 result; - XMVECTOR min = XMLoadFloat3(&(*vs.begin())); + XMVECTOR min = *vs.begin(); for (auto it = vs.begin() + 1; it != vs.end(); ++it) { - const XMVECTOR tmp = XMLoadFloat3(&(*it)); + const XMVECTOR tmp = *it; min = XMVectorMin(min, tmp); } @@ -328,10 +302,10 @@ namespace SHADE SHVec3 result; - XMVECTOR max = XMLoadFloat3(&(*vs.begin())); + XMVECTOR max = *vs.begin(); for (auto it = vs.begin() + 1; it != vs.end(); ++it) { - const XMVECTOR tmp = XMLoadFloat3(&(*it)); + const XMVECTOR tmp = *it; max = XMVectorMax(max, tmp); } @@ -343,11 +317,7 @@ namespace SHADE { SHVec3 result; - const XMVECTOR V = XMLoadFloat3(&v); - const XMVECTOR MIN = XMLoadFloat3(&vMin); - const XMVECTOR MAX = XMLoadFloat3(&vMax); - - XMStoreFloat3(&result, XMVectorClamp(V, MIN, MAX)); + XMStoreFloat3(&result, XMVectorClamp(v, vMin, vMax)); return result; } @@ -355,10 +325,7 @@ namespace SHADE { SHVec3 result; - const XMVECTOR V1 = XMLoadFloat3(&a); - const XMVECTOR V2 = XMLoadFloat3(&b); - - XMStoreFloat3(&result, XMVectorLerp(V1, V2, t)); + XMStoreFloat3(&result, XMVectorLerp(a, b, t)); return result; } @@ -382,7 +349,7 @@ namespace SHADE const XMVECTOR V1 = XMLoadFloat3(&lhs); const XMVECTOR V2 = XMLoadFloat3(&rhs); - return XMVectorGetX(XMVector3AngleBetweenVectors(V1, V2)); + return XMVectorGetX(XMVector3AngleBetweenVectors(lhs, rhs)); } float SHVec3::Dot(const SHVec3& lhs, const SHVec3& rhs) noexcept @@ -399,22 +366,18 @@ namespace SHADE { SHVec3 result; - const XMVECTOR U = XMLoadFloat3(&u); const float V_DOT_U = Dot(v, u); const float U_LENSQ = u.LengthSquared(); - XMStoreFloat3(&result, XMVectorScale(U, V_DOT_U / U_LENSQ)); + XMStoreFloat3(&result, XMVectorScale(u, V_DOT_U / U_LENSQ)); return result; } SHVec3 SHVec3::Reflect(const SHVec3& v, const SHVec3& normal) noexcept { SHVec3 result; - - const XMVECTOR V = XMLoadFloat3(&v); - const XMVECTOR N = XMLoadFloat3(&normal); - XMStoreFloat3(&result, XMVector3Reflect(V, N)); + XMStoreFloat3(&result, XMVector3Reflect(v, normal)); return result; } @@ -422,12 +385,9 @@ namespace SHADE { SHVec3 result; - const XMVECTOR V = XMLoadFloat3(&v); + const XMVECTOR Q = XMQuaternionRotationAxis(axis, angleInRad); - const XMVECTOR AXIS = XMLoadFloat3(&axis); - const XMVECTOR Q = XMQuaternionRotationAxis(AXIS, angleInRad); - - XMStoreFloat3(&result, XMVector3Rotate(V, Q)); + XMStoreFloat3(&result, XMVector3Rotate(v, Q)); return result; } @@ -435,10 +395,9 @@ namespace SHADE { SHVec3 result; - const XMVECTOR V = XMLoadFloat3(&v); - const XMMATRIX R = XMMatrixRotationX(angleInRad); + const XMMATRIX R = XMMatrixRotationX(angleInRad); - XMStoreFloat3(&result, XMVector3TransformCoord(V, R)); + XMStoreFloat3(&result, XMVector3TransformCoord(v, R)); return result; } @@ -446,10 +405,9 @@ namespace SHADE { SHVec3 result; - const XMVECTOR V = XMLoadFloat3(&v); - const XMMATRIX R = XMMatrixRotationY(angleInRad); + const XMMATRIX R = XMMatrixRotationY(angleInRad); - XMStoreFloat3(&result, XMVector3TransformCoord(V, R)); + XMStoreFloat3(&result, XMVector3TransformCoord(v, R)); return result; } @@ -457,10 +415,9 @@ namespace SHADE { SHVec3 result; - const XMVECTOR V = XMLoadFloat3(&v); - const XMMATRIX R = XMMatrixRotationZ(angleInRad); + const XMMATRIX R = XMMatrixRotationZ(angleInRad); - XMStoreFloat3(&result, XMVector3TransformCoord(V, R)); + XMStoreFloat3(&result, XMVector3TransformCoord(v, R)); return result; } @@ -468,10 +425,9 @@ namespace SHADE { SHVec3 result; - const XMVECTOR V = XMLoadFloat3(&v); const XMMATRIX TF = XMLoadFloat4x4(&transformMtx); - XMStoreFloat3(&result, XMVector3TransformCoord(V, TF)); + XMStoreFloat3(&result, XMVector3TransformCoord(v, TF)); return result; } } \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Vector/SHVec3.h b/SHADE_Engine/src/Math/Vector/SHVec3.h index cc0e043e..fb9d0d4a 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec3.h +++ b/SHADE_Engine/src/Math/Vector/SHVec3.h @@ -58,6 +58,7 @@ namespace SHADE ~SHVec3 () = default; SHVec3 () noexcept; + SHVec3 (const XMFLOAT3& xmfloat3) noexcept; SHVec3 (float n) noexcept; SHVec3 (float x, float y, float z) noexcept; @@ -68,6 +69,8 @@ namespace SHADE SHVec3& operator= (const SHVec3& rhs) = default; SHVec3& operator= (SHVec3&& rhs) = default; + operator DirectX::XMVECTOR () const noexcept; + SHVec3& operator+= (const SHVec3& rhs) noexcept; SHVec3& operator-= (const SHVec3& rhs) noexcept; SHVec3& operator*= (const SHVec3& rhs) noexcept; diff --git a/SHADE_Engine/src/Math/Vector/SHVec4.cpp b/SHADE_Engine/src/Math/Vector/SHVec4.cpp index d1c87865..bcf2ef97 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec4.cpp +++ b/SHADE_Engine/src/Math/Vector/SHVec4.cpp @@ -38,6 +38,10 @@ namespace SHADE : XMFLOAT4( 0.0f, 0.0f, 0.0f, 0.0f ) {} + SHVec4::SHVec4(const XMFLOAT4& xmfloat4) noexcept + : XMFLOAT4( xmfloat4.x, xmfloat4.y, xmfloat4.z, xmfloat4.w ) + {} + SHVec4::SHVec4(float _x, float _y, float _z, float _w) noexcept : XMFLOAT4( _x, _y, _z, _w ) {} @@ -46,6 +50,11 @@ namespace SHADE /* Operator Overload Definitions */ /*-----------------------------------------------------------------------------------*/ + SHVec4::operator XMVECTOR() const noexcept + { + return XMLoadFloat4(this); + } + SHVec4& SHVec4::operator+=(const SHVec4& rhs) noexcept { return *this = *this + rhs; @@ -79,22 +88,16 @@ namespace SHADE SHVec4 SHVec4::operator+(const SHVec4& rhs) const noexcept { SHVec4 result; - - const XMVECTOR V1 = XMLoadFloat4(this); - const XMVECTOR V2 = XMLoadFloat4(&rhs); - XMStoreFloat4(&result, XMVectorAdd(V1, V2)); + XMStoreFloat4(&result, XMVectorAdd(*this, rhs)); return result; } SHVec4 SHVec4::operator-(const SHVec4& rhs) const noexcept { SHVec4 result; - - const XMVECTOR V1 = XMLoadFloat4(this); - const XMVECTOR V2 = XMLoadFloat4(&rhs); - XMStoreFloat4(&result, XMVectorSubtract(V1, V2)); + XMStoreFloat4(&result, XMVectorSubtract(*this, rhs)); return result; } @@ -106,59 +109,43 @@ namespace SHADE SHVec4 SHVec4::operator*(const SHVec4& rhs) const noexcept { SHVec4 result; - - const XMVECTOR V1 = XMLoadFloat4(this); - const XMVECTOR V2 = XMLoadFloat4(&rhs); - XMStoreFloat4(&result, XMVectorMultiply(V1, V2)); + XMStoreFloat4(&result, XMVectorMultiply(*this, rhs)); return result; } SHVec4 SHVec4::operator*(float rhs) const noexcept { SHVec4 result; - - const XMVECTOR V = XMLoadFloat4(this); - XMStoreFloat4(&result, XMVectorScale(V, rhs)); + XMStoreFloat4(&result, XMVectorScale(*this, rhs)); return result; } SHVec4 SHVec4::operator/(const SHVec4& rhs) const noexcept { SHVec4 result; - - const XMVECTOR V1 = XMLoadFloat4(this); - const XMVECTOR V2 = XMLoadFloat4(&rhs); - XMStoreFloat4(&result, XMVectorDivide(V1, V2)); + XMStoreFloat4(&result, XMVectorDivide(*this, rhs)); return result; } SHVec4 SHVec4::operator/(float rhs) const noexcept { SHVec4 result; - - const XMVECTOR V = XMLoadFloat4(this); - XMStoreFloat4(&result, XMVectorScale(V, 1.0f / rhs)); + XMStoreFloat4(&result, XMVectorScale(*this, 1.0f / rhs)); return result; } bool SHVec4::operator==(const SHVec4& rhs) const noexcept { - const XMVECTOR V1 = XMLoadFloat4(this); - const XMVECTOR V2 = XMLoadFloat4(&rhs); - - return XMVector4Equal(V1, V2); + return XMVector4Equal(*this, rhs); } bool SHVec4::operator!=(const SHVec4& rhs) const noexcept { - const XMVECTOR V1 = XMLoadFloat4(this); - const XMVECTOR V2 = XMLoadFloat4(&rhs); - - return XMVector4NotEqual(V1, V2); + return XMVector4NotEqual(*this, rhs); } float& SHVec4::operator[](int index) @@ -220,10 +207,8 @@ namespace SHADE SHVec4 operator* (float lhs, const SHVec4& rhs) noexcept { SHVec4 result; - - const XMVECTOR V = XMLoadFloat4(&rhs); - XMStoreFloat4(&result, XMVectorScale(V, lhs)); + XMStoreFloat4(&result, XMVectorScale(rhs, lhs)); return result; } @@ -233,30 +218,22 @@ namespace SHADE float SHVec4::Length() const noexcept { - const XMVECTOR V = XMLoadFloat4(this); - - return XMVectorGetX(XMVector4Length(V)); + return XMVectorGetX(XMVector4Length(*this)); } float SHVec4::Length3D() const noexcept { - const XMVECTOR V = XMLoadFloat4(this); - - return XMVectorGetX(XMVector3Length(V)); + return XMVectorGetX(XMVector3Length(*this)); } float SHVec4::LengthSquared() const noexcept { - const XMVECTOR V = XMLoadFloat4(this); - - return XMVectorGetX(XMVector4LengthSq(V)); + return XMVectorGetX(XMVector4LengthSq(*this)); } float SHVec4::LengthSquared3D() const noexcept { - const XMVECTOR V = XMLoadFloat4(this); - - return XMVectorGetX(XMVector3LengthSq(V)); + return XMVectorGetX(XMVector3LengthSq(*this)); } std::string SHVec4::ToString() const noexcept @@ -269,28 +246,19 @@ namespace SHADE float SHVec4::Dot(const SHVec4& rhs) const noexcept { - const XMVECTOR V1 = XMLoadFloat4(this); - const XMVECTOR V2 = XMLoadFloat4(&rhs); - - return XMVectorGetX(XMVector4Dot(V1, V2)); + return XMVectorGetX(XMVector4Dot(*this, rhs)); } float SHVec4::Dot3D(const SHVec4& rhs) const noexcept { - const XMVECTOR V1 = XMLoadFloat4(this); - const XMVECTOR V2 = XMLoadFloat4(&rhs); - - return XMVectorGetX(XMVector3Dot(V1, V2)); + return XMVectorGetX(XMVector3Dot(*this, rhs)); } SHVec4 SHVec4::Cross3D(const SHVec4& rhs) const noexcept { SHVec4 result; - const XMVECTOR V1 = XMLoadFloat4(this); - const XMVECTOR V2 = XMLoadFloat4(&rhs); - - XMStoreFloat4(&result, XMVector3Cross(V1, V2)); + XMStoreFloat4(&result, XMVector3Cross(*this, rhs)); result.w = 1.0f; return result; } @@ -299,11 +267,7 @@ namespace SHADE { SHVec4 result; - const XMVECTOR V3 = XMLoadFloat4(this); - const XMVECTOR V1 = XMLoadFloat4(&v1); - const XMVECTOR V2 = XMLoadFloat4(&v2); - - XMStoreFloat4(&result, XMVector4Cross(V3, V1, V2)); + XMStoreFloat4(&result, XMVector4Cross(*this, v1, v2)); return result; } @@ -315,9 +279,7 @@ namespace SHADE { SHVec4 result; - const XMVECTOR V = XMLoadFloat4(&v); - - XMStoreFloat4(&result, XMVector4Normalize(V)); + XMStoreFloat4(&result, XMVector4Normalize(v)); return result; } @@ -325,9 +287,7 @@ namespace SHADE { SHVec4 result; - const XMVECTOR V = XMLoadFloat4(&v); - - XMStoreFloat4(&result, XMVector3Normalize(V)); + XMStoreFloat4(&result, XMVector3Normalize(v)); result.w = 1.0f; return result; } @@ -347,10 +307,10 @@ namespace SHADE SHVec4 result; - XMVECTOR min = XMLoadFloat4(&(*vs.begin())); + XMVECTOR min = *vs.begin(); for (auto it = vs.begin() + 1; it != vs.end(); ++it) { - const XMVECTOR tmp = XMLoadFloat4(&(*it)); + const XMVECTOR tmp = *it; min = XMVectorMin(min, tmp); } @@ -368,10 +328,10 @@ namespace SHADE SHVec4 result; - XMVECTOR max = XMLoadFloat4(&(*vs.begin())); + XMVECTOR max = *vs.begin(); for (auto it = vs.begin() + 1; it != vs.end(); ++it) { - const XMVECTOR tmp = XMLoadFloat4(&(*it)); + const XMVECTOR tmp = *it; max = XMVectorMax(max, tmp); } @@ -383,11 +343,7 @@ namespace SHADE { SHVec4 result; - const XMVECTOR V = XMLoadFloat4(&v); - const XMVECTOR MIN = XMLoadFloat4(&vMin); - const XMVECTOR MAX = XMLoadFloat4(&vMax); - - XMStoreFloat4(&result, XMVectorClamp(V, MIN, MAX)); + XMStoreFloat4(&result, XMVectorClamp(v, vMin, vMax)); return result; } @@ -395,10 +351,7 @@ namespace SHADE { SHVec4 result; - const XMVECTOR V1 = XMLoadFloat4(&a); - const XMVECTOR V2 = XMLoadFloat4(&b); - - XMStoreFloat4(&result, XMVectorLerp(V1, V2, t)); + XMStoreFloat4(&result, XMVectorLerp(a, b, t)); return result; } @@ -429,18 +382,12 @@ namespace SHADE float SHVec4::Angle(const SHVec4& lhs, const SHVec4& rhs) noexcept { - const XMVECTOR V1 = XMLoadFloat4(&lhs); - const XMVECTOR V2 = XMLoadFloat4(&rhs); - - return XMVectorGetX(XMVector4AngleBetweenVectors(V1, V2)); + return XMVectorGetX(XMVector4AngleBetweenVectors(lhs,rhs)); } float SHVec4::Angle3D(const SHVec4& lhs, const SHVec4& rhs) noexcept { - const XMVECTOR V1 = XMLoadFloat4(&lhs); - const XMVECTOR V2 = XMLoadFloat4(&rhs); - - return XMVectorGetX(XMVector3AngleBetweenVectors(V1, V2)); + return XMVectorGetX(XMVector3AngleBetweenVectors(lhs,rhs)); } float SHVec4::Dot(const SHVec4& lhs, const SHVec4& rhs) noexcept @@ -467,11 +414,10 @@ namespace SHADE { SHVec4 result; - const XMVECTOR U = XMLoadFloat4(&u); const float V_DOT_U = Dot(v, u); const float U_LENSQ = u.LengthSquared(); - XMStoreFloat4(&result, XMVectorScale(U, V_DOT_U / U_LENSQ)); + XMStoreFloat4(&result, XMVectorScale(u, V_DOT_U / U_LENSQ)); return result; } @@ -479,11 +425,10 @@ namespace SHADE { SHVec4 result; - const XMVECTOR U = XMLoadFloat4(&u); const float V_DOT_U = Dot3D(v, u); const float U_LENSQ = u.LengthSquared3D(); - XMStoreFloat4(&result, XMVectorScale(U, V_DOT_U / U_LENSQ)); + XMStoreFloat4(&result, XMVectorScale(u, V_DOT_U / U_LENSQ)); result.w = 1.0f; return result; } @@ -491,11 +436,8 @@ namespace SHADE SHVec4 SHVec4::Reflect(const SHVec4& v, const SHVec4& normal) noexcept { SHVec4 result; - - const XMVECTOR V = XMLoadFloat4(&v); - const XMVECTOR N = XMLoadFloat4(&normal); - XMStoreFloat4(&result, XMVector4Reflect(V, N)); + XMStoreFloat4(&result, XMVector4Reflect(v, normal)); result.w = 1.0f; return result; } @@ -503,11 +445,8 @@ namespace SHADE SHVec4 SHVec4::Reflect3D(const SHVec4& v, const SHVec4& normal) noexcept { SHVec4 result; - - const XMVECTOR V = XMLoadFloat4(&v); - const XMVECTOR N = XMLoadFloat4(&normal); - XMStoreFloat4(&result, XMVector3Reflect(V, N)); + XMStoreFloat4(&result, XMVector3Reflect(v, normal)); result.w = 1.0f; return result; } @@ -516,10 +455,9 @@ namespace SHADE { SHVec4 result; - const XMVECTOR V = XMLoadFloat4(&v); const XMMATRIX TF = XMLoadFloat4x4(&transformMtx); - XMStoreFloat4(&result, XMVector3TransformCoord(V, TF)); + XMStoreFloat4(&result, XMVector3TransformCoord(v, TF)); return result; } } \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Vector/SHVec4.h b/SHADE_Engine/src/Math/Vector/SHVec4.h index 59038065..911a714e 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec4.h +++ b/SHADE_Engine/src/Math/Vector/SHVec4.h @@ -52,8 +52,9 @@ namespace SHADE SHVec4 (SHVec4&& rhs) = default; ~SHVec4 () = default; - SHVec4 () noexcept; - SHVec4 (float x, float y, float z, float w) noexcept; + SHVec4 () noexcept; + SHVec4 (const XMFLOAT4& xmfloat4) noexcept; + SHVec4 (float x, float y, float z, float w) noexcept; /*---------------------------------------------------------------------------------*/ /* Operator Overloads */ @@ -62,6 +63,8 @@ namespace SHADE SHVec4& operator= (const SHVec4& rhs) = default; SHVec4& operator= (SHVec4&& rhs) = default; + operator DirectX::XMVECTOR () const noexcept; + SHVec4& operator+= (const SHVec4& rhs) noexcept; SHVec4& operator-= (const SHVec4& rhs) noexcept; SHVec4& operator*= (const SHVec4& rhs) noexcept; From 381217a1b870b90b3d8f2b80e4ebaf9a20e2a5f9 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Thu, 13 Oct 2022 03:03:21 +0800 Subject: [PATCH 7/8] Added Colour --- SHADE_Engine/src/Math/SHColour.cpp | 297 ++++++++++++++++++++++++ SHADE_Engine/src/Math/SHColour.h | 166 +++++++++++++ SHADE_Engine/src/Math/Vector/SHVec3.cpp | 1 + 3 files changed, 464 insertions(+) create mode 100644 SHADE_Engine/src/Math/SHColour.cpp create mode 100644 SHADE_Engine/src/Math/SHColour.h diff --git a/SHADE_Engine/src/Math/SHColour.cpp b/SHADE_Engine/src/Math/SHColour.cpp new file mode 100644 index 00000000..8aae2cb3 --- /dev/null +++ b/SHADE_Engine/src/Math/SHColour.cpp @@ -0,0 +1,297 @@ +/**************************************************************************************** + * \file SHColour.cpp + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for a Colour. + * + * \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 + + +#include + +// Primary Header +#include "SHColour.h" +// Project Headers +#include "SHMathHelpers.h" + +using namespace DirectX; + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Static Data Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + const SHColour SHColour::BEIGE = DirectX::Colors::Beige; + const SHColour SHColour::BLACK = DirectX::Colors::Black; + const SHColour SHColour::BLUE = DirectX::Colors::Blue; + const SHColour SHColour::BROWN = DirectX::Colors::Brown; + const SHColour SHColour::CHOCOLATE = DirectX::Colors::Chocolate; + const SHColour SHColour::CORAL = DirectX::Colors::Coral; + const SHColour SHColour::CRIMSON = DirectX::Colors::Crimson; + const SHColour SHColour::CYAN = DirectX::Colors::Cyan; + const SHColour SHColour::DARKBLUE = DirectX::Colors::DarkBlue; + const SHColour SHColour::DARKGRAY = DirectX::Colors::DarkGray; + const SHColour SHColour::DARKGREEN = DirectX::Colors::DarkGreen; + const SHColour SHColour::DARKMAGENTA = DirectX::Colors::DarkMagenta; + const SHColour SHColour::DARKORANGE = DirectX::Colors::DarkOrange; + const SHColour SHColour::DARKRED = DirectX::Colors::DarkRed; + const SHColour SHColour::DEEPPINK = DirectX::Colors::DeepPink; + const SHColour SHColour::FORESTGREEN = DirectX::Colors::ForestGreen; + const SHColour SHColour::FUCHSIA = DirectX::Colors::Fuchsia; + const SHColour SHColour::GOLD = DirectX::Colors::Gold; + const SHColour SHColour::GRAY = DirectX::Colors::Gray; + const SHColour SHColour::GREEN = DirectX::Colors::Green; + const SHColour SHColour::HOTPINK = DirectX::Colors::HotPink; + const SHColour SHColour::INDIGO = DirectX::Colors::Indigo; + const SHColour SHColour::LAVENDER = DirectX::Colors::Lavender; + const SHColour SHColour::LIGHTBLUE = DirectX::Colors::LightBlue; + const SHColour SHColour::LIGHTGRAY = DirectX::Colors::LightGray; + const SHColour SHColour::LIGHTGREEN = DirectX::Colors::LightGreen; + const SHColour SHColour::LIGHTPINK = DirectX::Colors::LightPink; + const SHColour SHColour::LIGHTYELLOW = DirectX::Colors::LightYellow; + const SHColour SHColour::LIME = DirectX::Colors::Lime; + const SHColour SHColour::LIMEGREEN = DirectX::Colors::LimeGreen; + const SHColour SHColour::MAGENTA = DirectX::Colors::Magenta; + const SHColour SHColour::MAROON = DirectX::Colors::Maroon; + const SHColour SHColour::MEDIUMBLUE = DirectX::Colors::MediumBlue; + const SHColour SHColour::MEDIUMPURPLE = DirectX::Colors::MediumPurple; + const SHColour SHColour::NAVY = DirectX::Colors::Navy; + const SHColour SHColour::OLIVE = DirectX::Colors::Olive; + const SHColour SHColour::ORANGE = DirectX::Colors::Orange; + const SHColour SHColour::ORCHID = DirectX::Colors::Orchid; + const SHColour SHColour::PINK = DirectX::Colors::Pink; + const SHColour SHColour::PURPLE = DirectX::Colors::Purple; + const SHColour SHColour::RED = DirectX::Colors::Red; + const SHColour SHColour::ROYALBLUE = DirectX::Colors::RoyalBlue; + const SHColour SHColour::SALMON = DirectX::Colors::Salmon; + const SHColour SHColour::SANDYBROWN = DirectX::Colors::SandyBrown; + const SHColour SHColour::SILVER = DirectX::Colors::Silver; + const SHColour SHColour::SKYBLUE = DirectX::Colors::SkyBlue; + const SHColour SHColour::SLATEGRAY = DirectX::Colors::SlateGray; + const SHColour SHColour::SNOW = DirectX::Colors::Snow; + const SHColour SHColour::STEELBLUE = DirectX::Colors::SteelBlue; + const SHColour SHColour::TAN = DirectX::Colors::Tan; + const SHColour SHColour::TEAL = DirectX::Colors::Teal; + const SHColour SHColour::TURQUOISE = DirectX::Colors::Turquoise; + const SHColour SHColour::VIOLET = DirectX::Colors::Violet; + const SHColour SHColour::WHITE = DirectX::Colors::White; + const SHColour SHColour::YELLOW = DirectX::Colors::Yellow; + + /*-----------------------------------------------------------------------------------*/ + /* Constructors & Destructor Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHColour::SHColour() noexcept + : SHVec4 { 0.0f, 0.0f, 0.0f, 1.0f } + {} + + SHColour::SHColour(float r, float g, float b) noexcept + : SHVec4 { r, g, b, 1.0f } + {} + + SHColour::SHColour(float r, float g, float b, float a) noexcept + : SHVec4 { r, g, b, a } + {} + + SHColour::SHColour(uint8_t r, uint8_t g, uint8_t b) noexcept + : SHVec4 + { + static_cast(r) / 255.0f, + static_cast(g) / 255.0f, + static_cast(b) / 255.0f, + 1.0f + } + {} + + SHColour::SHColour(uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept + : SHVec4 + { + static_cast(r) / 255.0f, + static_cast(g) / 255.0f, + static_cast(b) / 255.0f, + static_cast(a) / 255.0f + } + {} + + SHColour::SHColour(const DirectX::XMFLOAT3& colour) noexcept + : SHVec4 { colour.x, colour.y, colour.z, 1.0f } + {} + + SHColour::SHColour(const DirectX::XMVECTORF32& colour) noexcept + : SHVec4 + { + XMVectorGetX(colour), + XMVectorGetY(colour), + XMVectorGetZ(colour), + XMVectorGetW(colour) + } + {} + + /*-----------------------------------------------------------------------------------*/ + /* Operator Overload Definitions */ + /*-----------------------------------------------------------------------------------*/ + + bool SHColour::operator==(const SHColour& rhs) const noexcept + { + return XMColorEqual(*this, rhs); + } + + bool SHColour::operator!=(const SHColour& rhs) const noexcept + { + return XMColorNotEqual(*this, rhs); + } + + /*-----------------------------------------------------------------------------------*/ + /* Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHColourHSV SHColour::ToHSV() noexcept + { + SHColourHSV hsv; + + const float MIN = SHMath::Min({ x, y, z }); + const float MAX = SHMath::Max({ x, y, z }); + + hsv.v = MAX; + + const float DELTA = MAX - MIN; + hsv.s = (MAX != 0.0f) ? DELTA / MAX : 0.0f; + + static const float SIN_60 = sin(SHMath::DegreesToRadians(60.0f)); + + if (DELTA == 0.0f) + hsv.h = 0.0f; + else if (x == MAX) + hsv.h = (y - z) / DELTA; + else if (y == MAX) + hsv.h = 2.0f + (z - x) / DELTA; + else + hsv.h = 4.0f + (x - y) / DELTA; + + hsv.h *= 60; + if (hsv.h < 0.0f) + hsv.h += 360.f; + + return hsv; + } + + void SHColour::Negate() noexcept + { + XMStoreFloat4(this, XMColorNegative(*this)); + } + + void SHColour::Saturate() noexcept + { + XMStoreFloat4(this, XMVectorSaturate(*this)); + } + + void SHColour::AdjustSaturation(float saturation) noexcept + { + XMStoreFloat4(this, XMColorAdjustSaturation(*this, saturation)); + } + + void SHColour::AdjustContrast(float contrast) noexcept + { + XMStoreFloat4(this, XMColorAdjustContrast(*this, contrast)); + } + + /*-----------------------------------------------------------------------------------*/ + /* Static Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHColour SHColour::Modulate(const SHColour& lhs, const SHColour& rhs) noexcept + { + SHColour result; + XMStoreFloat4(&result, XMColorModulate(lhs, rhs)); + return result; + } + + SHColour SHColour::FromHSV(float hue, float saturation, float value) + { + if (hue < 0.0f || saturation < 0.0f || value < 0.0f) + throw std::invalid_argument("One or more of the hsv values are invalid!"); + + SHColour colour; + + if (saturation == 0.0f) + { + colour.x = colour.y = colour.z = value; + } + else + { + hue /= 60.0f; + + const int SECTOR = static_cast(hue); + + const float F = hue - static_cast(SECTOR); + const float P = value * (1.0f - saturation); + const float Q = value * (1.0f - saturation * F); + const float T = value * (1.0f - saturation * (1.0f - F)); + + switch (SECTOR) + { + case 0: + { + colour.x = value; + colour.y = T; + colour.z = P; + + break; + } + case 1: + { + colour.x = Q; + colour.y = value; + colour.z = P; + + break; + } + case 2: + { + colour.x = P; + colour.y = value; + colour.z = T; + + break; + } + case 3: + { + colour.x = P; + colour.y = Q; + colour.z = value; + + break; + } + case 4: + { + colour.x = T; + colour.y = P; + colour.z = value; + + break; + } + default: + { + colour.x = value; + colour.y = P; + colour.z = Q; + + break; + } + } + } + + return colour; + } + + SHColour SHColour::FromHSV(const SHColourHSV& hsv) + { + return FromHSV(hsv.h, hsv.s, hsv.v); + } + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/SHColour.h b/SHADE_Engine/src/Math/SHColour.h new file mode 100644 index 00000000..bd2bc9e7 --- /dev/null +++ b/SHADE_Engine/src/Math/SHColour.h @@ -0,0 +1,166 @@ +/**************************************************************************************** + * \file SHColour.h + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Interface for a Colour. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#pragma once + +#include + +// Project Headers +#include "SH_API.h" +#include "Vector/SHVec3.h" +#include "Vector/SHVec4.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Forward Declarations */ + /*-----------------------------------------------------------------------------------*/ + + class SHColour; + + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + + struct SH_API SHColourHSV + { + /*---------------------------------------------------------------------------------*/ + /* Data Members */ + /*---------------------------------------------------------------------------------*/ + + float h = 0.0f; + float s = 0.0f; + float v = 0.0f; + }; + + class SH_API SHColour : private SHVec4 + { + public: + /*---------------------------------------------------------------------------------*/ + /* Constructors & Destructor */ + /*---------------------------------------------------------------------------------*/ + + SHColour () noexcept; + SHColour (float r, float g, float b) noexcept; + SHColour (float r, float g, float b, float a) noexcept; + SHColour (uint8_t r, uint8_t g, uint8_t b) noexcept; + SHColour (uint8_t r, uint8_t g, uint8_t b, uint8_t a) noexcept; + + SHColour (const SHVec3& colour) noexcept; + SHColour (const DirectX::XMFLOAT3& colour) noexcept; + SHColour (const DirectX::XMVECTORF32& colour) noexcept; + + SHColour (const SHColour&) = default; + SHColour (SHColour&&) = default; + + ~SHColour () = default; + + /*---------------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*---------------------------------------------------------------------------------*/ + + SHColour& operator= (const SHColour&) = default; + SHColour& operator= (SHColour&&) = default; + + bool operator== (const SHColour& rhs) const noexcept; + bool operator!= (const SHColour& rhs) const noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Properties */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] inline float& r() noexcept { return x; } + [[nodiscard]] inline float& g() noexcept { return y; } + [[nodiscard]] inline float& b() noexcept { return z; } + [[nodiscard]] inline float& a() noexcept { return w; } + + /*---------------------------------------------------------------------------------*/ + /* Function Members */ + /*---------------------------------------------------------------------------------*/ + + SHColourHSV ToHSV () noexcept; + + void Negate () noexcept; + void Saturate () noexcept; + + void AdjustSaturation (float saturation) noexcept; + void AdjustContrast (float contrast) noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Static Function Members */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] static SHColour Modulate (const SHColour& lhs, const SHColour& rhs) noexcept; + [[nodiscard]] static SHColour FromHSV (float hue, float saturation, float value); + [[nodiscard]] static SHColour FromHSV (const SHColourHSV& hsv); + + /*---------------------------------------------------------------------------------*/ + /* Static Data Members */ + /*---------------------------------------------------------------------------------*/ + + static const SHColour BEIGE ; + static const SHColour BLACK ; + static const SHColour BLUE ; + static const SHColour BROWN ; + static const SHColour CHOCOLATE ; + static const SHColour CORAL ; + static const SHColour CRIMSON ; + static const SHColour CYAN ; + static const SHColour DARKBLUE ; + static const SHColour DARKGRAY ; + static const SHColour DARKGREEN ; + static const SHColour DARKMAGENTA ; + static const SHColour DARKORANGE ; + static const SHColour DARKRED ; + static const SHColour DEEPPINK ; + static const SHColour FORESTGREEN ; + static const SHColour FUCHSIA ; + static const SHColour GOLD ; + static const SHColour GRAY ; + static const SHColour GREEN ; + static const SHColour HOTPINK ; + static const SHColour INDIGO ; + static const SHColour LAVENDER ; + static const SHColour LIGHTBLUE ; + static const SHColour LIGHTGRAY ; + static const SHColour LIGHTGREEN ; + static const SHColour LIGHTPINK ; + static const SHColour LIGHTYELLOW ; + static const SHColour LIME ; + static const SHColour LIMEGREEN ; + static const SHColour MAGENTA ; + static const SHColour MAROON ; + static const SHColour MEDIUMBLUE ; + static const SHColour MEDIUMPURPLE; + static const SHColour NAVY ; + static const SHColour OLIVE ; + static const SHColour ORANGE ; + static const SHColour ORCHID ; + static const SHColour PINK ; + static const SHColour PURPLE ; + static const SHColour RED ; + static const SHColour ROYALBLUE ; + static const SHColour SALMON ; + static const SHColour SANDYBROWN ; + static const SHColour SILVER ; + static const SHColour SKYBLUE ; + static const SHColour SLATEGRAY ; + static const SHColour SNOW ; + static const SHColour STEELBLUE ; + static const SHColour TAN ; + static const SHColour TEAL ; + static const SHColour TURQUOISE ; + static const SHColour VIOLET ; + static const SHColour WHITE ; + static const SHColour YELLOW; + }; + + +} // namespace SHADE diff --git a/SHADE_Engine/src/Math/Vector/SHVec3.cpp b/SHADE_Engine/src/Math/Vector/SHVec3.cpp index 45fe4f61..c248ebce 100644 --- a/SHADE_Engine/src/Math/Vector/SHVec3.cpp +++ b/SHADE_Engine/src/Math/Vector/SHVec3.cpp @@ -23,6 +23,7 @@ namespace SHADE /*-----------------------------------------------------------------------------------*/ /* Static Data Member Definitions */ /*-----------------------------------------------------------------------------------*/ + SHVec3 const SHVec3::Zero { 0.0f, 0.0f, 0.0f }; SHVec3 const SHVec3::One { 1.0f, 1.0f, 1.0f }; SHVec3 const SHVec3::Left { -1.0f, 0.0f, 0.0f }; From e1fb92e3f1ae9d0d760462bbe0b2432b749cac68 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Thu, 13 Oct 2022 17:32:53 +0800 Subject: [PATCH 8/8] Adding Bounding Spheres --- .../src/Math/Geometry/SHBoundingBox.cpp | 17 +- .../src/Math/Geometry/SHBoundingBox.h | 4 +- .../src/Math/Geometry/SHBoundingSphere.cpp | 183 ++++++++++++++++++ .../src/Math/Geometry/SHBoundingSphere.h | 83 ++++++++ SHADE_Engine/src/Math/Geometry/SHShape.h | 3 +- SHADE_Engine/src/Physics/SHPhysicsSystem.cpp | 2 +- 6 files changed, 277 insertions(+), 15 deletions(-) create mode 100644 SHADE_Engine/src/Math/Geometry/SHBoundingSphere.cpp create mode 100644 SHADE_Engine/src/Math/Geometry/SHBoundingSphere.h diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp index 11a14eed..0dd02caa 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.cpp @@ -26,12 +26,12 @@ namespace SHADE SHBoundingBox::SHBoundingBox() noexcept { - type = Type::BOUNDING_BOX; + type = Type::BOX; } SHBoundingBox::SHBoundingBox(const SHVec3& c, const SHVec3& hE) noexcept { - type = Type::BOUNDING_BOX; + type = Type::BOX; Center = c; Extents = hE; @@ -43,7 +43,7 @@ namespace SHADE if (this == &rhs) return; - type = Type::BOUNDING_BOX; + type = Type::BOX; Center = rhs.Center; Extents = rhs.Extents; @@ -51,7 +51,7 @@ namespace SHADE SHBoundingBox::SHBoundingBox(SHBoundingBox&& rhs) noexcept { - type = Type::BOUNDING_BOX; + type = Type::BOX; Center = rhs.Center; Extents = rhs.Extents; @@ -63,14 +63,11 @@ namespace SHADE SHBoundingBox& SHBoundingBox::operator=(const SHBoundingBox& rhs) noexcept { - if (this == &rhs) - return *this; - - if (rhs.type != Type::BOUNDING_BOX) + if (rhs.type != Type::BOX) { SHLOG_WARNING("Cannot assign a non-bounding box to a bounding box!") } - else + else if (this != &rhs) { Center = rhs.Center; Extents = rhs.Extents; @@ -81,7 +78,7 @@ namespace SHADE SHBoundingBox& SHBoundingBox::operator=(SHBoundingBox&& rhs) noexcept { - if (rhs.type != Type::BOUNDING_BOX) + if (rhs.type != Type::BOX) { SHLOG_WARNING("Cannot assign a non-bounding box to a bounding box!") } diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h index 5c38e1b8..2fdc6634 100644 --- a/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingBox.h @@ -36,7 +36,7 @@ namespace SHADE /* Constructors & Destructor */ /*---------------------------------------------------------------------------------*/ - ~SHBoundingBox () noexcept = default; + ~SHBoundingBox () override = default; SHBoundingBox () noexcept; SHBoundingBox (const SHVec3& center, const SHVec3& halfExtents) noexcept; @@ -44,7 +44,7 @@ namespace SHADE SHBoundingBox (SHBoundingBox&& rhs) noexcept; /*---------------------------------------------------------------------------------*/ - /* Operator Overloads */ + /* Operator Overloads */ /*---------------------------------------------------------------------------------*/ SHBoundingBox& operator= (const SHBoundingBox& rhs) noexcept; diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.cpp b/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.cpp new file mode 100644 index 00000000..c9bdcfe2 --- /dev/null +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.cpp @@ -0,0 +1,183 @@ +/**************************************************************************************** + * \file SHBoundingSphere.cpp + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Implementation for a Bounding Sphere + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#include + +// Primary Header +#include "SHBoundingSphere.h" +// Project Headers +#include "Math/SHMathHelpers.h" +#include "Math/SHRay.h" + +using namespace DirectX; + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Constructors & Destructor Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHBoundingSphere::SHBoundingSphere() noexcept + { + type = Type::SPHERE; + } + + SHBoundingSphere::SHBoundingSphere(const SHVec3& center, float radius) noexcept + { + type = Type::SPHERE; + + Center = center; + Radius = radius; + } + + SHBoundingSphere::SHBoundingSphere(const SHBoundingSphere& rhs) noexcept + { + if (this == &rhs) + return; + + type = Type::SPHERE; + + Center = rhs.Center; + Radius = rhs.Radius; + } + + SHBoundingSphere::SHBoundingSphere(SHBoundingSphere&& rhs) noexcept + { + type = Type::SPHERE; + + Center = rhs.Center; + Radius = rhs.Radius; + } + + /*-----------------------------------------------------------------------------------*/ + /* Operator Overload Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHBoundingSphere& SHBoundingSphere::operator=(const SHBoundingSphere& rhs) noexcept + { + if (rhs.type != Type::SPHERE) + { + SHLOG_WARNING("Cannot assign a non-sphere to a sphere!") + } + else if (this != &rhs) + { + Center = rhs.Center; + Radius = rhs.Radius; + } + + return *this; + } + + SHBoundingSphere& SHBoundingSphere::operator=(SHBoundingSphere&& rhs) noexcept + { + if (rhs.type != Type::SPHERE) + { + SHLOG_WARNING("Cannot assign a non-sphere to a sphere!") + } + else + { + Center = rhs.Center; + Radius = rhs.Radius; + } + + return *this; + } + + /*-----------------------------------------------------------------------------------*/ + /* Getter Function Definitions */ + /*-----------------------------------------------------------------------------------*/ + + const SHVec3& SHBoundingSphere::GetCenter() const noexcept + { + return Center; + } + + float SHBoundingSphere::GetRadius() const noexcept + { + return Radius; + } + + /*-----------------------------------------------------------------------------------*/ + /* Setter Function Definitions */ + /*-----------------------------------------------------------------------------------*/ + + void SHBoundingSphere::SetCenter(const SHVec3& center) noexcept + { + Center = center; + } + + void SHBoundingSphere::SetRadius(float radius) noexcept + { + Radius = radius; + } + + /*-----------------------------------------------------------------------------------*/ + /* Public Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + bool SHBoundingSphere::TestPoint(const SHVec3& point) noexcept + { + return BoundingSphere::Contains(point); + } + + bool SHBoundingSphere::Raycast(const SHRay& ray, float& distance) noexcept + { + return Intersects(ray.position, ray.direction, distance); + } + + bool SHBoundingSphere::Contains(const SHBoundingSphere& rhs) const noexcept + { + return BoundingSphere::Contains(rhs); + } + + float SHBoundingSphere::Volume() const noexcept + { + return (4.0f / 3.0f) * SHMath::PI * (Radius * Radius * Radius); + } + + float SHBoundingSphere::SurfaceArea() const noexcept + { + return 4.0f * SHMath::PI * (Radius * Radius); + } + + /*-----------------------------------------------------------------------------------*/ + /* Static Function Member Definitions */ + /*-----------------------------------------------------------------------------------*/ + + SHBoundingSphere SHBoundingSphere::Combine(const SHBoundingSphere& lhs, const SHBoundingSphere& rhs) noexcept + { + SHBoundingSphere result; + CreateMerged(result, lhs, rhs); + return result; + } + + bool SHBoundingSphere::Intersect(const SHBoundingSphere& lhs, const SHBoundingSphere& rhs) noexcept + { + return lhs.Intersects(rhs); + } + + SHBoundingSphere SHBoundingSphere::BuildFromSpheres(const SHBoundingSphere* spheres, size_t numSpheres) noexcept + { + SHBoundingSphere result; + + for (size_t i = 1; i < numSpheres; ++i) + CreateMerged(result, spheres[i - 1], spheres[i]); + + return result; + } + + SHBoundingSphere SHBoundingSphere::BuildFromVertices(const SHVec3* vertices, size_t numVertices, size_t stride) noexcept + { + SHBoundingSphere result; + CreateFromPoints(result, numVertices, vertices, stride); + return result; + } + +} // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.h b/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.h new file mode 100644 index 00000000..00ce3e57 --- /dev/null +++ b/SHADE_Engine/src/Math/Geometry/SHBoundingSphere.h @@ -0,0 +1,83 @@ +/**************************************************************************************** + * \file SHBoundingSphere.h + * \author Diren D Bharwani, diren.dbharwani, 390002520 + * \brief Interface for a Bounding Sphere. + * + * \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. +****************************************************************************************/ + +#pragma once + +#include + +// Project Headers +#include "SHShape.h" +#include "SH_API.h" + +namespace SHADE +{ + /*-----------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------------*/ + + class SH_API SHBoundingSphere : public SHShape, + private DirectX::BoundingSphere + { + public: + /*---------------------------------------------------------------------------------*/ + /* Constructors & Destructor */ + /*---------------------------------------------------------------------------------*/ + + SHBoundingSphere () noexcept; + SHBoundingSphere (const SHVec3& center, float radius) noexcept; + SHBoundingSphere (const SHBoundingSphere& rhs) noexcept; + SHBoundingSphere (SHBoundingSphere&& rhs) noexcept; + + ~SHBoundingSphere () override = default; + + /*---------------------------------------------------------------------------------*/ + /* Operator Overloads */ + /*---------------------------------------------------------------------------------*/ + + SHBoundingSphere& operator= (const SHBoundingSphere& rhs) noexcept; + SHBoundingSphere& operator= (SHBoundingSphere&& rhs) noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Getter Functions */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] const SHVec3& GetCenter () const noexcept; + [[nodiscard]] float GetRadius () const noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Setter Functions */ + /*---------------------------------------------------------------------------------*/ + + void SetCenter (const SHVec3& center) noexcept; + void SetRadius (float radius) noexcept; + + /*---------------------------------------------------------------------------------*/ + /* Function Members */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] bool TestPoint (const SHVec3& point) noexcept override; + [[nodiscard]] bool Raycast (const SHRay& ray, float& distance) noexcept override; + + [[nodiscard]] bool Contains (const SHBoundingSphere& rhs) const noexcept; + [[nodiscard]] float Volume () const noexcept; + [[nodiscard]] float SurfaceArea () const noexcept; + + + /*---------------------------------------------------------------------------------*/ + /* Static Function Members */ + /*---------------------------------------------------------------------------------*/ + + [[nodiscard]] static SHBoundingSphere Combine (const SHBoundingSphere& lhs, const SHBoundingSphere& rhs) noexcept; + [[nodiscard]] static bool Intersect (const SHBoundingSphere& lhs, const SHBoundingSphere& rhs) noexcept; + [[nodiscard]] static SHBoundingSphere BuildFromSpheres (const SHBoundingSphere* spheres, size_t numSpheres) noexcept; + [[nodiscard]] static SHBoundingSphere BuildFromVertices (const SHVec3* vertices, size_t numVertices, size_t stride = 0) noexcept; + + }; +} // namespace SHADE diff --git a/SHADE_Engine/src/Math/Geometry/SHShape.h b/SHADE_Engine/src/Math/Geometry/SHShape.h index 69578cb3..ab2db558 100644 --- a/SHADE_Engine/src/Math/Geometry/SHShape.h +++ b/SHADE_Engine/src/Math/Geometry/SHShape.h @@ -31,11 +31,10 @@ namespace SHADE enum class Type { - BOUNDING_BOX + BOX , SPHERE , CAPSULE , CONVEX_HULL - , TRIANGLE , COUNT , NONE = -1 diff --git a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp index f3a4c276..6f3cadb6 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp @@ -398,7 +398,7 @@ namespace SHADE switch (shape->GetType()) { - case SHShape::Type::BOUNDING_BOX: + case SHShape::Type::BOX: { auto* box = reinterpret_cast(shape); const SHVec3& SHADE_EXTENTS = box->GetHalfExtents();