This site uses strictly necessary cookies. More Information

X- Home /

# 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:

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).We have a rotation quaternion from the world to the player coordinate frame

*q_w2p*(rotation from world to player).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*.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* ?

**Answer** by Bunny83
·
Aug 21, 2017 at 11:38 PM

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
```

`(q) x (v) x (q*)`

. Note that "x" is the complex quaternion product.

Answers my question. Thank you.

Please note that:

*v_w2o_w* is the DIRECTION from world to the object in the world coordinate frame, as stated in the question.

*q_w2p* is the ROTATION from the world coordinate frame to the player coordinate frame, as stated in the question.

*q_p2w* is the ROTATION from the player to the world coordinate frame (i.e., the conjugate of *q_w2p* )

Hope this makes it easier for the next person to understand both question and answer!

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Lerp takes me to 0,0,0 2 Answers

Rotation issue with remote players projectiles 0 Answers

Using custom Transform/GameObject classes? 0 Answers

Remember the start coordinates? 2 Answers

mousePosition.x value type. (Equations involving coordinates) 1 Answer