# Allegro 5 Mouse Look

I found good tutorial on how to do mouse look for 2D game here: http://www.atburrow.com/2010/03/08/objects-facing-the-cursor/

My code looks like this:

w = (double)mouse_x - (double)pos_x;
h = (double)mouse_y - (double)pos_y;
d = sqrt(w * w + h * h);
theta = asin(w / d) * 2 * ALLEGRO_PI;


Problem is i cant figure out how to translate it for Allegro 5. Tried this on many different ways and its working, but not correctly. First im not using OpenGL (its a simple project for 2D controlls) so what i did after this is:

al_draw_rotated_bitmap(playerLook, imWidth / 2, imHeight / 2, pos_x, pos_y, theta, 0);


Object is rotating a bit faster than it should be. So this is blocking me from further exploration. I also found good tutorial for blitz from guy that im subscribed: http://www.youtube.com/watch?v=zFAaILFJtCY

Just went out, but still there is something that confuse me when using Allegro fixed math and i cant learn it without good example.Do anyone know how to set this code right in allegro5 ? How to do 360 degree rotation using mouse in allegro5?

"He who would learn to fly one day must first learn to stand and walk and run and climb and dance; one cannot fly into flying..."

You appear to want to draw a sprite that is always facing the mouse, correct? Then your first task must be to draw a sprite that is always facing something. Pick an arbitrary point and then do the math to draw the sprite so that it faces it. Then pick another point; the math should still work.

For only once you have mastered the sprite, can you slave it to the mouse.

The al_draw_rotated_bitmap function takes an angle to rotate the bitmap by. The documentation sadly isn't specific about what direction it rotates in. It also doesn't say whether the angle is in degrees or radians.

You should be able to find out both of these via experimentation. You shouldn't have to, but there's not much you can do about it. Just draw a sprite, then draw the same sprite next to it with a 45.0 rotation. If that causes the sprite to rotate right by 45 degrees, then you know it's a clockwise rotation and its in degrees. If it goes left by 45, then it's counter-clockwise and in degrees. If it's something else, then you know it's in radians, and you should try again with 0.785.

Once you know how the function works, it's a simple matter to use it. Given the location of the sprite S and the location of the point of interest P, you can compute the vector direction from the sprite to that point (P - S).

Then, you want to compute the angle you need to rotate from whatever your zero facing is to face that point. Let's assume that a zero angle represents facing right (a direction of (1, 0)). This means your sprite, when not rotated, faces right. I make this assumption because it allows me to do this:

double angRadians = atan2(P.y - S.y, P.x - S.x);


If al_draw_rotated_bitmap takes radians, you can feed that directly to it. If it takes degrees, you'll have to convert it to degrees by multiplying by the radians by 180/Pi.

• Why standard code that you gave me is not working correctly? playerAngle = angRadians * (3 / ALLEGRO_PI); Can you explain me why this one work more correctly than yours? Its working almost perfect, but im aware that im missing the right number because as im moving mouse im loosing perfect 90 degree angle from imaginary right triangle and when playerAngle; goes from positive to negative i see glitch like im missing that right number of 360 degree. Standard code that you gave me just spin sprite like crazy :) – Marko Sep 29 '11 at 12:35
• The fact that i solved this with playerAngle = angRadians * 0.995929659; makes me worry even more. I wonder how to find where did i made mistake. – Marko Sep 29 '11 at 13:31
• @Marko: 0.9959 is approximately 1. So it seems that al_draw_rotated_bitmap simply takes radians. – Nicol Bolas Sep 29 '11 at 17:29