Three.js - 旋转不尊重当地的方向当地、方向、Three、js

2023-09-08 10:32:02 作者:沉舟侧畔过

我使用的是大阵周围场景的中心点内置对象,并需要操纵他们周围的局部轴。他们都面临着用一个空对象,并注视()的由来,然后我用this方法正确对齐的其他轴。获取初始旋转这样伟大的工作,遗憾的是,当我尝试旋转飞这些对象 object.rotation.x =<量GT; ,它不尊重物体的局部轴。

I'm using a large array of objects built around a center point in a scene, and need to manipulate them all around their local axis. They are all facing the origin using a blank object and lookAt(), then I used this method to align the other axes correctly. Getting the initial rotation this way worked great, unfortunately when I try to rotate these objects on the fly with object.rotation.x = <amount>, it does not respect the local axis of the object.

混乱的部分是,它甚至没有使用全局轴,它使用的轴看起来几乎完全是任意的。 我建立了一个的jsfiddle来这里证明这一点。正如你可以在129行, looker.rotation.z 看正常工作,它沿Z轴旋转时正常,但如果它更改为X或Y,它不沿着局部或全局坐标轴旋转。如果任何人都可以去神秘化发生了什么造成这一点,那将是巨大的。

The confusing part is, it's not even using the global axis, the axis it's using almost seems entirely arbitrary. I set up a JSFiddle to demonstrate this here. As you can see on line 129, looker.rotation.z works correctly, it rotates along the Z axis properly, but if it's changed to X or Y, it doesn't rotate along local or global axes. If anyone could demystify what is happening to cause this, that would be great.

推荐答案

正在发生的事情是,你想要一些轮换添加到当前的方向,并且设置变量 looker.rotation.z 意味着其他的事情。

What is happening is that you want to add some rotation to the current orientation, and setting the variable looker.rotation.z means other thing.

最后,计算出旁观者的旋转矩阵,将有类似的信息(伪code:该功能不是这些,而是​​你的想法):

At the end, to calculate the rotation matrix of the looker, there will be something like (pseudocode: the functions are not these, but you get the idea):

this.matrix.multiply( makeXRotationMatrix(this.rotation.x) )
this.matrix.multiply( makeYRotationMatrix(this.rotation.y) )
this.matrix.multiply( makeZRotationMatrix(this.rotation.z) )
DrawGeometry(this.geom, this.matrix)

和旋转的成分并不直观。这就是为什么它似乎并没有遵循任何轴系统。

and composition of rotations are not intuitive. This is why it doesn't seem to follow any axis system.

如果你想在某个坐标轴应用旋转现有的矩阵,它可以与职能方面取得 rotateX(角度) rotateY (角度) rotateZ(角度) rotateOnAxis(轴,角度)可以是一个 THREE.Vector3

If you want to apply a rotation in some axis to the existing matrix, it can be made with the functions rotateX (angle), rotateY (angle), rotateZ (angle), and rotateOnAxis (axis, angle). axis can be a THREE.Vector3.

直接修改 looker.rotation.z 的作品,因为它是最接近旋转的几何形状,也不会受其他旋转(记住,变换矩阵应用在相反的顺序,如 T * R * G 研究 otating的