[英]Libgdx How to rotate a 3D model on multiple axes with phone orientation
I'm trying to rotate a 3D model on multiple axes at once using the phone's accelerometers. 我正在尝试使用手机的加速度计一次在多个轴上旋转3D模型。 When I do this, I use setToRotation()
however, this only does one axis at a time. 当我这样做时,我使用setToRotation()
但是,这一次只能做一个轴。
For example: 例如:
ModelInstance modelInstance = instances.first();
//ROLL
modelInstance.transform.setToRotation(Vector3.Z, phoneAccel.y*9);
//PITCH
modelInstance.transform.setToRotation(Vector3.X, phoneAccel.z*9);
The phone is in forced-landscape mode. 手机处于强制横向模式。 I get the instance of the model I want to rotate. 我得到了我想要旋转的模型的实例。
I set the Vector3 phoneAccel
based on Gdx.input.getAccelerometerX/Y/Z()
. 我根据Gdx.input.getAccelerometerX/Y/Z()
设置了Vector3 phoneAccel
。
In the above example, both lines of code work correctly, but only independently. 在上面的示例中,两行代码都能正常工作,但只能独立工作。 When I try to use both (one after another) the first rotate (ROLL) is removed. 当我尝试使用两者(一个接一个)时,第一次旋转(ROLL)被移除。 I would have thought the two rotation matrices would be cumulated, ie, the Z Axis is applied a rotation, and then the X Axis is applied a rotation. 我原本认为两个旋转矩阵将累积,即Z轴应用旋转,然后X轴应用旋转。
Do I need to create my own cumulative rotation matrix, and then apply that at the end? 我是否需要创建自己的累积旋转矩阵,然后在最后应用它?
Any thoughts? 有什么想法吗? Cheers 干杯
Matrix4#setToRotation
will remove any other transformation (like rotation) previously set (hence the name "setTo"). Matrix4#setToRotation
将删除先前设置的任何其他转换(如旋转)(因此名称为“setTo”)。 To add a rotation transformation to an already rotated matrix, use the rotate
method instead. 要将旋转变换添加到已旋转的矩阵,请改用rotate
方法。 This will post-multiply the transformation. 这将使变换后加倍。
modelInstance.transform.setToRotation(Vector3.Z, phoneAccel.y*9);
modelInstance.transform.rotate(Vector3.X, phoneAccel.z*9);
However this will not produce your desired result, since you want the rotations to independent from each other (the rotation around the Z axis should not influence the rotation around the X axis for example). 但是,这不会产生您想要的结果,因为您希望旋转彼此独立(例如,围绕Z轴的旋转不应影响围绕X轴的旋转)。 For this you can use euler angles: 为此你可以使用欧拉角:
modelInstance.transform.setFromEulerAngles(yaw, pitch, roll);
The method setToRotation
sets the model to this rotation, as the name says. 方法setToRotation
将模型设置为此旋转,如名称所示。 This means, that you "overwrite" the rotation arround the Z-Axis and set it to a rotation ONLY arround the X-Axis. 这意味着,您“覆盖”Z轴周围的旋转,并将其设置为仅在X轴周围旋转。
There are different ways to do what you want to achieve: 有不同的方法可以实现您想要实现的目标:
Quaternion
s like @noone said. 你可以像@noone那样使用Quaternion
。 rotate
. 你可以使用rotate
。 If you do that you need to stor your current rotation in forexample a Vector2
, where the x value is the current rotation arround Z-Axis and the y value is the rotation arround the X-Axis. 如果你这样做,你需要在例如Vector2
当前的旋转,其中x值是Z轴的当前旋转,y值是X轴Vector2
的旋转。 setToRotation
, with a custom axis, which you construct our of the phoneAccel
values. 您可以使用setToRotation
和自定义轴,您可以使用自定义轴构建phoneAccel
值。 The setToRotation
method gives you the posibility to define a Vector3 direction
and a Vector3 face
, defining which face should look to this direction. setToRotation
方法为您提供了定义Vector3 direction
和Vector3 face
的可能性,定义了哪个面应朝向此方向。 For example: modelInstance.transform.setToRotation(Vector3.Z, Vector3.Y)
will make his top look inot Z-Direction. 例如: modelInstance.transform.setToRotation(Vector3.Z, Vector3.Y)
将使他的顶部外观为Z-Direction。 With a Vector3(0, -1, 0)
his bottom (maybe his feet) will look in that direction. 使用Vector3(0, -1, 0)
他的底部(也许是他的脚)会向那个方向看。
Hope it is clear 希望很清楚
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.