Skip to content

Commit

Permalink
fix useControllerLocomotion: use camera.matrix.decompose instead of c…
Browse files Browse the repository at this point in the history
…amera.getWorldQuaternion
  • Loading branch information
bbohlender committed Sep 29, 2024
1 parent 521e8cd commit 987b540
Show file tree
Hide file tree
Showing 3 changed files with 3,073 additions and 3,189 deletions.
13 changes: 10 additions & 3 deletions examples/rag-doll/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { Physics, usePlane } from '@react-three/cannon'
import { Cursor } from './helpers/Drag.js'
import { Guy } from './components/Guy.jsx'
import { Mug, Chair, Table, Lamp } from './components/Furniture.jsx'
import { createXRStore, XR, XROrigin } from '@react-three/xr'
import { Suspense } from 'react'
import { createXRStore, useControllerLocomotion, XR, XROrigin } from '@react-three/xr'
import { useRef, Suspense } from 'react'
import { Group } from 'three'

const store = createXRStore({
hand: { touchPointer: false },
Expand Down Expand Up @@ -55,14 +56,20 @@ export function App() {
</Physics>
</Suspense>
<group position={[0, -5, 0]}>
<XROrigin scale={10} />
<ControlledXROrigin />
</group>
</XR>
</Canvas>
</>
)
}

function ControlledXROrigin() {
const ref = useRef(null)
useControllerLocomotion(ref, { speed: 10 })
return <XROrigin ref={ref} scale={10} />
}

function Floor(props) {
const [ref] = usePlane(() => ({ type: 'Static', ...props }))
return (
Expand Down
5 changes: 4 additions & 1 deletion packages/xr/src/controller-locomotion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const thumbstickPropName = 'xr-standard-thumbstick'
const vectorHelper = new Vector3()
const quaternionHelper = new Quaternion()
const eulerHelper = new Euler()
const positionHelper = new Vector3()
const scaleHelper = new Vector3()

/**
* function for handling controller based locomotion in VR
Expand Down Expand Up @@ -94,7 +96,8 @@ export function createControllerLocomotionUpdate() {
}
const { speed = defaultSpeed } = translationOptions
vectorHelper.set(translationXAxis * speed, 0, translationYAxis * speed)
vectorHelper.applyQuaternion(camera.getWorldQuaternion(quaternionHelper))
camera.matrixWorld.decompose(positionHelper, quaternionHelper, scaleHelper)
vectorHelper.applyQuaternion(quaternionHelper)

if (yRotationChange) {
vectorHelper.applyEuler(eulerHelper.set(0, yRotationChange, 0, 'YXZ'))
Expand Down
Loading

0 comments on commit 987b540

Please sign in to comment.