Skip to content

Commit 291ea18

Browse files
committed
fix(app): controller locomotion with physics
1 parent a2d71fe commit 291ea18

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

app/src/components/xr/player.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
import type { RapierRigidBody } from '@react-three/rapier'
22
import type { Vector3 } from 'three'
33

4-
import { CapsuleCollider, RigidBody } from '@react-three/rapier'
4+
import { CapsuleCollider, euler, quat, RigidBody } from '@react-three/rapier'
55
import { useXRControllerLocomotion, XROrigin } from '@react-three/xr'
66
import { useRef } from 'react'
77

88
export const Player = () => {
99
const userRigidBodyRef = useRef<RapierRigidBody>(null)
1010

11-
const userMove = (inputVector: Vector3) => {
12-
if (!(userRigidBodyRef.current))
11+
useXRControllerLocomotion((velocity: Vector3, rotationVelocityY: number) => {
12+
if (!userRigidBodyRef.current)
1313
return
14-
const currentLinvel = userRigidBodyRef.current.linvel()
15-
const newLinvel = { x: inputVector.x, y: currentLinvel.y, z: inputVector.z }
16-
userRigidBodyRef.current.setLinvel(newLinvel, true)
1714

18-
// const currentAngvel = userRigidBodyRef.current.angvel()
19-
// const newAngvel = { x: currentAngvel.x, y: rotationVelocityY, z: currentAngvel.z }
20-
// userRigidBodyRef.current.setAngvel(newAngvel, true)
21-
}
15+
userRigidBodyRef.current.setRotation(
16+
quat(userRigidBodyRef.current.rotation())
17+
.multiply(quat().setFromEuler(euler().set(0, rotationVelocityY, 0, 'YXZ'))),
18+
true,
19+
)
2220

23-
useXRControllerLocomotion(userMove)
21+
userRigidBodyRef.current.setLinvel({ x: velocity.x, y: 0, z: velocity.z }, true)
22+
})
2423

2524
return (
2625
<RigidBody
2726
canSleep={false}
2827
colliders={false}
2928
enabledRotations={[false, false, false]}
29+
includeInvisible
30+
mass={1}
3031
position={[0, 2, 0]}
3132
ref={userRigidBodyRef}
32-
restitution={2}
3333
type="dynamic"
3434
>
35-
<CapsuleCollider args={[0.3, 0.5]} />
36-
<XROrigin position={[0, -1, 0]} />
35+
<CapsuleCollider args={[0.75, 0.5]} />
36+
<XROrigin position={[0, -1.25, 0]} />
3737
</RigidBody>
3838
)
3939
}

0 commit comments

Comments
 (0)