Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How can I map mouse coordinates to world coordinates using allegro transofrms?

Credits go to SiegeLord for helping out!
This thread is locked; no one can reply to it. rss feed Print
How can I map mouse coordinates to world coordinates using allegro transofrms?
Bungow
Member #16,693
June 2017

Hello, in first place sorry for my bad english and thanks for your time.

I have a 2d tiled base project, and is drawed to screen using this allegro transform:

#SelectExpand
1mkLevel *level = ctx->current_level; 2 ALLEGRO_MOUSE_STATE mouse; 3 al_get_mouse_state(&mouse); 4 5 float world_x = 6 -(((ctx->screen.width / 2) - mouse.x) - level->viewport.x); 7 float world_y = 8 -(((ctx->screen.height / 2) - mouse.y) - level->viewport.y); 9 10 al_identity_transform(&level->viewport.transform); 11 12 al_translate_transform(&level->viewport.transform, -level->viewport.x, 13 -level->viewport.y); 14 15 al_rotate_transform(&level->viewport.transform, level->viewport.rotate); 16 al_scale_transform(&level->viewport.transform, level->viewport.zoom, 17 level->viewport.zoom); 18 19 al_translate_transform(&level->viewport.transform, 20 ctx->screen.width * 0.5, 21 ctx->screen.height * 0.5); 22 23 //al_transform_coordinates(&level->viewport.transform, &world_x, 24 // &world_y); 25 26 27 al_use_transform(&level->viewport.transform); 28 /* DRAW START HERE */

How can I use this transform to translate my mouse position to world position? I tried to use al_transform_cordinates but I'm not sure how I have to treath the mouse value before passing to it. My current implementation of world_x and world_y only works before zoom or rotation are applied ::)

SiegeLord
Member #7,827
October 2006
avatar

al_invert_transform(&level->viewport.transform);
al_transform_coordinates(&level->viewport.transform, &mouse_x, &mouse_y);

That should work. Obviously it might make sense for you to copy the transformation before inverting it.

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

Bungow
Member #16,693
June 2017

Thanks!

Go to: