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
4 changed files with 107 additions and 23 deletions
Showing only changes of commit e68ef89c00 - Show all commits

View File

@ -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

View File

@ -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<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;
}
}
}
void SHPhysicsObject::SyncColliders() noexcept
rb->dirtyFlags = 0;
}
void SHPhysicsObject::SyncColliders(SHColliderComponent* c) const noexcept
{
}

View File

@ -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 DestroyRigidBody () noexcept;
void RemoveCollider (int index) noexcept;
void DestroyCollisionBody () noexcept;
void SyncRigidBody () noexcept;
void SyncColliders () noexcept;
void SyncRigidBody (SHRigidBodyComponent* rb) const noexcept;
void SyncColliders (SHColliderComponent* c) const noexcept;
private:
/*---------------------------------------------------------------------------------*/

View File

@ -477,11 +477,7 @@ namespace SHADE
if (!COMPONENT_ACTIVE)
continue;
if (comp.dirtyFlags > 0)
{
SyncRigidBody(physicsObject, &comp);
comp.dirtyFlags = 0;
}
physicsObject->SyncRigidBody(&comp);
}
}