How to rotate an object in processing according to yaw, pitch and roll values

huangapple go评论86阅读模式
英文:

How to rotate an object in processing according to yaw, pitch and roll values

问题

我正在尝试根据俯仰(pitch)和横滚(roll)的值旋转一个以 .obj 文件形式导入的对象。

我实际上使用了以下代码实现了这一目标:

object.rotateX(ConvertToRadians(pitch));

object.rotate(ConvertToRadians(roll), 0, 0, 1); // 这行代码基本上与 rotateZ 函数相同我使用它是因为 rotateZ 会出现奇怪的错误

然而,当新的俯仰和横滚值通过套接字连接传入,并且我使用以下代码时,它会在之前的旋转基础上叠加旋转。举个例子,当物体绕 X 轴旋转了 30 度,我试图使其旋转到 10 度。它会旋转到 40 度。

我尝试在给出新旋转之前保存之前的旋转,并通过以下代码撤销它:

pieta.rotateX(ConvertToRadians(pitch - prevpitch));

它似乎部分地起作用,但经过一段时间后,原始位置(pitch=0,roll=0 时平坦的位置)不再平坦。它会倾斜到某个角度。我不知道原因。我认为将对象的位置重置为原始位置会起作用。

我该如何实现我的目标?

如果您想要重新创建完整代码,这是我的完整代码:
项目文件

英文:

I am trying to rotate an object which was imported as .obj file according to pitch and roll values.

I actually achieved this with this code

object.rotateX(ConvertToRadians(pitch));

object.rotate(ConvertToRadians(roll), 0, 0, 1); // This line is basically the same thing with rotateZ function. I used it because rotateZ gives a weird error.

However, when new pitch and roll values arrive with the socket connection and I use these lines of code. It adds to the previous rotation. To give an example, when the object is rotated around X-axis for 30 degrees and I try to make it rotate to 10 degrees. It rotates it to 40 degrees.

I tried saving the previous rotation and undoing it before giving the new rotation with this line

pieta.rotateX(ConvertToRadians(pitch-prevpitch));

It seems to work partially but after some time the original position (pitch=0 and roll = 0 is not flat anymore. It tilts to some angle. I don't know the reason). I think resetting the object's position to the original will work.

How can I achieve my aim?

This is my full code if you want to recreate it
Project File

答案1

得分: 0

使用resetMatrix()函数在每一帧中设置一个全新的矩阵:

object.resetMatrix();
object.rotateX(ConvertToRadians(pitch));
英文:

Use resetMatrix() to setup a completely new matrix in every frame:

object.resetMatrix();
object.rotateX(ConvertToRadians(pitch));

huangapple
  • 本文由 发表于 2020年8月25日 21:20:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63579776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定