Reputation: 23
For a project I'm working on, I need to write a function that takes as input two points in 3D space that form a line, and gives as its output a rotation matrix that will make the line parallel to the Z axis.
I've learned enough about rotation matrices to know how to compose them from Euler angles. But I'm flummoxed on how to figure out the correct angles of the input line.
Could anyone please offer some advice? I'm writing this in Matlab but a theoretical treatment would be more valuable, I think.
Upvotes: 2
Views: 282
Reputation: 1032
Edit: Rewrote the answer since it wasn't complete enough. And instead of using the euler angles, you can create the matrix this way.
You get the Z vector and use cross product to get the other vectors to compose the matrix.
Vec3 start; // start of the line
Vec3 end; // end of the line
Vec3 Z = end - start;
Z.normalize();
Vec3 X = Vec3(0,1,0).cross(Z);
X.normalize();
Vec3 Y = Z.cross(X);
Y.normalize();
// here's the 3X3 rotation matrix
_m11 = X.x; _m12 = Y.x; _m13 = Z.x;
_m21 = X.y; _m22 = Y.y; _m23 = Z.y;
_m31 = X.z; _m32 = Y.z; _m33 = Z.z;
Link that explains the process of composing a rotation matrix: http://nghiaho.com/?page_id=846.
Upvotes: 3