2D Rotation

Started by
22 comments, last by Blake 24 years, 7 months ago
The formula to rotate a point n radians is:

new_x = x * cos(n) - y * sin(n);
new_y = y * cos(n) + x * sin(n);

Taken from Andre Lamothe's "Tricks of the Game Programming Gurus", so don't bitch at me if you think it's wrong

Advertisement
Then what the heck was I talking about? Mine seems to work when I use it...

------------------

Lack

Lack
Christianity, Creation, metric, Dvorak, and BeOS for all!
True rotation has it uses but alot of time you can get away with only 40 angles or less. In which case i think it saves alot of speed. Plus you can have more detail because i havnt seen a rotator yet that you dont loss detail or get jagged edges. Besides with pre rotated in 2d you can make it look alot more 3d but i guess it depends on your game view style. But starcraft,warcraft,and alot of your commercial games do not there has to be some reason?
You seem to forget that stacrcraft is no 2d shooter. in starcraft it's enough to have (let's say) 16 directions but in a 2d shooter you need to have a lot more angles for targeting and exact movement.
of course you could calculate the dirs more exact then you draw them but wouldn't it look stupid if you shoot in another direction then your gun points to?

and BTW: lets say one sprite is 40*40 * 40directions * 16 frames * 2bytes =2.048 mb!?

I personally have 8 directions pre-rendered but allow units to shoot in any angle. Since I use isometric view, rotation is not really an option.
But you're right it would look better if the units pointed in the exact direction of the shot, but thats a trade-off I can live with.

(Check it out at http://www.image.dk/~noshit/scrshots.html if you like)

/NJ

<b>/NJ</b>
I think it really comes down to the taste of the designer. It is impossible to do rotation with anti-aliasing, so your rotation is going to get the "jaggies" as it turns about. Pre-rendered images DO take a lot more ram, but they have the added benefit of speed and looks - you can use a 3D program to render the object in 36 frames as it spins once around the axis, and if you setup the light right, you get a good feeling of depth as the object moves - shadows are cast across the object's surface realisticly, etc. The other nice thing is that if you combine prerendered images with colour keying you can use the same bitmaps to draw multiple vessles with maybe, the windows or ensignias on the wings changing colour for each player. I REALLY hate the jaggies (one of the reasons I avoid 3D games), so I would opt for pre-rendered.
Niels: Your game looks really cool!!
Perhaps im a bit old fashion,but i myself say 8 angles is enough to do anything. Only a few types of games need more in my opnion.

I mean i am working on a isometric/action type game and you well be having 8-way movement.

As far as not being able to shoot any direction just move the ship ect over a pixel and your on a new angle for your shots.

Has anyone out there played HellFighter? That game is really cool and it uses 2D textured polygons. Not only that, but I have been in situations where there were like 20-30 ships on the same screen with no compromise to speed. And the textures look just fine at any angle.

Now, I've put together some of the things that you have all posted and now I have a simple polygon rotation demo. What nobody mentioned was that you have to draw your rectangle's vertices in each of the 4 quadrants:

     |   II  |  I     | -----------     |   III |  IV      |

Also, the formula(s) posted expect RADIANS instead of DEGREES. Other than that, my question has been pretty much answered. If you want to see the little demo I put together, download it here. Hopefully this weekend I will have some code to render a texture to the polygon.

Thanks to all that have posted.

Blake

[This message has been edited by Blake (edited September 24, 1999).]

I now have a much much better example of rotating a 2D polygon - Download it here!.

It now includes direct surface manipulations for pixel and line drawing on DirectX surfaces (16bit surfaces). Even if you aren't interested in rotating polygons, you may want to look at the code just to see how direct surface manipulations are done. There is a good pixel plotting function and an interpretation of Bres's Line Drawing function.

Pasting a texture to the polygon at this point should be a walk in the park. I'll repost an example as soon as I sit down and work it out.

Blake

This topic is closed to new replies.

Advertisement