Skip to content

Commit 4f4eae4

Browse files
committed
fix #383: delta value is cached and never recomputed when dependencies change
1 parent 4919569 commit 4f4eae4

File tree

2 files changed

+13
-18
lines changed

2 files changed

+13
-18
lines changed

examples/handle/app.tsx

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,11 @@ function PhysicsHandle({ children }: { children?: ReactNode }) {
4242
const ref = useRef<RapierRigidBody>(null)
4343
const groupRef = useRef<Group>(null)
4444
const targetRef = useMemo(
45-
() =>
46-
new Proxy<RefObject<Object3D>>(
47-
{ current: null },
48-
{
49-
get() {
50-
return groupRef.current?.parent
51-
},
52-
},
53-
),
45+
() => new Proxy<RefObject<Object3D>>({ current: null }, { get: () => groupRef.current?.parent }),
5446
[],
5547
)
5648
return (
57-
<RigidBody ref={ref} colliders="trimesh" position={[0, 1, 0]}>
49+
<RigidBody ref={ref} colliders="trimesh" type="dynamic" position={[0, 1, 0]}>
5850
<group ref={groupRef}>
5951
<Handle
6052
multitouch={false}
@@ -66,22 +58,20 @@ function PhysicsHandle({ children }: { children?: ReactNode }) {
6658
if (rigidBody == null) {
6759
return
6860
}
69-
if (state.first) {
70-
rigidBody.setBodyType(RigidBodyType.KinematicPositionBased, true)
71-
}
7261
if (state.last) {
7362
rigidBody.setBodyType(RigidBodyType.Dynamic, true)
74-
/*
75-
doesnt work yet. Probably because of:https://github.com/pmndrs/xr/issues/383
7663
if (state.delta != null) {
77-
rigidBody.setLinvel(state.delta.position.clone().divideScalar(deltaTime), true)
64+
const deltaTime = state.delta.time
65+
const deltaPosition = state.delta.position.clone().divideScalar(deltaTime)
66+
rigidBody.setLinvel(deltaPosition, true)
7867
const deltaRotation = state.delta.rotation.clone()
7968
deltaRotation.x /= deltaTime
8069
deltaRotation.y /= deltaTime
8170
deltaRotation.z /= deltaTime
8271
rigidBody.setAngvel(deltaRotation, true)
83-
}*/
72+
}
8473
} else {
74+
rigidBody.setBodyType(RigidBodyType.KinematicPositionBased, true)
8575
rigidBody.setRotation(state.current.quaternion, true)
8676
rigidBody.setTranslation(state.current.position, true)
8777
}

packages/handle/src/state.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ export class HandleStateImpl<T> implements HandleState<T> {
9595
this.first = true
9696
this.last = false
9797
this.memo = undefined
98+
this._delta = undefined
99+
this._offset = undefined
98100
}
99101

100102
update(event: PointerEvent | undefined, current: HandleTransformState) {
@@ -103,13 +105,16 @@ export class HandleStateImpl<T> implements HandleState<T> {
103105
this.current = current
104106
this.first = false
105107
this.last = false
108+
this._delta = undefined
109+
this._offset = undefined
106110
}
107111

108112
end(event: PointerEvent | undefined) {
109113
this.event = event
110-
this.previous = this.current
111114
this.first = false
112115
this.last = true
116+
this._delta = undefined
117+
this._offset = undefined
113118
}
114119

115120
get delta(): Omit<HandleTransformState, 'pointerAmount'> | undefined {

0 commit comments

Comments
 (0)