quaternion rotation issues

This is a discussion on quaternion rotation issues within the Game Programming forums, part of the General Programming Boards category; Hi. I am using quaternions to do vector rotation. For some reason it is not working properly. When I try ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    6

    quaternion rotation issues

    Hi. I am using quaternions to do vector rotation. For some reason it is not working properly. When I try to rotate the vector 0,0,1 90 degrees along the x axis with this quaternion-0.70710678, 0, 0, 0.70710678(with the last number being w) the output is 0, -1,
    -0.207107 which should be 0,-1,0. Here is my code for the vector rotation function.
    Code:
    ScePspFVector3 multvector(float quaternion[4], ScePspFVector3 vector)
    {
        float vectorQuaternion[4],resultQuaternion[4];
        ScePspFVector3 resultVector;
        vectorQuaternion[0] = vector.x;
        vectorQuaternion[1] = vector.y;
        vectorQuaternion[2] = vector.z;
        vectorQuaternion[3] = 0.0f; float inverseQuaternion[4] = {-quaternion[0],-quaternion[1],-quaternion[2],quaternion[3]};
        mult(resultQuaternion,quaternion, vectorQuaternion);
        mult(resultQuaternion,resultQuaternion, inverseQuaternion);
        resultVector.x = resultQuaternion[0];
        resultVector.y = resultQuaternion[1];
        resultVector.z = resultQuaternion[2];
        return resultVector;
    }
    What am I doing wrong?

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    Perform the calculation by hand first, with pen and paper (and check to make sure you get the correct result). Then step through the code with a debugger and check where you get the wrong values.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    Just hit me when i was preparing some dinner (yah yah my mind is weird sometimes...). I'm almost willing to bet money the problem is the second mult, where resultQuaternion works as both input and output variable. If you modify the output before completely calculating it, i.e. doing something like this
    Code:
    mult(output, lhs, rhs)
        output[0] = // stuff depending on lhs and rhs
        output[1] = // stuff depending on lhs and rhs
    by the time you calculate output[1] you will have modified lhs aswell.

    This can cause some trouble for you.

  4. #4

    Join Date
    May 2005
    Posts
    1,041
    Post your Mult function.

    EDIT:
    My guess is you've made a mistake in Mult, also possibly not normalizing in the right location.


    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    The code below is the Quaternion implementation from Octave 3.2.4 (basically open source MatLab). The syntax is simple so hopefully the language difference won't be a problem as this is a math question. I removed the error checking portions from the code but attached the full source in a zip file for you.

    Code:
    function v = qtrans (v, q)
    
      v = qmult (q, qmult (v, qinv (q)));
    
    endfunction
    Code:
    function retval = qmult (a, b)
    
      [a1, b1, c1, d1] = quaternion (a);
      [a2, b2, c2, d2] = quaternion (b);
      
      ri = b1*c2 - c1*b2 + d1*a2 + a1*d2;
      rj = c1*a2 - a1*c2 + d1*b2 + b1*d2;
      rk = a1*b2 - b1*a2 + d1*c2 + c1*d2;
      rr = -(a1*a2 + b1*b2 + c1*c2) + d1*d2;
      
      retval = quaternion (ri, rj, rk, rr);
    
    endfunction
    Code:
    function retval = qinv (q)
    
      if (norm (q) != 0)
        retval = qconj (q) / sum (q .* q);
      else
        error ("qinv: zero quaternion passed!");
      endif
    
    endfunction
    Code:
    function retval = qconj (q)
    
      [a, b, c, d] = quaternion (q);
    
      retval = quaternion (-a, -b, -c, d);
    
    endfunction
    Attached Files Attached Files
    Last edited by BobMcGee123; 01-16-2013 at 12:31 PM.
    I'm not immature, I'm refined in the opposite direction.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    6
    Here is my mult function. It does not normalize any quaternions.
    Code:
    void mult(float* C,float A[], float B[])
    {
      C[0] = A[3]*B[0] + A[0]*B[3] + A[1]*B[2] - A[2]*B[1];
      C[1] = A[3]*B[1] + A[1]*B[3] + A[2]*B[0] - A[0]*B[2];
      C[2] = A[3]*B[2] + A[2]*B[3] + A[0]*B[1] - A[1]*B[0];
      C[3] = A[3]*B[3] - A[0]*B[0] - A[1]*B[1] - A[2]*B[2];
    }

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    See my second post. When C and A point to the same array you will modify A when you write to C thus corrupting your calculations.

  7. #7

    Join Date
    May 2005
    Posts
    1,041
    Yeah, there's that too, which I managed to totally miss and is probably your problem

    Code:
        mult(resultQuaternion,quaternion, vectorQuaternion);
        mult(resultQuaternion,resultQuaternion, inverseQuaternion);
    I'm not immature, I'm refined in the opposite direction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ quaternion camera help
    By Gadersd in forum C++ Programming
    Replies: 0
    Last Post: 10-11-2012, 03:48 PM
  2. Vertice(s) location after quaternion Rotation
    By dgubser in forum C# Programming
    Replies: 8
    Last Post: 09-05-2012, 05:10 PM
  3. Replies: 3
    Last Post: 11-14-2011, 06:04 PM
  4. Expressing a quaternion with zero rotation
    By shrink_tubing in forum C++ Programming
    Replies: 12
    Last Post: 08-18-2010, 03:44 AM
  5. Quaternion Rotation
    By psychopath in forum Game Programming
    Replies: 16
    Last Post: 07-23-2006, 03:28 PM

Tags for this Thread


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