Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/minecraft/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Sky, PointerLockControls, KeyboardControls } from '@react-three/drei'
import { KeyboardControls, Sky } from '@react-three/drei'
import { Canvas } from '@react-three/fiber'
import { interactionGroups, Physics } from '@react-three/rapier'
import { createXRStore, XR } from '@react-three/xr'
Expand Down Expand Up @@ -56,7 +56,6 @@ export function App() {
<Cube collisionGroups={interactionGroups([0, 1], [0])} position={[0, 0.5, -10]} />
<Cubes />
</Physics>
<PointerLockControls />
</XR>
</Canvas>
</KeyboardControls>
Expand Down
27 changes: 20 additions & 7 deletions examples/minecraft/src/Player.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useKeyboardControls } from '@react-three/drei'
import { useFrame } from '@react-three/fiber'
import { PointerLockControls, useKeyboardControls } from '@react-three/drei'
import { useFrame, useThree } from '@react-three/fiber'
import {
CapsuleCollider,
interactionGroups,
Expand All @@ -8,7 +8,7 @@ import {
useRapier,
Vector3Object,
} from '@react-three/rapier'
import { IfInSessionMode } from '@react-three/xr'
import { IfInSessionMode, useXR } from '@react-three/xr'
import { useRef } from 'react'
import * as THREE from 'three'
import { Axe } from './Axe.jsx'
Expand All @@ -30,6 +30,8 @@ export function Player({ lerp = THREE.MathUtils.lerp }) {
const rigidBodyRef = useRef<RapierRigidBody>(null)
const { rapier, world } = useRapier()
const [, getKeys] = useKeyboardControls()
const { camera } = useThree()
const { mode } = useXR()

const playerMove = ({
forward,
Expand All @@ -38,6 +40,7 @@ export function Player({ lerp = THREE.MathUtils.lerp }) {
right,
rotationYVelocity,
velocity,
camera,
newVelocity,
}: {
forward: boolean
Expand All @@ -46,6 +49,7 @@ export function Player({ lerp = THREE.MathUtils.lerp }) {
right: boolean
rotationYVelocity: number
velocity?: Vector3Object
camera?: THREE.Camera
newVelocity?: THREE.Vector3
}) => {
if (rigidBodyRef.current == null) {
Expand All @@ -56,10 +60,15 @@ export function Player({ lerp = THREE.MathUtils.lerp }) {
}

//apply rotation
const { x, y, z, w } = rigidBodyRef.current.rotation()
quaternionHelper.set(x, y, z, w)
quaternionHelper.multiply(quaternionHelper2.setFromEuler(eulerHelper.set(0, rotationYVelocity, 0, 'YXZ')))
rigidBodyRef.current?.setRotation(quaternionHelper, true)
if (!camera) {
const { x, y, z, w } = rigidBodyRef.current.rotation()
quaternionHelper.set(x, y, z, w)
quaternionHelper.multiply(quaternionHelper2.setFromEuler(eulerHelper.set(0, rotationYVelocity, 0, 'YXZ')))
rigidBodyRef.current?.setRotation(quaternionHelper, true)
} else if (camera) {
quaternionHelper.setFromEuler(camera.rotation)
rigidBodyRef.current?.setRotation(quaternionHelper, true)
}

if (newVelocity) {
// If we have a new velocity, we're in VR mode
Expand Down Expand Up @@ -129,6 +138,8 @@ export function Player({ lerp = THREE.MathUtils.lerp }) {
right,
rotationYVelocity: 0,
velocity,
// Don't pass the camera in AR/VR mode, as we want the player to control rotation
camera: mode?.includes('immersive-ar') || mode?.includes('immersive-vr') ? undefined : state.camera,
})

if (jump) {
Expand Down Expand Up @@ -169,6 +180,8 @@ export function Player({ lerp = THREE.MathUtils.lerp }) {
<Axe position={[0.3, -0.35, 0.5]} />
</group>
</IfInSessionMode>

<PointerLockControls args={[camera]} />
</>
)
}