|
| 1 | +import MirabufSceneObject from "@/mirabuf/MirabufSceneObject" |
| 2 | +import { MiraType } from "@/mirabuf/MirabufLoader" |
| 3 | +import SceneRenderer from "../scene/SceneRenderer" |
| 4 | +import SimulationSystem from "@/systems/simulation/SimulationSystem" |
| 5 | +import World from "../World" |
| 6 | +import { convertJoltMat44ToThreeMatrix4 } from "@/util/TypeConversions" |
| 7 | +import JOLT from "@/util/loading/JoltSyncLoader" |
| 8 | +import * as THREE from "three" |
| 9 | + |
| 10 | +class RobotPositionTracker { |
| 11 | + private static _mapBoundaryY: number = -4 |
| 12 | + private static _offMapPenalty: number = 0 |
| 13 | + |
| 14 | + public static update(sceneRenderer: SceneRenderer): void { |
| 15 | + const robots = [...sceneRenderer.sceneObjects.values()].filter( |
| 16 | + (obj): obj is MirabufSceneObject => obj instanceof MirabufSceneObject && obj.miraType === MiraType.ROBOT |
| 17 | + ) |
| 18 | + |
| 19 | + robots.forEach(robot => { |
| 20 | + const rootNodeId = robot.getRootNodeId() |
| 21 | + if (!rootNodeId) { |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + const rootBody = World.physicsSystem.getBody(rootNodeId) |
| 26 | + const rootTransform = convertJoltMat44ToThreeMatrix4(rootBody.GetWorldTransform()) |
| 27 | + |
| 28 | + const rootPosition = new THREE.Vector3() |
| 29 | + const rootRotation = new THREE.Quaternion() |
| 30 | + const rootScale = new THREE.Vector3() |
| 31 | + rootTransform.decompose(rootPosition, rootRotation, rootScale) |
| 32 | + |
| 33 | + if (rootPosition.y < this._mapBoundaryY) { |
| 34 | + SimulationSystem.robotPenalty(robot, this._offMapPenalty, "Robot fell off the map") |
| 35 | + |
| 36 | + // TODO: Once driver station is implemented, we should reset the robot to the driver station position |
| 37 | + const resetPosition = new JOLT.RVec3(0, 0.2, 0) |
| 38 | + const resetRotation = JOLT.Quat.prototype.sIdentity() |
| 39 | + const zeroVelocity = new JOLT.Vec3(0, 0, 0) |
| 40 | + |
| 41 | + robot.mirabufInstance.parser.rigidNodes.forEach(rigidNode => { |
| 42 | + const bodyId = robot.mechanism.getBodyByNodeId(rigidNode.id) |
| 43 | + if (bodyId) { |
| 44 | + World.physicsSystem.setBodyPositionRotationAndVelocity( |
| 45 | + bodyId, |
| 46 | + resetPosition, |
| 47 | + resetRotation, |
| 48 | + zeroVelocity, |
| 49 | + zeroVelocity, |
| 50 | + true |
| 51 | + ) |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + JOLT.destroy(resetPosition) |
| 56 | + JOLT.destroy(resetRotation) |
| 57 | + JOLT.destroy(zeroVelocity) |
| 58 | + } |
| 59 | + }) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +export default RobotPositionTracker |
0 commit comments