#### Archived

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

# Simple 2d Rotation

This topic is 6796 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey.. I''m trying to make a small 2-player tank game in C++ (Visual C++ 6.0). This game has a 360-degree engine and I already have a lookup table with the sines and cosines for each degree for x and y.. unfortunately, although I understand the table I made (I can make things move in just about any degree) I haven''t really learned anything about trigonometry yet, and I really don''t get matrix math (working on it), and I need to do is rotate the images for the tanks (based on the center point) to the proper rotation.. Thanks in advance, lopar

##### Share on other sites
If your using DirectX and you are bliting the bitmap you can use this method:
---
DDBLTFX ddbfx;DDRAW_INIT_STRUCT(ddbfx);ddbfx.dwRotationAnge = angel;if(FAILED(lpddsback->Blt(&dest_rect,lpdds_image,&source_rect,(DDBLT_WAIT/DDBLT_KEYSRC/DDBLT_ROTATIONANGLE),&ddbfx)))return(0);

David "Dak Lozar" Loeser

##### Share on other sites
Unless hardare supports it, rotating a sprite can be bad news. For maximum accuracy, you must do a sin and cos lookup for each pixel. A 32x32 image has 1024 pixels. That is a lot of memory lookup. If I were you I''d do what all teh books do and create several sprites of your player pre-rotated and limit the number of possible angles. However, you can use some ingenious techniques to implement software rotation, but at tsake of accuracy loss.

##### Share on other sites
Actually this is a rather quick project so I''m using GDI.. I''m trying to do this pixel for pixel with the tables because I''m trying to learn the principles rather than just use a rotation function.

Thanks,
lopar

##### Share on other sites
And here is some code... Imagine that Pixels is a 2D array which represents the x and y coords...

{

return *3.14159/180

}

void RotateImage(CImage OrigImg, CImage* NewImg, int Theta)
{

int CenterX, CenterY;

double CosX, SinY;

int x, y, i, j, nx, ny;

NewImg->Width = OrigImg.Width;

NewImg->Height = OrigImg.Height;

CenterX = OrigImg.Width >> 1;

CenterY = OrigImg.Height >> 1;

for (int i = 0; i < OrigImg.Height; i++)

{

for (int j = 0; j < OrigImg.Width; j++)

{

x = j - CenterX;

y = i - CenterY;

nx = CenterX + (int) (x*CosX - y*SinY);

ny = CenterY + (int) (x*SinY + Y*CosX);

NewImg->Pixels[j,i] = OrigImg.Pixels[nx, ny];

}

}

}

##### Share on other sites
oppps... the following...

{
return *3.14159/180
}

{
return deg*3.14159/180;
}

and remember, it''s not optimized at all.. it''s just to show you how to implement it...

..-=ViKtOr=-..

##### Share on other sites
just a note, DDBLTFX barely works on any video cards... even newer ones like Voodoo3 don''t support it. What I do is use D3D and paste the sprite image on a polygon, then rotate the polygon... it''s fast, easy and as pretty as can be (bi-linear filtering baby.)

##### Share on other sites
No, no!
Don''t do a sine/cosine lookup for each pixel!
Hehe..

Represent the image to be rotated as four points, arranged in a square (or rectangle or whatever).. you rotate the points with your sine/cosine table.. and you texture map the tank image onto the rotated rectangle..

I believe it would be much more efficient than a sine/cosine lookup per pixel.. and look much better too.

##### Share on other sites
Thanks again.. I think I''ll try some of these suggestions and see which one works best...
now.. to find a tutorial on here on tex. mapping.. hmm...

- lopar

##### Share on other sites
Yes, when it comes to games/demos effects, you have to use every optimization you can think of possible (as long as it runs fast enough as it is)... soooo... i just gave the basic algorithm which works by using the cos/sin on pixel bases... adammil gave a very good suggestion on making the funcion work faster. You can do the same thing as texture mapping a polygon.. just rotate the vertices of your image (those being the 4 corner points).. let us know i you get stuck on the code... You can still keep the cos/sin lookup table sinec hat would make things even faster when you rotate the vertices of the image, and i''m sure you will use it for something else.. so leave that.. just implement the rotating algorithm.. first get it to work, then optimize.

..-=ViKtOr=-..

1. 1
Rutin
42
2. 2
3. 3
4. 4
5. 5

• 9
• 27
• 20
• 9
• 15
• ### Forum Statistics

• Total Topics
633392
• Total Posts
3011639
• ### Who's Online (See full list)

There are no registered users currently online

×