Skip to content

Commit

Permalink
Upgrade JoltPhysics.JS to v0.31.0 (#1119)
Browse files Browse the repository at this point in the history
* Upgrade to JoltPhysics 0.31.0

* chore: Format
  • Loading branch information
HunterBarclay authored Jan 9, 2025
1 parent cd8659c commit 6aa6f0c
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 55 deletions.
2 changes: 1 addition & 1 deletion fission/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"playwright:install": "bun x playwright install || npx playwright install"
},
"dependencies": {
"@barclah/jolt-physics": "^0.24.1",
"@barclah/jolt-physics": "^0.31.0",
"@haensl/google-analytics": "^1.2.0",
"@mui/base": "^5.0.0-beta.40",
"@mui/icons-material": "^5.15.20",
Expand Down
3 changes: 2 additions & 1 deletion fission/src/mirabuf/EjectableSceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
JoltMat44_ThreeMatrix4,
JoltQuat_ThreeQuaternion,
ThreeQuaternion_JoltQuat,
ThreeVector3_JoltRVec3,
ThreeVector3_JoltVec3,
} from "@/util/TypeConversions"
import * as THREE from "three"
Expand Down Expand Up @@ -88,7 +89,7 @@ class EjectableSceneObject extends SceneObject {
const rotation = new THREE.Quaternion(0, 0, 0, 1)
bodyTransform.decompose(position, rotation, new THREE.Vector3(1, 1, 1))

World.PhysicsSystem.SetBodyPosition(this._gamePieceBodyId, ThreeVector3_JoltVec3(position), false)
World.PhysicsSystem.SetBodyPosition(this._gamePieceBodyId, ThreeVector3_JoltRVec3(position), false)
World.PhysicsSystem.SetBodyRotation(this._gamePieceBodyId, ThreeQuaternion_JoltQuat(rotation), false)
}
}
Expand Down
4 changes: 2 additions & 2 deletions fission/src/mirabuf/IntakeSensorSceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Array_ThreeMatrix4,
JoltMat44_ThreeMatrix4,
ThreeQuaternion_JoltQuat,
ThreeVector3_JoltVec3,
ThreeVector3_JoltRVec3,
} from "@/util/TypeConversions"
import { OnContactPersistedEvent } from "@/systems/physics/ContactEvents"

Expand Down Expand Up @@ -70,7 +70,7 @@ class IntakeSensorSceneObject extends SceneObject {
const rotation = new THREE.Quaternion(0, 0, 0, 1)
bodyTransform.decompose(position, rotation, new THREE.Vector3(1, 1, 1))

World.PhysicsSystem.SetBodyPosition(this._joltBodyId, ThreeVector3_JoltVec3(position))
World.PhysicsSystem.SetBodyPosition(this._joltBodyId, ThreeVector3_JoltRVec3(position))
World.PhysicsSystem.SetBodyRotation(this._joltBodyId, ThreeQuaternion_JoltQuat(rotation))
}
}
Expand Down
6 changes: 3 additions & 3 deletions fission/src/mirabuf/ScoringZoneSceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
Array_ThreeMatrix4,
JoltMat44_ThreeMatrix4,
ThreeQuaternion_JoltQuat,
ThreeVector3_JoltVec3,
ThreeVector3_JoltRVec3,
} from "@/util/TypeConversions"
import MirabufSceneObject, { RigidNodeAssociate } from "./MirabufSceneObject"
import JOLT from "@/util/loading/JoltSyncLoader"
Expand Down Expand Up @@ -84,7 +84,7 @@ class ScoringZoneSceneObject extends SceneObject {
)
const props = DeltaFieldTransforms_PhysicalProp(this._deltaTransformation, fieldTransformation)

World.PhysicsSystem.SetBodyPosition(this._joltBodyId, ThreeVector3_JoltVec3(props.translation))
World.PhysicsSystem.SetBodyPosition(this._joltBodyId, ThreeVector3_JoltRVec3(props.translation))
World.PhysicsSystem.SetBodyRotation(this._joltBodyId, ThreeQuaternion_JoltQuat(props.rotation))
const shapeSettings = new JOLT.BoxShapeSettings(
new JOLT.Vec3(props.scale.x / 2, props.scale.y / 2, props.scale.z / 2)
Expand Down Expand Up @@ -148,7 +148,7 @@ class ScoringZoneSceneObject extends SceneObject {
)
const props = DeltaFieldTransforms_PhysicalProp(this._deltaTransformation, fieldTransformation)

World.PhysicsSystem.SetBodyPosition(this._joltBodyId, ThreeVector3_JoltVec3(props.translation))
World.PhysicsSystem.SetBodyPosition(this._joltBodyId, ThreeVector3_JoltRVec3(props.translation))
World.PhysicsSystem.SetBodyRotation(this._joltBodyId, ThreeQuaternion_JoltQuat(props.rotation))
const shapeSettings = new JOLT.BoxShapeSettings(
new JOLT.Vec3(props.scale.x / 2, props.scale.y / 2, props.scale.z / 2)
Expand Down
57 changes: 30 additions & 27 deletions fission/src/systems/physics/PhysicsSystem.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import {
JoltRVec3_JoltVec3,
JoltVec3_JoltRVec3,
JoltVec3_ThreeVector3,
MirabufFloatArr_JoltFloat3,
MirabufFloatArr_JoltVec3,
MirabufVector3_JoltRVec3,
MirabufVector3_JoltVec3,
ThreeMatrix4_JoltMat44,
ThreeVector3_JoltRVec3,
ThreeVector3_JoltVec3,
_JoltQuat,
} from "../../util/TypeConversions"
Expand Down Expand Up @@ -234,7 +238,7 @@ class PhysicsSystem extends WorldSystem {
const shape = new JOLT.BoxShape(size, 0.1)
JOLT.destroy(size)

const pos = position ? ThreeVector3_JoltVec3(position) : new JOLT.Vec3(0.0, 0.0, 0.0)
const pos = position ? ThreeVector3_JoltRVec3(position) : new JOLT.RVec3(0.0, 0.0, 0.0)
const rot = _JoltQuat(rotation)
const creationSettings = new JOLT.BodyCreationSettings(
shape,
Expand Down Expand Up @@ -269,7 +273,7 @@ class PhysicsSystem extends WorldSystem {
position: THREE.Vector3 | undefined,
rotation: THREE.Euler | THREE.Quaternion | undefined
) {
const pos = position ? ThreeVector3_JoltVec3(position) : new JOLT.Vec3(0.0, 0.0, 0.0)
const pos = position ? ThreeVector3_JoltRVec3(position) : new JOLT.RVec3(0.0, 0.0, 0.0)
const rot = _JoltQuat(rotation)
const creationSettings = new JOLT.BodyCreationSettings(
shape,
Expand Down Expand Up @@ -464,14 +468,14 @@ class PhysicsSystem extends WorldSystem {
const hingeConstraintSettings = new JOLT.HingeConstraintSettings()

const jointOrigin = jointDefinition.origin
? MirabufVector3_JoltVec3(jointDefinition.origin as mirabuf.Vector3)
: new JOLT.Vec3(0, 0, 0)
? MirabufVector3_JoltRVec3(jointDefinition.origin as mirabuf.Vector3)
: new JOLT.RVec3(0, 0, 0)
// TODO: Offset transformation for robot builder.
const jointOriginOffset = jointInstance.offset
? MirabufVector3_JoltVec3(jointInstance.offset as mirabuf.Vector3)
: new JOLT.Vec3(0, 0, 0)
? MirabufVector3_JoltRVec3(jointInstance.offset as mirabuf.Vector3)
: new JOLT.RVec3(0, 0, 0)

const anchorPoint = jointOrigin.Add(jointOriginOffset)
const anchorPoint = jointOrigin.AddRVec3(jointOriginOffset)
hingeConstraintSettings.mPoint1 = hingeConstraintSettings.mPoint2 = anchorPoint

const rotationalFreedom = jointDefinition.rotational!.rotationalFreedom!
Expand Down Expand Up @@ -532,14 +536,14 @@ class PhysicsSystem extends WorldSystem {
const sliderConstraintSettings = new JOLT.SliderConstraintSettings()

const jointOrigin = jointDefinition.origin
? MirabufVector3_JoltVec3(jointDefinition.origin as mirabuf.Vector3)
: new JOLT.Vec3(0, 0, 0)
? MirabufVector3_JoltRVec3(jointDefinition.origin as mirabuf.Vector3)
: new JOLT.RVec3(0, 0, 0)
// TODO: Offset transformation for robot builder.
const jointOriginOffset = jointInstance.offset
? MirabufVector3_JoltVec3(jointInstance.offset as mirabuf.Vector3)
: new JOLT.Vec3(0, 0, 0)
? MirabufVector3_JoltRVec3(jointInstance.offset as mirabuf.Vector3)
: new JOLT.RVec3(0, 0, 0)

const anchorPoint = jointOrigin.Add(jointOriginOffset)
const anchorPoint = jointOrigin.AddRVec3(jointOriginOffset)
sliderConstraintSettings.mPoint1 = sliderConstraintSettings.mPoint2 = anchorPoint

const prismaticFreedom = jointDefinition.prismatic!.prismaticFreedom!
Expand Down Expand Up @@ -594,27 +598,27 @@ class PhysicsSystem extends WorldSystem {
const fixedSettings = new JOLT.FixedConstraintSettings()

const jointOrigin = jointDefinition.origin
? MirabufVector3_JoltVec3(jointDefinition.origin as mirabuf.Vector3)
: new JOLT.Vec3(0, 0, 0)
? MirabufVector3_JoltRVec3(jointDefinition.origin as mirabuf.Vector3)
: new JOLT.RVec3(0, 0, 0)
const jointOriginOffset = jointInstance.offset
? MirabufVector3_JoltVec3(jointInstance.offset as mirabuf.Vector3)
: new JOLT.Vec3(0, 0, 0)
? MirabufVector3_JoltRVec3(jointInstance.offset as mirabuf.Vector3)
: new JOLT.RVec3(0, 0, 0)

const anchorPoint = jointOrigin.Add(jointOriginOffset)
const anchorPoint = jointOrigin.AddRVec3(jointOriginOffset)
fixedSettings.mPoint1 = fixedSettings.mPoint2 = anchorPoint

const rotationalFreedom = jointDefinition.rotational!.rotationalFreedom!

// No scaling, these are unit vectors
const miraAxis = rotationalFreedom.axis! as mirabuf.Vector3
const miraAxisX: number = (versionNum < 5 ? -miraAxis.x : miraAxis.x) ?? 0
const axis: Jolt.Vec3 = new JOLT.Vec3(miraAxisX, miraAxis.y ?? 0, miraAxis.z ?? 0)
const axis: Jolt.RVec3 = new JOLT.RVec3(miraAxisX, miraAxis.y ?? 0, miraAxis.z ?? 0)

const bounds = bodyWheel.GetShape().GetLocalBounds()
const radius = (bounds.mMax.GetY() - bounds.mMin.GetY()) / 2.0

const wheelSettings = new JOLT.WheelSettingsWV()
wheelSettings.mPosition = anchorPoint.Add(axis.Mul(0.1))
wheelSettings.mPosition = JoltRVec3_JoltVec3(anchorPoint.AddRVec3(axis.Mul(0.1)))
wheelSettings.mMaxSteerAngle = 0.0
wheelSettings.mMaxHandBrakeTorque = 0.0
wheelSettings.mRadius = radius * 1.05
Expand Down Expand Up @@ -733,7 +737,7 @@ class PhysicsSystem extends WorldSystem {
const c = constraints[i]
const hingeSettings = new JOLT.HingeConstraintSettings()
hingeSettings.mMaxFrictionTorque = c.friction
hingeSettings.mPoint1 = hingeSettings.mPoint2 = anchorPoint
hingeSettings.mPoint1 = hingeSettings.mPoint2 = JoltVec3_JoltRVec3(anchorPoint)
hingeSettings.mHingeAxis1 = hingeSettings.mHingeAxis2 = c.axis.Normalized()
hingeSettings.mNormalAxis1 = hingeSettings.mNormalAxis2 = getPerpendicular(hingeSettings.mHingeAxis1)
if (c.upper && c.lower) {
Expand Down Expand Up @@ -1029,7 +1033,7 @@ class PhysicsSystem extends WorldSystem {

const bodySettings = new JOLT.BodyCreationSettings(
shape,
new JOLT.Vec3(0.0, 0.0, 0.0),
new JOLT.RVec3(0.0, 0.0, 0.0),
new JOLT.Quat(0, 0, 0, 1),
rn.isDynamic ? JOLT.EMotionType_Dynamic : JOLT.EMotionType_Static,
rnLayer
Expand Down Expand Up @@ -1162,7 +1166,7 @@ class PhysicsSystem extends WorldSystem {
* @returns Either the hit results of the closest object in the ray's path, or undefined if nothing was hit.
*/
public RayCast(from: Jolt.Vec3, dir: Jolt.Vec3, ...ignoreBodies: Jolt.BodyID[]): RayCastHit | undefined {
const ray = new JOLT.RayCast(from, dir)
const ray = new JOLT.RRayCast(JoltVec3_JoltRVec3(from), dir)

const raySettings = new JOLT.RayCastSettings()
raySettings.mTreatConvexAsSolid = false
Expand All @@ -1181,7 +1185,7 @@ class PhysicsSystem extends WorldSystem {
if (!collector.HadHit()) return undefined

const hitPoint = ray.GetPointOnRay(collector.mHit.mFraction)
return { data: collector.mHit, point: hitPoint, ray: ray }
return { data: collector.mHit, point: JoltRVec3_JoltVec3(hitPoint), ray: ray }
}

/**
Expand Down Expand Up @@ -1287,7 +1291,7 @@ class PhysicsSystem extends WorldSystem {
const rot = new JOLT.Quat(0, 0, 0, 1)
const creationSettings = new JOLT.BodyCreationSettings(
shape,
position,
JoltVec3_JoltRVec3(position),
rot,
JOLT.EMotionType_Dynamic,
LAYER_GHOST
Expand Down Expand Up @@ -1326,8 +1330,7 @@ class PhysicsSystem extends WorldSystem {
this._bodies.push(ghostBodyId)

const constraintSettings = new JOLT.PointConstraintSettings()
constraintSettings.set_mPoint1(anchorPoint)
constraintSettings.set_mPoint2(anchorPoint)
constraintSettings.mPoint1 = constraintSettings.mPoint2 = JoltVec3_JoltRVec3(anchorPoint)
const constraint = constraintSettings.Create(ghostBody, body)
this._joltPhysSystem.AddConstraint(constraint)
this._constraints.push(constraint)
Expand Down Expand Up @@ -1610,7 +1613,7 @@ function tryGetPerpendicular(vec: Jolt.Vec3, toCheck: Jolt.Vec3): Jolt.Vec3 | un
export type RayCastHit = {
data: Jolt.RayCastResult
point: Jolt.Vec3
ray: Jolt.RayCast
ray: Jolt.RRayCast
}

/**
Expand Down
4 changes: 2 additions & 2 deletions fission/src/systems/scene/GizmoSceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import InputSystem from "../input/InputSystem"
import World from "../World"
import MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
import { Object3D, PerspectiveCamera } from "three"
import { ThreeQuaternion_JoltQuat, JoltMat44_ThreeMatrix4, ThreeVector3_JoltVec3 } from "@/util/TypeConversions"
import { ThreeQuaternion_JoltQuat, JoltMat44_ThreeMatrix4, ThreeVector3_JoltRVec3 } from "@/util/TypeConversions"
import { RigidNodeId } from "@/mirabuf/MirabufParser"

export type GizmoMode = "translate" | "rotate" | "scale"
Expand Down Expand Up @@ -210,7 +210,7 @@ class GizmoSceneObject extends SceneObject {

World.PhysicsSystem.SetBodyPositionAndRotation(
jBodyId,
ThreeVector3_JoltVec3(position),
ThreeVector3_JoltRVec3(position),
ThreeQuaternion_JoltQuat(rotation)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
import IntakeDriver from "../driver/IntakeDriver"
import EjectorDriver from "../driver/EjectorDriver"
import GamepieceManipBehavior from "../behavior/synthesis/GamepieceManipBehavior"
import { JoltVec3_JoltRVec3 } from "@/util/TypeConversions"

class SynthesisBrain extends Brain {
public static brainIndexMap = new Map<number, SynthesisBrain>()
Expand Down Expand Up @@ -128,14 +129,14 @@ class SynthesisBrain extends Brain {

// Determines which wheels and stimuli belong to which side of the robot
for (let i = 0; i < wheelDrivers.length; i++) {
const wheelPos = fixedConstraints[i].GetConstraintToBody1Matrix().GetTranslation()
const wheelPos = JoltVec3_JoltRVec3(fixedConstraints[i].GetConstraintToBody1Matrix().GetTranslation())

const robotCOM = World.PhysicsSystem.GetBody(
this._mechanism.constraints[0].childBody
).GetCenterOfMassPosition() as Jolt.Vec3
const rightVector = new JOLT.Vec3(1, 0, 0)
).GetCenterOfMassPosition()
const rightVector = new JOLT.RVec3(1, 0, 0)

const dotProduct = rightVector.Dot(wheelPos.Sub(robotCOM))
const dotProduct = rightVector.Dot(wheelPos.SubRVec3(robotCOM))

if (dotProduct < 0) {
rightWheels.push(wheelDrivers[i])
Expand Down
7 changes: 5 additions & 2 deletions fission/src/test/PhysicsSystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { test, expect, describe, assert } from "vitest"
import PhysicsSystem, { LayerReserve } from "../systems/physics/PhysicsSystem"
import MirabufParser from "@/mirabuf/MirabufParser"
import * as THREE from "three"
import Jolt from "@barclah/jolt-physics"
import MirabufCachingService, { MiraType } from "@/mirabuf/MirabufLoader"
import { JoltRVec3_JoltVec3 } from "@/util/TypeConversions"

describe("Physics Sansity Checks", () => {
test("Convex Hull Shape (Cube)", () => {
Expand Down Expand Up @@ -54,7 +54,10 @@ describe("GodMode", () => {
test("Basic", () => {
const system = new PhysicsSystem()
const box = system.CreateBox(new THREE.Vector3(1, 1, 1), 1, new THREE.Vector3(0, 0, 0), undefined)
const [ghostObject, ghostConstraint] = system.CreateGodModeBody(box.GetID(), box.GetPosition() as Jolt.Vec3)
const [ghostObject, ghostConstraint] = system.CreateGodModeBody(
box.GetID(),
JoltRVec3_JoltVec3(box.GetPosition())
)

assert(system.GetBody(ghostObject.GetID()) != undefined)
assert(system.GetBody(box.GetID()) != undefined)
Expand Down
10 changes: 5 additions & 5 deletions fission/src/test/util/TypeConversions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ describe("Mirabuf to Three Conversions", () => {
})

describe("Jolt to Three Conversions", () => {
function compareMat(jM: Jolt.Mat44, tM: THREE.Matrix4) {
function compareMat(jM: Jolt.RMat44, tM: THREE.Matrix4) {
const threeArr = tM.toArray()

for (let c = 0; c < 4; c++) {
Expand Down Expand Up @@ -253,7 +253,7 @@ describe("Jolt to Three Conversions", () => {
}

test("Jolt.Mat44 [Identity] -> THREE.Matrix4", () => {
const tmp = new JOLT.Mat44()
const tmp = new JOLT.RMat44()
const joltMat = tmp.sIdentity()
const threeMat = JoltMat44_ThreeMatrix4(joltMat)

Expand All @@ -263,7 +263,7 @@ describe("Jolt to Three Conversions", () => {
})

test("Jolt.Mat44 [+X Axis Rotation] -> THREE.Matrix4", () => {
const joltMat = new JOLT.Mat44()
const joltMat = new JOLT.RMat44()
const c0 = new JOLT.Vec4(1, 0, 0, 0)
const c1 = new JOLT.Vec4(0, 0, -1, 0)
const c2 = new JOLT.Vec4(0, 1, 0, 0)
Expand All @@ -285,7 +285,7 @@ describe("Jolt to Three Conversions", () => {
})

test("Jolt.Mat44 [-X Axis Rotation] -> THREE.Matrix4", () => {
const joltMat = new JOLT.Mat44()
const joltMat = new JOLT.RMat44()
const c0 = new JOLT.Vec4(1, 0, 0, 0)
const c1 = new JOLT.Vec4(0, 0, 1, 0)
const c2 = new JOLT.Vec4(0, -1, 0, 0)
Expand All @@ -307,7 +307,7 @@ describe("Jolt to Three Conversions", () => {
})

test("Jolt.Mat44 [X,Y Translation] -> THREE.Matrix4", () => {
const joltMat = new JOLT.Mat44()
const joltMat = new JOLT.RMat44()
const c0 = new JOLT.Vec4(1, 0, 0, 0)
const c1 = new JOLT.Vec4(0, 1, 0, 0)
const c2 = new JOLT.Vec4(0, 0, 1, 0)
Expand Down
11 changes: 7 additions & 4 deletions fission/src/ui/panels/DebugPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import { usePanelControlContext } from "../PanelContext"
import APS from "@/aps/APS"
import PreferencesSystem from "@/systems/preferences/PreferencesSystem"
import JOLT from "@/util/loading/JoltSyncLoader"
import Jolt from "@barclah/jolt-physics"
import Label from "../components/Label"
import { colorNameToVar } from "../ThemeContext"
import { SynthesisIcons } from "../components/StyledComponents"
import { Global_AddToast } from "../components/GlobalUIControls"
import { JoltRVec3_JoltVec3 } from "@/util/TypeConversions"

const LabelStyled = styled(Label)({
fontWeight: 700,
Expand All @@ -39,16 +39,19 @@ async function TestGodMode() {
return
}
const robotPosition = World.PhysicsSystem.GetBody(rootNodeId).GetPosition()
const [ghostBody, _ghostConstraint] = World.PhysicsSystem.CreateGodModeBody(rootNodeId, robotPosition as Jolt.Vec3)
const [ghostBody, _ghostConstraint] = World.PhysicsSystem.CreateGodModeBody(
rootNodeId,
JoltRVec3_JoltVec3(robotPosition)
)

// Move ghostBody to demonstrate godMode movement
await new Promise(f => setTimeout(f, 1000))
World.PhysicsSystem.SetBodyPosition(
ghostBody.GetID(),
new JOLT.Vec3(robotPosition.GetX(), robotPosition.GetY() + 2, robotPosition.GetZ())
new JOLT.RVec3(robotPosition.GetX(), robotPosition.GetY() + 2, robotPosition.GetZ())
)
await new Promise(f => setTimeout(f, 1000))
World.PhysicsSystem.SetBodyPosition(ghostBody.GetID(), new JOLT.Vec3(2, 2, 2))
World.PhysicsSystem.SetBodyPosition(ghostBody.GetID(), new JOLT.RVec3(2, 2, 2))
}

const DebugPanel: React.FC<PanelPropsImpl> = ({ panelId }) => {
Expand Down
Loading

0 comments on commit 6aa6f0c

Please sign in to comment.