Results 1 to 2 of 2

Thread: Rotating a Bitmap (C++ question)

  1. #1
    it's not fun, don't do it Moon Rabbits's Avatar
    Join Date
    Mar 2005
    Posts
    5,582

    Default Rotating a Bitmap (C++ question)

    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:

    Code:
    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?~

  2. #2
    Prinny God Recognized Member Endless's Avatar
    Join Date
    Aug 2000
    Location
    Prinny Moon
    Posts
    2,641
    Contributions
    • Former Cid's Knight

    Default

    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).

    And then there is Death

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •