Postby **jlv** » Mon Jan 28, 2019 4:06 am

A rotation of 0 has to be the identity matrix. It doesn't move the vertices so the matrix can't either.

A matrix is basically just a coordinate system. The first column is the X axis, the second column is the Y axis and the third is the Z axis. If you want to transform a vector from that coordinate system, you scale each axis vector by its corresponding vector element and add them together like this:

V' = X * v0 + Y * v1 + Z * v2

That's the same as a matrix * vector multplication:

V = [ v0, v1, v2]

X = [ m00, m10, m20]

Y = [ m01, m11, m21]

Z = [ m02, m12, m22]

v0' = m00 * v0 + m01 * v1 + m02 * v2

v1' = m10 * v0 + m11 * v1 + m12 * v2

v2' = m20 * v0 + m21 * v1 + m22 * v2

Now say you want to aim the Z axis at the vector P.

First set Z. Since you want it to point at P you set it to P, but since you don't want it to scale, divide by the length of P to get a unit length vector.

Z = P / |P|

Now for Y. Say you want it to generally point up, so we temporarily set it to the global Y. This is going to make it a shear matrix which we don't want, but we'll fix that later.

Y = [ 0, 1, 0 ]

For X you just want it to be perpendicular to Y and Z. So you set it to the cross product:

X = Z x Y

Now fix Y so it's perpendicular to X and Z:

Y = X x Z

Normalize X and Y and you're done.

X = X / |X|

Y = Y / |Y|

Now you should have a square and unit length rotation matrix where Z points at P. I didn't give any thought to the cross product order, so you'll probably have to flip either X or Y to get the right sign.

Since OpenGL stores matrices in column major order, your matrix should look like this:

m = [

x0, x1, x2,

y0, y1, y2,

z0, z1, z2

];

(If you remember the Patriot missile bug, this is related. They continuously concatenated rotation matrices without squaring them up and normalizing them. If the guidance system ran long enough the matrix would turn into a scaling and shearing matrix as the numeric errors accumulated.)

Josh Vanderhoof

Sole Proprietor

jlv@mxsimulator.comIf you email, put "MX Simulator" in the subject to make sure it gets through my spam filter.