Original Post
I'm trying to align two points in space, while also rotating them so their local coordinates system match. This image illustrates what I'm trying to do:
So, given two meshes (m1 and m2), two reference points (a1 and a2), and two local coordinate systems ([x1 y1 z1] and [x2 y2 z2]) I need to construct a matrix M that, when applied to m2, will move and rotate it so that a2 coincides with a1, and m2 is rotated opposite m1. This seems simple enough. What I have so far is (where T is a translation transformation, and R a rotation transformation). However, my problem is that this transformation only gives the desired result if z1 is aligned with the global Z axis. So, for example, given m1 and m2 centered on (0 0 0): which is the expected result, but swap a1, x1, y1, z1 for a2, x2, y2, z2 gives which is not what I want. It seems to be the last rotation that is wrong. In the first example m2 (correctly) get the opposite rotation of m1, while in the second example m2 (incorrectly) get the same rotation as m1. So can anyone help me out here? Are my assumptions wrong? Am I missing a step? Am I barking up the completly wrong tree here?
So, given two meshes (m1 and m2), two reference points (a1 and a2), and two local coordinate systems ([x1 y1 z1] and [x2 y2 z2]) I need to construct a matrix M that, when applied to m2, will move and rotate it so that a2 coincides with a1, and m2 is rotated opposite m1. This seems simple enough. What I have so far is
M = T(-a2) * R(x2, y2, z2) * R(-x1, y1, -z1) * T(a1)
a1 = (0 50 -50)
x1 = (-1 0 0)
y1 = (0 1 0)
z1 = (0 0 -1)
a2 = (50 50 0)
x2 = (0 0 -1)
y2 = (0 1 0)
z2 = (1 0 0)
M = ( 0 0 1 0
0 1 0 0
-1 0 0 0
0 0 -100 0 )
M = ( 0 0 1 0
0 1 0 0
-1 0 0 0
0 0 0 0 )