-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/dev' into dhruv/delete-assembly-fix
- Loading branch information
Showing
25 changed files
with
1,299 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import SceneObject from "@/systems/scene/SceneObject" | ||
import MirabufSceneObject from "./MirabufSceneObject" | ||
import Jolt from "@barclah/jolt-physics" | ||
import World from "@/systems/World" | ||
import { | ||
Array_ThreeMatrix4, | ||
JoltMat44_ThreeMatrix4, | ||
JoltQuat_ThreeQuaternion, | ||
ThreeQuaternion_JoltQuat, | ||
ThreeVector3_JoltVec3, | ||
} from "@/util/TypeConversions" | ||
import * as THREE from "three" | ||
|
||
class EjectableSceneObject extends SceneObject { | ||
private _parentAssembly: MirabufSceneObject | ||
private _gamePieceBodyId?: Jolt.BodyID | ||
|
||
private _parentBodyId?: Jolt.BodyID | ||
private _deltaTransformation?: THREE.Matrix4 | ||
private _ejectVelocity?: number | ||
|
||
public get gamePieceBodyId() { | ||
return this._gamePieceBodyId | ||
} | ||
|
||
public constructor(parentAssembly: MirabufSceneObject, gamePieceBody: Jolt.BodyID) { | ||
super() | ||
|
||
console.debug("Trying to create ejectable...") | ||
|
||
this._parentAssembly = parentAssembly | ||
this._gamePieceBodyId = gamePieceBody | ||
} | ||
|
||
public Setup(): void { | ||
if (this._parentAssembly.ejectorPreferences && this._gamePieceBodyId) { | ||
this._parentBodyId = this._parentAssembly.mechanism.nodeToBody.get( | ||
this._parentAssembly.ejectorPreferences.parentNode ?? this._parentAssembly.rootNodeId | ||
) | ||
|
||
this._deltaTransformation = Array_ThreeMatrix4(this._parentAssembly.ejectorPreferences.deltaTransformation) | ||
this._ejectVelocity = this._parentAssembly.ejectorPreferences.ejectorVelocity | ||
|
||
World.PhysicsSystem.DisablePhysicsForBody(this._gamePieceBodyId) | ||
|
||
console.debug("Ejectable created successfully!") | ||
} | ||
} | ||
|
||
public Update(): void { | ||
if (this._parentBodyId && this._deltaTransformation && this._gamePieceBodyId) { | ||
if (!World.PhysicsSystem.IsBodyAdded(this._gamePieceBodyId)) { | ||
this._gamePieceBodyId = undefined | ||
return | ||
} | ||
|
||
// I had a think and free wrote this matrix math on a whim. It worked first try and I honestly can't quite remember how it works... -Hunter | ||
const gpBody = World.PhysicsSystem.GetBody(this._gamePieceBodyId) | ||
const posToCOM = JoltMat44_ThreeMatrix4(gpBody.GetCenterOfMassTransform()).premultiply( | ||
JoltMat44_ThreeMatrix4(gpBody.GetWorldTransform()).invert() | ||
) | ||
|
||
const body = World.PhysicsSystem.GetBody(this._parentBodyId) | ||
const bodyTransform = posToCOM | ||
.invert() | ||
.premultiply( | ||
this._deltaTransformation.clone().premultiply(JoltMat44_ThreeMatrix4(body.GetWorldTransform())) | ||
) | ||
const position = new THREE.Vector3(0, 0, 0) | ||
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.SetBodyRotation(this._gamePieceBodyId, ThreeQuaternion_JoltQuat(rotation), false) | ||
} | ||
} | ||
|
||
public Eject() { | ||
if (!this._parentBodyId || !this._ejectVelocity || !this._gamePieceBodyId) { | ||
return | ||
} | ||
|
||
if (!World.PhysicsSystem.IsBodyAdded(this._gamePieceBodyId)) { | ||
this._gamePieceBodyId = undefined | ||
return | ||
} | ||
|
||
const parentBody = World.PhysicsSystem.GetBody(this._parentBodyId) | ||
const gpBody = World.PhysicsSystem.GetBody(this._gamePieceBodyId) | ||
const ejectDir = new THREE.Vector3(0, 0, 1) | ||
.applyQuaternion(JoltQuat_ThreeQuaternion(gpBody.GetRotation())) | ||
.normalize() | ||
|
||
World.PhysicsSystem.EnablePhysicsForBody(this._gamePieceBodyId) | ||
gpBody.SetLinearVelocity( | ||
parentBody.GetLinearVelocity().Add(ThreeVector3_JoltVec3(ejectDir.multiplyScalar(this._ejectVelocity))) | ||
) | ||
gpBody.SetAngularVelocity(parentBody.GetAngularVelocity()) | ||
|
||
this._parentBodyId = undefined | ||
} | ||
|
||
public Dispose(): void { | ||
console.debug("Destroying ejectable") | ||
|
||
if (this._gamePieceBodyId) { | ||
World.PhysicsSystem.EnablePhysicsForBody(this._gamePieceBodyId) | ||
} | ||
} | ||
} | ||
|
||
export default EjectableSceneObject |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import SceneObject from "@/systems/scene/SceneObject" | ||
import MirabufSceneObject, { RigidNodeAssociate } from "./MirabufSceneObject" | ||
import Jolt from "@barclah/jolt-physics" | ||
import * as THREE from "three" | ||
import World from "@/systems/World" | ||
import JOLT from "@/util/loading/JoltSyncLoader" | ||
import { | ||
Array_ThreeMatrix4, | ||
JoltMat44_ThreeMatrix4, | ||
ThreeQuaternion_JoltQuat, | ||
ThreeVector3_JoltVec3, | ||
} from "@/util/TypeConversions" | ||
|
||
class IntakeSensorSceneObject extends SceneObject { | ||
private _parentAssembly: MirabufSceneObject | ||
private _parentBodyId?: Jolt.BodyID | ||
private _deltaTransformation?: THREE.Matrix4 | ||
|
||
private _joltBodyId?: Jolt.BodyID | ||
private _mesh?: THREE.Mesh | ||
|
||
public constructor(parentAssembly: MirabufSceneObject) { | ||
super() | ||
|
||
console.debug("Trying to create intake sensor...") | ||
|
||
this._parentAssembly = parentAssembly | ||
} | ||
|
||
public Setup(): void { | ||
if (this._parentAssembly.intakePreferences) { | ||
this._parentBodyId = this._parentAssembly.mechanism.nodeToBody.get( | ||
this._parentAssembly.intakePreferences.parentNode ?? this._parentAssembly.rootNodeId | ||
) | ||
|
||
this._deltaTransformation = Array_ThreeMatrix4(this._parentAssembly.intakePreferences.deltaTransformation) | ||
|
||
this._joltBodyId = World.PhysicsSystem.CreateSensor( | ||
new JOLT.SphereShapeSettings(this._parentAssembly.intakePreferences.zoneDiameter / 2.0) | ||
) | ||
if (!this._joltBodyId) { | ||
console.error("Failed to create intake. No Jolt Body") | ||
return | ||
} | ||
|
||
this._mesh = World.SceneRenderer.CreateSphere( | ||
this._parentAssembly.intakePreferences.zoneDiameter / 2.0, | ||
World.SceneRenderer.CreateToonMaterial(0x5eeb67) | ||
) | ||
World.SceneRenderer.scene.add(this._mesh) | ||
|
||
console.debug("Intake sensor created successfully!") | ||
} | ||
} | ||
|
||
public Update(): void { | ||
if (this._joltBodyId && this._parentBodyId && this._deltaTransformation) { | ||
const parentBody = World.PhysicsSystem.GetBody(this._parentBodyId) | ||
const bodyTransform = this._deltaTransformation | ||
.clone() | ||
.premultiply(JoltMat44_ThreeMatrix4(parentBody.GetWorldTransform())) | ||
const position = new THREE.Vector3(0, 0, 0) | ||
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.SetBodyRotation(this._joltBodyId, ThreeQuaternion_JoltQuat(rotation)) | ||
|
||
if (this._mesh) { | ||
this._mesh.position.setFromMatrixPosition(bodyTransform) | ||
this._mesh.rotation.setFromRotationMatrix(bodyTransform) | ||
} | ||
|
||
if (!World.PhysicsSystem.isPaused) { | ||
// TEMPORARY GAME PIECE DETECTION | ||
const hitRes = World.PhysicsSystem.RayCast(ThreeVector3_JoltVec3(position), new JOLT.Vec3(0, 0, 3)) | ||
if (hitRes) { | ||
const gpAssoc = <RigidNodeAssociate>World.PhysicsSystem.GetBodyAssociation(hitRes.data.mBodyID) | ||
// This works, however the check for game piece is doing two checks. | ||
if (gpAssoc?.isGamePiece) { | ||
console.debug("Found game piece!") | ||
this._parentAssembly.SetEjectable(hitRes.data.mBodyID, false) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public Dispose(): void { | ||
console.debug("Destroying intake sensor") | ||
|
||
if (this._joltBodyId) { | ||
World.PhysicsSystem.DestroyBodyIds(this._joltBodyId) | ||
|
||
if (this._mesh) { | ||
this._mesh.geometry.dispose() | ||
;(this._mesh.material as THREE.Material).dispose() | ||
World.SceneRenderer.scene.remove(this._mesh) | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default IntakeSensorSceneObject |
Oops, something went wrong.