Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Scaling doesn't work with transformation.

This thread is locked; no one can reply to it. rss feed Print
Scaling doesn't work with transformation.
zero volt
Member #15,793
November 2014

When I use the following code:

al_identity_transform(&camera);
al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]);
al_scale_transform(&camera, 0.8, 0.8);
al_use_transform(&camera);

Why after scaling the character is not in the middle of the screen? instead, he's pushed a little to the left and up. and so the camera moves before the character is on the middle of the screen.

RPG Hacker
Member #12,492
January 2011
avatar

zero volt said:

Why after scaling the character is not in the middle of the screen? instead, he's pushed a little to the left and up.

Well, you just moved the translation transform down and right, so of course the player would be moved left and up, unless you apply another transform to the player (with the opposite values). What exactly were you expecting?

Quote:

and so the camera moves before the character is on the middle of the screen.

We can't really help you much there without seeing your full game loop.

Mark Oates
Member #1,146
March 2001
avatar

ok, alright... when using transforms and applying scale and rotation, you now have to consider the the origin of your camera. With the method I used in that thread from earlier, the camera's origin is at the upper left corner of the screen. This is fine and dandy for that, but when you scale or rotate the camera, it will scale relative to the top left corner of the screen.

So try this:

al_identity_transform(&camera);
al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]);
al_scale_transform(&camera, 0.8, 0.8);
al_translate_transform(&camera, SCREEN_WIDTH/2.0, SCREEN_HEIGHT/2.0); // ***
al_use_transform(&camera);

That new line of code I added will move the cameraPosition origin so that it now represents the center of the screen.

For fun, you can easily see the camera scaling to and from the origin like this:

al_identity_transform(&camera);
al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]);
float scale = sin(al_get_time()) * 0.2 + 0.8;
al_scale_transform(&camera, scale, scale);
al_translate_transform(&camera, SCREEN_WIDTH/2.0, SCREEN_HEIGHT/2.0); // ***
al_use_transform(&camera);

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

Go to: