简体   繁体   English

用于太空游戏的Three.js相机控件

[英]Three.js camera controls for space game

In a three.js project, I'm using a modified version of PointerLockControls.js for the camera control. 在three.js项目中,我将PointerLockControls.js的修改版本用于相机控件。 I want to modify the rotation functionality so that there is no absolute "up" axis by which the camera rotates, but rather moving the mouse up or down will pitch indefinitely, same for left and right for yawing (and keys for rolling). 我想修改旋转功能,以使相机没有旋转的绝对“上”轴,但是上下移动鼠标将无限期倾斜,左右偏航(以及滚动角度)相同。

I can't seem to get the yawing component working, as it seems to rotate around the same axis regardless of pitch. 我似乎无法使偏航组件正常工作,因为无论俯仰如何,偏航组件都围绕相同的轴旋转。 (ie moving left or right when face facing straight upwards will just rotate the camera) (即,当脸朝上朝上时向左或向右移动只会旋转相机)

Any help in the right direction would be great! 在正确方向上的任何帮助都将非常有用!

I had the same problem recently, so I had a look at some of the THREE.*Controls files, similar to you. 最近我遇到了同样的问题,因此我看了一些类似于您的THREE。* Controls文件。

Using those as a basis, I made this: https://github.com/squarefeet/THREE.ObjectControls 以此为基础,我做到了: https : //github.com/squarefeet/THREE.ObjectControls

The important bits are the following (see here for context): 以下是重要的位(有关上下文,请参见此处 ):

var updateTarget = function( dt ) {
    var velX = positionVector.x * dt,
        velY = positionVector.y * dt,
        velZ = positionVector.z * dt;

    rotationQuaternion.set(
        rotationVector.x * dt,
        rotationVector.y * dt,
        rotationVector.z * dt,
        1
    ).normalize();

    targetObject.quaternion.multiply( rotationQuaternion );

    targetObject.translateX( velX );
    targetObject.translateY( velY );
    targetObject.translateZ( velZ );
};

The rotationVector is probably of most interest to you, so here's what it's doing: rotationVector可能是您最感兴趣的,所以这是它的作用:

  • It's using a THREE.Vector3 to describe the rotation, the rotationVector variable in this example. 它使用THREE.Vector3描述旋转,在此示例中为rotationVector变量。

  • Each component of the rotationVector ( x , y , z ) is relative to pitch, yaw, and roll respectively. rotationVectorxyz )的每个分量分别相对于俯仰,偏航和侧倾。

  • Set a quaternion's x , y , and z values to the of the rotation vector, making sure the w component is always 1 (to learn what the w component does, see here , it's a great answer. 将四元数的xyz值设置为旋转向量的,确保w分量始终为1 (要了解w分量的作用,请参见此处 ,这是一个很好的答案。

  • Normalizing this quaternion will get us a quaternion of length 1 , which is very handy when we come to the next step... 对该四元数进行规范化将为我们提供长度为1的四元数,这在我们进行下一步时非常方便...

  • targetObject in this case is an instance of THREE.Object3D (a THREE.Mesh , which inherits from THREE.Object3D ), so it has a quaternion we can play with. targetObject在这种情况下的一个实例THREE.Object3D (一THREE.Mesh ,从继承THREE.Object3D ),所以它有一个四元数,我们可以一起玩。

  • So now, it's just a matter of multiplying your targetObject 's quaternion by your shiny new rotationQuaternion . 因此,现在,只需将targetObject的四元数乘以闪亮的新rotationQuaternion

  • Since our object is now rotated to where we want, we can move it along it's new axis angles by using translateX/Y/Z. 由于我们的对象现在已旋转到所需位置,因此我们可以使用translateX / Y / Z沿其新的轴角度移动它。

The important thing to note here is that quaternions don't act like Euler vectors. 这里要注意的重要一点是,四元数的行为不像Euler向量。 Rather than adding two quaternions together to get a new angle, you multiply them. 与其将两个四元数加在一起以获得一个新的角度,不如将它们相乘。

Anyway, I hope that helps you somewhat! 无论如何,希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM