Rotating a point using a quaternion, then using an inverse quaternion to rotate back?

This is a discussion on Rotating a point using a quaternion, then using an inverse quaternion to rotate back? within the Game Programming forums, part of the General Programming Boards category; I have a function that takes a unit quaternion <w,x,y,z> and a point <x,y,z> and spits out the rotated 3d ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Rotating a point using a quaternion, then using an inverse quaternion to rotate back?

    I have a function that takes a unit quaternion <w,x,y,z> and a point <x,y,z> and spits out the rotated 3d point.
    void rotate(const float *quaternion, const float *point, float *newpoint)

    This code works OK:
    void rotate(quaternion, point, newpoint)

    I would now like to rotate the point back to where it was originally. I understand this is possible using the inverse of the quaternion (which for a unit quaternion is equivalent to the conjugate, right?):

    This is how I do this this:
    inverse_quaternion[4];
    inverse_quaternion[0] = quaternion[0]; //w
    inverse_quaternion[1] = -quaternion[1]; //x
    inverse_quaternion[2] = -quaternion[2]; //y
    inverse_quaternion[3] = -quaternion[3]; //z
    void rotate(inverse_quaternion, rotated_point, final3dpoint)


    Unfortunately this is not doing what I expect. What am I missing?
    Last edited by fuse; 11-11-2011 at 02:04 PM.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    What you have will only work if the quaternion is normalized, otherwise you've got to multiply by 1/[magnitude squared] (or 1/[Q dot Q]):
    Code:
    float dot = quaternion[0]*quaternion[0] + quaternion[1]*quaternion[1] + quaternion[2]*quaternion[2] + quaternion[3]*quaternion[3];
    inverse_quaternion[0] = quaternion[0] / dot; //w
    inverse_quaternion[1] = -quaternion[1] / dot; //x
    inverse_quaternion[2] = -quaternion[2] / dot; //y
    inverse_quaternion[3] = -quaternion[3] / dot; //z
    And if this isn't the problem, could you post your code for rotate()? It can be hard to catch mistakes in all the similar-looking arithmetic involved in this sort of stuff.
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Ok, I modified my multiplication code and now it works out (sans some small math errors. Hmm, guess I better figure out what's going on there! When mapped, the z and y axis are correct, but the x axis appears to be inversed.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Quote Originally Posted by fuse View Post
    Ok, I modified my multiplication code and now it works out (sans some small math errors. Hmm, guess I better figure out what's going on there! When mapped, the z and y axis are correct, but the x axis appears to be inversed.
    This sounds like a problem with your world matrix or the way you are constructing it from the up, right, and look vectors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quaternion fun... Is there a more efficient way to do this?
    By wallsocket in forum Game Programming
    Replies: 2
    Last Post: 10-12-2011, 04:20 PM
  2. Expressing a quaternion with zero rotation
    By shrink_tubing in forum C++ Programming
    Replies: 12
    Last Post: 08-18-2010, 03:44 AM
  3. Quaternion Rotation
    By psychopath in forum Game Programming
    Replies: 16
    Last Post: 07-23-2006, 03:28 PM
  4. Quaternion question
    By Mr Q in forum Game Programming
    Replies: 15
    Last Post: 04-21-2004, 09:34 AM
  5. Quaternion class problem
    By Lurker in forum C++ Programming
    Replies: 14
    Last Post: 11-18-2003, 05:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21