Allegro.cc Forums » Programming Questions » Rotational Transformation

 Rotational Transformation
 ngiacomelli Member #5,114 October 2004 I have a triangle that I wish to rotate to represent the player. I want the tip of the triangle (+) to represent their current angle and the direction in which they would travel (if they were to apply thrust).``` + / \ / \ /_____\ ``` The movement code isn't my problem here, though. It's actually drawing the triangle. I want to do this using primitives (so no drawing to a bitmap, then rotating). I came up with some messy code here as a quick test:``` float t_x, t_y, t_x2, t_y2, t_x3, t_y3; t_x = 100 * fixtof(fcos(angle)) - 100 * fixtof(fsin(angle)); t_y = 100 * fixtof(fcos(angle)) + 100 * fixtof(fsin(angle)); t_x2 = 200 * fixtof(fcos(angle)) - 200 * fixtof(fsin(angle)); t_y2 = 200 * fixtof(fcos(angle)) + 200 * fixtof(fsin(angle)); t_x3 = 0 * fixtof(fcos(angle)) - 200 * fixtof(fsin(angle)); t_y3 = 200 * fixtof(fcos(angle)) + 0 * fixtof(fsin(angle)); ``` Which works to slowly rotate the triangle when the angle is adjusted. But I really need the pivot point to be in the triangles centre (I should think). So how do you do this effectively?
 Pavel Hilser Member #5,788 April 2005 I did rotation around a point, if this is what you want, i did it this way``` x ------ 1 \ \ \ \ 2 ``` rotating point 1 to 2 - adding 45degrees, rotating around xget x<->1 lengtget x<->1 angleget new angle (angle+45)get rotated position (2) - from x rotate to new angle and use the current length ``` //get the length (to point 0,0) delka=sqrt(((f*5)*(f*5))+((g*5)*(g*5))); //get the angle curr_angle = (atan2(g*5, f*5)*180)/M_PI; //new angle curr_angle+=angle; //new position mymodel[f+5][g+5].square_x = delka * cos((float)(M_PI*((float)curr_angle))/180); mymodel[f+5][g+5].square_y = (delka * sin((float)(M_PI*((float)curr_angle))/180))/2; ``` (it's taken from my new project, where points are rotated around zero)anyway, if you have fixed triangle sides, and its always triangle, you can use the pivot as a center of a circle and the two poinst could be easi calculated with the last point (youw kno both angles and both length (cause its a triangle)) _____________________________________Mein Cannon - see my new 2d game
 Ceagon Xylas Member #5,495 February 2005 Angles, rotations, and circles sure make for some 'leet' looking code.What are the first 2 arrays used for in mymodel[][]?
Member #3,861
September 2003

it's taken from his project, so you can imagine that:

```  //get the length (to point 0,0)
delka=sqrt(((f*5)*(f*5))+((g*5)*(g*5)));

//get the angle
curr_angle = (atan2(g*5, f*5)*180)/M_PI;

//new angle
curr_angle+=angle;

//new position
square_x = delka * cos((float)(M_PI*((float)curr_angle))/180);
square_y = (delka * sin((float)(M_PI*((float)curr_angle))/180))/2;
```

is what you need.

EDIT:
where square_x & y are the new position a point rotated around zero.

```void rotate_around( int cx , int cy , int *X , int *Y , float angle ){

int tx,
ty;

/* translation to make cx,cy the center of our rotation */
tx = *X - cx;
ty = *Y - cy;

*X = cx +  tx * cos(angle) + ty * sin(angle);
*Y = cy -  tx * sin(angle) + ty * cos(angle);

};
```

I'll test this and update my post to make it doing the trick.

LAST EDIT:

WORKING EXAMPLE:

 1 #include 2 #include 3 4 #include 5 6 void rotate_around( int cx , int cy , float *X , float *Y , float angle ); 7 8 9 float X=150 , Y = 150 ; 10 11 12 int main( int argc , char *argv[] ){ 13 14 allegro_init(); 15 16 install_keyboard(); 17 install_timer(); 18 19 set_color_depth( 32 ); 20 21 set_gfx_mode( GFX_AUTODETECT_WINDOWED , 640 , 480 , 0 , 0 ); 22 23 clear( screen ); 24 25 do{ 26 27 rotate_around( 320 , 200 , &X , &Y , M_PI/180 ); 28 29 putpixel( screen , X , Y , makecol( 255 , 255 , 255 )); 30 31 rest(50); 32 33 34 }while (!key[KEY_ESC]); 35 36 allegro_exit(); 37 38 }END_OF_MAIN(); 39 40 41 /*!\fn void rotate_around( int cx , int cy , float *X , float *Y , float angle ) 42 * 43 *\brief rotate a point around center cx,cy 44 * 45 *\param cx the X center 46 *\param cy the Y center 47 *\param X the X coordinate of the point to move 48 *\param Y the Y coordinate of the point to move 49 *\param angle the amount of radian of the rotation (1 degree = PI/180 rad) 50 * 51 */ 52 53 void rotate_around( int cx , int cy , float *X , float *Y , float angle ){ 54 55 float tx, 56 ty; 57 58 /* translation to make cx,cy the center of our rotation */ 59 tx = *X - cx; 60 ty = *Y - cy; 61 62 *X = cx + tx * cos(angle) + ty * sin(angle); 63 *Y = cy - tx * sin(angle) + ty * cos(angle); 64 65 };

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

 Paul Pridham Member #250 April 2000 Turtle is a nice and simple abstraction for Asteroids-like vector drawing with relative movement and rotations.
 gillius Member #119 April 2000 If you are wanting to draw an equilateral triangle with the pivot point in its center then it is easy as the three points are at theta, theta + 120deg, and theta + 240deg, where theta is the direction you are pointing. Then given theta you do sin and cos and multiply by one half of the height of the triangle (since the point is in the center, you want use use "radius" of the triangle). GilliusGillius's Programming -- http://gillius.org/
 GullRaDriel Member #3,861 September 2003 With all this code posted and all those ideas , can you give your mean of the whole, Nial ? "Code is like shit - it only smells if it is not yours"Allegro Wiki, full of examples and articles !!
 Go to: Allegro Development Installation, Setup & Configuration Allegro.cc Comments Off-Topic Ordeals The Depot Game Design & Concepts Programming Questions Recent Threads