Search code examples
mathgraphicsgeometryrotational-matrices

Find the New Position of Vertices Given a Rotation Matrix


I have two triangles facing in arbitrary directions. I have the forward vector for both triangles, and I want to align each of the forward vectors to face the same direction. I only have the ability to do rotations about the world x, y and z axis (The software API I am using is very limited).

So, let A = forward vector of the first triangle, and B = forward vector of the second triangle. I am able to find the Rotation Matrix using this equation:

v = B X A
s = ||v||
c = A dot B

vx = skew-symmetric cross-product matrix of v

R = I + [vx] + [vx]^2 * (1-c)/s^2

I am able to find R.

I am not sure how to use R so that I can move the vertices of triangle B such that the triangle B and triangle A are facing in the same direction.

Picture for Reference:

enter image description here

Thank you all in advance for the help.


Solution

  • You can use normalized vector v as an axis and the angle T between A and B to compute a rotation matrix (right-hand rule) from an axis-angle in the following way:

    | cosT + x*x*(1 - cosT)      y*x*(1 - cosT) + z*sinT    z*x*(1 - cosT) - y*sinT |
    | x*y*(1 - cosT) - z*sinT    cosT + y*y*(1 - cosT)      z*y*(1 - cosT) + x*sinT |
    | x*z*(1 - cosT) + y*sinT    y*z*(1 - cosT) - x*sinT    cosT + z*z*(1 - cosT)   |
    

    x, y, z values refers to normalized v coordinates.

    Now, you apply this matrix to each vertex in B.

    PS: This matrix is in column-major order, you might want to transpose it.