Transform vector to other coordinate frame using quaternions

In common math, one can describe a vector in another coordinate frame by pre an post multiplying the vector with a quaternion as described on page 25/26 of this document:

In short:


  1. We have a vector in the world coordinate frame from the world to an object v_w2o_w (vector from world to object in world coordinates).

  2. We have a rotation quaternion from the world to the player coordinate frame q_w2p (rotation from world to player).

  3. We are interested in the direction the object is in according to the world coordinates, but in the player coordinate frame. Therefore, we are interested in the vector v_w2o_p.

  4. According to literature, you can obtain this vector by doing the following:

    v_w2o_p = q_w2p X v_w2o_w X q_w2p


The problem is that Unity does not allow post-multiplication of quaternions. Without using parent objects, how do we obtain the vector v_w2o_p ?

Uhm, your question is very confusing. First of all a Quaternion does not represent a coordinate system but just a rotation or rotational space. So you only deal with directions. Directions don’t have a reference point.

Next page 26 mainly deals with transformation matrices which do represent a coordinate system as they combine a rotation and a translation.

If i understood your problem right you have an object “O” somewhere in worldspace. You also have your player object “P”. You want a local space direction vector inside the player space from P towards O.

First you would have to create the world space direction vector from P to O. So all you have to do is subtracting the two positions

V = O - P

V will be the worldspace direction from P to O. To convert this world space direction into a local space direction you either need the inverse transformation matrix (worldToLocal) or the inverse of the Quaternion of your player. You can calculate the inverse of a quaternion by using Quaternion.Inverse. All you have to do now is multiplying the resulting quaternion with your vector V.

You may be confused by Unity’s Quaternion struct. When you multiply a quaterion with a Vector3 you actually rotate the vector. So it does both multiplications at once. If “q” is your rotation quaterion and “q*” the conjugate, what unity basically does when you multiply a quaternion q with a vector v is this:

(q) x (v) x (q*). Note that “x” is the complex quaternion product.

As you might know q and q* actually look like this:

q = w + x*i + y*j + z*k
q* = w - x*i - y*j - z*k