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.SetWorldScale(TEST_OBJ_SCALE);
|
||||
|
||||
auto* box = collider.AddBoundingBox();
|
||||
box->SetHalfExtents(transform.GetWorldScale() * 0.5f);
|
||||
if (const bool IS_EVEN = (y * NUM_ROWS + x) % 2; IS_EVEN)
|
||||
collider.AddBoundingBox(SHVec3::One * 0.5f, SHVec3::Zero);
|
||||
else
|
||||
collider.AddBoundingSphere(0.5f, SHVec3::Zero);
|
||||
|
||||
stressTestObjects.emplace_back(entity);
|
||||
}
|
||||
|
|
|
@ -95,12 +95,12 @@ namespace SHADE
|
|||
/* Getter Function Definitions */
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
|
||||
const SHVec3& SHBoundingBox::GetCenter() const noexcept
|
||||
SHVec3 SHBoundingBox::GetCenter() const noexcept
|
||||
{
|
||||
return Center;
|
||||
}
|
||||
|
||||
const SHVec3& SHBoundingBox::GetHalfExtents() const noexcept
|
||||
SHVec3 SHBoundingBox::GetHalfExtents() const noexcept
|
||||
{
|
||||
return Extents;
|
||||
}
|
||||
|
|
|
@ -54,8 +54,8 @@ namespace SHADE
|
|||
/* Getter Functions */
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
||||
[[nodiscard]] const SHVec3& GetCenter () const noexcept;
|
||||
[[nodsicard]] const SHVec3& GetHalfExtents() const noexcept;
|
||||
[[nodiscard]] SHVec3 GetCenter () const noexcept;
|
||||
[[nodiscard]] SHVec3 GetHalfExtents() const noexcept;
|
||||
[[nodiscard]] SHVec3 GetMin () const noexcept;
|
||||
[[nodiscard]] SHVec3 GetMax () const noexcept;
|
||||
[[nodiscard]] std::vector<SHVec3> GetVertices () const noexcept;
|
||||
|
|
|
@ -94,7 +94,7 @@ namespace SHADE
|
|||
/* Getter Function Definitions */
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
|
||||
const SHVec3& SHBoundingSphere::GetCenter() const noexcept
|
||||
SHVec3 SHBoundingSphere::GetCenter() const noexcept
|
||||
{
|
||||
return Center;
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ namespace SHADE
|
|||
/* Getter Functions */
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
||||
[[nodiscard]] const SHVec3& GetCenter () const noexcept;
|
||||
[[nodiscard]] SHVec3 GetCenter () const noexcept;
|
||||
[[nodiscard]] float GetRadius () const noexcept;
|
||||
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
|
||||
// Project Headers
|
||||
#include "SH_API.h"
|
||||
#include "Math/Transform/SHTransform.h"
|
||||
#include "Math/SHRay.h"
|
||||
|
||||
|
||||
|
|
|
@ -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,16 @@ namespace SHADE
|
|||
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
|
||||
{
|
||||
return rhs * lhs;
|
||||
|
|
|
@ -11,6 +11,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <DirectXMath.h>
|
||||
#include <reactphysics3d/mathematics/Quaternion.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
// Project Headers
|
||||
|
@ -53,6 +55,11 @@ namespace SHADE
|
|||
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,11 @@ 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;
|
||||
operator reactphysics3d::Vector3 () const noexcept;
|
||||
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
/* Getter Functions */
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
|
|
@ -50,6 +50,10 @@ namespace SHADE
|
|||
: XMFLOAT2( _x, _y )
|
||||
{}
|
||||
|
||||
SHVec2::SHVec2(const reactphysics3d::Vector2& rp3dVec2) noexcept
|
||||
: XMFLOAT2( rp3dVec2.x, rp3dVec2.y )
|
||||
{}
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
/* 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 result;
|
||||
|
|
|
@ -11,6 +11,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <DirectXMath.h>
|
||||
#include <reactphysics3d/mathematics/Vector2.h>
|
||||
|
||||
#include <string>
|
||||
#include <initializer_list>
|
||||
|
||||
|
@ -57,6 +59,10 @@ namespace SHADE
|
|||
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 */
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
@ -64,7 +70,10 @@ namespace SHADE
|
|||
SHVec2& operator= (const SHVec2& rhs) = default;
|
||||
SHVec2& operator= (SHVec2&& rhs) = default;
|
||||
|
||||
// Conversion to other math types used by SHADE
|
||||
|
||||
operator DirectX::XMVECTOR () const noexcept;
|
||||
operator reactphysics3d::Vector2 () const 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[] (size_t index) const;
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
/* Function Members */
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#include "SHVec3.h"
|
||||
// Project Headers
|
||||
#include "Math/SHMatrix.h"
|
||||
#include "Math/SHQuaternion.h"
|
||||
#include "Tools/SHLogger.h"
|
||||
|
||||
using namespace DirectX;
|
||||
|
@ -56,6 +57,14 @@ namespace SHADE
|
|||
: 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 */
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
|
@ -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 result;
|
||||
|
|
|
@ -11,6 +11,9 @@
|
|||
#pragma once
|
||||
|
||||
#include <DirectXMath.h>
|
||||
#include <reactphysics3d/mathematics/Vector3.h>
|
||||
#include <reactphysics3d/mathematics/Quaternion.h>
|
||||
|
||||
#include <string>
|
||||
#include <initializer_list>
|
||||
|
||||
|
@ -62,6 +65,11 @@ namespace SHADE
|
|||
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::Quaternion& rp3dVec3) noexcept;
|
||||
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
/* Operator Overloads */
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
@ -69,6 +77,10 @@ namespace SHADE
|
|||
SHVec3& operator= (const SHVec3& rhs) = default;
|
||||
SHVec3& operator= (SHVec3&& rhs) = default;
|
||||
|
||||
// Conversion to other math types used by SHADE
|
||||
|
||||
operator reactphysics3d::Vector3 () const noexcept;
|
||||
operator reactphysics3d::Quaternion () const noexcept;
|
||||
operator DirectX::XMVECTOR () const noexcept;
|
||||
|
||||
SHVec3& operator+= (const SHVec3& rhs) noexcept;
|
||||
|
@ -94,7 +106,6 @@ namespace SHADE
|
|||
[[nodiscard]] float operator[] (int index) const;
|
||||
[[nodiscard]] float operator[] (size_t index) const;
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
/* Function Members */
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
// Project Headers
|
||||
#include "ECS_Base/Managers/SHSystemManager.h"
|
||||
#include "Math/SHMathHelpers.h"
|
||||
#include "Physics/SHPhysicsSystem.h"
|
||||
|
||||
namespace SHADE
|
||||
|
@ -25,6 +26,7 @@ namespace SHADE
|
|||
|
||||
SHColliderComponent::SHColliderComponent() noexcept
|
||||
: system { nullptr }
|
||||
, colliders {}
|
||||
{}
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
|
@ -87,12 +89,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<SHTransformComponent>(GetEID());
|
||||
|
||||
collider.SetPositionOffset(posOffset);
|
||||
collider.SetAsBoundingBox(tf->GetWorldScale() * halfExtents);
|
||||
|
||||
if (!system)
|
||||
{
|
||||
|
@ -101,23 +108,37 @@ namespace SHADE
|
|||
}
|
||||
|
||||
// Notify Physics System
|
||||
system->AddCollisionShape(GetEID(), collider.GetShape());
|
||||
system->AddCollisionShape(GetEID(), &collider);
|
||||
|
||||
return reinterpret_cast<SHBoundingBox*>(collider.GetShape());
|
||||
}
|
||||
|
||||
//void SHColliderComponent::AddSphere() noexcept
|
||||
//{
|
||||
// const auto TYPE = SHCollider::Type::SPHERE;
|
||||
SHBoundingSphere* SHColliderComponent::AddBoundingSphere(float radius, const SHVec3& posOffset) noexcept
|
||||
{
|
||||
const auto TYPE = SHCollider::Type::SPHERE;
|
||||
|
||||
// if (!system)
|
||||
// {
|
||||
// SHLOG_ERROR("Physics system does not exist, unable to add Sphere Collider!")
|
||||
// return;
|
||||
// }
|
||||
auto spherePair = std::make_pair(SHCollider{ TYPE }, true);
|
||||
auto& collider = colliders.emplace_back(spherePair).first;
|
||||
|
||||
// // 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)
|
||||
{
|
||||
|
|
|
@ -15,6 +15,13 @@
|
|||
// Project Headers
|
||||
#include "ECS_Base/Components/SHComponent.h"
|
||||
#include "Physics/SHCollider.h"
|
||||
#include "Math/Geometry/SHBoundingBox.h"
|
||||
#include "Math/Geometry/SHBoundingSphere.h"
|
||||
|
||||
//namespace SHADE
|
||||
//{
|
||||
// class SHPhysicsSystem;
|
||||
//}
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
|
@ -30,6 +37,7 @@ namespace SHADE
|
|||
/*---------------------------------------------------------------------------------*/
|
||||
|
||||
friend class SHPhysicsSystem;
|
||||
friend class SHPhysicsObject;
|
||||
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
/* Type Definitions */
|
||||
|
@ -76,10 +84,11 @@ namespace SHADE
|
|||
void OnCreate () override;
|
||||
void OnDestroy () override;
|
||||
|
||||
SHBoundingBox* AddBoundingBox () noexcept;
|
||||
|
||||
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:
|
||||
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
|
|
@ -393,8 +393,8 @@ RTTR_REGISTRATION
|
|||
registration::enumeration<SHRigidBodyComponent::Type>("RigidBody Type")
|
||||
(
|
||||
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")
|
||||
|
|
|
@ -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:
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
@ -137,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
|
||||
|
@ -162,10 +172,6 @@ namespace SHADE
|
|||
SHVec3 position;
|
||||
SHQuaternion orientation;
|
||||
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
/* Function Members */
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
||||
RTTR_ENABLE()
|
||||
};
|
||||
} // namespace SHADE
|
|
@ -12,6 +12,9 @@
|
|||
|
||||
// Primary Header
|
||||
#include "SHCollider.h"
|
||||
// Project Headers
|
||||
#include "Math/Geometry/SHBoundingBox.h"
|
||||
#include "Math/Geometry/SHBoundingSphere.h"
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
|
@ -25,34 +28,41 @@ namespace SHADE
|
|||
, dirty { true }
|
||||
, 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
|
||||
: type { rhs.type}
|
||||
, isTrigger { rhs.isTrigger }
|
||||
, dirty { true }
|
||||
, shape { nullptr }
|
||||
{
|
||||
CreateShape();
|
||||
|
||||
// TODO(Diren): Copy transform data over
|
||||
}
|
||||
, shape { rhs.shape }
|
||||
, positionOffset { rhs.positionOffset }
|
||||
{}
|
||||
|
||||
SHCollider::SHCollider(SHCollider&& rhs) noexcept
|
||||
: type { rhs.type}
|
||||
, isTrigger { rhs.isTrigger }
|
||||
, dirty { true }
|
||||
, shape { nullptr }
|
||||
{
|
||||
CreateShape();
|
||||
|
||||
// TODO(Diren): Copy transform data over
|
||||
}
|
||||
, shape { rhs.shape }
|
||||
, positionOffset { rhs.positionOffset }
|
||||
{}
|
||||
|
||||
SHCollider::~SHCollider() noexcept
|
||||
{
|
||||
delete shape;
|
||||
shape = nullptr;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
|
@ -61,13 +71,14 @@ namespace SHADE
|
|||
|
||||
SHCollider& SHCollider::operator=(const SHCollider& rhs) noexcept
|
||||
{
|
||||
if (this == &rhs)
|
||||
return *this;
|
||||
|
||||
type = rhs.type;
|
||||
isTrigger = rhs.isTrigger;
|
||||
dirty = true;
|
||||
|
||||
CreateShape();
|
||||
|
||||
// TODO(Diren): Copy transform data over
|
||||
shape = rhs.shape;
|
||||
positionOffset = rhs.positionOffset;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
@ -77,10 +88,8 @@ namespace SHADE
|
|||
type = rhs.type;
|
||||
isTrigger = rhs.isTrigger;
|
||||
dirty = true;
|
||||
|
||||
CreateShape();
|
||||
|
||||
// TODO(Diren): Copy transform data over
|
||||
shape = rhs.shape;
|
||||
positionOffset = rhs.positionOffset;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
@ -104,11 +113,6 @@ namespace SHADE
|
|||
return type;
|
||||
}
|
||||
|
||||
SHShape* SHCollider::GetShape() const noexcept
|
||||
{
|
||||
return shape;
|
||||
}
|
||||
|
||||
float SHCollider::GetFriction() const noexcept
|
||||
{
|
||||
// TODO(Diren): Fix after implementing materials
|
||||
|
@ -126,36 +130,37 @@ 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;
|
||||
|
||||
delete shape;
|
||||
shape = new SHBoundingSphere{ positionOffset, radius };
|
||||
}
|
||||
|
||||
void SHCollider::SetIsTrigger(bool trigger) noexcept
|
||||
|
@ -184,32 +189,21 @@ namespace SHADE
|
|||
dirty = true;
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
#include <rttr/registration>
|
||||
|
||||
// Project Headers
|
||||
#include "Math/Geometry/SHBoundingBox.h"
|
||||
#include "Math/Geometry/SHShape.h"
|
||||
#include "Math/SHQuaternion.h"
|
||||
|
||||
namespace SHADE
|
||||
|
@ -32,15 +34,13 @@ namespace SHADE
|
|||
BOX
|
||||
, SPHERE
|
||||
, CAPSULE
|
||||
, CONVEX_HULL
|
||||
, CONVEX_MESH
|
||||
};
|
||||
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
/* Constructors & Destructor */
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
||||
SHCollider (Type colliderType);
|
||||
SHCollider (Type colliderType = Type::BOX);
|
||||
|
||||
SHCollider (const SHCollider& rhs) noexcept;
|
||||
SHCollider (SHCollider&& rhs) noexcept;
|
||||
|
@ -62,21 +62,21 @@ 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;
|
||||
|
@ -96,13 +96,7 @@ namespace SHADE
|
|||
SHShape* shape;
|
||||
SHVec3 positionOffset;
|
||||
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
/* Function Members */
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
||||
void CreateShape ();
|
||||
void CreateBoundingBox ();
|
||||
void CreateSphere ();
|
||||
RTTR_ENABLE()
|
||||
};
|
||||
|
||||
} // namespace SHADE
|
||||
|
|
|
@ -15,7 +15,8 @@
|
|||
|
||||
// Project Headers
|
||||
#include "ECS_Base/Managers/SHSystemManager.h"
|
||||
#include "SHPhysicsSystem.h"
|
||||
#include "ECS_Base/Managers/SHComponentManager.h"
|
||||
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
|
@ -23,15 +24,19 @@ 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
|
||||
{
|
||||
factory = nullptr;
|
||||
world = nullptr;
|
||||
rp3dBody = nullptr;
|
||||
}
|
||||
|
||||
|
@ -44,10 +49,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 +59,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 +69,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 +80,14 @@ namespace SHADE
|
|||
|
||||
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;
|
||||
rp3dTF.setPosition(RP3D_POS);
|
||||
rp3dTF.setPosition(position);
|
||||
rp3dTF.setOrientation(rp3dBody->getTransform().getOrientation());
|
||||
|
||||
rp3dBody->setTransform(rp3dTF);
|
||||
|
@ -96,11 +96,15 @@ namespace SHADE
|
|||
|
||||
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;
|
||||
rp3dTF.setPosition(rp3dBody->getTransform().getPosition());
|
||||
rp3dTF.setOrientation(RP3D_ORIENTATION);
|
||||
rp3dTF.setOrientation(orientation);
|
||||
|
||||
rp3dBody->setTransform(rp3dTF);
|
||||
prevTransform = rp3dTF;
|
||||
|
@ -108,14 +112,253 @@ 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, 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
|
|
@ -13,9 +13,9 @@
|
|||
#include <reactphysics3d/reactphysics3d.h>
|
||||
|
||||
// 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;
|
||||
|
@ -67,6 +65,21 @@ namespace SHADE
|
|||
void SetOrientation (const SHQuaternion& orientation) 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:
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
/* Data Members */
|
||||
|
@ -76,14 +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
|
|
@ -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);
|
||||
|
@ -207,69 +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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
auto* physicsObject = CreatePhysicsObject(entityID);
|
||||
|
||||
physicsObject->CreateRigidBody
|
||||
(
|
||||
EnsureTransform(entityID),
|
||||
SHComponentManager::GetComponent<SHRigidBodyComponent>(entityID),
|
||||
SHComponentManager::GetComponent_s<SHColliderComponent>(entityID)
|
||||
);
|
||||
}
|
||||
|
||||
void SHPhysicsSystem::AddCollider(EntityID entityID) noexcept
|
||||
|
@ -278,63 +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 = &(map.emplace(entityID, SHPhysicsObject{}).first->second);
|
||||
physicsObject->entityID = entityID;
|
||||
}
|
||||
auto* physicsObject = CreatePhysicsObject(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 };
|
||||
|
||||
// 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;
|
||||
physicsObject->CreateCollisionBody
|
||||
(
|
||||
EnsureTransform(entityID),
|
||||
SHComponentManager::GetComponent<SHColliderComponent>(entityID)
|
||||
);
|
||||
}
|
||||
|
||||
void SHPhysicsSystem::RemoveRigidBody(EntityID entityID) noexcept
|
||||
|
@ -343,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<SHColliderComponent>(entityID));
|
||||
|
||||
if (physicsObject->rp3dBody == nullptr)
|
||||
DestroyPhysicsObject(entityID);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
}
|
||||
physicsObject->AddCollider(collider);
|
||||
}
|
||||
|
||||
void SHPhysicsSystem::RemoveCollisionShape(EntityID entityID, int index)
|
||||
|
@ -475,6 +349,7 @@ namespace SHADE
|
|||
if (system->worldUpdated)
|
||||
{
|
||||
system->SyncTransforms();
|
||||
|
||||
// TODO(Diren): Handle trigger messages for scripting
|
||||
}
|
||||
}
|
||||
|
@ -483,6 +358,18 @@ namespace SHADE
|
|||
/* 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
|
||||
{
|
||||
const auto it = map.find(entityID);
|
||||
|
@ -495,6 +382,19 @@ 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();
|
||||
if (RP3D_ACTIVE != componentActive)
|
||||
physicsObject->rp3dBody->setIsActive(componentActive);
|
||||
}
|
||||
|
||||
|
||||
void SHPhysicsSystem::SyncRigidBodyComponents(std::vector<SHRigidBodyComponent>& denseArray) noexcept
|
||||
{
|
||||
if (denseArray.empty())
|
||||
|
@ -505,23 +405,16 @@ 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;
|
||||
|
||||
if (comp.dirtyFlags > 0)
|
||||
{
|
||||
SyncRigidBody(physicsObject, &comp);
|
||||
comp.dirtyFlags = 0;
|
||||
}
|
||||
physicsObject->SyncRigidBody(&comp);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -535,29 +428,23 @@ 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;
|
||||
|
||||
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;
|
||||
|
||||
|
@ -567,7 +454,7 @@ namespace SHADE
|
|||
|
||||
if (physicsObject.isRigidBody)
|
||||
{
|
||||
auto const* rbComponent = SHComponentManager::GetComponent<SHRigidBodyComponent>(ENTITY_ID);
|
||||
auto* rbComponent = SHComponentManager::GetComponent<SHRigidBodyComponent>(entityID);
|
||||
if (rbComponent->GetType() == SHRigidBodyComponent::Type::STATIC)
|
||||
continue;
|
||||
|
||||
|
@ -585,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<SHColliderComponent>(entityID);
|
||||
colliderComponent->position = CURRENT_TF.getPosition();
|
||||
colliderComponent->orientation = CURRENT_TF.getOrientation();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -593,155 +490,29 @@ 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<SHTransformComponent>(entityID);
|
||||
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
|
||||
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;
|
||||
const size_t NUM_FLAGS = sizeof(RB_FLAGS) * 8U;
|
||||
for (size_t i = 0; i < NUM_FLAGS; ++i)
|
||||
// Possibly redundant
|
||||
if (!tf)
|
||||
{
|
||||
// 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<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;
|
||||
}
|
||||
}
|
||||
}
|
||||
SHComponentManager::AddComponent<SHTransformComponent>(entityID);
|
||||
tf = SHComponentManager::GetComponent<SHTransformComponent>(entityID);
|
||||
}
|
||||
|
||||
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);
|
||||
return tf;
|
||||
}
|
||||
|
||||
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
|
|
@ -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"
|
||||
|
@ -100,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);
|
||||
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
|
@ -185,18 +186,18 @@ namespace SHADE
|
|||
/*---------------------------------------------------------------------------------*/
|
||||
/* Function Members */
|
||||
/*---------------------------------------------------------------------------------*/
|
||||
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<SHRigidBodyComponent>& denseArray) noexcept;
|
||||
void SyncColliderComponents (std::vector<SHColliderComponent>& 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 SyncCollider (SHPhysicsObject const* physicsObject, SHColliderComponent* comp) noexcept;
|
||||
// TODO(Diren): Remove when responsibility shifted to editor
|
||||
SHTransformComponent* EnsureTransform (EntityID entityID);
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -32,6 +32,7 @@ project "SHADE_Managed"
|
|||
"%{IncludeDir.yamlcpp}",
|
||||
"%{IncludeDir.RTTR}/include",
|
||||
"%{IncludeDir.dotnet}\\include",
|
||||
"%{IncludeDir.reactphysics3d}\\include",
|
||||
"%{wks.location}/SHADE_Engine/src"
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue