Reputation: 81
I have a reference model, (for example a triangle), with its 'up' vector definied as <0 1 0> and its 'over' vector defined as <1 0 0>.
Now I have another triangle of the same size rotated and positioned arbitrarily in a 3d space. What I want to do is to find out this new triangle's 'up' and 'over' vector. I.e. if the triangle is rotated 180 degrees around the X-axis, it's 'up' vector should be <0 -1 0> and its 'over' vector should be the same.
How do I find the rotation transformation? If I have this, I can just 'rotate' the 'up' vector by the inverse to get the new 'up' vector and then I'm done.
Any ideas?
Upvotes: 2
Views: 1098
Reputation: 81
Ok everyone. Thanks for the help, but I managed to figure it out.
Here are the steps.
Center both objects at the origin
Rotate one vector onto another, for examble (a0) -> (b0). Now you have to vectors overlapping one another. a0/b0, although points, form vectors from the origin
Find the normals of each of these vectors, this can be done by using an additional point in each coordinate space and finding the cross products. For example:
an = a0 x a1 bn = b0 x b1
Now just rotate an onto bn and you are finished.
The easiest way to do the rotations is to add a 'rotateTo' member function that returns a quaternion (Avoids gimbal lock). Then you can multiply the two quaternions together to get the resulting rotation transformation.
Upvotes: 0
Reputation: 23550
Have to answer your comment in another answer because i want to add graphics.
If your model has more than 4 points but is a rigid body then it is sufficient to take any 3 non-identical points to get the perpendicular vector (and then take the 3 corresponding points on the object you want to rotate it into). You rotate the one perpendicular vector into the other by calculating the angle between them, this has to be done separately for the 3 axis'. I will demonstrate it for how you do it to calculate the angle by which you rotate around the z-axis. So in the following graphic the bold red and blue arrows are the original 3-dimensional vectors.
You project them on the x-y-plane by just setting their z-value to 0. You calculate the angle between them by using their dot-product, norm and arcus cosine as in the following formula from http://en.wikipedia.org/wiki/Dot_product. That theta is now the angle by which you have to rotate around the z-axis. Do the same thing for the x- and y-axis and you have your rotation-angles.
Upvotes: 1
Reputation: 23550
To do this you need the vector that is perpendicular to the vectors that make up the triangle. You can compute it using the cross product:
So given a triangle composed of the points A,B,C you calculate the cross-product of the 2 vectors B-A and C-A. You can then rotate the triangle to fit your reference-triangle by rotating the perpendicular vector.
Is this what you searched for? If not please comment and i will think some more.
Upvotes: 1