Given a vector n1, I find u = n1 x {0,1,0}. Then I find theta = acos(n1 . {0,1,0}).

Then I form an axis-angle rotation matrix from u and theta. Then I form a rotation quaternion from that rotation matrix.

My question is: Can I easily find n1 given the rotation quaternion?

Another version of this question, with more background information:

Assume the reference "up" is Y, and positive Z goes into your screen.

I have a 3D plane defined by it's normal, n1. n1_y is always positive.

I have a 3-vector, n2, which is relative to the XZ plane. I want to find the vector that results from rotating n2 so that it is relative to the plane defined by n1.

Currently, I accomplish this by finding the axis of rotation, by taking n1 cross the XZ unit vector:

float3 u = cross(n1, float3(0,1,0));

Then I find the angle to rotate by:

float theta = acos(dot(float3(0,1,0), n1));

Then I form a rotation matrix from the axis and angle:

float3x3 rotMatrix = float3x3(
        cos(theta) + u.x*u.x*(1-cos(theta)), u.x*u.y*(1-cos(theta))-u.z*sin(theta), u.x*u.z*(1-cos(theta)) + u.y*sin(theta),
        u.y*u.x*(1-cos(theta)) + u.z*sin(theta), cos(theta) + u.y*u.y*(1-cos(theta)), u.y*u.z*(1-cos(theta))-u.x*sin(theta),
        u.z*u.x*(1-cos(theta)) - u.y*sin(theta), u.z*u.y*(1-cos(theta))+u.x*sin(theta), cos(theta)+u.z*u.z*(1-cos(theta))

Then I multiply n2 by the rotation matrix to yield the new vector I wanted.

Currently n1 is precomputed and stored, so I have to find the axis and angle and rotation matrix from n1.

I was hoping there is a way to store a quaternion instead (representing the axis and angle), and easily recover the normal n1 from that quaternion. Is there a way to do that? How?

Or, is there another more efficient way to do this computation?

  • $\begingroup$ I'm new at asking math questions (this is my first one here), so please tell me if something is not clear. $\endgroup$ – TheBigO Apr 22 '11 at 13:59
  • $\begingroup$ Given a quaternion, why not convert it to a rotation matrix $R$, and then recover $n_1$ as $n_1=R^Tv$, where $v=[0,1,0]^T$. $\endgroup$ – Shiyu Apr 22 '11 at 16:13
  • $\begingroup$ BTW, using latex can make the question more clear and easier to read:) $\endgroup$ – Shiyu Apr 22 '11 at 16:15
  • $\begingroup$ Shiyu, do I just write the latex in a Ctrl+K code block? When writing code in comments you put it in ` marks, what do you use for latex in comments? $\endgroup$ – TheBigO Apr 22 '11 at 17:30
  • $\begingroup$ Shiyu, you should post your answer as an answer. I think that will work, I'll try it now. $\endgroup$ – TheBigO Apr 22 '11 at 17:33

Let $v=[0,1,0]^T$. Suppose $R$ is the rotation matrix you obtained. Then $R$ can rotate vector $n_1$ to $v$. That is $Rn_1=v$. Hence $n_1=R^Tv$.

So once you have a quaternion, you can convert it to a rotation matrix $R$ first. Then recover the $n_1$ as $n_1=R^Tv$.

  • 1
    $\begingroup$ I believe this is the right idea, but you don't need to convert the quaternion to a rotation matrix first; you can simply invert the quaternion and apply it to $v$. $\endgroup$ – Rahul Apr 23 '11 at 4:43
  • $\begingroup$ @Rahul: I think your answer is better. $\endgroup$ – Shiyu Apr 23 '11 at 6:00
  • $\begingroup$ Thanks to both of you! Rahul, feel free to add that as an answer, for some rep. $\endgroup$ – TheBigO Apr 23 '11 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.