Why won't my Quaternion rotate properly?

≡放荡痞女 提交于 2019-12-04 13:06:11

I'm still not 100% sure what your question is asking, but I'll give it a shot.

Problem:

Given a quaternion representing a 0 degree rotation about x, y, z, generate a new quaternion representing a 45 degree rotation about the x axis

  • Start with a quaternion representing no rotation, call it q1

q1 = (w1, x1, y1, z1)

q1.w1 = cos(0/2) = 1

q1.x1 = 0 * sin(0/2) = 0

q1.y1 = 0 * sin(0/2) = 0

q1.z1 = 0 * sin(0/2) = 0

So q1 = (1, 0, 0, 0)

  • Generate a new rotation that is 45 degrees (PI/4 radians) about the X axis We need a temporary quaternion to modify q1. Let's call it q2.

q2 = (w2, x2, y2, z2)

q2.w2 = cos(PI/4 / 2) = cos(PI/8)

q2.x2 = 1.0 * sin(PI/4 / 2) = 1.0 * sin(PI/8) = sin(PI/8)

q2.y2 = 0.0 * sin(PI/4 / 2) = 0.0

q2.z2 = 0.0 * sin(PI/4 / 2) = 0.0

so q2 = (cos(PI/8), sin(PI/8), 0, 0)

  • Now this last step is important, you modify your original quaternion by a left-hand multiplication of the temporary quaternion

What I mean is this:

q1 = q2 * q1

Your multiplication function is written correctly, so the problem is not there. Remember that quaternion multiplications are not commutative. That is q2 * q1 is NOT the same as q1*q2!

At this point q1 is modified to represent a 45 degree rotation about the X axis.

To print out the angle in degrees, you need to compute 2.0 * acos(q1.w) / PI * 180

Your code is incorrectly computing q1.w/PI * 180 to get the angle in degrees.

More specifically, change

toANGLE(resQuat.getW())

to

toANGLE(2.0f * Math.acos(resQuat.getW()))

I haven't looked at your code beyond that, but try applying these concepts and see if that fixes your problem.

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