Skip to content

Commit

Permalink
Adaptive Physics Time Step (#989)
Browse files Browse the repository at this point in the history
  • Loading branch information
HunterBarclay authored Jun 29, 2024
2 parents 900c890 + d18dc59 commit 8fbe56b
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 16 deletions.
16 changes: 12 additions & 4 deletions .github/workflows/FissionESLintFormat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,25 @@ jobs:
cd fission
npm run lint
continue-on-error: true
- name: Check EsLint
run: |
if [ ${{ steps.linter-validation.outcome }} == "success" ]; then
echo "EsLint Validation Passed"
else
echo "EsLint Validation Failed"
exit 1
fi
- name: Prettier
id: prettier-validation
run: |
cd fission
npm run prettier
continue-on-error: true
- name: Check Success
- name: Check Prettier
run: |
if [ ${{ steps.linter-validation.outcome }} == "success" ]; then
echo "Format Validation Passed"
if [ ${{ steps.prettier-validation.outcome }} == "success" ]; then
echo "Prettier Validation Passed"
else
echo "Format Validation Failed"
echo "Prettier Validation Failed"
exit 1
fi
6 changes: 3 additions & 3 deletions fission/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"scripts": {
"dev": "vite --open",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives",
"preview": "vite preview --base=/fission/",
"test": "vitest",
"lint:fix": "bun run lint --fix || npm run lint --fix",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives",
"lint:fix": "eslint . --ext ts,tsx --report-unused-disable-directives --fix",
"prettier": "bun x prettier src --check || npx prettier src --check",
"prettier:fix": "bun run prettier --write || npm run prettier --write",
"prettier:fix": "bun x prettier src --write || npx prettier src --write",
"format": "(bun run prettier:fix && bun run lint:fix) || (npm run prettier:fix && npm run lint:fix)",
"build:prod": "tsc && vite build --base=/fission/",
"assetpack": "curl -o public/assetpack.zip https://synthesis.autodesk.com/Downloadables/assetpack.zip && tar -xf public/assetpack.zip -C public/",
Expand Down
3 changes: 2 additions & 1 deletion fission/src/mirabuf/MirabufParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ class MirabufParser {
}

// 1: Initial Rigidgroups from ancestorial breaks in joints
(Object.keys(assembly.data!.joints!.jointInstances!) as string[]).forEach(key => {
const jointInstanceKeys = Object.keys(assembly.data!.joints!.jointInstances!) as string[]
jointInstanceKeys.forEach(key => {
if (key != GROUNDED_JOINT_ID) {
const jInst = assembly.data!.joints!.jointInstances![key]
const [ancestorA, ancestorB] = this.FindAncestorialBreak(jInst.parentPart!, jInst.childPart!)
Expand Down
28 changes: 24 additions & 4 deletions fission/src/systems/physics/PhysicsSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,18 @@ const LAYER_GHOST = 10
// Please update this accordingly.
const COUNT_OBJECT_LAYERS = 11

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 MIN_SUBSTEPS = 2
const MAX_SUBSTEPS = 6
const STANDARD_SUB_STEPS = 4
const TIMESTEP_ADJUSTMENT = 0.0001

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

// Friction constants
const FLOOR_FRICTION = 0.7
Expand Down Expand Up @@ -704,8 +714,18 @@ 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))

let substeps = Math.max(1, Math.floor((lastDeltaT / STANDARD_SIMULATION_PERIOD) * STANDARD_SUB_STEPS))
substeps = Math.min(MAX_SUBSTEPS, Math.max(MIN_SUBSTEPS, substeps))

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, { DriverControlMode } 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 @@ -61,7 +61,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, { DriverControlMode } 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 @@ -62,7 +62,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.999

motorSettings.mSpringSettings = springSettings
Expand Down
3 changes: 3 additions & 0 deletions fission/src/ui/modals/mirabuf/ImportMirabufModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ const ImportMirabufModal: React.FC<ModalPropsImpl> = ({ modalId }) => {
const [selectedFolder, setSelectedFolder] = useState<Folder | undefined>(undefined)

const [hubs, setHubs] = useState<Hub[] | undefined>(undefined)
// prettier-ignore
useEffect(() => {
(async () => {
setHubs(await getHubs())
})()
}, [])

const [projects, setProjects] = useState<Project[] | undefined>(undefined)
// prettier-ignore
useEffect(() => {
(async () => {
if (selectedHub) {
Expand All @@ -49,6 +51,7 @@ const ImportMirabufModal: React.FC<ModalPropsImpl> = ({ modalId }) => {
}, [selectedHub])

const [folderData, setFolderData] = useState<Data[] | undefined>(undefined)
// prettier-ignore
useEffect(() => {
(async () => {
if (selectedProject) {
Expand Down
2 changes: 2 additions & 0 deletions fission/src/ui/modals/spawning/SpawningModals.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const AddRobotsModal: React.FC<ModalPropsImpl> = ({ modalId }) => {

const [remoteRobots, setRemoteRobots] = useState<MirabufEntry[] | null>(null)

// prettier-ignore
useEffect(() => {
(async () => {
fetch("/api/mira/manifest.json")
Expand Down Expand Up @@ -95,6 +96,7 @@ export const AddFieldsModal: React.FC<ModalPropsImpl> = ({ modalId }) => {

const [remoteFields, setRemoteFields] = useState<MirabufEntry[] | null>(null)

// prettier-ignore
useEffect(() => {
(async () => {
fetch("/api/mira/manifest.json")
Expand Down
1 change: 1 addition & 0 deletions fission/src/util/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const mousePosition = (x: number, y: number) => {
el?.dispatchEvent(event)
}

// prettier-ignore
export const addGlobalFunc = <T>(name: string, func: (...args: any[]) => T) => {
(window as any)[name] = func
}
Expand Down

0 comments on commit 8fbe56b

Please sign in to comment.