Skip to content

Commit

Permalink
Merge pull request #1851 from wonderunit/xr-elevation-control
Browse files Browse the repository at this point in the history
Xr Elevation Control
  • Loading branch information
setpixel authored Nov 6, 2019
2 parents 7a893c8 + 984d2f0 commit ef3b234
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/js/xr/src/hooks/use-vr-controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const useVrControllers = ({
onAxesChanged,
onPressEndA,
onPressEndB,
onPressEndX
onPressEndX,
onPressStartThumbstick
}) => {
const { gl } = useThree()
const [list, setList] = useState([])
Expand All @@ -39,6 +40,7 @@ const useVrControllers = ({
const onPressEndARef = useRef()
const onPressEndBRef = useRef()
const onPressEndXRef = useRef()
const onPressStartThumbstickRef = useRef()

onTriggerStartRef.current = onTriggerStart
onTriggerEndRef.current = onTriggerEnd
Expand All @@ -48,6 +50,7 @@ const useVrControllers = ({
onPressEndARef.current = onPressEndA
onPressEndBRef.current = onPressEndB
onPressEndXRef.current = onPressEndX
onPressStartThumbstickRef.current = onPressStartThumbstick

useRender(() => {
THREE.VRController.update()
Expand All @@ -69,6 +72,7 @@ const useVrControllers = ({
controller.addEventListener('A press ended', event => onPressEndARef.current(modifyEvent(event, gl)))
controller.addEventListener('B press ended', event => onPressEndBRef.current(modifyEvent(event, gl)))
controller.addEventListener('X press ended', event => onPressEndXRef.current(modifyEvent(event, gl)))
controller.addEventListener('thumbstick press began', event => onPressStartThumbstickRef.current(modifyEvent(event, gl)))
controller.addEventListener('disconnected', event => {
console.log('disconnected', event)
setList(getList(THREE.VRController.controllers, gl))
Expand Down
27 changes: 22 additions & 5 deletions src/js/xr/src/use-interactions-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,22 @@ const useInteractionsManager = ({
interactionService.send({ type: 'PRESS_END_X', controller: event.target })
}

const onPressStartThumbstick = event => {
let rightController = getControllerByName(controllers, "right");
let worldScale = useStoreApi.getState().worldScale
let hmd = camera.parent
let elevationDisplacement = 0.5
if(event.target.uuid === rightController.uuid) {
hmd.position.y += elevationDisplacement
}
else {
if(hmd.position.y !== 0)
hmd.position.y -= elevationDisplacement
}
teleport(camera, (hmd.position.x + camera.position.x) * worldScale, hmd.position.y * worldScale, (hmd.position.z + camera.position.z) * worldScale)
}

const thumbStickSensitivity = 0.25
const onMoveCamera = event => {
if (didMoveCamera != null) {
if (event.axes[1] === 0) {
Expand All @@ -671,12 +687,12 @@ const useInteractionsManager = ({
let value = event.axes[1]

// backward
if (value > 0.075) {
if (value > thumbStickSensitivity) {
distance = +0.5
}

// forward
if (value < -0.075) {
if (value < -thumbStickSensitivity) {
distance = -0.5
}

Expand All @@ -697,14 +713,14 @@ const useInteractionsManager = ({
if (Math.abs(event.axes[0]) < Math.abs(event.axes[1])) return

// right
if (event.axes[0] > 0.075) {
if (event.axes[0] > thumbStickSensitivity) {
setDidRotateCamera(-45)
playSound('teleport-rotate')
rotateCameraByRadians(camera, THREE.Math.degToRad(-45))
}

// left
if (event.axes[0] < -0.075) {
if (event.axes[0] < -thumbStickSensitivity) {
setDidRotateCamera(45)
playSound('teleport-rotate')
rotateCameraByRadians(camera, THREE.Math.degToRad(45))
Expand All @@ -721,7 +737,8 @@ const useInteractionsManager = ({
onAxesChanged,
onPressEndA,
onPressEndB,
onPressEndX
onPressEndX,
onPressStartThumbstick
})

const reusableVector = useRef()
Expand Down

0 comments on commit ef3b234

Please sign in to comment.