Reworked Physics Simulate Body #429

Merged
direnbharwani merged 4 commits from SP3-2-Physics into main 2023-03-20 14:56:29 +08:00
2 changed files with 3 additions and 4 deletions
Showing only changes of commit 97013ba73c - Show all commits

View File

@ -275,8 +275,6 @@ namespace SHADE
for (size_t i = 0; i < SHVec3::SIZE; ++i) for (size_t i = 0; i < SHVec3::SIZE; ++i)
localInvInertia[i] = 1.0f / localInvInertia[i]; localInvInertia[i] = 1.0f / localInvInertia[i];
// Build raycast layer from colliders. If none exist....then this never stops simulating technically. // 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. // I'm too lazy to handle that case, so I'll just throw an error.
uint16_t raycastLayers = 0; uint16_t raycastLayers = 0;
@ -302,7 +300,7 @@ namespace SHADE
int iterationCounter = simInfo.maxSteps; int iterationCounter = simInfo.maxSteps;
do 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.position = bodyPosition;
raycastInfo.ray.direction = SHVec3::Normalise(linearVelocity); raycastInfo.ray.direction = SHVec3::Normalise(linearVelocity);
@ -337,7 +335,7 @@ namespace SHADE
// Integrate Velocities // Integrate Velocities
// Integrate forces and gravity into linear velocity // Integrate forces and gravity into linear velocity
const SHVec3 LINEAR_ACCELERATION = accumulatedForce * invMass; 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; linearVelocity += (LINEAR_ACCELERATION + GRAVITATIONAL_ACCELERATION) * simInfo.timeStep * LINEAR_LOCK;
angularVelocity += worldInvInertia * (accumulatedTorque * simInfo.timeStep); angularVelocity += worldInvInertia * (accumulatedTorque * simInfo.timeStep);

View File

@ -81,6 +81,7 @@ namespace SHADE
// Whether or not to clear the force after the first iteration // Whether or not to clear the force after the first iteration
bool continuousForce = false; bool continuousForce = false;
bool simulateGravity = true;
float timeStep = static_cast<float>(SHPhysicsConstants::DEFAULT_FIXED_DT); float timeStep = static_cast<float>(SHPhysicsConstants::DEFAULT_FIXED_DT);
int maxSteps = -1; int maxSteps = -1;
}; };