# How to find a rotation of another coord system ?

## Recommended Posts

ryt    294

Lets say we have a global coord system and local coord system and that local coord system is rotated by some amount by some arbitrary vector.
How could I find this rotation vector and angle given the two coord systems ?

##### Share on other sites
alvaro    21246

The amount of rotation does not depend on the coordinate system. The vector around which you are rotating should be expressed in the basis of the appropriate coordinate system. You normally convert between coordinate systems by a matrix multiplication, but I am not entirely sure what format you have your coordinate systems in.

##### Share on other sites
ryt    294

Matrix solution would be ok, but I was looking for a quaternion solution.

Ill try to give an eg. Lets say we have a normal coord system, and a vector defined in it as v(-0.5, 0, -1). And that there is another coord system (of an object) located at (2, 1, 1) and it is rotated by facing directly on opposite z, that is its x and y are the same as original coord system but its z is pointing in (0, 0, -1) in respect to original coord system.

What I want to know what are the coords of vector v in respect to the second coord system.

##### Share on other sites
alvaro    21246

The location of the origin of the coordinate systems is irrelevant for this particular problem, because we are converting only vectors. When you say "and it is rotated by facing directly on opposite z", you have to realize that that transformation is not a rotation at all: It's a reflection. The coordinates of v in the second coordinate system are (-0.5, 0, 1). If v is the axis of a rotation, you'll actually have to negate the rotation amount because the coordinate systems have different [url="http://en.wikipedia.org/wiki/Orientation_%28vector_space%29"]orientation[/url].

Edited by Álvaro

##### Share on other sites
ryt    294

Hmm, Im all confused.

So you say the coords of vector v remain unchanged. I don't understand this.

Lets then say that a second coord system is not a reflection but a rotation with its forward vector (z component) its facing (0.5, 0, -1), and its up (y) and right (x) are orthonormalized in respect to forward (z), that is they are the same as in first coord system. What would than be the coord of a vector v ?

Would it still remain the same ?

I don't mean the rotation around vector v, just its coords in respect to second coord system.

Edited by ryt

##### Share on other sites
alvaro    21246

So you say the coords of vector v remain unchanged.

No, that's not what I say. The sign of z changed.

Lets then say that a second coord system is not a reflection but a rotation with its forward vector (z component) its facing (0.5, 0, -1), and its up (y) and right (x) are orthonormalized in respect to forward (z), that is they are the same as in first coord system.

I don't understand that description of a coordinate system. Just tell me what vector is (1,0,0) in the new coordinates, what vector is (0,1,0) and what vector is (0,0,1). From that we can build a 3x3 matrix and do all the operations easily.

##### Share on other sites
ryt    294

So you say the coords of vector v remain unchanged.

No, that's not what I say. The sign of z changed.

Lets then say that a second coord system is not a reflection but a rotation with its forward vector (z component) its facing (0.5, 0, -1), and its up (y) and right (x) are orthonormalized in respect to forward (z), that is they are the same as in first coord system.

I don't understand that description of a coordinate system. Just tell me what vector is (1,0,0) in the new coordinates, what vector is (0,1,0) and what vector is (0,0,1). From that we can build a 3x3 matrix and do all the operations easily.

Oh ok. To a response on your first quote.

I didnt notice that sign change, things are starting to be a little more clear now. So my question now is how did you do that ? Is there some matrix, quaternion inversion or maybe something else ?

On a second quote.

I meant for second coord system to be some object in space with its forward (z), up (y) and right (x) as a coord system. So for this object in its local coord system its basis are forward (0, 0, 1), up (0, 1, 0), right (1, 0, 0). But in terms of first coord system (the one where this object is placed) its basis are forward (0.5, 0, -1), up (0, 1, 0), right (1, 0, 0).

This time in second quote the object is not translated, its located at origin (for easier rapresentation). Note that its up and right are unchanged from first coord system, also for easier rapresentation.

So the thing I was actualy looking for is the vector v in terms of this object, that is its local coord system.

##### Share on other sites

You can just write down the matrix if you know what it does to (1, 0, 0), (0, 1, 0) and (0, 0, 1), it is just the images of those vectors after the transform as the rows of the matrix.

So to map (1, 0, 0) to (1, 0, 0); (0, 1, 0) to (0, 1, 0) and (0, 0, 1) to (0.5, 0, -1) you would use the matrix

(1, 0, 0)

(0, 1, 0)

(0.5, 0, -1)

and to map back you use the inverse of that matrix.

The matrix has determinant -1 (see http://www.wolframalpha.com/input/?i=det{{1%2C+0%2C+0}%2C+{0%2C+1%2C+0}%2C+{0.5%2C+0%2C+-1}} ), so it corresponds to a reflection and the orientation is flipped. You can't represent that transform with a unit quaternion since unit quaternions correspond to rotations and they all have determinant +1.

##### Share on other sites
alvaro    21246
The problem with the matrix in Paradigm Shifter's post is that it is not orthonormal. If you want your rotations to still be recognizable as rotations in the new coordinates, you need to use an orthonormal basis.

##### Share on other sites

Yeah I was gonna mention that at the end but deleted it.

You can make an orthonormal basis from independent ones using cross products and normalisation or use the Gram-Schmidt process http://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process

Unit quaternion rotations always give an orthonormal matrix (ignoring floating point inaccuracy) but you can't represent a reflection unless you also use a scale matrix to scale with a negative determinant.

##### Share on other sites
tonemgub    2008

The terms "local coordinate sytem" and "global coordinate system" are misnomers. In reality, there is only one coordinate system (that is used in computer graphics, anyway): the carthesian coordinate system. The "local" and "global" terms are usually used only to differentiate between coordinates before a certain transformation is applied to them (the "local coordinates") and the same, but already transformed coordinates (the "global coordinates"). From my experience, these terms are very rarely used in technical documentation, and when they are, they are used informally.

The transformation of one set of coordinates (a position vector) to another is done using a transformation matrix. The amount of rotation applied is held in this matrix, along with translation and scaling amounts. So if you have this matrix, you can get the rotation/translation and scaling amounts from it, using a function like D3DXMatrixDecompose.

Edited by tonemgub

##### Share on other sites
ryt    294

Yea, its my mistake, sorry. I was faster at typing than thinking. I thought they were orthogonal but obviously they cant be if two of them are (1, 0, 0), (0, 1, 0) and the third is rotated.

Here it is again complete, right (0.5, 0, -1), up (0, 1, 0), forward (1, 0, 0.5). Here they are orthogonal but if we normalize they become right (0.44, 0, -0.89), up (0, 1, 0), forward (0.89, 0, 0.44). Now they are also orthonormal.

The way Paradigm Shifter suggested it we could make a matrix from them like:

(044, 0, -0.89)

(0, 1, 0)

(0.89, 0, 0.44)

And the vector v is (-0.5, 0, -1). So I multiplied this matrix (lets call it M) with v and I got v' (0.67, 0, -0.88).

Now I wonder, are this the coords of a same point in space but in respect to the space of M ?

I really am not sure, always when I wanted to rotate some vector I would do it this way. So I think that this vector v' is a rotated vector and not the same vector in space.

Edited by ryt

##### Share on other sites

You need your right vector as the first row, and your forward as the 3rd. I think you have them swapped around.

To repeat, the first row of the matrix is what (1, 0, 0) is mapped to (so the right vector), 2nd row is what happens to (0, 1, 0) (your up), 3rd row is what happens to (0, 0, 1) (your forward).

##### Share on other sites
ryt    294

Yes, they are written in correct order. I switched them (right is now (0.5, 0, -1)) from previous example because I thought that this way it would be easier.

Edited by ryt

##### Share on other sites

Ok. Do you want to transform in the other direction then? Use the inverse of M to do that. If the matrix is orthonormal, the inverse is the transpose.

##### Share on other sites
ryt    294

I don't know really, all I want is to now find the vector v' cords.

Actually v == v', they are the same point in space. v is defined as (-0.5, 0, -1), but I want its coords in therms of matrix M.

Imagine it like this, at the beginning we are located at origin of our coord system, we look around and we see another object (also at origin) with its local coord system as M. Its rotated a little bit counter clockwise (right hand system). And then you also see a point in space located at v (-0.5, 0, -1).

Now you transfer to the object coord system, and suddenly the matrix M that you saw before becomes:

(1, 0, 0)

(0, 1, 0)

(0, 0, 1)

And also everything else changes (coords of all objects) because we now look at everything from another coord system. But all objects and points haven't moved. They are at the same place as before. Now you take a look a point v and you see it at same position as before, that's why v == v'.

So now I want to know the coords of point v', that is from the point of view of our object (that rotated a little bit).

Edited by ryt

##### Share on other sites

I don't understand what you mean.

If you have v' = Mv

then

M-1v' = v (multiply both sides on the left by M-1)

and if M is orthonormal then M-1 = MT

##### Share on other sites
ryt    294

I want to know the coord of vector v (-0.5, 0, -1) from the object (M) point of view.

I haven't sad that v' = Mv. I sad that I just multiplied it like that to get some point (to se what Ill get). But that's wrong because that's rotated point, and point v doesn't rotate and move. No point in space or object moves or rotates.

I simply transfer my point at "looking" at the world. The matrix M becomes my original point of view. Because of this every coordinate changes, even of point v.

The original coord system, the one where everything was defined, also changes and its right, up, forward coords when looked from new object (M).

Point v has not changed its position in terms where its located it space physically, just its coords change because we look at it from another point (M).

Edited by ryt

##### Share on other sites

I still don't understand what you are after. Applying a matrix to a vector changes the vector, and world space isn't special.

I think you are trying to do one of the following:

a) find v = M * (-0.5, 0, -1)T  (note, I use transpose there since the vector should be a column vector). That is easy, it's just a matrix * vector multiply.

or b) find vector p such that

(-0.5, 0, -1)T = M * p

Then p = M-1 * (-0.5, 0, -1)T

##### Share on other sites
alvaro    21246

I want to know the coord of vector v (-0.5, 0, -1) from the object (M) point of view.
I haven't sad that v' = Mv. I sad that I just multiplied it like that to get some point (to se what Ill get). But that's wrong because that's rotated point, and point v doesn't rotate and move. No point in space or object moves or rotates.

I like that you have this intuition that the point is a point and different coordinates are just different ways to describe the same unmoving object. Well, v is actually a vector, not a point, but the central idea is the same.

The thing is, when you convert coordinates in one basis to coordinates in another basis, the arithmetic involved is the same as in a transformation of the space. One way to think about it is that whether you move the camera or you move the world around it, the relative positions of the camera and the world are the same. Hopefully that clears up some of the miscommunication.

Now, let's use v_A' to denote the coordinates of v in basis A and v_B' to denote the coordinates of v in basis B. You can convert between v_A and v_B by matrix multiplication:

M * v_A = v_B

M is the matrix whose columns are the coordinates in B of the vectors that form basis A. The matrix that maps v_B to v_A is the inverse of M.

If you are dealing with orthonormal bases, the matrices involved are all rotations or reflected rotations, and the inverse is just the transpose, which is much easier to compute.

##### Share on other sites
ryt    294

I think I've got it, thank you very much, it just needs to be multiplied with inverse matrix M.

But I think I set up my matrix wrong so Ill set it up again.

Original matrix M, we set its columns as right, up and forward:

(0.44, 0, 0.89)

(0, 1, 0)

(-0.89, 0, 0.44)

Inverse of M, M(-1):

(0.44, 0, -0.89)

(0, 1, 0)

(0.89, 0, 0.44)

So the result of multiplying M(-1)*v = v', where v' (0.67, 0, -0.88) and its the same vector in space.

Edited by ryt

##### Share on other sites

Phew, that was hard work ;)

Glad you got it in the end though.

Are you learning linear algebra or just looking at game programming material? A bit of linear algebra would help you (especially so your terminology is understandable to the likes of Alvaro and myself). Note how we managed to say what you were trying to achieve in a few equations which are a lot less ambiguous than wordy descriptions.

EDIT: If you want the quaternion solution, it's fairly similar.

If you have

vout = q * vin * q-1

then (multiplying on left by q-1)

q-1 * vout = q-1 * q * vin * q-1 = vin * q-1

(multiply on right by q)

q-1 * vout * q = vin * q-1 * q = vin

so

vin = q-1 * vout * q

an since quaternions are normally unit length then q-1 = conjugate(q).

Note, this is similar to the matrix version because if you have

vout = M * vin then vin = M-1 * vout. In the quaternion version, you have

vout = q1 * vin * q1-1

and

vin = q1-1 * vout * q1

then writing q2 = q1-1 we have

vin = q2 * vout * q2-1

which is the same equation as for vout but with q1 replaced with q1-1

EDIT2: The algebra works the same way as for matrices, you have to be careful whether you are multiplying on the right or the left hand side of each equation though, since matrices and quaternions aren't commutative (a*b != b*a in general) but are associative (i.e. you don't need brackets for multiplication since a*(b*c) = (a*b)*c, so you can write a*b*c with no ambiguity). A matrix and its inverse always commutes though, as does a quaternion and its inverse.

##### Share on other sites
ryt    294

Well I must say thank you guys once again.

It feels so clear now and easy. It was some time since I learned that in college. I got pretty good grades too, probably I knew that stuff but when I had to implement it I stuck.

Quaternion math is pretty similar to matrix, although I did not understand all that you have written by looking at it. I did calculate some inverse quaternion and I got the same result.