Advertisement Jump to content
Sign in to follow this  

Rotating to Mouse (using allegro, but not necessary)

This topic is 4934 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, needing some help rotating my sprite using allegro (this is my first post). But first of all, I'm not a newbie. I can make some decent arcade side and top scrollers and do tile mapping stuff as well as basic particle effects. Right, onto my prob. I want to make a sprite rotate to the direction of the mouse. I can get the mouse coords, sprite coords, then subtract accordingly to get the centre of the sprite which I want to act as the 'origin' at all times. This is to allow the sprite to move around while still being able to turn to the mouse pointer. Then I go onto using atan2 on the coords to get my angle which i believe is in radians, then convert it using the routine used in the Allegro Quick Reference guide by . Which is (x/2/M_PI*0xFFFFFF) (x being the angle from atan2) Then i fire it into the draw rotate sprite function but the sprite doesnt correctly follow the mouse pointer. Depending on the location of the cursor, the image rotates slowly or quickly or makes several 360 degree turns just from moving 100 or so pixels. Have tried with and without itofix. Any help will be greatly appreciated. Be it psuedo or source. Will credit any sources in my current project. Thanks alot ppl. Mava.

Share this post

Link to post
Share on other sites
I am not familiar with Allegro but I think I understand the problem.

atan2 and all std:: math functions use radians and I'm assuming allegro uses degrees?

I don't understand where the formula (x/2/M_PI*0xFFFFFF) came from

1 radian = 180/pi = 57.2957795 degrees

let's say you have your mouseX and mouseY and you use atan2 to find the angle in radians

angleDeg = angleRad*(180/M_PI)

sorry if this wasn't your problem, I'm not familiar enough with allegro to fully understand your problem

Share this post

Link to post
Share on other sites
Allegro uses 1-255 for 0-360 degress rotation. And the formula i mentioned converts it to that (i think :)


It seems as though i wasnt using it properly. Now i've got it working!
Thanks, i checked over the formula again coz u mentioned it. Newbie mistake really..

Many Thanks again.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!