Some rotation problems involve knowing the rotation angle to begin with - for instance, the user may input the angle and the power of the gun on a tank before firing. However, most rotation problems don't initially involve angles.
The most common case is when you want to align two vectors on an object. For instance, you want to point a cannon at a duck. There is a vector that points from the center of the cannon down the muzzle of the cannon, and there is a vector that points from the center of the cannon to the duck. You want to rotate the cannon so that these vectors are aligned.
Here is a function that finds the value of the sin and cosine without all of the messy trig and inverse trig functions, using as input the initial vector
i (the cannon muzzle), and the final vector
f (the duck).
void findSinCos(const vec2& i, const vec2& f, float& sin, float& cos){ float xx = i.x*f.x; float xy = i.x*f.y; float yx = i.y*f.x; float yy = i.y*f.y; cos = xx + yy; //vector dot product sin = xy - yx; //vector cross product in 2D //find normalization constant float norm = xx*xx + xy*xy + yx*yx + yy*yy; norm = 1.0f / sqrt(norm); //apply normalization constant cos *= norm; sin *= norm;}
More than half of the cost of this funciton is performing normalization, but it is cheaper to do it here rather than normalizing the vectors externally and passing them in. If you have the normalized vectors sitting around for other purposes anyway, just make a similar function that leaves out the normalization step.
Now make a function to construct the matrix
void findRotMatrix(const vec2& i, const vec2& f, mat22& mat){ float s, c; findSinCos(i, f, s, c); mat.m00 = c; mat.m11 = c; mat.m01 = -s; mat.m10 = s;}