Archived

This topic is now archived and is closed to further replies.

PhilVaz

Rotating a Polygon formula

Recommended Posts

I wrote a short demo using Windows GDI graphics to move and rotate polygons (like the old 1980s asteroids game) with nine points. Keeping it simple. The asteroids should move and rotate slowly. Using GDI function "Polygon" to draw them. The moving is fine (except the points flip when they reach the other side of screen, have to fix that), but the rotation goes haywire (points get distorted?). I use a look-up table for the sin/cos value in radians (0 to 359 degrees). Just wondering if my basic rotation formula is correct, or what I need to change. I saw this in a few asteroids demos online, but not sure I understand the formula. There is a for loop for the 9 points, and to rotate (xnew and ynew are floats, x and y are integers, angle is an int from 0 to 359 for the sin/cos look up table) I use this -- // rotate x and y point using sin/cos formula xnew = (float)x * cos_table[angle] - (float)y * sin_table[angle]; ynew = (float)y * cos_table[angle] + (float)x * sin_table[angle]; // round up and save newly translated point x = (int)(floor(xnew + .5)); y = (int)(floor(ynew + .5)); Then I store the x, y point into an array, until all 9 are rotated. Something is wrong, since the rotation makes the polygon distort. You press CTRL to begin rotation, and SPACE to stop rotation. The full source is here AsteroidsFullScreen.cpp Written in plain C, should be easy to follow. Compiles fine with MSVC++ 6, but my rotation don't want to work. Phil Porvaznik [edited by - PhilVaz on May 1, 2003 2:59:14 AM]

Share this post


Link to post
Share on other sites
Some interesting reading:
Programming/Graphics/Bitmap Rotation (if you don't want to use simple polys)
Programming/Graphics/Entering The 3rd Dimension (discusses 2d rotation first)

EDIT: I would suggest using the standard library functions sin() and cos() rather than a lookup table. It is also better to use radians than degrees.




[edited by - CWizard on May 1, 2003 3:30:23 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
make sure you rotate the original point each time, and not the previously found one. otherwise you will accumulate error and the polys will distort.

Share this post


Link to post
Share on other sites
quote:
Original post by brassfish89
please explain.
It''s the "standard" way. All standard library trig function work with radians, so does any other library worth its salt. To internally have angles represented as radians is therefor more consistent.

If you want to be able to input or output/display angles in degrees, it is better to convert in the "outer layer". What I mean is that it is best to have radians in your internal structures, and when reading degrees to convert them to radians before placing them in the internal structures. When you want to display an anlgle, read the radian value and convert it to degrees before displaying it.

As a note, I don''t think I ever seen any (non-beginner) trig program that use degrees internally.


Share this post


Link to post
Share on other sites
quote:
Original post by brassfish89
It is also better to use radians than degrees.
please explain.


That math involved in approximating trigonometric functions result in radian approximations. Its why all sin/cos/etc functions deal with radians. If they dealt with degree there would be another layer of approximation as they converted the radian to degrees..

Share this post


Link to post
Share on other sites
yes indeed philvaz, the first problem that springs to mind in your code is the extreme rounding of the numbers, by converting them to ints, and using these rounded values your next game loop.
the best way to do this is to let each asteroid type have a member int/float rotation, float if you would be using radians, witch i also promote, and DO NOT change the initial positions of the vertices of the polygon. just have two polygons for each asteroid, one with the original vertices, and one you set anew each frame, to the original rotated by .rotation. just draw the last one.

Share this post


Link to post
Share on other sites