-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathGrabbableObject.ts
More file actions
414 lines (337 loc) · 14 KB
/
GrabbableObject.ts
File metadata and controls
414 lines (337 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import TrackedHand from "SpectaclesInteractionKit.lspkg/Providers/HandInputData/TrackedHand"
import Event from "SpectaclesInteractionKit.lspkg/Utils/Event"
import {MatchTransform} from "./MatchTransform"
/**
* Makes an object grabbable via pinch or grab gesture.
* Requires a MatchTransform component and a Physics Body component.
*/
@component
export class GrabbableObject extends BaseScriptComponent {
// Public events for visual feedback systems
public onGrabStartEvent: Event = new Event()
public onGrabEndEvent: Event = new Event()
@input
@widget(
new ComboBoxWidget([
new ComboBoxItem("Ball", "Ball"),
new ComboBoxItem("Racket", "Racket"),
new ComboBoxItem("Darts", "Darts")
])
)
@hint("Type of object - determines grab behavior and rotation")
objectType: string = "Ball"
@input
@hint("Reference to the MatchTransform component on this object")
matchTransform: MatchTransform
@input
@hint("Time in seconds before destroying the object after it's dropped")
destroyDelay: number = 4.5
@input
@hint("For Darts: The dartboard/scoreboard to look at when grabbed")
@showIf("objectType", "Darts")
scoreBoard: SceneObject
@input
@hint("For Darts: Force applied when releasing (throw strength)")
@showIf("objectType", "Darts")
dartThrowForce: number = 800.0
@input
@hint("For Ball/Racket: Force applied when releasing (throw strength)")
ballThrowForce: number = 150.0
@input
@hint("For Ball/Racket: Multiplier for hand velocity (higher = more responsive to hand movement)")
handVelocityMultiplier: number = 0.3
private bodyComponent: BodyComponent | null = null
private colliderComponent: ColliderComponent | null = null
private isGrabbed: boolean = false
private destroyEvent: DelayedCallbackEvent | null = null
private grabbedHand: TrackedHand | null = null
private updateEvent: SceneEvent | null = null
private previousHandPosition: vec3 = vec3.zero()
private handVelocity: vec3 = vec3.zero()
onAwake() {
// Get the body component
this.bodyComponent = this.getSceneObject().getComponent("Physics.BodyComponent")
// Get the collider component (fallback if no body)
if (!this.bodyComponent) {
this.colliderComponent = this.getSceneObject().getComponent("Physics.ColliderComponent")
}
// Validate required components
if (!this.matchTransform) {
print("GrabbableObject: MatchTransform component is required!")
}
if (!this.bodyComponent && !this.colliderComponent) {
print("GrabbableObject: Physics Body or Collider component is required!")
}
// MatchTransform starts with isMatching=false by default, no need to call disableMatching()
}
/**
* Called by GestureManager when object is grabbed
*/
onGrab(hand: TrackedHand) {
if (this.isGrabbed) return
this.isGrabbed = true
this.grabbedHand = hand
// Cancel any pending destroy
if (this.destroyEvent) {
this.destroyEvent.cancel()
this.destroyEvent = null
}
// IMPORTANT: Save current world position BEFORE unparenting
const currentWorldPos = this.getSceneObject().getTransform().getWorldPosition()
const currentWorldRot = this.getSceneObject().getTransform().getWorldRotation()
// Unparent the object so it's not affected by parent transforms
this.getSceneObject().setParent(null)
// IMPORTANT: Restore world position immediately after unparenting
// This prevents any visual jump from parenting changes
this.getSceneObject().getTransform().setWorldPosition(currentWorldPos)
this.getSceneObject().getTransform().setWorldRotation(currentWorldRot)
// If body is dynamic, make it static/kinematic during grab
if (this.bodyComponent && this.bodyComponent.dynamic) {
this.bodyComponent.dynamic = false
}
// Set up MatchTransform to follow index tip
if (
this.matchTransform &&
this.matchTransform.resetOffset &&
this.matchTransform.setTarget &&
this.matchTransform.enableMatching
) {
const afterUnparentPos = this.getSceneObject().getTransform().getWorldPosition()
const afterUnparentRot = this.getSceneObject().getTransform().getWorldRotation()
print(`GrabbableObject (${this.getSceneObject().name}): After unparent - pos: ${afterUnparentPos}`)
print(`GrabbableObject: Hand index tip - pos: ${hand.indexTip.position}`)
this.matchTransform.resetOffset()
// IMPORTANT: Set initial target BEFORE enabling to prevent jump
// This initializes the offset before the first update
this.matchTransform.setTarget(hand.indexTip.position, hand.indexTip.rotation)
// Now enable it - offset is already calculated
this.matchTransform.enableMatching()
// For darts, disable rotation updates in MatchTransform (we override rotation every frame)
if (this.objectType === "Darts") {
this.matchTransform.disableRotationUpdates()
print(`GrabbableObject: Rotation updates disabled for dart (will be overridden)`)
} else {
this.matchTransform.enableRotationUpdates()
}
print(`GrabbableObject: MatchTransform enabled with offset initialized`)
} else {
print("GrabbableObject: ERROR - MatchTransform not available or missing methods!")
}
// Initialize hand velocity tracking
this.previousHandPosition = hand.indexTip.position
this.handVelocity = vec3.zero()
// Create update event to track hand position
this.updateEvent = this.createEvent("UpdateEvent")
this.updateEvent.bind(this.onUpdateWhileGrabbed.bind(this))
// Trigger grab start event for visual feedback
this.onGrabStartEvent.invoke()
print(`GrabbableObject (${this.getSceneObject().name}): Grabbed! Update event created.`)
}
private updateCounter = 0
/**
* Update loop while grabbed to follow hand
*/
private onUpdateWhileGrabbed() {
if (!this.isGrabbed || !this.grabbedHand || !this.matchTransform) {
return
}
// Check if hand is still tracked
if (!this.grabbedHand.isTracked()) {
// Hand lost tracking, release
this.onRelease()
return
}
// Track hand velocity for natural throwing (all types)
const currentHandPos = this.grabbedHand.indexTip.position
if (getDeltaTime() > 0) {
// Calculate velocity as change in position over time
this.handVelocity = currentHandPos.sub(this.previousHandPosition).uniformScale(1 / getDeltaTime())
}
this.previousHandPosition = currentHandPos
// Update target position from hand's index tip
// For rotation: Ball uses index rotation, Racket uses wrist, Darts override
if (this.matchTransform.setTarget) {
if (this.objectType === "Ball") {
// Ball follows index tip rotation
this.matchTransform.setTarget(this.grabbedHand.indexTip.position, this.grabbedHand.indexTip.rotation)
} else if (this.objectType === "Racket") {
// Racket follows wrist orientation (more natural for racket holding)
this.matchTransform.setTarget(this.grabbedHand.indexTip.position, this.grabbedHand.wrist.rotation)
} else {
// Only Darts ignore hand rotation, will override below
this.matchTransform.setTarget(
this.grabbedHand.indexTip.position,
this.getSceneObject().getTransform().getWorldRotation() // Keep current rotation
)
}
}
// Apply special rotation behavior based on object type (AFTER MatchTransform)
this.applyTypeSpecificRotation()
// Debug logging every 30 frames
this.updateCounter++
if (this.updateCounter % 30 === 0) {
print(`GrabbableObject UPDATE: Setting target to ${this.grabbedHand.indexTip.position}`)
}
}
/**
* Apply rotation behavior specific to object type
*/
private applyTypeSpecificRotation() {
const transform = this.getSceneObject().getTransform()
if (this.objectType === "Darts") {
// Darts look at the score board (overrides hand rotation completely)
if (this.scoreBoard) {
const boardPos = this.scoreBoard.getTransform().getWorldPosition()
const myPos = transform.getWorldPosition()
const directionToBoard = boardPos.sub(myPos).normalize()
// Create a lookAt rotation
const lookAtRotation = quat.lookAt(directionToBoard, vec3.up())
// Add 90-degree rotation on X axis to make dart point forward properly
const xRotation = quat.angleAxis(90 * MathUtils.DegToRad, vec3.right())
const finalRotation = lookAtRotation.multiply(xRotation)
transform.setWorldRotation(finalRotation)
}
}
// Ball and Racket use the default rotation from MatchTransform (follows hand rotation)
}
/**
* Get the gesture type required for this object
*/
getGestureType(): "pinch" | "grab" {
if (this.objectType === "Racket") {
return "grab"
}
// Ball and Darts use pinch
return "pinch"
}
/**
* Called by GestureManager when object is released
*/
onRelease() {
if (!this.isGrabbed) return
this.isGrabbed = false
// Clean up update event
if (this.updateEvent) {
this.updateEvent.enabled = false
this.updateEvent = null
}
// Disable MatchTransform
if (this.matchTransform && this.matchTransform.disableMatching) {
this.matchTransform.disableMatching()
}
// Enable dynamic on body component so it falls/moves
if (this.bodyComponent) {
this.bodyComponent.dynamic = true
// Apply throw force based on object type (BEFORE clearing grabbedHand)
if (this.objectType === "Darts" && this.scoreBoard) {
this.throwDart()
} else if (this.objectType === "Ball") {
this.throwBall()
}
// Racket just falls naturally - no throw force
}
// Clear grabbed hand AFTER using it for throw direction
this.grabbedHand = null
// Reset velocity tracking
this.handVelocity = vec3.zero()
this.previousHandPosition = vec3.zero()
// Schedule destruction
this.scheduleDestroy()
print(`GrabbableObject (${this.getSceneObject().name}): Released!`)
// Trigger grab end event for visual feedback (AFTER all cleanup)
print(`GrabbableObject: Invoking onGrabEndEvent`)
this.onGrabEndEvent.invoke()
}
/**
* Throw the dart using hand velocity but guided towards the score board
*/
private throwDart() {
if (!this.bodyComponent || !this.scoreBoard) return
const myPos = this.getSceneObject().getTransform().getWorldPosition()
const boardPos = this.scoreBoard.getTransform().getWorldPosition()
// Calculate direction from dart to score board
const directionToBoard = boardPos.sub(myPos).normalize()
// Use hand velocity magnitude for throw strength (feels natural)
let throwStrength = this.handVelocity.length * this.handVelocityMultiplier
// If hand velocity is too low, use base force
if (throwStrength < 2) {
throwStrength = this.dartThrowForce
} else {
// Add base force to velocity-based strength
throwStrength += this.dartThrowForce
}
// Throw in direction of board (guided) with natural strength
const forceVector = directionToBoard.uniformScale(throwStrength)
// Clear any existing rotation/angular velocity
this.bodyComponent.angularVelocity = vec3.zero()
// Set high angular damping to prevent spinning during flight
this.bodyComponent.angularDamping = 0.95 // High damping = less spinning
print(`GrabbableObject: Throwing dart with strength ${throwStrength.toFixed(1)} towards board`)
print(`GrabbableObject: Hand velocity: ${this.handVelocity.length.toFixed(1)}`)
// Apply impulse force (linear only, no torque)
this.bodyComponent.addForce(forceVector, Physics.ForceMode.Impulse)
}
/**
* Throw the ball/racket based on hand velocity for natural throwing
*/
private throwBall() {
if (!this.bodyComponent) return
// Use hand velocity for more natural throwing
let throwVelocity = this.handVelocity.uniformScale(this.handVelocityMultiplier)
// If velocity is too low (not moving hand), use forward direction with base force
if (throwVelocity.length < 2) {
if (this.grabbedHand) {
const handForward = this.grabbedHand.indexTip.rotation.multiplyVec3(vec3.forward())
throwVelocity = handForward.uniformScale(this.ballThrowForce)
} else {
// Fallback: throw forward
throwVelocity = vec3.forward().uniformScale(this.ballThrowForce)
}
} else {
// Add base force to the velocity-based throw
if (this.grabbedHand) {
const handForward = this.grabbedHand.indexTip.rotation.multiplyVec3(vec3.forward())
const baseForce = handForward.uniformScale(this.ballThrowForce)
throwVelocity = throwVelocity.add(baseForce)
}
}
print(`GrabbableObject: Throwing ${this.objectType} with velocity ${throwVelocity.length.toFixed(1)}`)
print(`GrabbableObject: Hand velocity contribution: ${this.handVelocity.length.toFixed(1)}`)
// Apply as impulse force
this.bodyComponent.addForce(throwVelocity, Physics.ForceMode.Impulse)
}
/**
* Schedule the object to be destroyed after a delay
*/
private scheduleDestroy() {
if (this.destroyEvent) {
this.destroyEvent.cancel()
}
this.destroyEvent = this.createEvent("DelayedCallbackEvent")
this.destroyEvent.bind(() => {
// Don't destroy darts that have stuck to the board (they're parented to it)
if (this.objectType === "Darts" && this.scoreBoard) {
const parent = this.getSceneObject().getParent()
if (parent === this.scoreBoard) {
print("GrabbableObject: Dart stuck to board - skipping destroy")
return
}
}
print("GrabbableObject: Destroying object after delay")
this.getSceneObject().destroy()
})
this.destroyEvent.reset(this.destroyDelay)
}
/**
* Check if this object is currently grabbed
*/
isCurrentlyGrabbed(): boolean {
return this.isGrabbed
}
/**
* Get the collider component for overlap detection
*/
getCollider(): ColliderComponent | null {
return this.bodyComponent || this.colliderComponent
}
}