Merge pull request #85 from SHADE-DP/SP3-2-Physics
SP3-2 Cleaned Up Physics System NEW Added Sphere Collider UPDATES Reworked Physics System and Cleaned up Interface Added Support for multiple colliders & collider offsets
This commit is contained in:
commit
28ec02afee
|
@ -104,8 +104,10 @@ namespace Sandbox
|
||||||
transform.SetWorldRotation(SHMath::GenerateRandomNumber(), SHMath::GenerateRandomNumber(), SHMath::GenerateRandomNumber());
|
transform.SetWorldRotation(SHMath::GenerateRandomNumber(), SHMath::GenerateRandomNumber(), SHMath::GenerateRandomNumber());
|
||||||
transform.SetWorldScale(TEST_OBJ_SCALE);
|
transform.SetWorldScale(TEST_OBJ_SCALE);
|
||||||
|
|
||||||
auto* box = collider.AddBoundingBox();
|
if (const bool IS_EVEN = (y * NUM_ROWS + x) % 2; IS_EVEN)
|
||||||
box->SetHalfExtents(transform.GetWorldScale() * 0.5f);
|
collider.AddBoundingBox(SHVec3::One * 0.5f, SHVec3::Zero);
|
||||||
|
else
|
||||||
|
collider.AddBoundingSphere(0.5f, SHVec3::Zero);
|
||||||
|
|
||||||
stressTestObjects.emplace_back(entity);
|
stressTestObjects.emplace_back(entity);
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,12 +95,12 @@ namespace SHADE
|
||||||
/* Getter Function Definitions */
|
/* Getter Function Definitions */
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
const SHVec3& SHBoundingBox::GetCenter() const noexcept
|
SHVec3 SHBoundingBox::GetCenter() const noexcept
|
||||||
{
|
{
|
||||||
return Center;
|
return Center;
|
||||||
}
|
}
|
||||||
|
|
||||||
const SHVec3& SHBoundingBox::GetHalfExtents() const noexcept
|
SHVec3 SHBoundingBox::GetHalfExtents() const noexcept
|
||||||
{
|
{
|
||||||
return Extents;
|
return Extents;
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,8 +54,8 @@ namespace SHADE
|
||||||
/* Getter Functions */
|
/* Getter Functions */
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
[[nodiscard]] const SHVec3& GetCenter () const noexcept;
|
[[nodiscard]] SHVec3 GetCenter () const noexcept;
|
||||||
[[nodsicard]] const SHVec3& GetHalfExtents() const noexcept;
|
[[nodiscard]] SHVec3 GetHalfExtents() const noexcept;
|
||||||
[[nodiscard]] SHVec3 GetMin () const noexcept;
|
[[nodiscard]] SHVec3 GetMin () const noexcept;
|
||||||
[[nodiscard]] SHVec3 GetMax () const noexcept;
|
[[nodiscard]] SHVec3 GetMax () const noexcept;
|
||||||
[[nodiscard]] std::vector<SHVec3> GetVertices () const noexcept;
|
[[nodiscard]] std::vector<SHVec3> GetVertices () const noexcept;
|
||||||
|
|
|
@ -94,7 +94,7 @@ namespace SHADE
|
||||||
/* Getter Function Definitions */
|
/* Getter Function Definitions */
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
const SHVec3& SHBoundingSphere::GetCenter() const noexcept
|
SHVec3 SHBoundingSphere::GetCenter() const noexcept
|
||||||
{
|
{
|
||||||
return Center;
|
return Center;
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,8 +48,8 @@ namespace SHADE
|
||||||
/* Getter Functions */
|
/* Getter Functions */
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
[[nodiscard]] const SHVec3& GetCenter () const noexcept;
|
[[nodiscard]] SHVec3 GetCenter () const noexcept;
|
||||||
[[nodiscard]] float GetRadius () const noexcept;
|
[[nodiscard]] float GetRadius () const noexcept;
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
/* Setter Functions */
|
/* Setter Functions */
|
||||||
|
|
|
@ -12,7 +12,6 @@
|
||||||
|
|
||||||
// Project Headers
|
// Project Headers
|
||||||
#include "SH_API.h"
|
#include "SH_API.h"
|
||||||
#include "Math/Transform/SHTransform.h"
|
|
||||||
#include "Math/SHRay.h"
|
#include "Math/SHRay.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -67,6 +67,19 @@ namespace SHADE
|
||||||
XMStoreFloat4(this, XMQuaternionRotationMatrix(M));
|
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 */
|
/* Operator Overload Definitions */
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
@ -176,6 +189,16 @@ namespace SHADE
|
||||||
return XMQuaternionNotEqual(Q1, Q2);
|
return XMQuaternionNotEqual(Q1, Q2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SHQuaternion::operator reactphysics3d::Quaternion() const noexcept
|
||||||
|
{
|
||||||
|
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
|
SHQuaternion operator*(float lhs, const SHQuaternion& rhs) noexcept
|
||||||
{
|
{
|
||||||
return rhs * lhs;
|
return rhs * lhs;
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <DirectXMath.h>
|
#include <DirectXMath.h>
|
||||||
|
#include <reactphysics3d/mathematics/Quaternion.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
// Project Headers
|
// Project Headers
|
||||||
|
@ -46,12 +48,17 @@ namespace SHADE
|
||||||
SHQuaternion (const SHQuaternion& rhs) = default;
|
SHQuaternion (const SHQuaternion& rhs) = default;
|
||||||
SHQuaternion (SHQuaternion&& rhs) = default;
|
SHQuaternion (SHQuaternion&& rhs) = default;
|
||||||
|
|
||||||
SHQuaternion () noexcept;
|
SHQuaternion () noexcept;
|
||||||
SHQuaternion (float x, float y, float z, float w) noexcept;
|
SHQuaternion (float x, float y, float z, float w) noexcept;
|
||||||
SHQuaternion (float yaw, float pitch, float roll) noexcept;
|
SHQuaternion (float yaw, float pitch, float roll) noexcept;
|
||||||
SHQuaternion (const SHVec3& eulerAngles) noexcept;
|
SHQuaternion (const SHVec3& eulerAngles) noexcept;
|
||||||
SHQuaternion (const SHVec3& axis, float angleInRad) noexcept;
|
SHQuaternion (const SHVec3& axis, float angleInRad) noexcept;
|
||||||
SHQuaternion (const SHMatrix& rotationMatrix) 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 */
|
/* Operator Overloads */
|
||||||
|
@ -76,6 +83,11 @@ namespace SHADE
|
||||||
[[nodiscard]] bool operator== (const SHQuaternion& rhs) const noexcept;
|
[[nodiscard]] bool operator== (const SHQuaternion& rhs) const noexcept;
|
||||||
[[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;
|
||||||
|
operator reactphysics3d::Vector3 () const noexcept;
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
/* Getter Functions */
|
/* Getter Functions */
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
|
@ -50,6 +50,10 @@ namespace SHADE
|
||||||
: XMFLOAT2( _x, _y )
|
: XMFLOAT2( _x, _y )
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
SHVec2::SHVec2(const reactphysics3d::Vector2& rp3dVec2) noexcept
|
||||||
|
: XMFLOAT2( rp3dVec2.x, rp3dVec2.y )
|
||||||
|
{}
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
/* Operator Overload Definitions */
|
/* Operator Overload Definitions */
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
@ -200,6 +204,11 @@ namespace SHADE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SHVec2::operator reactphysics3d::Vector2() const noexcept
|
||||||
|
{
|
||||||
|
return reactphysics3d::Vector2{ x, y };
|
||||||
|
}
|
||||||
|
|
||||||
SHVec2 operator* (float lhs, const SHVec2& rhs) noexcept
|
SHVec2 operator* (float lhs, const SHVec2& rhs) noexcept
|
||||||
{
|
{
|
||||||
SHVec2 result;
|
SHVec2 result;
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <DirectXMath.h>
|
#include <DirectXMath.h>
|
||||||
|
#include <reactphysics3d/mathematics/Vector2.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
|
|
||||||
|
@ -57,6 +59,10 @@ namespace SHADE
|
||||||
SHVec2 (float n) noexcept;
|
SHVec2 (float n) noexcept;
|
||||||
SHVec2 (float x, float y) noexcept;
|
SHVec2 (float x, float y) noexcept;
|
||||||
|
|
||||||
|
// Conversion from other math types to SHADE
|
||||||
|
|
||||||
|
SHVec2 (const reactphysics3d::Vector2& rp3dVec2) noexcept;
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
/* Operator Overloads */
|
/* Operator Overloads */
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
@ -64,7 +70,10 @@ namespace SHADE
|
||||||
SHVec2& operator= (const SHVec2& rhs) = default;
|
SHVec2& operator= (const SHVec2& rhs) = default;
|
||||||
SHVec2& operator= (SHVec2&& rhs) = default;
|
SHVec2& operator= (SHVec2&& rhs) = default;
|
||||||
|
|
||||||
|
// Conversion to other math types used by SHADE
|
||||||
|
|
||||||
operator DirectX::XMVECTOR () const noexcept;
|
operator DirectX::XMVECTOR () const noexcept;
|
||||||
|
operator reactphysics3d::Vector2 () const noexcept;
|
||||||
|
|
||||||
SHVec2& operator+= (const SHVec2& rhs) noexcept;
|
SHVec2& operator+= (const SHVec2& rhs) noexcept;
|
||||||
SHVec2& operator-= (const SHVec2& rhs) noexcept;
|
SHVec2& operator-= (const SHVec2& rhs) noexcept;
|
||||||
|
@ -89,6 +98,7 @@ namespace SHADE
|
||||||
[[nodiscard]] float operator[] (int index) const;
|
[[nodiscard]] float operator[] (int index) const;
|
||||||
[[nodiscard]] float operator[] (size_t index) const;
|
[[nodiscard]] float operator[] (size_t index) const;
|
||||||
|
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
/* Function Members */
|
/* Function Members */
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include "SHVec3.h"
|
#include "SHVec3.h"
|
||||||
// Project Headers
|
// Project Headers
|
||||||
#include "Math/SHMatrix.h"
|
#include "Math/SHMatrix.h"
|
||||||
|
#include "Math/SHQuaternion.h"
|
||||||
#include "Tools/SHLogger.h"
|
#include "Tools/SHLogger.h"
|
||||||
|
|
||||||
using namespace DirectX;
|
using namespace DirectX;
|
||||||
|
@ -56,6 +57,14 @@ namespace SHADE
|
||||||
: XMFLOAT3( _x, _y, _z )
|
: XMFLOAT3( _x, _y, _z )
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
SHVec3::SHVec3(const reactphysics3d::Vector3& rp3dVec3) noexcept
|
||||||
|
: XMFLOAT3( rp3dVec3.x, rp3dVec3.y, rp3dVec3.z )
|
||||||
|
{}
|
||||||
|
|
||||||
|
SHVec3::SHVec3(const reactphysics3d::Quaternion& rp3dVec3) noexcept
|
||||||
|
: XMFLOAT3( SHQuaternion{rp3dVec3}.ToEuler() )
|
||||||
|
{}
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
/* Operator Overload Definitions */
|
/* Operator Overload Definitions */
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
@ -212,6 +221,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 operator* (float lhs, const SHVec3& rhs) noexcept
|
||||||
{
|
{
|
||||||
SHVec3 result;
|
SHVec3 result;
|
||||||
|
|
|
@ -11,6 +11,9 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <DirectXMath.h>
|
#include <DirectXMath.h>
|
||||||
|
#include <reactphysics3d/mathematics/Vector3.h>
|
||||||
|
#include <reactphysics3d/mathematics/Quaternion.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
|
|
||||||
|
@ -62,38 +65,46 @@ namespace SHADE
|
||||||
SHVec3 (float n) noexcept;
|
SHVec3 (float n) noexcept;
|
||||||
SHVec3 (float x, float y, float z) 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::Quaternion& rp3dVec3) noexcept;
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
/* Operator Overloads */
|
/* Operator Overloads */
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
SHVec3& operator= (const SHVec3& rhs) = default;
|
SHVec3& operator= (const SHVec3& rhs) = default;
|
||||||
SHVec3& operator= (SHVec3&& rhs) = default;
|
SHVec3& operator= (SHVec3&& rhs) = default;
|
||||||
|
|
||||||
operator DirectX::XMVECTOR () const noexcept;
|
// Conversion to other math types used by SHADE
|
||||||
|
|
||||||
SHVec3& operator+= (const SHVec3& rhs) noexcept;
|
operator reactphysics3d::Vector3 () const noexcept;
|
||||||
SHVec3& operator-= (const SHVec3& rhs) noexcept;
|
operator reactphysics3d::Quaternion () const noexcept;
|
||||||
SHVec3& operator*= (const SHVec3& rhs) noexcept;
|
operator DirectX::XMVECTOR () const 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;
|
SHVec3& operator+= (const SHVec3& rhs) noexcept;
|
||||||
[[nodiscard]] SHVec3 operator- (const SHVec3& rhs) const noexcept;
|
SHVec3& operator-= (const SHVec3& rhs) noexcept;
|
||||||
[[nodiscard]] SHVec3 operator- () const noexcept;
|
SHVec3& operator*= (const SHVec3& rhs) noexcept;
|
||||||
[[nodiscard]] SHVec3 operator* (const SHVec3& rhs) const noexcept;
|
SHVec3& operator*= (float rhs) noexcept;
|
||||||
[[nodiscard]] SHVec3 operator* (float rhs) const noexcept;
|
SHVec3& operator/= (const SHVec3& rhs) noexcept;
|
||||||
[[nodiscard]] SHVec3 operator/ (const SHVec3& rhs) const noexcept;
|
SHVec3& operator/= (float rhs) noexcept;
|
||||||
[[nodiscard]] SHVec3 operator/ (float rhs) const noexcept;
|
|
||||||
|
|
||||||
[[nodiscard]] bool operator== (const SHVec3& rhs) const noexcept;
|
[[nodiscard]] SHVec3 operator+ (const SHVec3& rhs) const noexcept;
|
||||||
[[nodiscard]] bool 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]] float& operator[] (int index);
|
[[nodiscard]] bool operator== (const SHVec3& rhs) const noexcept;
|
||||||
[[nodiscard]] float& operator[] (size_t index);
|
[[nodiscard]] bool operator!= (const SHVec3& rhs) const noexcept;
|
||||||
[[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;
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
/* Function Members */
|
/* Function Members */
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
|
|
||||||
// Project Headers
|
// Project Headers
|
||||||
#include "ECS_Base/Managers/SHSystemManager.h"
|
#include "ECS_Base/Managers/SHSystemManager.h"
|
||||||
|
#include "Math/SHMathHelpers.h"
|
||||||
#include "Physics/SHPhysicsSystem.h"
|
#include "Physics/SHPhysicsSystem.h"
|
||||||
|
|
||||||
namespace SHADE
|
namespace SHADE
|
||||||
|
@ -24,7 +25,8 @@ namespace SHADE
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
SHColliderComponent::SHColliderComponent() noexcept
|
SHColliderComponent::SHColliderComponent() noexcept
|
||||||
: system { nullptr }
|
: system { nullptr }
|
||||||
|
, colliders {}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
@ -87,12 +89,17 @@ namespace SHADE
|
||||||
system->RemoveCollider(GetEID());
|
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 TYPE = SHCollider::Type::BOX;
|
||||||
|
|
||||||
const auto BOX_PAIR = std::make_pair(SHCollider{TYPE}, true);
|
auto boxPair = std::make_pair(SHCollider{TYPE}, true);
|
||||||
auto& collider = colliders.emplace_back(BOX_PAIR).first;
|
auto& collider = colliders.emplace_back(boxPair).first;
|
||||||
|
|
||||||
|
const auto* tf = SHComponentManager::GetComponent<SHTransformComponent>(GetEID());
|
||||||
|
|
||||||
|
collider.SetPositionOffset(posOffset);
|
||||||
|
collider.SetAsBoundingBox(tf->GetWorldScale() * halfExtents);
|
||||||
|
|
||||||
if (!system)
|
if (!system)
|
||||||
{
|
{
|
||||||
|
@ -101,23 +108,37 @@ namespace SHADE
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify Physics System
|
// Notify Physics System
|
||||||
system->AddCollisionShape(GetEID(), collider.GetShape());
|
system->AddCollisionShape(GetEID(), &collider);
|
||||||
|
|
||||||
return reinterpret_cast<SHBoundingBox*>(collider.GetShape());
|
return reinterpret_cast<SHBoundingBox*>(collider.GetShape());
|
||||||
}
|
}
|
||||||
|
|
||||||
//void SHColliderComponent::AddSphere() noexcept
|
SHBoundingSphere* SHColliderComponent::AddBoundingSphere(float radius, const SHVec3& posOffset) noexcept
|
||||||
//{
|
{
|
||||||
// const auto TYPE = SHCollider::Type::SPHERE;
|
const auto TYPE = SHCollider::Type::SPHERE;
|
||||||
|
|
||||||
// if (!system)
|
auto spherePair = std::make_pair(SHCollider{ TYPE }, true);
|
||||||
// {
|
auto& collider = colliders.emplace_back(spherePair).first;
|
||||||
// SHLOG_ERROR("Physics system does not exist, unable to add Sphere Collider!")
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Notify Physics System
|
const auto* tf = SHComponentManager::GetComponent<SHTransformComponent>(GetEID());
|
||||||
//}
|
|
||||||
|
collider.SetPositionOffset(posOffset);
|
||||||
|
|
||||||
|
const SHVec3 TF_WORLD_SCALE = tf->GetWorldScale();
|
||||||
|
const float MAX_SCALE = SHMath::Max({ TF_WORLD_SCALE.x, TF_WORLD_SCALE.y, TF_WORLD_SCALE.z });
|
||||||
|
collider.SetAsBoundingSphere(MAX_SCALE * 0.5f);
|
||||||
|
|
||||||
|
if (!system)
|
||||||
|
{
|
||||||
|
SHLOG_ERROR("Physics system does not exist, unable to add Sphere Collider!")
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify Physics System
|
||||||
|
system->AddCollisionShape(GetEID(), &collider);
|
||||||
|
|
||||||
|
return reinterpret_cast<SHBoundingSphere*>(collider.GetShape());
|
||||||
|
}
|
||||||
|
|
||||||
void SHColliderComponent::RemoveCollider(int index)
|
void SHColliderComponent::RemoveCollider(int index)
|
||||||
{
|
{
|
||||||
|
|
|
@ -15,6 +15,13 @@
|
||||||
// Project Headers
|
// Project Headers
|
||||||
#include "ECS_Base/Components/SHComponent.h"
|
#include "ECS_Base/Components/SHComponent.h"
|
||||||
#include "Physics/SHCollider.h"
|
#include "Physics/SHCollider.h"
|
||||||
|
#include "Math/Geometry/SHBoundingBox.h"
|
||||||
|
#include "Math/Geometry/SHBoundingSphere.h"
|
||||||
|
|
||||||
|
//namespace SHADE
|
||||||
|
//{
|
||||||
|
// class SHPhysicsSystem;
|
||||||
|
//}
|
||||||
|
|
||||||
namespace SHADE
|
namespace SHADE
|
||||||
{
|
{
|
||||||
|
@ -30,6 +37,7 @@ namespace SHADE
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
friend class SHPhysicsSystem;
|
friend class SHPhysicsSystem;
|
||||||
|
friend class SHPhysicsObject;
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
/* Type Definitions */
|
/* Type Definitions */
|
||||||
|
@ -76,10 +84,11 @@ namespace SHADE
|
||||||
void OnCreate () override;
|
void OnCreate () override;
|
||||||
void OnDestroy () override;
|
void OnDestroy () override;
|
||||||
|
|
||||||
SHBoundingBox* AddBoundingBox () noexcept;
|
|
||||||
|
|
||||||
void RemoveCollider (int index);
|
void RemoveCollider (int index);
|
||||||
|
|
||||||
|
SHBoundingBox* AddBoundingBox (const SHVec3& halfExtents = SHVec3::One, const SHVec3& posOffset = SHVec3::Zero) noexcept;
|
||||||
|
SHBoundingSphere* AddBoundingSphere (float radius = 1.0f, const SHVec3& posOffset = SHVec3::Zero) noexcept;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
|
@ -392,9 +392,9 @@ RTTR_REGISTRATION
|
||||||
|
|
||||||
registration::enumeration<SHRigidBodyComponent::Type>("RigidBody Type")
|
registration::enumeration<SHRigidBodyComponent::Type>("RigidBody Type")
|
||||||
(
|
(
|
||||||
value("Static", SHRigidBodyComponent::Type::STATIC),
|
value("Static", SHRigidBodyComponent::Type::STATIC),
|
||||||
value("Dynamic", SHRigidBodyComponent::Type::DYNAMIC),
|
value("Kinematic", SHRigidBodyComponent::Type::KINEMATIC),
|
||||||
value("Kinematic", SHRigidBodyComponent::Type::KINEMATIC)
|
value("Dynamic", SHRigidBodyComponent::Type::DYNAMIC)
|
||||||
);
|
);
|
||||||
|
|
||||||
registration::class_<SHRigidBodyComponent>("RigidBody Component")
|
registration::class_<SHRigidBodyComponent>("RigidBody Component")
|
||||||
|
|
|
@ -14,7 +14,13 @@
|
||||||
|
|
||||||
// Project Headers
|
// Project Headers
|
||||||
#include "ECS_Base/Components/SHComponent.h"
|
#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
|
namespace SHADE
|
||||||
{
|
{
|
||||||
|
@ -30,6 +36,7 @@ namespace SHADE
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
friend class SHPhysicsSystem;
|
friend class SHPhysicsSystem;
|
||||||
|
friend class SHPhysicsObject;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
@ -137,6 +144,9 @@ namespace SHADE
|
||||||
/* Data Members */
|
/* Data Members */
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
static constexpr size_t NUM_FLAGS = 8;
|
||||||
|
static constexpr size_t NUM_DIRTY_FLAGS = 16;
|
||||||
|
|
||||||
Type type;
|
Type type;
|
||||||
|
|
||||||
// rX rY rZ pX pY pZ slp g
|
// rX rY rZ pX pY pZ slp g
|
||||||
|
@ -162,10 +172,6 @@ namespace SHADE
|
||||||
SHVec3 position;
|
SHVec3 position;
|
||||||
SHQuaternion orientation;
|
SHQuaternion orientation;
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
|
||||||
/* Function Members */
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
RTTR_ENABLE()
|
RTTR_ENABLE()
|
||||||
};
|
};
|
||||||
} // namespace SHADE
|
} // namespace SHADE
|
|
@ -12,6 +12,9 @@
|
||||||
|
|
||||||
// Primary Header
|
// Primary Header
|
||||||
#include "SHCollider.h"
|
#include "SHCollider.h"
|
||||||
|
// Project Headers
|
||||||
|
#include "Math/Geometry/SHBoundingBox.h"
|
||||||
|
#include "Math/Geometry/SHBoundingSphere.h"
|
||||||
|
|
||||||
namespace SHADE
|
namespace SHADE
|
||||||
{
|
{
|
||||||
|
@ -25,34 +28,41 @@ namespace SHADE
|
||||||
, dirty { true }
|
, dirty { true }
|
||||||
, shape { nullptr }
|
, shape { nullptr }
|
||||||
{
|
{
|
||||||
CreateShape();
|
switch (type)
|
||||||
|
{
|
||||||
|
case Type::BOX:
|
||||||
|
{
|
||||||
|
SetAsBoundingBox(SHVec3::One);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Type::SPHERE:
|
||||||
|
{
|
||||||
|
SetAsBoundingSphere(1.0f);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SHCollider::SHCollider(const SHCollider& rhs) noexcept
|
SHCollider::SHCollider(const SHCollider& rhs) noexcept
|
||||||
: type { rhs.type}
|
: type { rhs.type}
|
||||||
, isTrigger { rhs.isTrigger }
|
, isTrigger { rhs.isTrigger }
|
||||||
, dirty { true }
|
, dirty { true }
|
||||||
, shape { nullptr }
|
, shape { rhs.shape }
|
||||||
{
|
, positionOffset { rhs.positionOffset }
|
||||||
CreateShape();
|
{}
|
||||||
|
|
||||||
// TODO(Diren): Copy transform data over
|
|
||||||
}
|
|
||||||
|
|
||||||
SHCollider::SHCollider(SHCollider&& rhs) noexcept
|
SHCollider::SHCollider(SHCollider&& rhs) noexcept
|
||||||
: type { rhs.type}
|
: type { rhs.type}
|
||||||
, isTrigger { rhs.isTrigger }
|
, isTrigger { rhs.isTrigger }
|
||||||
, dirty { true }
|
, dirty { true }
|
||||||
, shape { nullptr }
|
, shape { rhs.shape }
|
||||||
{
|
, positionOffset { rhs.positionOffset }
|
||||||
CreateShape();
|
{}
|
||||||
|
|
||||||
// TODO(Diren): Copy transform data over
|
|
||||||
}
|
|
||||||
|
|
||||||
SHCollider::~SHCollider() noexcept
|
SHCollider::~SHCollider() noexcept
|
||||||
{
|
{
|
||||||
delete shape;
|
shape = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
@ -61,26 +71,25 @@ namespace SHADE
|
||||||
|
|
||||||
SHCollider& SHCollider::operator=(const SHCollider& rhs) noexcept
|
SHCollider& SHCollider::operator=(const SHCollider& rhs) noexcept
|
||||||
{
|
{
|
||||||
type = rhs.type;
|
if (this == &rhs)
|
||||||
isTrigger = rhs.isTrigger;
|
return *this;
|
||||||
dirty = true;
|
|
||||||
|
|
||||||
CreateShape();
|
type = rhs.type;
|
||||||
|
isTrigger = rhs.isTrigger;
|
||||||
// TODO(Diren): Copy transform data over
|
dirty = true;
|
||||||
|
shape = rhs.shape;
|
||||||
|
positionOffset = rhs.positionOffset;
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
SHCollider& SHCollider::operator=(SHCollider&& rhs) noexcept
|
SHCollider& SHCollider::operator=(SHCollider&& rhs) noexcept
|
||||||
{
|
{
|
||||||
type = rhs.type;
|
type = rhs.type;
|
||||||
isTrigger = rhs.isTrigger;
|
isTrigger = rhs.isTrigger;
|
||||||
dirty = true;
|
dirty = true;
|
||||||
|
shape = rhs.shape;
|
||||||
CreateShape();
|
positionOffset = rhs.positionOffset;
|
||||||
|
|
||||||
// TODO(Diren): Copy transform data over
|
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -104,11 +113,6 @@ namespace SHADE
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
SHShape* SHCollider::GetShape() const noexcept
|
|
||||||
{
|
|
||||||
return shape;
|
|
||||||
}
|
|
||||||
|
|
||||||
float SHCollider::GetFriction() const noexcept
|
float SHCollider::GetFriction() const noexcept
|
||||||
{
|
{
|
||||||
// TODO(Diren): Fix after implementing materials
|
// TODO(Diren): Fix after implementing materials
|
||||||
|
@ -126,36 +130,37 @@ namespace SHADE
|
||||||
return 0.0f;
|
return 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
SHVec3 SHCollider::GetPosition() const noexcept
|
|
||||||
{
|
|
||||||
// TODO(Diren): Fix after linking transform data
|
|
||||||
return SHVec3::Zero;
|
|
||||||
}
|
|
||||||
|
|
||||||
const SHVec3& SHCollider::GetPositionOffset() const noexcept
|
const SHVec3& SHCollider::GetPositionOffset() const noexcept
|
||||||
{
|
{
|
||||||
return positionOffset;
|
return positionOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
SHQuaternion SHCollider::GetOrientation() const noexcept
|
SHShape* SHCollider::GetShape() noexcept
|
||||||
{
|
{
|
||||||
// TODO(Diren): Fix after linking transform data
|
dirty = true;
|
||||||
return SHQuaternion::Identity;
|
return shape;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
/* Setter Function Definitions */
|
/* Setter Function Definitions */
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
void SHCollider::SetType(Type newType) noexcept
|
void SHCollider::SetAsBoundingBox(const SHVec3& halfExtents)
|
||||||
{
|
{
|
||||||
if (type == newType)
|
|
||||||
return;
|
|
||||||
|
|
||||||
dirty = true;
|
dirty = true;
|
||||||
|
type = Type::BOX;
|
||||||
|
|
||||||
type = newType;
|
delete shape;
|
||||||
CreateShape();
|
shape = new SHBoundingBox{ positionOffset, halfExtents };
|
||||||
|
}
|
||||||
|
|
||||||
|
void SHCollider::SetAsBoundingSphere(float radius)
|
||||||
|
{
|
||||||
|
dirty = true;
|
||||||
|
type = Type::SPHERE;
|
||||||
|
|
||||||
|
delete shape;
|
||||||
|
shape = new SHBoundingSphere{ positionOffset, radius };
|
||||||
}
|
}
|
||||||
|
|
||||||
void SHCollider::SetIsTrigger(bool trigger) noexcept
|
void SHCollider::SetIsTrigger(bool trigger) noexcept
|
||||||
|
@ -184,32 +189,21 @@ namespace SHADE
|
||||||
dirty = true;
|
dirty = true;
|
||||||
positionOffset = posOffset;
|
positionOffset = posOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------------*/
|
|
||||||
/* Private Member Function Definitions */
|
|
||||||
/*-----------------------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
void SHCollider::CreateShape()
|
|
||||||
{
|
|
||||||
// Remove current shape
|
|
||||||
delete shape;
|
|
||||||
|
|
||||||
switch (type)
|
|
||||||
{
|
|
||||||
case Type::BOX: CreateBoundingBox(); break;
|
|
||||||
case Type::SPHERE: CreateSphere(); break;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SHCollider::CreateBoundingBox()
|
|
||||||
{
|
|
||||||
shape = new SHBoundingBox{ SHVec3::Zero, SHVec3::One };
|
|
||||||
}
|
|
||||||
|
|
||||||
void SHCollider::CreateSphere()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace SHADE
|
} // namespace SHADE
|
||||||
|
|
||||||
|
RTTR_REGISTRATION
|
||||||
|
{
|
||||||
|
using namespace SHADE;
|
||||||
|
using namespace rttr;
|
||||||
|
|
||||||
|
registration::enumeration<SHCollider::Type>("Collider Type")
|
||||||
|
(
|
||||||
|
value("Box", SHCollider::Type::BOX),
|
||||||
|
value("Sphere", SHCollider::Type::SPHERE)
|
||||||
|
// TODO(Diren): Add More Shapes
|
||||||
|
);
|
||||||
|
|
||||||
|
registration::class_<SHCollider>("Collider")
|
||||||
|
.property("Position Offset", &SHCollider::GetPositionOffset, &SHCollider::SetPositionOffset);
|
||||||
|
// TODO(Diren): Add Physics Materials
|
||||||
|
}
|
|
@ -10,8 +10,10 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <rttr/registration>
|
||||||
|
|
||||||
// Project Headers
|
// Project Headers
|
||||||
#include "Math/Geometry/SHBoundingBox.h"
|
#include "Math/Geometry/SHShape.h"
|
||||||
#include "Math/SHQuaternion.h"
|
#include "Math/SHQuaternion.h"
|
||||||
|
|
||||||
namespace SHADE
|
namespace SHADE
|
||||||
|
@ -32,15 +34,13 @@ namespace SHADE
|
||||||
BOX
|
BOX
|
||||||
, SPHERE
|
, SPHERE
|
||||||
, CAPSULE
|
, CAPSULE
|
||||||
, CONVEX_HULL
|
|
||||||
, CONVEX_MESH
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
/* Constructors & Destructor */
|
/* Constructors & Destructor */
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
SHCollider (Type colliderType);
|
SHCollider (Type colliderType = Type::BOX);
|
||||||
|
|
||||||
SHCollider (const SHCollider& rhs) noexcept;
|
SHCollider (const SHCollider& rhs) noexcept;
|
||||||
SHCollider (SHCollider&& rhs) noexcept;
|
SHCollider (SHCollider&& rhs) noexcept;
|
||||||
|
@ -62,28 +62,28 @@ namespace SHADE
|
||||||
[[nodiscard]] bool IsTrigger () const noexcept;
|
[[nodiscard]] bool IsTrigger () const noexcept;
|
||||||
|
|
||||||
[[nodiscard]] Type GetType () const noexcept;
|
[[nodiscard]] Type GetType () const noexcept;
|
||||||
[[nodiscard]] SHShape* GetShape () const noexcept;
|
|
||||||
|
|
||||||
[[nodiscard]] float GetFriction () const noexcept;
|
[[nodiscard]] float GetFriction () const noexcept;
|
||||||
[[nodiscard]] float GetBounciness () const noexcept;
|
[[nodiscard]] float GetBounciness () const noexcept;
|
||||||
[[nodiscard]] float GetDensity () const noexcept;
|
[[nodiscard]] float GetDensity () const noexcept;
|
||||||
|
|
||||||
[[nodiscard]] SHVec3 GetPosition () const noexcept;
|
|
||||||
[[nodiscard]] const SHVec3& GetPositionOffset () const noexcept;
|
[[nodiscard]] const SHVec3& GetPositionOffset () const noexcept;
|
||||||
[[nodiscard]] SHQuaternion GetOrientation () const noexcept;
|
|
||||||
|
[[nodiscard]] SHShape* GetShape () noexcept;
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
/* Setter Functions */
|
/* Setter Functions */
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
void SetType (Type newType) noexcept;
|
void SetAsBoundingBox (const SHVec3& halfExtents);
|
||||||
|
void SetAsBoundingSphere (float radius);
|
||||||
|
|
||||||
void SetIsTrigger (bool isTrigger) noexcept;
|
void SetIsTrigger (bool isTrigger) noexcept;
|
||||||
void SetFriction (float friction) noexcept;
|
void SetFriction (float friction) noexcept;
|
||||||
void SetBounciness (float bounciness) noexcept;
|
void SetBounciness (float bounciness) noexcept;
|
||||||
void SetDensity (float density) noexcept;
|
void SetDensity (float density) noexcept;
|
||||||
|
|
||||||
void SetPositionOffset (const SHVec3& positionOffset) noexcept;
|
void SetPositionOffset (const SHVec3& positionOffset) noexcept;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
@ -96,13 +96,7 @@ namespace SHADE
|
||||||
SHShape* shape;
|
SHShape* shape;
|
||||||
SHVec3 positionOffset;
|
SHVec3 positionOffset;
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
RTTR_ENABLE()
|
||||||
/* Function Members */
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
void CreateShape ();
|
|
||||||
void CreateBoundingBox ();
|
|
||||||
void CreateSphere ();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace SHADE
|
} // namespace SHADE
|
||||||
|
|
|
@ -15,7 +15,8 @@
|
||||||
|
|
||||||
// Project Headers
|
// Project Headers
|
||||||
#include "ECS_Base/Managers/SHSystemManager.h"
|
#include "ECS_Base/Managers/SHSystemManager.h"
|
||||||
#include "SHPhysicsSystem.h"
|
#include "ECS_Base/Managers/SHComponentManager.h"
|
||||||
|
|
||||||
|
|
||||||
namespace SHADE
|
namespace SHADE
|
||||||
{
|
{
|
||||||
|
@ -23,16 +24,20 @@ namespace SHADE
|
||||||
/* Constructors & Destructor Definitions */
|
/* Constructors & Destructor Definitions */
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
SHPhysicsObject::SHPhysicsObject() noexcept
|
SHPhysicsObject::SHPhysicsObject(EntityID eid, rp3d::PhysicsCommon* physicsFactory, rp3d::PhysicsWorld* physicsWorld) noexcept
|
||||||
: entityID { MAX_EID }
|
: entityID { eid }
|
||||||
, isRigidBody { false }
|
, isRigidBody { false }
|
||||||
, hasColliders{ false }
|
, hasColliders{ false }
|
||||||
|
, factory { physicsFactory }
|
||||||
|
, world { physicsWorld }
|
||||||
, rp3dBody { nullptr }
|
, rp3dBody { nullptr }
|
||||||
{}
|
{}
|
||||||
|
|
||||||
SHPhysicsObject::~SHPhysicsObject() noexcept
|
SHPhysicsObject::~SHPhysicsObject() noexcept
|
||||||
{
|
{
|
||||||
rp3dBody = nullptr;
|
factory = nullptr;
|
||||||
|
world = nullptr;
|
||||||
|
rp3dBody = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
@ -44,10 +49,7 @@ namespace SHADE
|
||||||
SHVec3 result;
|
SHVec3 result;
|
||||||
|
|
||||||
if (rp3dBody)
|
if (rp3dBody)
|
||||||
{
|
result = SHVec3{ rp3dBody->getTransform().getPosition() };
|
||||||
const auto& RP3D_RESULT = rp3dBody->getTransform().getPosition();
|
|
||||||
result = SHVec3{ RP3D_RESULT.x, RP3D_RESULT.y, RP3D_RESULT.z };
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -57,10 +59,7 @@ namespace SHADE
|
||||||
SHQuaternion result;
|
SHQuaternion result;
|
||||||
|
|
||||||
if (rp3dBody)
|
if (rp3dBody)
|
||||||
{
|
result = SHQuaternion{ rp3dBody->getTransform().getOrientation() };
|
||||||
const auto& RP3D_RESULT = rp3dBody->getTransform().getOrientation();
|
|
||||||
result = SHQuaternion{ RP3D_RESULT.x, RP3D_RESULT.y, RP3D_RESULT.z, RP3D_RESULT.w };
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -70,10 +69,7 @@ namespace SHADE
|
||||||
SHVec3 result;
|
SHVec3 result;
|
||||||
|
|
||||||
if (rp3dBody)
|
if (rp3dBody)
|
||||||
{
|
result = SHQuaternion{ rp3dBody->getTransform().getOrientation() }.ToEuler();
|
||||||
const auto& RP3D_RESULT = rp3dBody->getTransform().getOrientation();
|
|
||||||
result = SHQuaternion{ RP3D_RESULT.x, RP3D_RESULT.y, RP3D_RESULT.z, RP3D_RESULT.w }.ToEuler();
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -84,10 +80,14 @@ namespace SHADE
|
||||||
|
|
||||||
void SHPhysicsObject::SetPosition(const SHVec3& position) noexcept
|
void SHPhysicsObject::SetPosition(const SHVec3& position) noexcept
|
||||||
{
|
{
|
||||||
const rp3d::Vector3 RP3D_POS { position.x, position.y, position.z };
|
if (!rp3dBody)
|
||||||
|
{
|
||||||
|
SHLOG_ERROR("Cannot set position of a non-existent physics body for Entity {}", entityID)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
rp3d::Transform rp3dTF;
|
rp3d::Transform rp3dTF;
|
||||||
rp3dTF.setPosition(RP3D_POS);
|
rp3dTF.setPosition(position);
|
||||||
rp3dTF.setOrientation(rp3dBody->getTransform().getOrientation());
|
rp3dTF.setOrientation(rp3dBody->getTransform().getOrientation());
|
||||||
|
|
||||||
rp3dBody->setTransform(rp3dTF);
|
rp3dBody->setTransform(rp3dTF);
|
||||||
|
@ -96,11 +96,15 @@ namespace SHADE
|
||||||
|
|
||||||
void SHPhysicsObject::SetOrientation(const SHQuaternion& orientation) noexcept
|
void SHPhysicsObject::SetOrientation(const SHQuaternion& orientation) noexcept
|
||||||
{
|
{
|
||||||
const rp3d::Quaternion RP3D_ORIENTATION { orientation.x, orientation.y, orientation.z, orientation.w };
|
if (!rp3dBody)
|
||||||
|
{
|
||||||
|
SHLOG_ERROR("Cannot set orientation of a non-existent physics body for Entity {}", entityID)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
rp3d::Transform rp3dTF;
|
rp3d::Transform rp3dTF;
|
||||||
rp3dTF.setPosition(rp3dBody->getTransform().getPosition());
|
rp3dTF.setPosition(rp3dBody->getTransform().getPosition());
|
||||||
rp3dTF.setOrientation(RP3D_ORIENTATION);
|
rp3dTF.setOrientation(orientation);
|
||||||
|
|
||||||
rp3dBody->setTransform(rp3dTF);
|
rp3dBody->setTransform(rp3dTF);
|
||||||
prevTransform = rp3dTF;
|
prevTransform = rp3dTF;
|
||||||
|
@ -108,14 +112,253 @@ namespace SHADE
|
||||||
|
|
||||||
void SHPhysicsObject::SetRotation(const SHVec3& rotation) noexcept
|
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;
|
rp3d::Transform rp3dTF;
|
||||||
rp3dTF.setPosition(rp3dBody->getTransform().getPosition());
|
rp3dTF.setPosition(rp3dBody->getTransform().getPosition());
|
||||||
rp3dTF.setOrientation(RP3D_ORIENTATION);
|
rp3dTF.setOrientation(rotation);
|
||||||
|
|
||||||
rp3dBody->setTransform(rp3dTF);
|
rp3dBody->setTransform(rp3dTF);
|
||||||
prevTransform = rp3dTF;
|
prevTransform = rp3dTF;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
/* Public Function Member Definitions */
|
||||||
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
void SHPhysicsObject::CreateRigidBody(const SHTransformComponent* tf, SHRigidBodyComponent* rb, SHColliderComponent* c)
|
||||||
|
{
|
||||||
|
// If collider already exists, recreate the collision body as a rigid body
|
||||||
|
if (hasColliders)
|
||||||
|
world->destroyCollisionBody(rp3dBody);
|
||||||
|
|
||||||
|
rp3dBody = world->createRigidBody(rp3d::Transform{ tf->GetWorldPosition(), tf->GetWorldRotation() });
|
||||||
|
isRigidBody = true;
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
switch (collider->GetType())
|
||||||
|
{
|
||||||
|
case SHCollider::Type::BOX:
|
||||||
|
{
|
||||||
|
const auto* box = reinterpret_cast<SHBoundingBox*>(collider->GetShape());
|
||||||
|
rp3d::BoxShape* newBox = factory->createBoxShape(box->GetHalfExtents());
|
||||||
|
|
||||||
|
rp3dBody->addCollider(newBox, rp3d::Transform{ collider->GetPositionOffset(), SHQuaternion::Identity });
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case SHCollider::Type::SPHERE:
|
||||||
|
{
|
||||||
|
const auto* sphere = reinterpret_cast<SHBoundingSphere*>(collider->GetShape());
|
||||||
|
rp3d::SphereShape* newSphere = factory->createSphereShape(sphere->GetRadius());
|
||||||
|
|
||||||
|
rp3dBody->addCollider(newSphere, rp3d::Transform{ collider->GetPositionOffset(), SHQuaternion::Identity });
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// TODO(Diren): Add more collider shapes
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return static_cast<int>(rp3dBody->getNbColliders()) - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SHPhysicsObject::DestroyRigidBody(SHColliderComponent* c) noexcept
|
||||||
|
{
|
||||||
|
world->destroyRigidBody(reinterpret_cast<rp3d::RigidBody*>(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)
|
||||||
|
{
|
||||||
|
const int NUM_COLLIDERS = static_cast<int>(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
|
||||||
|
{
|
||||||
|
SHASSERT(rp3dBody != nullptr, "ReactPhysics body does not exist!")
|
||||||
|
|
||||||
|
if (rb->dirtyFlags == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto* rigidBody = reinterpret_cast<rp3d::RigidBody*>(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<rp3d::BodyType>(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(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<SHBoundingBox*>(collider.GetShape());
|
||||||
|
|
||||||
|
auto* rp3dBoxShape = reinterpret_cast<rp3d::BoxShape*>(rp3dCollider->getCollisionShape());
|
||||||
|
rp3dBoxShape->setHalfExtents(box->GetHalfExtents());
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case SHCollider::Type::SPHERE:
|
||||||
|
{
|
||||||
|
const auto* sphere = reinterpret_cast<SHBoundingSphere*>(collider.GetShape());
|
||||||
|
|
||||||
|
auto* rp3dSphereShape = reinterpret_cast<rp3d::SphereShape*>(rp3dCollider->getCollisionShape());
|
||||||
|
rp3dSphereShape->setRadius(sphere->GetRadius());
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
|
||||||
|
dirty = false;
|
||||||
|
++index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace SHADE
|
} // namespace SHADE
|
|
@ -13,9 +13,9 @@
|
||||||
#include <reactphysics3d/reactphysics3d.h>
|
#include <reactphysics3d/reactphysics3d.h>
|
||||||
|
|
||||||
// Project Headers
|
// Project Headers
|
||||||
#include "Math/Vector/SHVec3.h"
|
#include "Math/Transform/SHTransformComponent.h"
|
||||||
#include "Math/SHQuaternion.h"
|
#include "Components/SHRigidBodyComponent.h"
|
||||||
#include "ECS_Base/Entity/SHEntity.h"
|
#include "Components/SHColliderComponent.h"
|
||||||
|
|
||||||
namespace SHADE
|
namespace SHADE
|
||||||
{
|
{
|
||||||
|
@ -31,15 +31,13 @@ namespace SHADE
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
friend class SHPhysicsSystem;
|
friend class SHPhysicsSystem;
|
||||||
friend class SHRigidBodyComponent;
|
|
||||||
friend class SHColliderComponent;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
/* Constructors & Destructor */
|
/* Constructors & Destructor */
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
SHPhysicsObject () noexcept;
|
SHPhysicsObject (EntityID eid, rp3d::PhysicsCommon* physicsFactory, rp3d::PhysicsWorld* physicsWorld) noexcept;
|
||||||
SHPhysicsObject (const SHPhysicsObject& rhs) noexcept = default;
|
SHPhysicsObject (const SHPhysicsObject& rhs) noexcept = default;
|
||||||
SHPhysicsObject (SHPhysicsObject&& rhs) noexcept = default;
|
SHPhysicsObject (SHPhysicsObject&& rhs) noexcept = default;
|
||||||
virtual ~SHPhysicsObject () noexcept;
|
virtual ~SHPhysicsObject () noexcept;
|
||||||
|
@ -63,9 +61,24 @@ namespace SHADE
|
||||||
/* Setter Functions */
|
/* Setter Functions */
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
void SetPosition (const SHVec3& position) noexcept;
|
void SetPosition (const SHVec3& position) noexcept;
|
||||||
void SetOrientation (const SHQuaternion& orientation) noexcept;
|
void SetOrientation (const SHQuaternion& orientation) noexcept;
|
||||||
void SetRotation (const SHVec3& rotation) noexcept;
|
void SetRotation (const SHVec3& rotation) noexcept;
|
||||||
|
|
||||||
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
/* Function Members */
|
||||||
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
void CreateRigidBody (const SHTransformComponent* tf, SHRigidBodyComponent* rb, SHColliderComponent* c);
|
||||||
|
void CreateCollisionBody (const SHTransformComponent* tf, SHColliderComponent* c);
|
||||||
|
int AddCollider (SHCollider* collider);
|
||||||
|
|
||||||
|
void DestroyRigidBody (SHColliderComponent* c) noexcept;
|
||||||
|
void RemoveCollider (int index);
|
||||||
|
void DestroyCollisionBody () noexcept;
|
||||||
|
|
||||||
|
void SyncRigidBody (SHRigidBodyComponent* rb) const noexcept;
|
||||||
|
void SyncColliders (SHColliderComponent* c) const noexcept;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
@ -76,14 +89,9 @@ namespace SHADE
|
||||||
bool isRigidBody;
|
bool isRigidBody;
|
||||||
bool hasColliders;
|
bool hasColliders;
|
||||||
|
|
||||||
|
rp3d::PhysicsCommon* factory;
|
||||||
|
rp3d::PhysicsWorld* world;
|
||||||
rp3d::CollisionBody* rp3dBody; // Can be either a collision body or a rigid body
|
rp3d::CollisionBody* rp3dBody; // Can be either a collision body or a rigid body
|
||||||
rp3d::Transform prevTransform; // Cached transform for interpolation
|
rp3d::Transform prevTransform; // Cached transform for interpolation
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
|
||||||
/* Function Members */
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
} // namespace SHADE
|
} // namespace SHADE
|
|
@ -68,10 +68,7 @@ namespace SHADE
|
||||||
|
|
||||||
if (world)
|
if (world)
|
||||||
{
|
{
|
||||||
const auto RP3D_GRAVITY = world->getGravity();
|
result = world->getGravity();
|
||||||
result.x = RP3D_GRAVITY.x;
|
|
||||||
result.y = RP3D_GRAVITY.y;
|
|
||||||
result.z = RP3D_GRAVITY.z;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -112,8 +109,7 @@ namespace SHADE
|
||||||
{
|
{
|
||||||
if (world)
|
if (world)
|
||||||
{
|
{
|
||||||
const rp3d::Vector3 G { gravity.x, gravity.y, gravity.z };
|
world->setGravity(gravity);
|
||||||
world->setGravity(G);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -161,8 +157,7 @@ namespace SHADE
|
||||||
{
|
{
|
||||||
if (world)
|
if (world)
|
||||||
{
|
{
|
||||||
const rp3d::Vector3 G { settings.gravity.x, settings.gravity.y, settings.gravity.z };
|
world->setGravity(settings.gravity);
|
||||||
world->setGravity(G);
|
|
||||||
world->setNbIterationsVelocitySolver(settings.numVelocitySolverIterations);
|
world->setNbIterationsVelocitySolver(settings.numVelocitySolverIterations);
|
||||||
world->setNbIterationsPositionSolver(settings.numPositionSolverIterations);
|
world->setNbIterationsPositionSolver(settings.numPositionSolverIterations);
|
||||||
world->enableSleeping(settings.sleepingEnabled);
|
world->enableSleeping(settings.sleepingEnabled);
|
||||||
|
@ -207,69 +202,14 @@ namespace SHADE
|
||||||
SHLOG_INFO("Adding a Rigidbody to the Physics World.")
|
SHLOG_INFO("Adding a Rigidbody to the Physics World.")
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Check if entity is already a physics object
|
auto* physicsObject = CreatePhysicsObject(entityID);
|
||||||
auto* physicsObject = GetPhysicsObject(entityID);
|
|
||||||
if (!physicsObject)
|
|
||||||
{
|
|
||||||
physicsObject = &(map.emplace(entityID, SHPhysicsObject{}).first->second);
|
|
||||||
physicsObject->entityID = entityID;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get entity transform
|
|
||||||
auto const* SHADE_TF = SHComponentManager::GetComponent_s<SHTransformComponent>(entityID);
|
|
||||||
|
|
||||||
// Possibly redundant
|
|
||||||
if (!SHADE_TF)
|
|
||||||
{
|
|
||||||
SHComponentManager::AddComponent<SHTransformComponent>(entityID);
|
|
||||||
SHADE_TF = SHComponentManager::GetComponent<SHTransformComponent>(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 };
|
|
||||||
|
|
||||||
// If collider already exists
|
|
||||||
if (physicsObject->hasColliders)
|
|
||||||
world->destroyCollisionBody(physicsObject->rp3dBody);
|
|
||||||
|
|
||||||
physicsObject->rp3dBody = world->createRigidBody(RP3D_TF);
|
|
||||||
physicsObject->isRigidBody = true;
|
|
||||||
|
|
||||||
// Recreate colliders
|
|
||||||
if (physicsObject->hasColliders)
|
|
||||||
{
|
|
||||||
const auto& COLLIDERS = SHComponentManager::GetComponent<SHColliderComponent>(entityID)->GetColliders();
|
|
||||||
for (const auto& collider : COLLIDERS | std::views::keys)
|
|
||||||
{
|
|
||||||
switch (collider.GetType())
|
|
||||||
{
|
|
||||||
case SHCollider::Type::BOX:
|
|
||||||
{
|
|
||||||
SHBoundingBox* box = reinterpret_cast<SHBoundingBox*>(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);
|
|
||||||
|
|
||||||
// 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<SHRigidBodyComponent>(entityID),
|
||||||
|
SHComponentManager::GetComponent_s<SHColliderComponent>(entityID)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SHPhysicsSystem::AddCollider(EntityID entityID) noexcept
|
void SHPhysicsSystem::AddCollider(EntityID entityID) noexcept
|
||||||
|
@ -278,63 +218,13 @@ namespace SHADE
|
||||||
SHLOG_INFO("Adding a Collider to the Physics World.")
|
SHLOG_INFO("Adding a Collider to the Physics World.")
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Check if entity is already a physics object
|
auto* physicsObject = CreatePhysicsObject(entityID);
|
||||||
auto* physicsObject = GetPhysicsObject(entityID);
|
|
||||||
if (!physicsObject)
|
|
||||||
{
|
|
||||||
physicsObject = &(map.emplace(entityID, SHPhysicsObject{}).first->second);
|
|
||||||
physicsObject->entityID = entityID;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get entity transform
|
physicsObject->CreateCollisionBody
|
||||||
auto const* SHADE_TF = SHComponentManager::GetComponent_s<SHTransformComponent>(entityID);
|
(
|
||||||
|
EnsureTransform(entityID),
|
||||||
// Possibly redundant
|
SHComponentManager::GetComponent<SHColliderComponent>(entityID)
|
||||||
if (!SHADE_TF)
|
);
|
||||||
{
|
|
||||||
SHComponentManager::AddComponent<SHTransformComponent>(entityID);
|
|
||||||
SHADE_TF = SHComponentManager::GetComponent<SHTransformComponent>(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 };
|
|
||||||
|
|
||||||
// No rb
|
|
||||||
if (!physicsObject->isRigidBody)
|
|
||||||
physicsObject->rp3dBody = world->createCollisionBody(RP3D_TF);
|
|
||||||
|
|
||||||
const auto& COLLIDERS = SHComponentManager::GetComponent<SHColliderComponent>(entityID)->GetColliders();
|
|
||||||
for (const auto& collider : COLLIDERS | std::views::keys)
|
|
||||||
{
|
|
||||||
switch (collider.GetType())
|
|
||||||
{
|
|
||||||
case SHCollider::Type::BOX:
|
|
||||||
{
|
|
||||||
SHBoundingBox* box = reinterpret_cast<SHBoundingBox*>(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);
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SHPhysicsSystem::RemoveRigidBody(EntityID entityID) noexcept
|
void SHPhysicsSystem::RemoveRigidBody(EntityID entityID) noexcept
|
||||||
|
@ -343,6 +233,13 @@ namespace SHADE
|
||||||
SHLOG_INFO("Removing a Rigidbody from the Physics World.")
|
SHLOG_INFO("Removing a Rigidbody from the Physics World.")
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
auto* physicsObject = GetPhysicsObject(entityID);
|
||||||
|
SHASSERT(physicsObject != nullptr, "Physics object has been lost from the world!")
|
||||||
|
|
||||||
|
physicsObject->DestroyRigidBody(SHComponentManager::GetComponent_s<SHColliderComponent>(entityID));
|
||||||
|
|
||||||
|
if (physicsObject->rp3dBody == nullptr)
|
||||||
|
DestroyPhysicsObject(entityID);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SHPhysicsSystem::RemoveCollider(EntityID entityID) noexcept
|
void SHPhysicsSystem::RemoveCollider(EntityID entityID) noexcept
|
||||||
|
@ -392,33 +289,10 @@ namespace SHADE
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SHPhysicsSystem::AddCollisionShape(EntityID entityID, SHShape* shape)
|
void SHPhysicsSystem::AddCollisionShape(EntityID entityID, SHCollider* collider)
|
||||||
{
|
{
|
||||||
auto* physicsObject = GetPhysicsObject(entityID);
|
auto* physicsObject = GetPhysicsObject(entityID);
|
||||||
|
physicsObject->AddCollider(collider);
|
||||||
switch (shape->GetType())
|
|
||||||
{
|
|
||||||
case SHShape::Type::BOX:
|
|
||||||
{
|
|
||||||
auto* box = reinterpret_cast<SHBoundingBox*>(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);
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SHPhysicsSystem::RemoveCollisionShape(EntityID entityID, int index)
|
void SHPhysicsSystem::RemoveCollisionShape(EntityID entityID, int index)
|
||||||
|
@ -475,6 +349,7 @@ namespace SHADE
|
||||||
if (system->worldUpdated)
|
if (system->worldUpdated)
|
||||||
{
|
{
|
||||||
system->SyncTransforms();
|
system->SyncTransforms();
|
||||||
|
|
||||||
// TODO(Diren): Handle trigger messages for scripting
|
// TODO(Diren): Handle trigger messages for scripting
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -483,6 +358,18 @@ namespace SHADE
|
||||||
/* Private Function Member Definitions */
|
/* Private Function Member Definitions */
|
||||||
/*-----------------------------------------------------------------------------------*/
|
/*-----------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
SHPhysicsObject* SHPhysicsSystem::CreatePhysicsObject(EntityID entityID) noexcept
|
||||||
|
{
|
||||||
|
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
|
SHPhysicsObject* SHPhysicsSystem::GetPhysicsObject(EntityID entityID) noexcept
|
||||||
{
|
{
|
||||||
const auto it = map.find(entityID);
|
const auto it = map.find(entityID);
|
||||||
|
@ -495,6 +382,19 @@ namespace SHADE
|
||||||
return &(it->second);
|
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();
|
||||||
|
if (RP3D_ACTIVE != componentActive)
|
||||||
|
physicsObject->rp3dBody->setIsActive(componentActive);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void SHPhysicsSystem::SyncRigidBodyComponents(std::vector<SHRigidBodyComponent>& denseArray) noexcept
|
void SHPhysicsSystem::SyncRigidBodyComponents(std::vector<SHRigidBodyComponent>& denseArray) noexcept
|
||||||
{
|
{
|
||||||
if (denseArray.empty())
|
if (denseArray.empty())
|
||||||
|
@ -505,23 +405,16 @@ namespace SHADE
|
||||||
const EntityID ENTITY_ID = comp.GetEID();
|
const EntityID ENTITY_ID = comp.GetEID();
|
||||||
|
|
||||||
// Get physicsObject
|
// 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
|
// TODO(Diren): Check if active in hierarchy
|
||||||
const bool COMPONENT_ACTIVE = comp.isActive;
|
const bool COMPONENT_ACTIVE = comp.isActive;
|
||||||
|
SyncActiveStates(physicsObject, COMPONENT_ACTIVE);
|
||||||
if (RP3D_ACTIVE != COMPONENT_ACTIVE)
|
|
||||||
physicsObject->rp3dBody->setIsActive(COMPONENT_ACTIVE);
|
|
||||||
|
|
||||||
if (!COMPONENT_ACTIVE)
|
if (!COMPONENT_ACTIVE)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (comp.dirtyFlags > 0)
|
physicsObject->SyncRigidBody(&comp);
|
||||||
{
|
|
||||||
SyncRigidBody(physicsObject, &comp);
|
|
||||||
comp.dirtyFlags = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -535,29 +428,23 @@ namespace SHADE
|
||||||
const EntityID ENTITY_ID = comp.GetEID();
|
const EntityID ENTITY_ID = comp.GetEID();
|
||||||
|
|
||||||
// Get physicsObject
|
// 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
|
// TODO(Diren): Check if active in hierarchy
|
||||||
const bool COMPONENT_ACTIVE = comp.isActive;
|
const bool COMPONENT_ACTIVE = comp.isActive;
|
||||||
|
SyncActiveStates(physicsObject, COMPONENT_ACTIVE);
|
||||||
if (RP3D_ACTIVE != COMPONENT_ACTIVE)
|
|
||||||
physicsObject->rp3dBody->setIsActive(COMPONENT_ACTIVE);
|
|
||||||
|
|
||||||
if (!COMPONENT_ACTIVE)
|
if (!COMPONENT_ACTIVE)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
SyncCollider(physicsObject, &comp);
|
physicsObject->SyncColliders(&comp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SHPhysicsSystem::SyncTransforms() noexcept
|
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::Vector3 rp3dPos;
|
||||||
rp3d::Quaternion rp3dRot;
|
rp3d::Quaternion rp3dRot;
|
||||||
|
|
||||||
|
@ -567,7 +454,7 @@ namespace SHADE
|
||||||
|
|
||||||
if (physicsObject.isRigidBody)
|
if (physicsObject.isRigidBody)
|
||||||
{
|
{
|
||||||
auto const* rbComponent = SHComponentManager::GetComponent<SHRigidBodyComponent>(ENTITY_ID);
|
auto* rbComponent = SHComponentManager::GetComponent<SHRigidBodyComponent>(entityID);
|
||||||
if (rbComponent->GetType() == SHRigidBodyComponent::Type::STATIC)
|
if (rbComponent->GetType() == SHRigidBodyComponent::Type::STATIC)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -585,6 +472,16 @@ namespace SHADE
|
||||||
rp3dPos = CURRENT_TF.getPosition();
|
rp3dPos = CURRENT_TF.getPosition();
|
||||||
rp3dRot = CURRENT_TF.getOrientation();
|
rp3dRot = CURRENT_TF.getOrientation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rbComponent->position = CURRENT_TF.getPosition();
|
||||||
|
rbComponent->orientation = CURRENT_TF.getOrientation();
|
||||||
|
|
||||||
|
if (physicsObject.hasColliders)
|
||||||
|
{
|
||||||
|
auto* colliderComponent = SHComponentManager::GetComponent<SHColliderComponent>(entityID);
|
||||||
|
colliderComponent->position = CURRENT_TF.getPosition();
|
||||||
|
colliderComponent->orientation = CURRENT_TF.getOrientation();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -593,155 +490,29 @@ namespace SHADE
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert RP3D Transform to SHADE
|
// Convert RP3D Transform to SHADE
|
||||||
const SHVec3 SHADE_POS = SHVec3{ rp3dPos.x, rp3dPos.y, rp3dPos.z };
|
auto* tfComponent = SHComponentManager::GetComponent<SHTransformComponent>(entityID);
|
||||||
const SHVec3 SHADE_ROT = SHQuaternion{ rp3dRot.x, rp3dRot.y, rp3dRot.z, rp3dRot.w }.ToEuler();
|
tfComponent->SetWorldPosition(rp3dPos);
|
||||||
|
tfComponent->SetWorldRotation(SHQuaternion{ rp3dRot }.ToEuler());
|
||||||
|
|
||||||
auto* tfComponent = SHComponentManager::GetComponent<SHTransformComponent>(ENTITY_ID);
|
|
||||||
tfComponent->SetWorldPosition(SHADE_POS);
|
|
||||||
tfComponent->SetWorldRotation(SHADE_ROT);
|
|
||||||
|
|
||||||
// Cache transforms
|
// Cache transforms
|
||||||
physicsObject.prevTransform = CURRENT_TF;
|
physicsObject.prevTransform = CURRENT_TF;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SHPhysicsSystem::SyncRigidBody(SHPhysicsObject const* physicsObject, const SHRigidBodyComponent* comp) noexcept
|
SHTransformComponent* SHPhysicsSystem::EnsureTransform(EntityID entityID)
|
||||||
{
|
{
|
||||||
auto* rigidBody = reinterpret_cast<rp3d::RigidBody*>(physicsObject->rp3dBody);
|
auto* tf = SHComponentManager::GetComponent_s<SHTransformComponent>(entityID);
|
||||||
|
|
||||||
const uint16_t RB_FLAGS = comp->dirtyFlags;
|
// Possibly redundant
|
||||||
const size_t NUM_FLAGS = sizeof(RB_FLAGS) * 8U;
|
if (!tf)
|
||||||
for (size_t i = 0; i < NUM_FLAGS; ++i)
|
|
||||||
{
|
{
|
||||||
// Check if current dirty flag has been set to true
|
SHComponentManager::AddComponent<SHTransformComponent>(entityID);
|
||||||
if (RB_FLAGS & 1U << i)
|
tf = SHComponentManager::GetComponent<SHTransformComponent>(entityID);
|
||||||
{
|
|
||||||
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<rp3d::BodyType>(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
|
|
||||||
{
|
|
||||||
const SHVec3& SHADE_VEL = comp->GetLinearVelocity();
|
|
||||||
rp3d::Vector3 RP3D_VEL { SHADE_VEL.x, SHADE_VEL.y, SHADE_VEL.z };
|
|
||||||
|
|
||||||
rigidBody->setLinearVelocity(RP3D_VEL);
|
|
||||||
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);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return tf;
|
||||||
}
|
}
|
||||||
|
|
||||||
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<SHBoundingBox*>(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<rp3d::BoxShape*>(physicsObject->rp3dBody->getCollider(index)->getCollisionShape());
|
|
||||||
rp3dBoxShape->setHalfExtents(RP3D_EXTENTS);
|
|
||||||
|
|
||||||
if (rp3dBoxShape)
|
|
||||||
{
|
|
||||||
SHLOG_INFO("Updating box things")
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case SHCollider::Type::SPHERE:
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
|
|
||||||
dirty = false;
|
|
||||||
++index;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace SHADE
|
} // namespace SHADE
|
|
@ -19,6 +19,7 @@
|
||||||
#include "SHPhysicsObject.h"
|
#include "SHPhysicsObject.h"
|
||||||
#include "Components/SHRigidBodyComponent.h"
|
#include "Components/SHRigidBodyComponent.h"
|
||||||
#include "Components/SHColliderComponent.h"
|
#include "Components/SHColliderComponent.h"
|
||||||
|
#include "Math/Transform/SHTransformComponent.h"
|
||||||
|
|
||||||
#include "Scene/SHSceneGraph.h"
|
#include "Scene/SHSceneGraph.h"
|
||||||
#include "ECS_Base/System/SHSystemRoutine.h"
|
#include "ECS_Base/System/SHSystemRoutine.h"
|
||||||
|
@ -81,8 +82,8 @@ namespace SHADE
|
||||||
/* Function Members */
|
/* Function Members */
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
void Init () override;
|
void Init () override;
|
||||||
void Exit () override;
|
void Exit () override;
|
||||||
|
|
||||||
void AddRigidBody (EntityID entityID) noexcept;
|
void AddRigidBody (EntityID entityID) noexcept;
|
||||||
void AddCollider (EntityID entityID) noexcept;
|
void AddCollider (EntityID entityID) noexcept;
|
||||||
|
@ -100,7 +101,7 @@ namespace SHADE
|
||||||
void AddTorque (EntityID entityID, const SHVec3& torque) const noexcept;
|
void AddTorque (EntityID entityID, const SHVec3& torque) const noexcept;
|
||||||
void AddRelativeTorque (EntityID entityID, const SHVec3& relativeTorque) 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);
|
void RemoveCollisionShape (EntityID entityID, int index);
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
|
@ -185,18 +186,18 @@ namespace SHADE
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
/* Function Members */
|
/* Function Members */
|
||||||
/*---------------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------------*/
|
||||||
SHPhysicsObject* GetPhysicsObject (EntityID entityID) noexcept;
|
SHPhysicsObject* CreatePhysicsObject (EntityID entityID) noexcept;
|
||||||
|
SHPhysicsObject* GetPhysicsObject (EntityID entityID) noexcept;
|
||||||
|
void DestroyPhysicsObject (EntityID entityID) noexcept;
|
||||||
|
|
||||||
void SyncRigidBodyComponents (std::vector<SHRigidBodyComponent>& denseArray) noexcept;
|
void SyncActiveStates (SHPhysicsObject* physicsObject, bool componentActive) noexcept;
|
||||||
void SyncColliderComponents (std::vector<SHColliderComponent>& denseArray) noexcept;
|
void SyncRigidBodyComponents (std::vector<SHRigidBodyComponent>& denseArray) noexcept;
|
||||||
void SyncTransforms () noexcept;
|
void SyncColliderComponents (std::vector<SHColliderComponent>& denseArray) noexcept;
|
||||||
|
void SyncTransforms () noexcept;
|
||||||
// TODO(Diren): Trigger handling
|
// TODO(Diren): Trigger handling
|
||||||
|
|
||||||
static void SyncRigidBody (SHPhysicsObject const* physicsObject, const SHRigidBodyComponent* comp) noexcept;
|
// TODO(Diren): Remove when responsibility shifted to editor
|
||||||
static void SetRP3DLinearConstraints (rp3d::RigidBody const* rp3dRigidBody, uint8_t rbFlags) noexcept;
|
SHTransformComponent* EnsureTransform (EntityID entityID);
|
||||||
static void SetRP3DAngularConstraints (rp3d::RigidBody const* rp3dRigidBody, uint8_t rbFlags) noexcept;
|
|
||||||
|
|
||||||
static void SyncCollider (SHPhysicsObject const* physicsObject, SHColliderComponent* comp) noexcept;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,7 @@ project "SHADE_Managed"
|
||||||
"%{IncludeDir.yamlcpp}",
|
"%{IncludeDir.yamlcpp}",
|
||||||
"%{IncludeDir.RTTR}/include",
|
"%{IncludeDir.RTTR}/include",
|
||||||
"%{IncludeDir.dotnet}\\include",
|
"%{IncludeDir.dotnet}\\include",
|
||||||
|
"%{IncludeDir.reactphysics3d}\\include",
|
||||||
"%{wks.location}/SHADE_Engine/src"
|
"%{wks.location}/SHADE_Engine/src"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue