diff --git a/src/lib/Hero.js b/src/lib/Hero.js index 1aed274..c6225d7 100644 --- a/src/lib/Hero.js +++ b/src/lib/Hero.js @@ -1,11 +1,13 @@ -import { AnimationMixer, Vector3 } from 'three'; +import { AnimationMixer, Vector3, Vector2 } from 'three'; import { getBoundingBox, getBoundingBoxSize } from './MeshUtils'; import { QueryFilterFlags } from '@dimforge/rapier3d'; +const zero2 = new Vector2(0,0); class Hero{ walkDirection = new Vector3() rotateAngle = new Vector3(0, 1, 0) - #cameraZ = 6 + cameraMode = 'rotate'; + #cameraZ = 4 get cameraZ(){ return this.#cameraZ; @@ -91,15 +93,17 @@ class Hero{ update(delta){ if (this.ready && this.engine.physics.started && !this.disableInput) { this.updateCharacterControls(delta) - // if (this.engine.renderer.xr.isPresenting){ - // this.camera = this.engine.cameraWorld; - // // this.engine.activeObjects.position.x = -this.camera.position.x; - // // this.engine.activeObjects.position.z = -this.camera.position.z; - // }else{ - // this.camera = this.engine.cameraWorld; - // this.engine.camera.position.set(0,0,0); - // this.engine.camera.rotation.set(0,0,0); - // } + if (this.engine.renderer.xr.isPresenting){ + this.cameraMode = 'fixed' + //this.camera = this.engine.cameraWorld; + // this.engine.activeObjects.position.x = -this.camera.position.x; + // this.engine.activeObjects.position.z = -this.camera.position.z; + }else{ + this.cameraMode = 'rotate' + // this.camera = this.engine.cameraWorld; + // this.engine.camera.position.set(0,0,0); + // this.engine.camera.rotation.set(0,0,0); + } } } @@ -116,17 +120,22 @@ class Hero{ } if (!pc.isLocked && !this.model.visible){ this.model.visible = true; - this.#cameraZ = 6 + this.#cameraZ = 4 this.engine.camera.fov = 45; this.engine.camera.updateProjectionMatrix(); } let input = pc.input; + if (this.cameraMode == 'fixed'){ + let vec = new Vector2(input[0], input[1]); + vec.rotateAround(zero2, this.model.rotation.y - this.cameraDelta); + input = [vec.x, vec.y]; + } + let play = this.currentAction || 'idle', velocity = this.walkVelocity; this.fadeDuration = 0.2; if (input[1] && pc.running) { play = 'run'; - velocity = this.runVelocity } else if (input[1] > 0) { play = 'walk' } else if (input[1] < 0) { @@ -232,21 +241,26 @@ class Hero{ let cameraPosition = new Vector3().copy(this.camera.position) let cameraDesiredPosition; - if (true){ + if (this.cameraMode == 'rotate'){ cameraDesiredPosition = new Vector3( this.model.position.x + this.#cameraZ* Math.sin(this.model.rotation.y + Math.PI + this.cameraDelta + this.cameraIdleDelta), this.model.position.y + (pc.isLocked? this.size.y*0.9 : this.cameraY), this.model.position.z + this.#cameraZ* Math.cos(this.model.rotation.y + Math.PI + this.cameraDelta + this.cameraIdleDelta) ) } else { + // cameraDesiredPosition = new Vector3( + // this.model.position.x, + // this.model.position.y + (pc.isLocked? this.size.y*0.9 : this.cameraY), + // this.model.position.z - this.#cameraZ + // ) cameraDesiredPosition = new Vector3( - this.model.position.x, + this.model.position.x + this.#cameraZ* Math.sin(0+ Math.PI + this.cameraDelta + this.cameraIdleDelta), this.model.position.y + (pc.isLocked? this.size.y*0.9 : this.cameraY), - this.model.position.z - this.#cameraZ + this.model.position.z + this.#cameraZ* Math.cos(0+ Math.PI + this.cameraDelta + this.cameraIdleDelta) ) } - cameraPosition.lerp(cameraDesiredPosition, delta*2) + cameraPosition.lerp(cameraDesiredPosition, this.cameraMode == 'fixed' ? 1 : delta*2) this.camera.position.copy(cameraPosition) if (!pc.isLocked){ this.camera.lookAt( diff --git a/src/lib/PointerControls.js b/src/lib/PointerControls.js index a549c80..33b2766 100644 --- a/src/lib/PointerControls.js +++ b/src/lib/PointerControls.js @@ -1,4 +1,4 @@ -import { Vector3, Controls, Euler } from 'three'; +import { Vector3, Vector2, Controls, Euler } from 'three'; const _MOUSE_SENSITIVITY = 0.002; @@ -117,15 +117,15 @@ class PointerControls extends Controls { } get cameraLeft(){ - return this.kb['KeyQ'] || false + return this.kb['KeyQ'] || this.engine.xrController2?.gamepad?.axes[2] > 0.5 || false } get cameraRight(){ - return this.kb['KeyE'] || false + return this.kb['KeyE'] || this.engine.xrController2?.gamepad?.axes[2] < -0.5 || false } get moving(){ - return this.moveForward || this.moveBackward; + return this.moveForward || this.moveBackward || this.moveLeft || this.moveRight; } get rotating(){