PDA

View Full Version : Rotating a Bitmap (C++ question)



Moon Rabbits
03-28-2007, 03:36 AM
I'm making a Missile Defense type clone in C++ with the Allegro library.

A tank is drawn at the bottom of the screen, and right now I want to make a cannon on the tank rotate so that the nozzle faces the current coordinates of the mouse (mouse_x, mouse_y).

A function in Allegro called pivot_sprite() is what I want to use:


void pivot_sprite(BITMAP *bmp, BITMAP *sprite, int x, int y, int cx, int cy, fixed angle);

Where *bmp is the bitmap being drawn to, *sprite is the sprite being drawn to *bmp, (x, y) is the location to draw *sprite at on *bmp, (cx, cy) is the point that the bitmap will pivot around, and angle is the angle of rotation.

Now I, lacking math skills, can't figure out how I would go about rotating a BITMAP (*cannon) to face the mouse. The pivot point (cx, cy) would be the center of the tank.

Help?~

Endless
03-28-2007, 06:47 PM
If your sprite is of length (x axis) n and height (y axis) m, then cx = n/2, cy = m/2, x = x_tank and y=y_tank are where you want the center of the tank to be on the big bitmap, and angle should be atan((x_tank-x_mouse)/(y_tank-y_mouse)). Don't forget to boundary check since you may divide by 0, which gives an infinite which actually makes sense (atan(infinity) = pi/2 radians).