Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » ALLEGRO_TRANSFORM and Movement

This thread is locked; no one can reply to it. rss feed Print
ALLEGRO_TRANSFORM and Movement
Shpinxis
Member #16,080
September 2015

So, I put a camera scrolling feature into my game which has the camera following the player movement.
I did this using the ALLEGRO_TRANSFORM and these lines:

al_identity_transform(&CameraTransform);
al_translate_transform(&CameraTransform, -(m_XPosition), -(m_YPosition));
al_use_transform(&CameraTransform);

Now the follow camera works fine when I use my keyboard controls, but it broke my mouse movement I had in my game. I move the player by mouse clicks as well, by finding the position of the mouse click and moving the player to that position.

I do not fully understand what the allegro transform/translate are doing to the screen and maybe this is somehow messing with the x / y click positions? The mouse movement seems to work on the left top half of the screen but not where else. Hopefully this makes sense and someone has a suggestion. Thanks.

Mark Oates
Member #1,146
March 2001
avatar

Mkay, the way an ALLEGRO_TRANSFORM works is that whatever you draw something, it will be moved in the direction of the transform.

So if you were to draw a box at (20, 30) while an ALLEGRO_TRANSFORM of (100, 400) is in effect, then it will be drawn at (120, 430).

Simple enough.

However, it doesn't transform the position of your mouse cursor, which will always be given in screen coordinates. So for that, you'll want to use al_transform_coordinates().

So grab your mouse coordinates when you catch the ALLEGRO_MOUSE_EVENT:

float mouse_on_screen_x = current_event.mouse.x;
float mouse_on_screen_y = current_event.mouse.y;

Then later in your drawing code:

// build your transform here
ALLEGRO_TRANSFORM camera_transform;
al_translate_transform(&camera_transform, -camera_x, -camera_y);

// get convert the mouse position to world coordinates
float mouse_in_world_x = mouse_on_screen_x;
float mouse_in_world_y = mouse_on_screen_y;
al_transform_coordinates(&camera_transform, &mouse_in_world_x, &mouse_in_world_y);

// Now you can use mouse_in_world_x and mouse_in_world_y as the cursor on your screen
al_use_transform(&camera_transform);
al_draw_circle(mouse_in_world_x, mouse_in_world_y, 5.0, al_color_name("dodgerblue"), 1.0);

[edit] I made a few edits for clarity.

Shpinxis
Member #16,080
September 2015

Thank you for the reply and help.

I have it mostly working how I want with the suggestions you posted, the only problem is that after the transform_coordinates the values do not seem to change. Also, the numbers over the initial mouse coordinates are crazy negative numbers, that probably doesn't matter. Not sure if there would be a reason that you would know why it seems that the transform_coordinates isn't changing the numbers.

float TempMouseXCoordinate = m_MouseXCoordinate;
float TempMouseYCoordinate = m_MouseYCoordinate;

al_transform_coordinates(&CameraTransform, &TempMouseXCoordinate, &TempMouseYCoordinate);

Rodolfo Lam
Member #16,045
August 2015

The numbers appearing as random negatives at the beginning looks to me as variables not correctly initialized.

Instead of having int foo; consider using int foo = 0;. That way the variable doesn't start its life with the previous data that was held on that position.

The second issue you have is happening maybe because you are only updating once in the program's lifetime the mouse coordinates. Try to put it somewhere logical on you main loop. A good position is on the ALLEGRO_EVENT_TIMER event block, so that it updates once per frame.

Go to: