Skip to content

Commit d4d6655

Browse files
Add quadratic easing + future reference comments
1 parent 088abf6 commit d4d6655

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

fission/src/mirabuf/EjectableSceneObject.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ class EjectableSceneObject extends SceneObject {
6464
)
6565
this._ejectVelocity = this._parentAssembly.ejectorPreferences.ejectorVelocity
6666

67-
// Animation start at game piece
67+
// Record start transform at the game piece center of mass
6868
const gpBody = World.physicsSystem.getBody(this._gamePieceBodyId)
69-
7069
this._startTranslation = new THREE.Vector3(0, 0, 0)
7170
this._startRotation = new THREE.Quaternion(0, 0, 0, 1)
7271
convertJoltMat44ToThreeMatrix4(gpBody.GetCenterOfMassTransform()).decompose(
@@ -80,7 +79,7 @@ class EjectableSceneObject extends SceneObject {
8079

8180
World.physicsSystem.disablePhysicsForBody(this._gamePieceBodyId)
8281

83-
// Remove from scoring zones
82+
// Remove from any scoring zones
8483
const zones = [...World.sceneRenderer.sceneObjects.entries()]
8584
.filter(x => x[1] instanceof ScoringZoneSceneObject)
8685
.map(x => x[1]) as ScoringZoneSceneObject[]
@@ -96,7 +95,11 @@ class EjectableSceneObject extends SceneObject {
9695
public update(): void {
9796
const now = performance.now()
9897
const elapsed = (now - this._animationStartTime) / 1000
99-
const t = Math.min(elapsed / this._animationDuration, 1)
98+
const tRaw = elapsed / this._animationDuration
99+
const t = Math.min(tRaw, 1)
100+
101+
// ease-in curve for gradual acceleration
102+
const easedT = t * t
100103

101104
if (this._parentBodyId && this._deltaTransformation && this._gamePieceBodyId) {
102105
if (!World.physicsSystem.isBodyAdded(this._gamePieceBodyId)) {
@@ -113,17 +116,23 @@ class EjectableSceneObject extends SceneObject {
113116
let desiredPosition = new THREE.Vector3(0, 0, 0)
114117
let desiredRotation = new THREE.Quaternion(0, 0, 0, 1)
115118

119+
// Compute target world transform
116120
const desiredTransform = this._deltaTransformation
117121
.clone()
118122
.premultiply(convertJoltMat44ToThreeMatrix4(body.GetWorldTransform()))
119123

120124
desiredTransform.decompose(desiredPosition, desiredRotation, new THREE.Vector3(1, 1, 1))
121125

122126
if (t < 1 && this._startTranslation && this._startRotation) {
123-
desiredPosition = new THREE.Vector3().lerpVectors(this._startTranslation, desiredPosition, t)
124-
desiredRotation = new THREE.Quaternion().copy(this._startRotation).slerp(desiredRotation, t)
127+
// gradual acceleration via easedT
128+
desiredPosition = new THREE.Vector3().lerpVectors(this._startTranslation, desiredPosition, easedT)
129+
desiredRotation = new THREE.Quaternion().copy(this._startRotation).slerp(desiredRotation, easedT)
130+
} else if (t >= 1) {
131+
// snap instantly and re-enable physics
132+
World.physicsSystem.enablePhysicsForBody(this._gamePieceBodyId)
125133
}
126134

135+
// apply the transform
127136
desiredTransform.identity().compose(desiredPosition, desiredRotation, new THREE.Vector3(1, 1, 1))
128137

129138
const bodyTransform = posToCOM.clone().invert().premultiply(desiredTransform)
@@ -132,7 +141,11 @@ class EjectableSceneObject extends SceneObject {
132141
const rotation = new THREE.Quaternion(0, 0, 0, 1)
133142
bodyTransform.decompose(position, rotation, new THREE.Vector3(1, 1, 1))
134143

135-
World.physicsSystem.setBodyPosition(this._gamePieceBodyId, convertThreeVector3ToJoltRVec3(position), false)
144+
World.physicsSystem.setBodyPosition(
145+
this._gamePieceBodyId,
146+
convertThreeVector3ToJoltRVec3(position),
147+
false
148+
)
136149
World.physicsSystem.setBodyRotation(
137150
this._gamePieceBodyId,
138151
convertThreeQuaternionToJoltQuat(rotation),

0 commit comments

Comments
 (0)