SP3-2 Cleaned Up Physics System #85

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

View File

@ -67,6 +67,19 @@ namespace SHADE
XMStoreFloat4(this, XMQuaternionRotationMatrix(M));
}
SHQuaternion::SHQuaternion(const reactphysics3d::Vector3& rp3dEuler) noexcept
: XMFLOAT4( 0.0f, 0.0f, 0.0f, 1.0f )
{
const SHVec3& SHADE_VEC{ rp3dEuler };
const XMVECTOR V = XMLoadFloat3(&SHADE_VEC);
XMStoreFloat4(this, XMQuaternionRotationRollPitchYawFromVector(V));
}
SHQuaternion::SHQuaternion(const reactphysics3d::Quaternion& rp3dQuat) noexcept
: XMFLOAT4( rp3dQuat.x, rp3dQuat.y, rp3dQuat.z, rp3dQuat.w )
{}
/*-----------------------------------------------------------------------------------*/
/* Operator Overload Definitions */
/*-----------------------------------------------------------------------------------*/
@ -176,6 +189,11 @@ namespace SHADE
return XMQuaternionNotEqual(Q1, Q2);
}
SHQuaternion::operator reactphysics3d::Quaternion() const noexcept
{
return reactphysics3d::Quaternion{ x, y, z, w };
}
SHQuaternion operator*(float lhs, const SHQuaternion& rhs) noexcept
{
return rhs * lhs;

View File

@ -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,10 @@ namespace SHADE
[[nodiscard]] bool operator== (const SHQuaternion& rhs) const noexcept;
[[nodiscard]] bool operator!= (const SHQuaternion& rhs) const noexcept;
// Conversion to other math types used by SHADE
operator reactphysics3d::Quaternion () const noexcept;
/*---------------------------------------------------------------------------------*/
/* Getter Functions */
/*---------------------------------------------------------------------------------*/

View File

@ -46,6 +46,10 @@ namespace SHADE
: XMFLOAT2( _x, _y )
{}
SHVec2::SHVec2(const reactphysics3d::Vector2& rp3dVec2) noexcept
: XMFLOAT2( rp3dVec2.x, rp3dVec2.y )
{}
/*-----------------------------------------------------------------------------------*/
/* Operator Overload Definitions */
/*-----------------------------------------------------------------------------------*/
@ -213,6 +217,11 @@ namespace SHADE
}
}
SHVec2::operator reactphysics3d::Vector2() const noexcept
{
return reactphysics3d::Vector2{ x, y };
}
SHVec2 operator* (float lhs, const SHVec2& rhs) noexcept
{
SHVec2 result;

View File

@ -11,6 +11,8 @@
#pragma once
#include <DirectXMath.h>
#include <reactphysics3d/mathematics/Vector2.h>
#include <string>
#include <initializer_list>
@ -56,6 +58,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 */
/*---------------------------------------------------------------------------------*/
@ -86,6 +92,10 @@ namespace SHADE
[[nodiscard]] float operator[] (int index) const;
[[nodiscard]] float operator[] (size_t index) const;
// Conversion to other math types used by SHADE
operator reactphysics3d::Vector2 () const noexcept;
/*---------------------------------------------------------------------------------*/
/* Function Members */
/*---------------------------------------------------------------------------------*/

View File

@ -51,6 +51,10 @@ namespace SHADE
: XMFLOAT3( _x, _y, _z )
{}
SHVec3::SHVec3(const reactphysics3d::Vector3& rp3dVec3) noexcept
: XMFLOAT3( rp3dVec3.x, rp3dVec3.y, rp3dVec3.z )
{}
/*-----------------------------------------------------------------------------------*/
/* Operator Overload Definitions */
/*-----------------------------------------------------------------------------------*/
@ -223,6 +227,16 @@ namespace SHADE
}
}
SHVec3::operator reactphysics3d::Vector3() const noexcept
{
return reactphysics3d::Vector3{ x, y , z };
}
SHVec3::operator reactphysics3d::Quaternion() const noexcept
{
return reactphysics3d::Quaternion::fromEulerAngles(x, y, z);
}
SHVec3 operator* (float lhs, const SHVec3& rhs) noexcept
{
SHVec3 result;

View File

@ -11,6 +11,9 @@
#pragma once
#include <DirectXMath.h>
#include <reactphysics3d/mathematics/Vector3.h>
#include <reactphysics3d/mathematics/Quaternion.h>
#include <string>
#include <initializer_list>
@ -61,6 +64,10 @@ 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;
/*---------------------------------------------------------------------------------*/
/* Operator Overloads */
/*---------------------------------------------------------------------------------*/
@ -91,6 +98,10 @@ namespace SHADE
[[nodiscard]] float operator[] (int index) const;
[[nodiscard]] float operator[] (size_t index) const;
// Conversion to other math types used by SHADE
operator reactphysics3d::Vector3 () const noexcept;
operator reactphysics3d::Quaternion () const noexcept;
/*---------------------------------------------------------------------------------*/
/* Function Members */

View File

@ -44,10 +44,7 @@ namespace SHADE
SHVec3 result;
if (rp3dBody)
{
const auto& RP3D_RESULT = rp3dBody->getTransform().getPosition();
result = SHVec3{ RP3D_RESULT.x, RP3D_RESULT.y, RP3D_RESULT.z };
}
result = SHVec3{ rp3dBody->getTransform().getPosition() };
return result;
}
@ -57,10 +54,7 @@ namespace SHADE
SHQuaternion result;
if (rp3dBody)
{
const auto& RP3D_RESULT = rp3dBody->getTransform().getOrientation();
result = SHQuaternion{ RP3D_RESULT.x, RP3D_RESULT.y, RP3D_RESULT.z, RP3D_RESULT.w };
}
result = SHQuaternion{ rp3dBody->getTransform().getOrientation() };
return result;
}
@ -70,10 +64,7 @@ namespace SHADE
SHVec3 result;
if (rp3dBody)
{
const auto& RP3D_RESULT = rp3dBody->getTransform().getOrientation();
result = SHQuaternion{ RP3D_RESULT.x, RP3D_RESULT.y, RP3D_RESULT.z, RP3D_RESULT.w }.ToEuler();
}
result = SHQuaternion{ rp3dBody->getTransform().getOrientation() }.ToEuler();
return result;
}
@ -84,10 +75,8 @@ namespace SHADE
void SHPhysicsObject::SetPosition(const SHVec3& position) noexcept
{
const rp3d::Vector3 RP3D_POS { position.x, position.y, position.z };
rp3d::Transform rp3dTF;
rp3dTF.setPosition(RP3D_POS);
rp3dTF.setPosition(position);
rp3dTF.setOrientation(rp3dBody->getTransform().getOrientation());
rp3dBody->setTransform(rp3dTF);
@ -96,11 +85,9 @@ namespace SHADE
void SHPhysicsObject::SetOrientation(const SHQuaternion& orientation) noexcept
{
const rp3d::Quaternion RP3D_ORIENTATION { orientation.x, orientation.y, orientation.z, orientation.w };
rp3d::Transform rp3dTF;
rp3dTF.setPosition(rp3dBody->getTransform().getPosition());
rp3dTF.setOrientation(RP3D_ORIENTATION);
rp3dTF.setOrientation(orientation);
rp3dBody->setTransform(rp3dTF);
prevTransform = rp3dTF;

View File

@ -82,8 +82,5 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* Function Members */
/*---------------------------------------------------------------------------------*/
};
} // namespace SHADE

View File

@ -68,10 +68,7 @@ namespace SHADE
if (world)
{
const auto RP3D_GRAVITY = world->getGravity();
result.x = RP3D_GRAVITY.x;
result.y = RP3D_GRAVITY.y;
result.z = RP3D_GRAVITY.z;
result = world->getGravity();
}
else
{
@ -112,8 +109,7 @@ namespace SHADE
{
if (world)
{
const rp3d::Vector3 G { gravity.x, gravity.y, gravity.z };
world->setGravity(G);
world->setGravity(gravity);
}
else
{
@ -161,8 +157,7 @@ namespace SHADE
{
if (world)
{
const rp3d::Vector3 G { settings.gravity.x, settings.gravity.y, settings.gravity.z };
world->setGravity(G);
world->setGravity(settings.gravity);
world->setNbIterationsVelocitySolver(settings.numVelocitySolverIterations);
world->setNbIterationsPositionSolver(settings.numPositionSolverIterations);
world->enableSleeping(settings.sleepingEnabled);
@ -225,13 +220,7 @@ namespace SHADE
SHADE_TF = SHComponentManager::GetComponent<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 };
const rp3d::Transform RP3D_TF { SHADE_TF->GetWorldPosition(), SHADE_TF->GetWorldRotation() };
// If collider already exists
if (physicsObject->hasColliders)
@ -251,10 +240,7 @@ namespace SHADE
case SHCollider::Type::BOX:
{
SHBoundingBox* box = reinterpret_cast<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);
rp3d::BoxShape* newBox = factory.createBoxShape(box->GetHalfExtents());
// TODO(Diren): Handle offsets
physicsObject->rp3dBody->addCollider(newBox, RP3D_TF);
@ -296,13 +282,7 @@ namespace SHADE
SHADE_TF = SHComponentManager::GetComponent<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 };
const rp3d::Transform RP3D_TF { SHADE_TF->GetWorldPosition(), SHADE_TF->GetWorldRotation() };
// No rb
if (!physicsObject->isRigidBody)
@ -316,10 +296,7 @@ namespace SHADE
case SHCollider::Type::BOX:
{
SHBoundingBox* box = reinterpret_cast<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);
rp3d::BoxShape* newBox = factory.createBoxShape(box->GetHalfExtents());
// TODO(Diren): Handle offsets
physicsObject->rp3dBody->addCollider(newBox, RP3D_TF);
@ -401,10 +378,7 @@ namespace SHADE
case SHShape::Type::BOUNDING_BOX:
{
auto* box = reinterpret_cast<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);
rp3d::BoxShape* newBox = factory.createBoxShape(box->GetHalfExtents());
// TODO(Diren): Handle offsets
@ -593,12 +567,9 @@ namespace SHADE
}
// Convert RP3D Transform to SHADE
const SHVec3 SHADE_POS = SHVec3{ rp3dPos.x, rp3dPos.y, rp3dPos.z };
const SHVec3 SHADE_ROT = SHQuaternion{ rp3dRot.x, rp3dRot.y, rp3dRot.z, rp3dRot.w }.ToEuler();
auto* tfComponent = SHComponentManager::GetComponent<SHTransformComponent>(ENTITY_ID);
tfComponent->SetWorldPosition(SHADE_POS);
tfComponent->SetWorldRotation(SHADE_ROT);
tfComponent->SetWorldPosition(rp3dPos);
tfComponent->SetWorldRotation(SHQuaternion{ rp3dRot }.ToEuler());
// Cache transforms
physicsObject.prevTransform = CURRENT_TF;
@ -660,18 +631,12 @@ namespace SHADE
}
case 8: // Linear Velocity
{
const SHVec3& SHADE_VEL = comp->GetLinearVelocity();
rp3d::Vector3 RP3D_VEL { SHADE_VEL.x, SHADE_VEL.y, SHADE_VEL.z };
rigidBody->setLinearVelocity(RP3D_VEL);
rigidBody->setLinearVelocity(comp->GetLinearVelocity());
break;
}
case 9: // Angular Velocity
{
const SHVec3& SHADE_VEL = comp->GetAngularVelocity();
rp3d::Vector3 RP3D_VEL { SHADE_VEL.x, SHADE_VEL.y, SHADE_VEL.z };
rigidBody->setAngularVelocity(RP3D_VEL);
rigidBody->setAngularVelocity(comp->GetAngularVelocity());
break;
}
default: break;
@ -718,12 +683,9 @@ namespace SHADE
case SHCollider::Type::BOX:
{
SHBoundingBox* box = reinterpret_cast<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);
rp3dBoxShape->setHalfExtents(box->GetHalfExtents());
if (rp3dBoxShape)
{

View File

@ -32,6 +32,7 @@ project "SHADE_Managed"
"%{IncludeDir.yamlcpp}",
"%{IncludeDir.RTTR}/include",
"%{IncludeDir.dotnet}\\include",
"%{IncludeDir.reactphysics3d}\\include",
"%{wks.location}/SHADE_Engine/src"
}