diff --git a/fission/src/mirabuf/MirabufSceneObject.ts b/fission/src/mirabuf/MirabufSceneObject.ts index 30ab81727..4bbba5ea8 100644 --- a/fission/src/mirabuf/MirabufSceneObject.ts +++ b/fission/src/mirabuf/MirabufSceneObject.ts @@ -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" @@ -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 { @@ -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 { diff --git a/fission/src/systems/physics/PhysicsSystem.ts b/fission/src/systems/physics/PhysicsSystem.ts index e688a3a7f..1d501f758 100644 --- a/fission/src/systems/physics/PhysicsSystem.ts +++ b/fission/src/systems/physics/PhysicsSystem.ts @@ -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) @@ -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 } @@ -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 } diff --git a/fission/src/systems/scene/SceneRenderer.ts b/fission/src/systems/scene/SceneRenderer.ts index e552493e1..2c7bef9d4 100644 --- a/fission/src/systems/scene/SceneRenderer.ts +++ b/fission/src/systems/scene/SceneRenderer.ts @@ -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)