Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Transforming

This thread is locked; no one can reply to it. rss feed Print
Transforming
dakatt
Member #10,695
February 2009

Hi! I've recently been learning about basic physics in games and I'm up to the point of angles/angular velocity etc. Basically I want to transform an al_rectangle to a specific angle. I know next to nothing about transformations even after reading doc files and a few tutorials. The best I can do at present is set the entire world to the desired angle.

[code]
float x=100,y=100,a=0.0f;
float x2=400,y2=100;

void Draw(){
al_draw_filled_rectangle(x2,y2,x2+50,y2+50,al_map_rgb(255,255,0));

//some transformation code here I'm assuming to modify the box below
al_draw_filed_rectangle(x,y,x+50,y+50,al_map_rgb(0,255,255));
//then something to rotate the world back to the original position, maybe?

}
void Update(){
a+=0.01;
}

[/code]

I've been using ALLEGRO_TRANSFORM and have been getting some pretty crazy results. Can anybody modify the above code to change just one of the rectangles angles? Or explain why what I'm doing won't work? :) Thanks guys

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

dakatt,

Please show more code. Please show us what you have or are trying right now, and tell us what you expect it to do.

You say you've got the world rotated now. That's a good first step. So you know how to rotate. That's good.

To rotate a rectangle in place takes a little more work, but not much.

1. Get the current view matrix from allegro using al_get_current_transform.

ALLEGRO_TRANSFORM t,original;
t = *al_get_current_transform();
original = t;
// save for later

2. Decide on a pivot for your shape. Usually it's the center. You will translate by this vector before doing anything else so the rotation will work properly.

al_translate_transform(&t , -centerx , -centery);

3. Now that you have made the center of your rectangle the origin (0,0) with al_translate_transform, you can safely rotate it around this pivot.

al_rotate_transform(&t , 45.0*M_PI/180.0);// rotate by 45 degrees

4. Now you have to move your shape back to where it was, so translate by the opposite of the first translation.

al_translate_transform(&t , centerx , centery);

5. Now use the new transform and you're ready to draw your rectangle.

al_use_transform(&t);
/// Draw a regular rectangle 
//void al_draw_rectangle(float x1, float y1, float x2, float y2,
//                       ALLEGRO_COLOR color, float thickness)

al_draw_rectangle(0 , 0 , rwidth , rheight , al_map_rgb(0,255,0) , 5.0);

You draw the rectangle at (0,0,w,h) because your code translates by the rectangle's center so you don't have to draw it at its real position.

6. Now clean up after yourself.

al_use_transform(&original);

dakatt
Member #10,695
February 2009

Thank you Edgar, that's basically everything I needed. I'm following a youtube series on physics engines which looks to be building up to something similar to box2D. I was understanding the physics side of things, but when they started using functions from their own language I got lost. I was stretching my skills writing a custom vector class, I think writing a transformation function would be a bit beyond my skill level :)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Go to: