Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a rotation matrix based on two vectors

I am looking to create a rotation matrix (M) when I pass in a vector (V) where M * [0, 0, 1] (forward) = V.

I am doing this because I then want to use this matrix to multiply other vectors to put them in local space (not sure if that is the correct term but hopefully you understand).

V = [0, 1, 0]
M = ?
Result = M * V = [0, 0, -1]

So if [0,0,1] has pitched 90 deg upwards to become [0,1,0] after being multiplied by M... what is M?

like image 946
Kevin Orriss Avatar asked Mar 26 '13 22:03

Kevin Orriss


1 Answers

Three-dimensional rotations are tricky to think about and even trickier to explain in text. However, it's possible to form your left hand into something approximating a set of three-dimensional axes. Look at the following Wikipedia page that describes Fleming's left-hand rule; in particular, look at the second diagram, which has the fingers labeled as I, B, and F: http://en.wikipedia.org/wiki/Fleming's_left-hand_rule_for_motors

Make the same formation with your own hand, and instead of labeling the fingers I, B, and F, let's call them x, y, and z. Further, we'll say that the point where these three fingers meet at the palm of your hand is the origin, point (0 0 0), and moving from the origin towards the tip of one of these fingers/thumb is moving in a positive direction.

The vector v = (0 1 0) is a point along your index finger (which we've called y). We want to rotate this point to form the point (0 0 -1). This point sits on the z axis (the thumb), but it is negative, so it sits one unit "below" the origin, in the direction from the tip of the thumb towards the origin.

So, to rotate the point (0 1 0) to be (0 0 -1), we need to rotate it around the x axis (your middle finger). Imagine putting a compact disc on your middle finger, pushing it so that it sits on the plane defined by your index finger and thumb — the (x, y) plane — and putting a mark on the disc one unit from its center. Now imagine aligning that mark with your index finger, so that mark sits at the point (0 1 0). You can rotate the disc around your middle finger so that the mark sits at the point (0 0 -1). So, the required rotation is a rotation around the x axis.

The following Wikipedia page gives you the equations for rotations in three-dimensional space around the x, y, and z axes. The matrix for rotation around the x axis is:

/1     0        0   \
|0   cos θ    -sin θ|
\0   sin θ     cos θ/

If you were to use your right hand to rotate the disc, the matrix is defined so that a negative value for θ corresponds to a clockwise motion of your right hand (and vice versa for a positive value). The angle we need to rotate by is a negative quarter turn and the required matrix is therefore:

/1    0     0\
|0    0     1|
\0   -1     0/

Remember that angles can be expressed in degrees or radians, so if you implement more general rotations in code, you'll need to check what your math library expects.

like image 99
Chris Avatar answered Sep 28 '22 06:09

Chris