Reputation: 1186
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:
Thank you all in advance for the help.
Upvotes: 2
Views: 1512
Reputation: 188
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.
Upvotes: 2
Reputation: 1819
You can do this more easily by calculating the angle between to forward vectors first:
theta = arccos(dot(A, B)/(length(A)*length(B)))
This gives you the angle by which you want to rotate your triangle. Then you can put this angle in the 2D rotation matrix and use it to calculate each vertex's new position:
vector2 newPos = R*oldPos, Where R is the rotation matrix
Upvotes: 0