• Announcements

Archived

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

2D Rotation

Recommended Posts

Guest Anonymous Poster
Hi Blake !

Did you already check the resources of GameDev? Look at the section Programming-General/Graphics, where you will find an aricle titled "Texture Mapping". It explains the principle of linear texture mapping, wich is exactly what you need for 2d. http://www.gamedev.net/reference/programming/graphics/article366.asp

Share on other sites
Blake    122
Thanks for the reply, I wasn't quite sure where to begin looking. I'm working exclusively in 2D and that seemed to be under a 3D category. It is very helpful, however it assumes that I already know how to draw and rotate polygons. I only want to draw and rotate 2D rectangular or square polygon. Do you know of any documentation that explains this?

Blake

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

Share on other sites
Niels    122
There is a few books you need to get hold of:

Abrash: Black book of graphics programming (a must-have)

The bible: Computer graphics, principles and practice

And the less theory, more code series: Graphics Gems 1-(whatever)...

You will be amazed by the knowledge compiled into these books ..

/NJ

Share on other sites
Mike    141
Well, I can give you the source code to rotate a 2D image ( 8, 15/16, 24/32 bits), but I'm not to good at explaining things. If you want the source code (sparcly commented) let me know via E-mail.

Share on other sites
PyroFragger    122

I think it would be best to have pre-rotated bitmaps and set it up so that when theuser presses the left arrow key, it rotates it 15 degrees to the left (or some other increment you can count by)

Share on other sites
Mike    141
I dissagree with the idea of drawing a bunch of pictures at varous angles. If you have an object with five frame of animation, and you need to have each of those frames drawn for every 15 degrees, that a good deal of wasted space.

Share on other sites
Blake    122
No kidding! I have about 16 frames of animation and I am not going to draw 240 separate frames for 1 object! In theory this should be very simple:

1) Setup a rectangular polygon (4 vectors)
2) rotate vectors clockwise or counterclockwise
3) Blit pixels line by line onto polygon

Voila!

Simple simple simple but I don't know enough trigonometry to be able to rotate points. That's all I need to know how to do.

I went and bought a cheap trig book today and I can already tell the book explains exactly what it is I'm trying to do. I will post the fruits of my labor in the form of a tutorial when I am successful. However, I still welcome any examples. My e-mail is blake@cyberstation.net.

Thanks to all that replied!

Blake

Share on other sites
Zenroth    127
I say go with pre rotated bitmaps to. Simple reason speed speed speed. Sure you make huge memory footprint but you get speed. Why do you think all theese games like starcraft,warcraft ect use prerendered frames for speed. 1 rotating ship thing on a polygon in like 640x480 wont hurt that bad you get 100 on the screen i doubt you have any frame rate left. Only way to do rotation where it works well is use a direct3d or opengl map the texture onto a triangle stript and use 3d acceleration.

Share on other sites
LackOfKnack    122
I have to disagree. Does anyone have Grand Theft Auto? All those cars an people would take up 360x the memory they do, but they don't because they just rotate them. I think a tiny bit of slowdown is worth the memory... GTA runs pretty fast, and they have tons of rotated objects flying around.

The calculation I use for finding an angle is:

x = Sin((90 + ang) * pi / 180) * rad
y = Sin(ang * pi / 180) * rad

This may or may not be what you're looking for. You feed it pi, rad(the radius) and ang(the angle) and it gives you the x,y relative to 0,0.

Oh, and that was in VB. I don't know exactly what it would be in C, sorry. Should be a staright-forward conversion though.

Hope this helps.

BTW- does anyone know how to do the blitting on an angle, but in DirectX? Or do you just read each pixel and rotate it yourself?

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

Lack

Share on other sites
Guest Anonymous Poster
You can pre-calculate the rotation angles for your ship and put them into a lookup table. That will speed up your rotation at run time.

Share on other sites
Guest Anonymous Poster
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

Share on other sites
LackOfKnack    122
Then what the heck was I talking about? Mine seems to work when I use it...

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

Lack

Share on other sites
Zenroth    127
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?

Share on other sites
Guest Anonymous Poster
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!?

Share on other sites
Niels    122
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

Share on other sites
Sphet    631
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.

Share on other sites
Guest Anonymous Poster
Niels: Your game looks really cool!!

Share on other sites
Zenroth    127
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.

Share on other sites
Blake    122
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).]

Share on other sites
Blake    122
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

Share on other sites
Seyedof    123
hi,
It depends on which type of texture
mapping you wanna do, there are several
ways and optimizations. if you want to
texture map a quadrangle i offer you
(it's a side scrolling game), i have
got this source from x2ftp.oulu.fi
There's a nice texture mapper function
among too many other useful functions.

If you want just to rotate a texture
map, for example a tile based game in
which the tile mapped ground rotates
there are some optimizations to do,
Have you ever seen the Seek 'n Destroy
game from Safari? they have done such
thing, the rotate the tile mapped
ground, the method they used was to
divide a whole 360 degrees of rotation
into some steps, for example each
10 degrees, then they have precalced
and saved the result so there is no
texture calculations done in runtime,
it's all precalced

I'm a game developer, we have developed
a game like Seek 'n Destroy but with
too much better quality and features,
it supports not only rotations but scaling
as the chop moves up or down there
are some tricks to do in such engines.

Some ideas are :

First notice that if you want to
texture map a quadrangle, which is
symetric around it's center ,you can
only calculate the pixels for half
calced using the first half.

If you do just rotate a bitmap
(no scaling) the best way is to
precalculate the pixel offsets and
save them as your game data.

And a really big optimization is :
if you want to texture map several
quads with the same rotation angle and
the same size (like a tiled ground)
you can texture map only one tile in
a buffer and use the same pixel offsets

Our game uses the last method and
runs fast in 640x480x256 mode under
windows (with Direct X). if you want
some screen shots from the game please
email me.

Hope you are making a great game

Share on other sites
Cthulhu    122
You don't need to have 360 precalced pictures to have rotation in accuracy of 1 angles. You can have only 90 because 90 degree rotations are fast to do. And of course it would propably be enough to have accuracy of 5 angles to have smooth animation, that would be total of 18 precalced pictures.

------------------
"Tank, I need an exit. Fast!"

Share on other sites
khawk    2902

I'm gonna lock this thread now. It's straying off of its original topic.

Kevin