Solved

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

Posted on 2006-06-23
9
1,377 Views
Last Modified: 2008-01-16
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 :).
0
Comment
Question by:imnotapro
  • 3
  • 3
9 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16971014
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|) )
0
 

Author Comment

by:imnotapro
ID: 16975294
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!
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 16975303
I'll be happy to provide an example; but, what do you mean by "rotation vector" ?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:imnotapro
ID: 16975943
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.
0
 
LVL 25

Accepted Solution

by:
InteractiveMind earned 250 total points
ID: 16976074
Oh, ok.

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! :)
0
 

Author Comment

by:imnotapro
ID: 16979158
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?
0
 
LVL 1

Assisted Solution

by:lawyerboy780
lawyerboy780 earned 250 total points
ID: 17113123
I'm confused.  Can anything be absolutely located if all or any  attempts at imposing order result in a systemic increase in chaos?  You know the entropy is inversely related to enthalpy thing?
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error in calculation 3 51
My 2003 Land Rover discovery 2 key for code for car alarm 7 122
Revenue table 8 99
Calculating T-SCORE inside Excel. 3 135
A Guide to the PMT, FV, IPMT and PPMT Functions In MS Excel we have the PMT, FV, IPMT and PPMT functions, which do a fantastic job for interest rate calculations.  But what if you don't have Excel ? This article is for programmers looking to re…
Lithium-ion batteries area cornerstone of today's portable electronic devices, and even though they are relied upon heavily, their chemistry and origin are not of common knowledge. This article is about a device on which every smartphone, laptop, an…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now