From e68ef89c00f9411501ed92682becde42ac7fe193 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Mon, 10 Oct 2022 21:21:20 +0800 Subject: [PATCH] synced rigidbodies through physics object --- .../Physics/Components/SHRigidBodyComponent.h | 3 + SHADE_Engine/src/Physics/SHPhysicsObject.cpp | 109 ++++++++++++++++-- SHADE_Engine/src/Physics/SHPhysicsObject.h | 12 +- SHADE_Engine/src/Physics/SHPhysicsSystem.cpp | 6 +- 4 files changed, 107 insertions(+), 23 deletions(-) diff --git a/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.h b/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.h index 285df810..8b62b7ff 100644 --- a/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.h +++ b/SHADE_Engine/src/Physics/Components/SHRigidBodyComponent.h @@ -144,6 +144,9 @@ namespace SHADE /* Data Members */ /*---------------------------------------------------------------------------------*/ + static constexpr size_t NUM_FLAGS = 8; + static constexpr size_t NUM_DIRTY_FLAGS = 16; + Type type; // rX rY rZ pX pY pZ slp g diff --git a/SHADE_Engine/src/Physics/SHPhysicsObject.cpp b/SHADE_Engine/src/Physics/SHPhysicsObject.cpp index a007c5fe..bbe8c726 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsObject.cpp +++ b/SHADE_Engine/src/Physics/SHPhysicsObject.cpp @@ -143,37 +143,122 @@ namespace SHADE rb->orientation = tf->GetWorldRotation(); } - void SHPhysicsObject::DestroyRigidBody() noexcept - { - - } - void SHPhysicsObject::CreateCollisionBody(const SHTransformComponent* tf, SHColliderComponent* c) { } - void SHPhysicsObject::DestroyCollisionBody() noexcept - { - - } - int SHPhysicsObject::AddCollider(SHCollider* collider) { return 0; } + void SHPhysicsObject::DestroyRigidBody() noexcept + { + + } + + void SHPhysicsObject::DestroyCollisionBody() noexcept + { + + } + void SHPhysicsObject::RemoveCollider(int index) noexcept { } - void SHPhysicsObject::SyncRigidBody() noexcept + void SHPhysicsObject::SyncRigidBody(SHRigidBodyComponent* rb) const noexcept { + SHASSERT(rp3dBody != nullptr, "ReactPhysics body does not exist!") + + if (rb->dirtyFlags == 0) + return; + + auto* rigidBody = reinterpret_cast(rp3dBody); + + const uint16_t RB_FLAGS = rb->dirtyFlags; + for (size_t i = 0; i < SHRigidBodyComponent::NUM_DIRTY_FLAGS; ++i) + { + // Check if current dirty flag has been set to true + if (RB_FLAGS & 1U << i) + { + switch (i) + { + case 0: // Gravity + { + rigidBody->enableGravity(rb->IsGravityEnabled()); + break; + } + case 1: // Sleeping + { + rigidBody->setIsAllowedToSleep(rb->IsAllowedToSleep()); + break; + } + case 2: // Linear Constraints + { + const rp3d::Vector3 CONSTRAINTS + { + rb->flags & 1U << 2 ? 0.0f : 1.0f, + rb->flags & 1U << 3 ? 0.0f : 1.0f, + rb->flags & 1U << 4 ? 0.0f : 1.0f + }; + + rigidBody->setLinearLockAxisFactor(CONSTRAINTS); + break; + } + case 3: // Angular Constraints + { + const rp3d::Vector3 CONSTRAINTS + { + rb->flags & 1U << 5 ? 0.0f : 1.0f, + rb->flags & 1U << 6 ? 0.0f : 1.0f, + rb->flags & 1U << 7 ? 0.0f : 1.0f + }; + + rigidBody->setAngularLockAxisFactor(CONSTRAINTS); + break; + } + case 4: // Type + { + rigidBody->setType(static_cast(rb->GetType())); + break; + } + case 5: // Mass + { + rigidBody->setMass(rb->GetMass()); + break; + } + case 6: // Drag + { + rigidBody->setLinearDamping(rb->GetDrag()); + break; + } + case 7: // Angular Drag + { + rigidBody->setAngularDamping(rb->GetAngularDrag()); + break; + } + case 8: // Linear Velocity + { + rigidBody->setLinearVelocity(rb->GetLinearVelocity()); + break; + } + case 9: // Angular Velocity + { + rigidBody->setAngularVelocity(rb->GetAngularVelocity()); + break; + } + default: break; + } + } + } + + rb->dirtyFlags = 0; } - void SHPhysicsObject::SyncColliders() noexcept + void SHPhysicsObject::SyncColliders(SHColliderComponent* c) const noexcept { } diff --git a/SHADE_Engine/src/Physics/SHPhysicsObject.h b/SHADE_Engine/src/Physics/SHPhysicsObject.h index c6ecd250..8562a0a1 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsObject.h +++ b/SHADE_Engine/src/Physics/SHPhysicsObject.h @@ -70,15 +70,15 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ void CreateRigidBody (const SHTransformComponent* tf, SHRigidBodyComponent* rb, SHColliderComponent* c); - void DestroyRigidBody () noexcept; - void CreateCollisionBody (const SHTransformComponent* tf, SHColliderComponent* c); int AddCollider (SHCollider* collider); - void RemoveCollider (int index) noexcept; - void DestroyCollisionBody () noexcept; - void SyncRigidBody () noexcept; - void SyncColliders () noexcept; + void DestroyRigidBody () noexcept; + void RemoveCollider (int index) noexcept; + void DestroyCollisionBody () noexcept; + + void SyncRigidBody (SHRigidBodyComponent* rb) const noexcept; + void SyncColliders (SHColliderComponent* c) const noexcept; private: /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp index 38c7e19c..b922cdd7 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp @@ -477,11 +477,7 @@ namespace SHADE if (!COMPONENT_ACTIVE) continue; - if (comp.dirtyFlags > 0) - { - SyncRigidBody(physicsObject, &comp); - comp.dirtyFlags = 0; - } + physicsObject->SyncRigidBody(&comp); } }