Skip to content

Commit

Permalink
fix: Move assemblies into the center of the field.
Browse files Browse the repository at this point in the history
Also finally moved the ground up as well.
  • Loading branch information
HunterBarclay committed Sep 22, 2024
1 parent ceaf33f commit 269d333
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
34 changes: 31 additions & 3 deletions fission/src/mirabuf/MirabufSceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import MirabufInstance from "./MirabufInstance"
import MirabufParser, { ParseErrorSeverity, RigidNodeId, RigidNodeReadOnly } from "./MirabufParser"
import World from "@/systems/World"
import Jolt from "@barclah/jolt-physics"
import { JoltMat44_ThreeMatrix4 } from "@/util/TypeConversions"
import { JoltMat44_ThreeMatrix4, JoltVec3_ThreeVector3 } from "@/util/TypeConversions"
import * as THREE from "three"
import JOLT from "@/util/loading/JoltSyncLoader"
import { BodyAssociate, LayerReserve } from "@/systems/physics/PhysicsSystem"
Expand Down Expand Up @@ -177,6 +177,27 @@ class MirabufSceneObject extends SceneObject {
setSpotlightAssembly(this)

this.UpdateBatches()

const bounds = this.ComputeBoundingBox()
if (!Number.isFinite(bounds.min.y)) return

const offset = new JOLT.Vec3(
-(bounds.min.x + bounds.max.x) / 2.0,
0.1 + ((bounds.max.y - bounds.min.y) / 2.0) - ((bounds.min.y + bounds.max.y) / 2.0),
-(bounds.min.z + bounds.max.z) / 2.0
)

this._mirabufInstance.parser.rigidNodes.forEach(rn => {
const jBodyId = this._mechanism.GetBodyByNodeId(rn.id)
if (!jBodyId) return

const newPos = World.PhysicsSystem.GetBody(jBodyId).GetPosition().Add(offset)
World.PhysicsSystem.SetBodyPosition(jBodyId, newPos)

JOLT.destroy(newPos)
})

this.UpdateMeshTransforms()
}

public Update(): void {
Expand Down Expand Up @@ -410,8 +431,15 @@ class MirabufSceneObject extends SceneObject {
}

const jBody = World.PhysicsSystem.GetBody(jRootId)
const comTransform = JoltMat44_ThreeMatrix4(jBody.GetCenterOfMassTransform())
gizmo.SetTransform(comTransform)
if (jBody.IsStatic()) {
const aaBox = jBody.GetWorldSpaceBounds()
const mat = new THREE.Matrix4(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)
const center = aaBox.mMin.Add(aaBox.mMax).Div(2.0)
mat.compose(JoltVec3_ThreeVector3(center), new THREE.Quaternion(0,0,0,1), new THREE.Vector3(1,1,1))
gizmo.SetTransform(mat)
} else {
gizmo.SetTransform(JoltMat44_ThreeMatrix4(jBody.GetCenterOfMassTransform()))
}
}

private getPreferences(): void {
Expand Down
6 changes: 3 additions & 3 deletions fission/src/systems/physics/PhysicsSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class PhysicsSystem extends WorldSystem {
const ground = this.CreateBox(
new THREE.Vector3(5.0, 0.5, 5.0),
undefined,
new THREE.Vector3(0.0, -2.0, 0.0),
new THREE.Vector3(0.0, -0.5, 0.0),
undefined
)
ground.SetFriction(FLOOR_FRICTION)
Expand Down Expand Up @@ -1056,7 +1056,7 @@ class PhysicsSystem extends WorldSystem {
* @param id The id of the body
* @param position The new position of the body
*/
public SetBodyPosition(id: Jolt.BodyID, position: Jolt.Vec3, activate: boolean = true): void {
public SetBodyPosition(id: Jolt.BodyID, position: Jolt.RVec3, activate: boolean = true): void {
if (!this.IsBodyAdded(id)) {
return
}
Expand All @@ -1080,7 +1080,7 @@ class PhysicsSystem extends WorldSystem {
)
}

public SetBodyPositionAndRotation(id: Jolt.BodyID, position: Jolt.Vec3, rotation: Jolt.Quat, activate: boolean = true): void {
public SetBodyPositionAndRotation(id: Jolt.BodyID, position: Jolt.RVec3, rotation: Jolt.Quat, activate: boolean = true): void {
if (!this.IsBodyAdded(id)) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion fission/src/systems/scene/SceneRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class SceneRenderer extends WorldSystem {
this._scene.add(ambientLight)

const ground = new THREE.Mesh(new THREE.BoxGeometry(10, 1, 10), this.CreateToonMaterial(GROUND_COLOR))
ground.position.set(0.0, -2.0, 0.0)
ground.position.set(0.0, -0.5, 0.0)
ground.receiveShadow = true
ground.castShadow = true
this._scene.add(ground)
Expand Down

0 comments on commit 269d333

Please sign in to comment.