Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » floating mouse coordinates

Credits go to Edgar Reynaldo for helping out!
This thread is locked; no one can reply to it. rss feed Print
floating mouse coordinates
Loki66
Member #17,089
May 2019

Hello, sorry for my bad englishI am an Italian boy and I apologize for my bad English
My problem is this:
how to get the coordinates of the float mouse instead of int?

LennyLen
Member #5,313
December 2004
avatar

Assuming you're using Allegro 5, you could do this:

ALLEGRO_MOUSE_STATE state;
float mx, my;

al_get_mouse_state(&state);
mx = (float)state.x;
my = (float)state.y;

Why do you want the co-ordinates as floats?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

LennyLen, I think we're missing the real purpose here.

To get floating point precision mouse coordinates, you'll need to either A) use OS specific system calls to do it or B) Interface with a mouse driver that provides support for high dpi aware pointing devices.

Allegro does not provide anything other than on screen integral mouse coordinates.

Loki66
Member #17,089
May 2019

Hi, and thanks for the quick reply.
I apologize for my English (translated by google)
I need to move a circle with the coordinates of the mouse and I create a vector for the direction of the circle.
I Using the mouse functions: event.mouse.x
event.mouse.dx.
When I move the mouse slowly the values of event.mouse.dx and event.mouse.dy are:
1,0 (0°)
1,1 (45°)
0,1 (90°)
The reason is this.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Loki66, that is as expected. dx and dy will probably never be more than two or three.

First, you need to know about the different types of variables there are. You can declare a float and assign it the value of an int. You can also cast an int to float or double with (float) or (double).

Second, to track the direction, you need to track the starting point.

If the distance between your starting point and your end point are small, then you will get large intervals between angles because of low precision. Are you tracking mouse movement? You can always do it like this ;

#SelectExpand
1int mx = 0,mxstart = 0; 2int my = 0,mystart = 0; 3 4if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { 5 mxstart = ev.mouse.x; 6 mystart = ev.mouse.y; 7 drag = true; 8} 9 10if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { 11 drag = false; 12} 13 14if (ev.type == ALLEGRO_EVENT_MOUSE_AXES && drag) { 15 mx += ev.mouse.dx; 16 my += ev.mouse.dy; 17 double angle = atan2(my - mystart , mx - mxstart); 18 double degrees = angle*180.0/M_PI; 19}

SiegeLord
Member #7,827
October 2006
avatar

If there are platforms which return sub-pixel mouse positions, we can easily add support for them like we did with the "sub-pixel" mouse-wheel positions (via al_set_mouse_wheel_precision)

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

Loki66
Member #17,089
May 2019

Hello everyone, I solved with atan2 as suggested by Edgar Reynaldo
Using event.mouse.dy and event.mouse.dx
it returns the exact angle of each quadrant in double format

// calculate destination points x, y for the vector
void fv_vector_c1(double d_c1x, double d_c1y, double d_c1dx, double d_c1dy) {

// values used are: d_c1dy = event.mouse.dy // d_c1dx = event.mouse.dx
double d_angle = atan2(d_c1dy, d_c1dx);
cout<<"angolo "<<d_angle<<endl;
double d_degrees = d_angle *180.0 /3.14;
cout<<"angolo "<<d_degrees<<endl;
}

Thank you all
Loki666 from Torino, Italy

Go to: