Link to home
Start Free TrialLog in
Avatar of imnotapro
imnotapro

asked on

How to calculate an absolute position/rotation given a relative position/rotation and a parent position/rotation vector and vice versa?

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 :).
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

Absolute parent position: vector P.


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|) )
Avatar of imnotapro
imnotapro

ASKER

I get the logic in what you're saying, but I think I need an example. Say the parent position vector is called p (p.x p.y and p.z). The parent rotation vector is called pr (pr.x pr.y and pr.z). Then we have a child position vector that is relative to the parent position and rotation (c.x c.y and c.z) and a child rotation vector cr (cr.x cr.y and cr.z). I know nothing about matrices. How would you step for step go to find the child absolute position and rotation (a.x a.y a.z and ar.x ar.y ar.z)?

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!
I'll be happy to provide an example; but, what do you mean by "rotation vector" ?
A vector with 3 floats defining the rotation of the mesh in radians. One for X axis, one for Y and one for Z. Ie  pi/2,0,0 is 90 degrees along the X axis.
ASKER CERTIFIED SOLUTION
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay, I've written up some code using the example you provided and I'm about to test it once I have a way of doing this the other way around. To get the relative position using parent position, parent rotation and child absolute position/rotation I guess I have to unrotate the child first using the absolute rotation of the child and the absolute rotation of the parent? Would it work taking the difference between those rotations (child absolute rotation minus parent absolute rotation), make them negative and then rotate them using what you said above?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial