Skip to content

Commit 987b540

Browse files
committed
fix useControllerLocomotion: use camera.matrix.decompose instead of camera.getWorldQuaternion
1 parent 521e8cd commit 987b540

File tree

3 files changed

+3073
-3189
lines changed

3 files changed

+3073
-3189
lines changed

examples/rag-doll/src/App.jsx

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { Physics, usePlane } from '@react-three/cannon'
44
import { Cursor } from './helpers/Drag.js'
55
import { Guy } from './components/Guy.jsx'
66
import { Mug, Chair, Table, Lamp } from './components/Furniture.jsx'
7-
import { createXRStore, XR, XROrigin } from '@react-three/xr'
8-
import { Suspense } from 'react'
7+
import { createXRStore, useControllerLocomotion, XR, XROrigin } from '@react-three/xr'
8+
import { useRef, Suspense } from 'react'
9+
import { Group } from 'three'
910

1011
const store = createXRStore({
1112
hand: { touchPointer: false },
@@ -55,14 +56,20 @@ export function App() {
5556
</Physics>
5657
</Suspense>
5758
<group position={[0, -5, 0]}>
58-
<XROrigin scale={10} />
59+
<ControlledXROrigin />
5960
</group>
6061
</XR>
6162
</Canvas>
6263
</>
6364
)
6465
}
6566

67+
function ControlledXROrigin() {
68+
const ref = useRef(null)
69+
useControllerLocomotion(ref, { speed: 10 })
70+
return <XROrigin ref={ref} scale={10} />
71+
}
72+
6673
function Floor(props) {
6774
const [ref] = usePlane(() => ({ type: 'Static', ...props }))
6875
return (

packages/xr/src/controller-locomotion.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const thumbstickPropName = 'xr-standard-thumbstick'
2222
const vectorHelper = new Vector3()
2323
const quaternionHelper = new Quaternion()
2424
const eulerHelper = new Euler()
25+
const positionHelper = new Vector3()
26+
const scaleHelper = new Vector3()
2527

2628
/**
2729
* function for handling controller based locomotion in VR
@@ -94,7 +96,8 @@ export function createControllerLocomotionUpdate() {
9496
}
9597
const { speed = defaultSpeed } = translationOptions
9698
vectorHelper.set(translationXAxis * speed, 0, translationYAxis * speed)
97-
vectorHelper.applyQuaternion(camera.getWorldQuaternion(quaternionHelper))
99+
camera.matrixWorld.decompose(positionHelper, quaternionHelper, scaleHelper)
100+
vectorHelper.applyQuaternion(quaternionHelper)
98101

99102
if (yRotationChange) {
100103
vectorHelper.applyEuler(eulerHelper.set(0, yRotationChange, 0, 'YXZ'))

0 commit comments

Comments
 (0)