(Login Mikrondel) Moderator Posted Sep 5, 2009 11:00 PM
Rotating a 2D sprite is an interesting business, with speed vs quality trade-offs to be made.
Then there's rotating your viewpoint in a 3D world. Or rotating a single object in a 3D world. This is a bit difficult and best done using matrices.
But, I'll presume you just want to rotate some sort of line drawing on the screen. To do this, you simply need to rotate each of the endpoints of the lines (and then draw the lines).
Now, "x,y" co-ordinates are called *rectangular co-ordinates*. There is another sort, called *polar co-ordinates*, traditionally using the names r and θ (theta). r is the distance from the origin, and θ is the angle measured from the x axis.
Now, if you have all your points in polar form, it's easy to rotate them. Just add the same thing to each of their θ values.
Converting from polar to rectangular is easy:
x = r * cos(θ)
y = r * sin(θ)
Converting from rectangular to polar is a little harder. Depending on how your program works, you may be able to avoid doing this, but if you must:
r = sqrt(x*x + y*y) '(by Pythagoras' theorem)
θ = atan2(x, y)
where atan2 is something like this:
FUNCTION Atan2! (X AS SINGLE, Y AS SINGLE)
'Code borrowed from London
Atan2 = ATN(Y / X) - ATN(1) * 4 * (X < 0 - 2 * (X < 0 AND Y < 0))
If you want to rotate around some other point (i.e. not the origin), you need to figure out the polar co-ordinates around that point, and after rotating and returning to rectangular, shift everything back to normal.