How can i achieve exact functionality like pointerlock example in threejs but move around programmatically instead of mouse

旧时模样 提交于 2019-12-25 07:47:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!