Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adaptive Physics Time Step #989

Merged
merged 9 commits into from
Jun 29, 2024
25 changes: 21 additions & 4 deletions fission/src/systems/physics/PhysicsSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ const RobotLayers: number[] = [
// Please update this accordingly.
const COUNT_OBJECT_LAYERS = 10

export const SIMULATION_PERIOD = 1.0 / 120.0
const STANDARD_SUB_STEPS = 3
export const STANDARD_SIMULATION_PERIOD = 1.0 / 60.0
const MIN_SIMULATION_PERIOD = 1.0 / 120.0
const MAX_SIMULATION_PERIOD = 1.0 / 10.0
const STANDARD_SUB_STEPS = 2
const TIMESTEP_ADJUSTMENT = 0.0001

let lastDeltaT = STANDARD_SIMULATION_PERIOD
export function GetLastDeltaT(): number {
return lastDeltaT
}

/**
* The PhysicsSystem handles all Jolt Phyiscs interactions within Synthesis.
Expand Down Expand Up @@ -701,8 +709,17 @@ class PhysicsSystem extends WorldSystem {
return this._joltPhysSystem.GetBodyLockInterface().TryGetBody(bodyId)
}

public Update(_: number): void {
this._joltInterface.Step(SIMULATION_PERIOD, STANDARD_SUB_STEPS)
public Update(deltaT: number): void {
const diffDeltaT = deltaT - lastDeltaT
lastDeltaT = lastDeltaT + Math.min(TIMESTEP_ADJUSTMENT, Math.max(-TIMESTEP_ADJUSTMENT, diffDeltaT))

lastDeltaT = Math.min(MAX_SIMULATION_PERIOD, Math.max(MIN_SIMULATION_PERIOD, lastDeltaT))

const substeps = Math.max(1, Math.floor((STANDARD_SIMULATION_PERIOD / lastDeltaT) * STANDARD_SUB_STEPS))

console.log(`DeltaT: ${lastDeltaT.toFixed(5)}, Substeps: ${substeps}`)

this._joltInterface.Step(lastDeltaT, substeps)
}

public Destroy(): void {
Expand Down
4 changes: 2 additions & 2 deletions fission/src/systems/simulation/driver/HingeDriver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Jolt from "@barclah/jolt-physics"
import Driver from "./Driver"
import { SIMULATION_PERIOD } from "@/systems/physics/PhysicsSystem"
import { GetLastDeltaT } from "@/systems/physics/PhysicsSystem"
import JOLT from "@/util/loading/JoltSyncLoader"

class HingeDriver extends Driver {
Expand Down Expand Up @@ -60,7 +60,7 @@ class HingeDriver extends Driver {
const springSettings = motorSettings.mSpringSettings

// These values were selected based on the suggestions of the documentation for stiff control.
springSettings.mFrequency = 20 * (1.0 / SIMULATION_PERIOD)
springSettings.mFrequency = 20 * (1.0 / GetLastDeltaT())
springSettings.mDamping = 0.995

motorSettings.mSpringSettings = springSettings
Expand Down
4 changes: 2 additions & 2 deletions fission/src/systems/simulation/driver/SliderDriver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Jolt from "@barclah/jolt-physics"
import Driver from "./Driver"
import { SIMULATION_PERIOD } from "@/systems/physics/PhysicsSystem"
import { GetLastDeltaT } from "@/systems/physics/PhysicsSystem"
import JOLT from "@/util/loading/JoltSyncLoader"

class SliderDriver extends Driver {
Expand Down Expand Up @@ -33,7 +33,7 @@ class SliderDriver extends Driver {

const motorSettings = this._constraint.GetMotorSettings()
const springSettings = motorSettings.mSpringSettings
springSettings.mFrequency = 20 * (1.0 / SIMULATION_PERIOD)
springSettings.mFrequency = 20 * (1.0 / GetLastDeltaT())
springSettings.mDamping = 0.995

motorSettings.mSpringSettings = springSettings
Expand Down
Loading