Fixed a fatal error with rigid body rotations.

This commit is contained in:
Diren D Bharwani 2023-01-16 02:43:31 +08:00
parent 6c76849cd8
commit dab109bc77
4 changed files with 52 additions and 50 deletions

View File

@ -109,10 +109,7 @@ namespace SHADE
prevOrientation = orientation; prevOrientation = orientation;
SHQuaternion qv{ velocity.x * dt, velocity.y * dt, velocity.z * dt, 0.0f }; orientation += orientation * SHQuaternion{ velocity.x, velocity.y, velocity.z, 0.0f } * dt * 0.5f;
qv *= orientation;
orientation += qv * 0.5f;
orientation = SHQuaternion::Normalise(orientation); orientation = SHQuaternion::Normalise(orientation);
} }

View File

@ -142,7 +142,7 @@ namespace SHADE
rigidBody.linearVelocity += (LINEAR_ACCELERATION + GRAVITATIONAL_ACCELERATION) * dt; rigidBody.linearVelocity += (LINEAR_ACCELERATION + GRAVITATIONAL_ACCELERATION) * dt;
// Integrate torque into angular velocity // Integrate torque into angular velocity
rigidBody.angularVelocity += rigidBody.worldInvInertia * (rigidBody.accumulatedTorque * dt); rigidBody.angularVelocity += rigidBody.worldInvInertia * rigidBody.accumulatedTorque * dt;
// Apply drag (exponentially applied) // Apply drag (exponentially applied)
rigidBody.linearVelocity *= 1.0f / (1.0f + dt * rigidBody.linearDrag); rigidBody.linearVelocity *= 1.0f / (1.0f + dt * rigidBody.linearDrag);

View File

@ -637,16 +637,17 @@ namespace SHADE
void SHRigidBody::ComputeWorldData() noexcept void SHRigidBody::ComputeWorldData() noexcept
{ {
const SHMatrix R = SHMatrix::Rotate(motionState.orientation);
const SHMatrix RT = SHMatrix::Transpose(R);
// Compute world centroid
worldCentroid = (R * localCentroid) + motionState.position;
if (bodyType == Type::STATIC) if (bodyType == Type::STATIC)
return; return;
const SHMatrix ROTATION = SHMatrix::Rotate(motionState.orientation);
// Compute world inertia // Compute world inertia
worldInvInertia = SHMatrix::Transpose(ROTATION) * localInvInertia * ROTATION; worldInvInertia = R * (localInvInertia * RT);
// Compute world centroid
worldCentroid = (ROTATION * localCentroid) + motionState.position;
} }
void SHRigidBody::ComputeMassData() noexcept void SHRigidBody::ComputeMassData() noexcept

View File

@ -19,7 +19,6 @@
#include "Scene/SHSceneManager.h" #include "Scene/SHSceneManager.h"
#include "Scripting/SHScriptEngine.h" #include "Scripting/SHScriptEngine.h"
namespace SHADE namespace SHADE
{ {
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -48,6 +47,8 @@ namespace SHADE
// Interpolate transforms for rendering. // Interpolate transforms for rendering.
// Only rigid bodies can move due to physics, so we run through the rigid body component dense set. // Only rigid bodies can move due to physics, so we run through the rigid body component dense set.
if (physicsSystem->worldUpdated)
{
const auto& RIGIDBODY_DENSE = SHComponentManager::GetDense<SHRigidBodyComponent>(); const auto& RIGIDBODY_DENSE = SHComponentManager::GetDense<SHRigidBodyComponent>();
for (auto& rigidBodyComponent : RIGIDBODY_DENSE) for (auto& rigidBodyComponent : RIGIDBODY_DENSE)
{ {
@ -91,6 +92,9 @@ namespace SHADE
* TODO: Test if the scene graph transforms abides by setting world position. Collisions will ignore the scene graph hierarchy. * TODO: Test if the scene graph transforms abides by setting world position. Collisions will ignore the scene graph hierarchy.
*/ */
} }
}
// Collision & Trigger messages // Collision & Trigger messages
if (scriptingSystem != nullptr) if (scriptingSystem != nullptr)