Search code examples
c++rotationallegro

C++ Allegro First Display Program - Translate Rotate Scale


I'm using C++, Allegro5, and codeblocks. I'm trying to write a test program before I write a very basic Asteroids knock-off. The only rules are use C++, Allegro, and no sprites or bitmaps (this specifically has to use primitives like lines and shapes).

I'm drawing the shape in quadrant1, redrawing it in quadrant2 translated from a relative origin, redrawing it in three rotated by certain number of degrees, and finally drawing it rescaled in quadrant4.

Parts 1 2 and 4 work fine. My problem is the rotate function. It is not rotating the correct amount, or not rotating at all. At less than 90 it rotates half of the entered degrees. After that it doesn't rotate or doesnt draw at all.

I'll post rotation method code below. I know it's bloated and needs to be cleaned up (I just overhauled some of the logic to fix 1, 2, and 4), but I really need help with rotation. I think I'm using atan2 correctly and I've turned

void rotation (float degrees)
{
   ALLEGRO_COLOR color_black = al_map_rgb(0,0,0);
   ALLEGRO_COLOR color_blue = al_map_rgb(150,150,150);
   ALLEGRO_COLOR color_orange = al_map_rgb(255,135,135);
   ALLEGRO_COLOR color_red = al_map_rgb(255,0,0);
   al_clear_to_color(al_map_rgb(255,255,255));  //clear screen to white
//draw black grid
   al_draw_line(400,0, 400,600, color_black, 4.0);
   al_draw_line(0,300, 800,300, color_black, 4.0);
   al_draw_line(200,0, 200,600, color_black, 1.0);
   al_draw_line(600,0, 600,600, color_black, 1.0);
   al_draw_line(0,150, 800,150, color_black, 1.0);
   al_draw_line(0,450, 800,450, color_black, 1.0);

   float rx[13], ry[13];
   float phi, theta, radius; //phi original angle theta added angle of rotation
   float ycenter = 0, xcenter = 0;  //later will be used with varying object position

   theta = degrees * PI/180.0;

   for (int i = 0; i < 13; i++)
   {
     phi = atan2(yarray[i], xarray[i]);
     radius = sqrt(pow((xarray[i]-xcenter),2.0) + pow((yarray[i]-ycenter),2.0));
     rx[i] = xarray[i] + radius * cos(phi + theta);
     ry[i] = yarray[i] + radius * sin(phi + theta);
   }

   float x = quad[2][0];
   float y = quad[2][1];

   al_draw_triangle(x+rx[0],y+ry[0], x+rx[1],y+ry[1], x+rx[2],y+ry[2], color_red, 10);
   al_draw_filled_triangle(x+rx[3],y+ry[3], x+rx[4],y+ry[4], x+rx[5],y+ry[5],   color_orange);
   al_draw_filled_triangle(x+rx[6],y+ry[6], x+rx[7],y+ry[7], x+rx[8],y+ry[8], color_blue);   //body
   al_draw_filled_triangle(x+rx[9],y+ry[9], x+rx[10],y+ry[10], x+rx[11],y+ry[11],color_blue);  //wing
   al_draw_filled_ellipse(x+rx[12],y+ry[12], 4, 8, color_black);        //cockpit

   al_flip_display();
}

Solution

  • I found teh answser after working it all out with paper and LOTS of print statements. I had to remove the xarray and yarray addition for rx and ry. See below.

      rx[i] = radius * cos(phi + theta);
      ry[i] = radius * sin(phi + theta);
    

    replaces rx[i] = xarray[i] + radius * cos(phi + theta); ry[i] = yarray[i] + radius * sin(phi + theta);