Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

I'm making an application where I need to be able to calculate an absolute position/rotation given a relative position/rotation and a parent position/rotation it is relative to. The order of rotation will be X, Y then Z. I also need to be able to go the opposite way. That is finding a relative position/rotation given an absolute position/rotation and a parent position/rotation. What is the most efficient and easiest way to integrate this in an application that uses position and rotation vectors (radians) to store object placements?

If I'm not too wrong I can rotate it by X by multiplying the relative position X with cos(rotation x) and relative position Y with cos(rotation x). Not sure about Y and Z rotation though. I'm thankful for any solution :).

If I'm not too wrong I can rotate it by X by multiplying the relative position X with cos(rotation x) and relative position Y with cos(rotation x). Not sure about Y and Z rotation though. I'm thankful for any solution :).

To rotate a vector around an axis by an angle 'a', you multiply the vector with the appropriate matrix:

Rotate around X axis:

[ 1 0 0 0 ]

[ 0 cos a sin a 0 ]

[ 0 -sin a cos a 0 ]

[ 0 0 0 1 ]

Rotate around Y axis:

[ cos a 0 -sin a 0 ]

[ 0 1 0 0 ]

[ sin a 0 cos a 0 ]

[ 0 0 0 1 ]

Rotate around Z axis:

[ cos a sin a 0 0 ]

[-sin a cos a 0 0 ]

[ 0 0 1 0 ]

[ 0 0 0 1 ]

You need to watch out for what is known as "Gimbal lock", where one or more of the rotations have no effect due to the order in which you perform the rotation.

To find the angle between two vectors around each axis, treat each vector as being 3 different 2D vectors, and use the dot product:

For vectors u and v:

u dot v = u.x * v.x + u.y*v.y + .. = |u| * |v| * Cos a

Therefore:

a = arccos( u dot v / (|u| * |v|) )

Also say we have the child absolute position/rotation (a and ar) and the parent absolute position/rotation (p and pr). Any example on how to find the child relative position/rotation (c and cr)?

I'm a programmer and I'm not very experienced on vector math and matrix math so I need it step for step :). Thanks!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

Well, to multiply a vector with a matrix, you must first extend the vector to a 4x1 matrix, giving the 4th element (w) the value of 1.

So the vector V, becomes:

[ V.x ]

[ V.y ]

[ V.z ]

[ 1 ]

(This is just another notation for what you've used above: (V.x, V.y, V.z, 1)).

To then multiply this with a 4x4 matrix (which all of the transformation matrices are), let's take the following matrix as an example:

[ a b c d ]

[ e f g h ]

[ i j k l ]

[ m n o p ]

You multiply each row with the vectors column, and sum those components. So you get the following as the result:

[ a*V.x + b*V.y + c*V.z + d ]

[ e*V.x + f*V.y + g*V.z + h ]

[ i*V.x + j*V.y + k*V.z + l ]

[ m*V.x + n*V.y + o*V.z + p ]

And with the transformation matrices, the last row in that result can be neglected. So your new, transformed vector, becomes just:

[ a*V.x + b*V.y + c*V.z + d ] // x component

[ e*V.x + f*V.y + g*V.z + h ] // y component

[ i*V.x + j*V.y + k*V.z + l ] // z component

And that's your transformed vector! :)