From 97013ba73ca16501a7bd13c5f12fe35e519800f6 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Thu, 9 Mar 2023 21:18:05 +0800 Subject: [PATCH] Added override for simulating gravity in SimulateBody method --- SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp | 6 ++---- SHADE_Engine/src/Physics/System/SHPhysicsSystem.h | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp index 2e5f5c2c..f19002e6 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp @@ -275,8 +275,6 @@ namespace SHADE for (size_t i = 0; i < SHVec3::SIZE; ++i) localInvInertia[i] = 1.0f / localInvInertia[i]; - - // Build raycast layer from colliders. If none exist....then this never stops simulating technically. // I'm too lazy to handle that case, so I'll just throw an error. uint16_t raycastLayers = 0; @@ -302,7 +300,7 @@ namespace SHADE int iterationCounter = simInfo.maxSteps; do { - raycastInfo.distance = linearVelocity.Length(); + raycastInfo.distance = linearVelocity.Length() * simInfo.timeStep; // Do not take the entire velocity's length as that is for an entire second. raycastInfo.ray.position = bodyPosition; raycastInfo.ray.direction = SHVec3::Normalise(linearVelocity); @@ -337,7 +335,7 @@ namespace SHADE // Integrate Velocities // Integrate forces and gravity into linear velocity const SHVec3 LINEAR_ACCELERATION = accumulatedForce * invMass; - const SHVec3 GRAVITATIONAL_ACCELERATION = rigidBody->IsGravityEnabled() ? worldState.settings.gravity * rigidBody->GetGravityScale() : SHVec3::Zero; + const SHVec3 GRAVITATIONAL_ACCELERATION = simInfo.simulateGravity ? worldState.settings.gravity * rigidBody->GetGravityScale() : SHVec3::Zero; linearVelocity += (LINEAR_ACCELERATION + GRAVITATIONAL_ACCELERATION) * simInfo.timeStep * LINEAR_LOCK; angularVelocity += worldInvInertia * (accumulatedTorque * simInfo.timeStep); diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h index f466481d..0a1e2057 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h +++ b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.h @@ -81,6 +81,7 @@ namespace SHADE // Whether or not to clear the force after the first iteration bool continuousForce = false; + bool simulateGravity = true; float timeStep = static_cast(SHPhysicsConstants::DEFAULT_FIXED_DT); int maxSteps = -1; };