# arbitrary rotation

This topic is 5021 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello, i have 2 polygons and i want the second one to have the same orientation as the first one. so i thought, i could calculate the angle between its normals and then apply a matrixtransformation to the second polygon. right? so, how can i get the corresponding rotation matrix out of the normals? i know i can get the angle using: but from that i dont know the axis? thanks!

##### Share on other sites
ok, that's not too difficult.

so you want to rotate from one direction to another. YOu can use the atan, acos, asin function to work out the angle between the two vectors, and then use that angle to rotate the object / vector so the orietnations match. It's a rather useful function, so you should keep it somewhere so you can reuse it for other purposes.

float CalculateAngle(Vector A, Vector B){    float dot   = A.x * B.x + A.y * B.y;    float cross = A.x * B.y - A.y * B.x;    float angle = atan2(cross, dot);    return angle;}

then you can rotate vertices using the angle.

Vector RotateVector(Vector A, Vector Centre, float angle){    float co = cos(angle);    float si = sin(angle);    Vector D = A - Centre;    Vector T;        T.x = D.x * co + D.y * si;    T.y = D.x *-si + D.y * co;    T += Centre;    return T;}

##### Share on other sites
BTW, the rotation matrix would be

[ co  si ][-si  co ]

from the second routine.

##### Share on other sites
thank you!

so far i always used the dot product to calculate the angle,
but now with the cross product giving me the axis along which to rotate, it works!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 10
• 9
• 35
• 16
• ### Forum Statistics

• Total Topics
634125
• Total Posts
3015678
×