Jump to content
  • Advertisement

Archived

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

lopar

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Thanks for the reply..
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 this post


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




double rad(double deg)
{

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;


CosX = cos(rad(Theta));


SinY = sin(rad(Theta));



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 this post


Link to post
Share on other sites
oppps... the following...

double rad(double deg)
{
return *3.14159/180
}

...should read

double rad(double deg)
{
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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=-..

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!