问题
How can i achieve THREE.js pointerlock controls example functionality but move around programmatically (values from accelerometer device) instead of mouse movement?
回答1:
THREE.PointerLockControls (next: Module) works this way:
- Module makes a construction yawObject -> pitchObject -> camera (pitchObject is a child of yawObject and camera becomes a child of pitchObject).
- You add yawObject (which you could get from Module with
yourControls.getObject()
function) to your scene (to keep transforms updated). - Module adds 'mousemove' event listener and updates yawObject.rotation.y and pitchObject.rotation.x when you move mouse if
yourControls.enabled !== false
. - Next if you are interested in actually locking cursor you could do it in your client code as in example.
- And to update your camera position you could manipulate yawObject's position (
yourControls.getObject().position
).
So to manipulate controls without mouse you could setup Module this way:
camera.position.set(0, 0, 0);
camera.rotation.set(0, 0, 0); // THREE.PointerLockControls does this too
var myControls = new THREE.PointerLockControls(camera);
var controlsObject = myControls.getObject();
controlsObject.position.set(myEntryX, myEntryY, myEntryZ); // set starting point
controlsObject.rotation.y = myEntryYaw; // rotate yaw obj
controlsObject.children[0].rotation.x = myEntryPitch; // rotate pitch obj
scene.add(controlsObject);
and then keeping myControls.enabled = false
manipulate controlsObject.position
, controlsObject.rotation.y
, controlsObject.children[0].rotation.x
.
来源:https://stackoverflow.com/questions/38922281/how-can-i-achieve-exact-functionality-like-pointerlock-example-in-threejs-but-mo