I have an initial and terminal point. I have a unit cube located at the origin. I want to construct a projection matrix to move the cube to the origin, rotate, and scale it so that it becomes a rectangular prism that touches the initial and terminal point. I can use the following functions to manipulate a matrix as well as anything else that could be useful:
- translate(
mat: matrix,vec: vector) =>matrix - rotate(
mat: matrix,angle: radians,axis: vector) =>matrix - scale(
mat: mat,ratio: vector) =>matrix - radians(
degrees: real) =>real - degrees(
radians: real) =>real
I can translate the cube to the midpoint(I, T), and scale it to the proper length easily with the help of distance(I, T). However I am struggling to figure out how to do the rotation. In my picture I drew yellow dots that are projected from the cube at the origin to the prism between the point I and T.
You could imagine a terminal point coming out of the paper. How would you determine the axis of rotation? How would you determine how many radians to rotate around the axis of rotation?
These posts seem to be the most relevant that I have found so far:
- Find rotation matrix to match points in parallel projection
- Finding an appropriate axis of rotation for two points such that they can be rotated and translated to overlay a given line
Source
Spektre has answered this question in full detail here.
Cube to be Transformed
Summary of Solution
We want to compute transform matrix that will convert
A,BintoC,D. While were at it we can also scale down the axes that are perpendicular to the vectorABto get a thickness. We assumeABis on thex-axis. They-axisis vertical, andz-axiscomes out of the screen.We can then perform the transformation with the following sequence of functions. The following functions are based on OpenGL Mathematics notation.
Define the points
Set
Mequal to the identity matrixTranslate the unit cube to the midpoint of the vector
CD.Cross the rejections of
ABandCDto get a normal.We only rotate around the normal if it is not zero.
Finally, we scale the cube to be a rectangular prism that touchs the two points.
Apply the Matrix
MNow
M * A = C. This means we can multiple all the vertices on the cube byMto get the transformed cube.End Result