Implemented a custom physics engine #316

Merged
direnbharwani merged 95 commits from SHPhysics into main 2023-01-23 15:55:45 +08:00
5 changed files with 74 additions and 11 deletions
Showing only changes of commit 6b8232ae91 - Show all commits

View File

@ -11,7 +11,7 @@
RigidBody Component:
Type: Dynamic
Auto Mass: false
Mass: 1
Mass: 10
Drag: 1
Angular Drag: 1
Use Gravity: false

View File

@ -83,8 +83,8 @@ public class PhysicsTestObj : Script
if (Input.GetKeyDown(moveInputKeys[i]))
move[i] = true;
//if (Input.GetKeyDown(rotateInputKeys[i]))
// rotate[i] = true;
if (Input.GetKeyDown(rotateInputKeys[i]))
rotate[i] = true;
}
}
@ -97,8 +97,9 @@ public class PhysicsTestObj : Script
if (shouldMove)
{
Vector3 offset = new Vector3(0.25f, 0.0f, 0.0f);
rb.AddForceAtLocalPos(moveVec[i] * forceAmount, offset);
//Vector3 offset = new Vector3(0.25f, 0.0f, 0.0f);
//rb.AddForceAtLocalPos(moveVec[i] * forceAmount, offset);
rb.AddForce(moveVec[i] * forceAmount);
move[i] = false;
}

View File

@ -511,11 +511,12 @@ namespace SHADE
// Reset angular velocity along X-axis
angularVelocity.x = 0.0f;
// Set inertia tensor on the x-axis to 0
localInvInertia.m[0][0] = worldInvInertia.m[0][0] = 0.0f;
localInvInertia.m[0][0] = 0.0f;
}
else
{
flags &= ~VALUE;
computeInertiaTensor();
}
}
@ -530,11 +531,12 @@ namespace SHADE
// Reset angular velocity along Y-axis
angularVelocity.y = 0.0f;
// Set inertia tensor on the y-axis to 0
localInvInertia.m[1][1] = worldInvInertia.m[1][1] = 0.0f;
localInvInertia.m[1][1] = 0.0f;
}
else
{
flags &= ~VALUE;
computeInertiaTensor();
}
}
@ -549,11 +551,12 @@ namespace SHADE
// Reset angular velocity along Z-axis
angularVelocity.z = 0.0f;
// Set inertia tensor on the z-axis to 0
localInvInertia.m[2][2] = worldInvInertia.m[2][2] = 0.0f;
localInvInertia.m[2][2] = 0.0f;
}
else
{
flags &= ~VALUE;
computeInertiaTensor();
}
}
@ -606,20 +609,59 @@ namespace SHADE
void SHRigidBody::ComputeMassData() noexcept
{
// TODO: Compute total inertia and centroid from composited colliders using the Parallel Axis Theorem.
computeCentroid();
computeMassAndInertiaTensor();
}
/*-----------------------------------------------------------------------------------*/
/* Private Member Function Definition */
/*-----------------------------------------------------------------------------------*/
void SHRigidBody::computeCentroid() noexcept
{
// TODO
}
void SHRigidBody::computeMass() noexcept
{
// TODO: If auto mass in enabled, compute total mass based from each collider.
// TODO: If auto mass disabled, compute inertia tensor for each shape using the ratio of its mass / total mass by comparing the volume / total volume.
}
if (collider && !collider->GetCollisionShapes().empty())
void SHRigidBody::computeInertiaTensor() noexcept
{
// TODO: Compute total inertia from composited colliders using the Parallel Axis Theorem.
if (!collider || collider->GetCollisionShapes().empty())
{
localInvInertia.m[0][0] = localInvInertia.m[1][1] = localInvInertia.m[2][2] = invMass;
}
else
{
// HACK: For now, take only the first shape as we are testing with non-composited colliders. We are using the center as the centroid.
const auto* FIRST_SHAPE = collider->GetCollisionShape(0);
localInvInertia = SHMatrix::Inverse(FIRST_SHAPE->GetInertiaTensor(1.0f / invMass));
}
else
}
void SHRigidBody::computeMassAndInertiaTensor() noexcept
{
// TODO: If auto mass in enabled, compute total mass based from each collider.
// TODO: If auto mass disabled, compute inertia tensor for each shape using the ratio of its mass / total mass by comparing the volume / total volume.
// TODO: Compute total inertia from composited colliders using the Parallel Axis Theorem.
if (!collider || collider->GetCollisionShapes().empty())
{
localInvInertia.m[0][0] = localInvInertia.m[1][1] = localInvInertia.m[2][2] = invMass;
}
else
{
// HACK: For now, take only the first shape as we are testing with non-composited colliders. We are using the center as the centroid.
const auto* FIRST_SHAPE = collider->GetCollisionShape(0);
localInvInertia = SHMatrix::Inverse(FIRST_SHAPE->GetInertiaTensor(1.0f / invMass));
}
}
} // namespace SHADE

View File

@ -252,6 +252,16 @@ namespace SHADE
uint16_t flags;
SHMotionState motionState;
/*-----------------------------------------------------------------------------------*/
/* Member Functions */
/*-----------------------------------------------------------------------------------*/
void computeCentroid () noexcept;
void computeMass () noexcept;
void computeInertiaTensor () noexcept;
void computeMassAndInertiaTensor() noexcept;
};
} // namespace SHADE

View File

@ -92,8 +92,12 @@ namespace SHADE
// Link with collider if it exists
if (collider)
{
collider->SetRigidBody(rigidBody);
rigidBody->SetCollider(collider);
}
rigidBody->ComputeMassData();
return rigidBody;
}
@ -119,7 +123,10 @@ namespace SHADE
// Link with rigidBody if it exists
if (rigidBody)
{
rigidBody->SetCollider(collider);
collider->SetRigidBody(rigidBody);
}
return collider;
}
@ -131,7 +138,10 @@ namespace SHADE
// Unlink with rigid body
if (rigidBody)
{
rigidBody->SetCollider(nullptr);
rigidBody->ComputeMassData();
}
}
/*-----------------------------------------------------------------------------------*/