|
1 | 1 | import type { RapierRigidBody } from '@react-three/rapier' |
2 | 2 | import type { Vector3 } from 'three' |
3 | 3 |
|
4 | | -import { CapsuleCollider, RigidBody } from '@react-three/rapier' |
| 4 | +import { CapsuleCollider, euler, quat, RigidBody } from '@react-three/rapier' |
5 | 5 | import { useXRControllerLocomotion, XROrigin } from '@react-three/xr' |
6 | 6 | import { useRef } from 'react' |
7 | 7 |
|
8 | 8 | export const Player = () => { |
9 | 9 | const userRigidBodyRef = useRef<RapierRigidBody>(null) |
10 | 10 |
|
11 | | - const userMove = (inputVector: Vector3) => { |
12 | | - if (!(userRigidBodyRef.current)) |
| 11 | + useXRControllerLocomotion((velocity: Vector3, rotationVelocityY: number) => { |
| 12 | + if (!userRigidBodyRef.current) |
13 | 13 | 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) |
17 | 14 |
|
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 | + ) |
22 | 20 |
|
23 | | - useXRControllerLocomotion(userMove) |
| 21 | + userRigidBodyRef.current.setLinvel({ x: velocity.x, y: 0, z: velocity.z }, true) |
| 22 | + }) |
24 | 23 |
|
25 | 24 | return ( |
26 | 25 | <RigidBody |
27 | 26 | canSleep={false} |
28 | 27 | colliders={false} |
29 | 28 | enabledRotations={[false, false, false]} |
| 29 | + includeInvisible |
| 30 | + mass={1} |
30 | 31 | position={[0, 2, 0]} |
31 | 32 | ref={userRigidBodyRef} |
32 | | - restitution={2} |
33 | 33 | type="dynamic" |
34 | 34 | > |
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]} /> |
37 | 37 | </RigidBody> |
38 | 38 | ) |
39 | 39 | } |
0 commit comments