Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » 2d Camera - translate, rotate, and zoom

This thread is locked; no one can reply to it. rss feed Print
2d Camera - translate, rotate, and zoom
Schillie
Member #14,112
March 2012

Hello,

I am trying to develop a 2d camera for my game and have run into some issues. I'm attempting to use the ALLEGRO_TRANSFORM struct and functions in order to do so, since I need my camera to be able to rotate and zoom. The current code i have is this:

#SelectExpand
1// Update Transforms 2al_identity_transform(cam); // Reset 3al_scale_transform(cam, camZoom*D_HEIGHT, -camZoom*D_HEIGHT); // Scale to range from -1 to 1 4al_rotate_transform(cam, camRot); // Rotate 5al_translate_transform(cam, D_WIDTH/2, D_HEIGHT/2); // Center the camera 6al_translate_transform(cam, camX, camY); // Move the camera around 7al_use_transform(cam); // Use the transform

What i wish this to do is scale my coordinate system into a resolution independent system, like openGL does, then allow for rotation, zooming, and translation. Basically, a full-fledged 2d camera.

The translation is working fine, along with the resolution independent scaling. However, both the zooming and the rotation rotate all objects about the origin, which is not the desired result.

Any help would be greatly appreciated.

SiegeLord
Member #7,827
October 2006
avatar

Try this:

// Update Transforms
al_identity_transform(cam); // Reset
al_translate_transform(cam, -D_WIDTH/2, -D_HEIGHT/2); // Center the camera
al_scale_transform(cam, camZoom*D_HEIGHT, -camZoom*D_HEIGHT); // Scale to range from -1 to 1
al_rotate_transform(cam, camRot); // Rotate
al_translate_transform(cam, camX, camY); // Move the camera around
al_use_transform(cam); // Use the transform

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Schillie
Member #14,112
March 2012

Thanks for the reply,

I attempted to use it, but it seemed to scale/translate things such that I was unable to find my box that I am drawing using the following command:

#SelectExpand
1al_draw_filled_rectangle(-0.5,-0.5,0.5,0.5,al_map_rgb(255,255,255));

Using the order of transformations that I originally posted, this box appears and correctly fills up the entire height of the screen, with even space on each side.

I'm allowing camera interaction thru the following code:

#SelectExpand
1if(keys[UP]) 2 camY += 1; 3if(keys[DOWN]) 4 camY -= 1; 5if(keys[LEFT]) 6 camX += 1; 7if(keys[RIGHT]) 8 camX -= 1; 9if(keys[ROTL]) 10 camRot -= PI/50; 11if(keys[ROTR]) 12 camRot += PI/50; 13if(keys[ZIN]) 14 camZoom += 0.01; 15if(keys[ZOUT]) 16 camZoom -= 0.01;

I see where your going with this SiegeLord, tomorrow I'll try to think thru some of the math to figure out how to move the camera to the proper place before rotating/scaling.

Until then, other replies are appreciated, thanks.

Go to: