8 Way Movement Via Mouse Control (2D Game)

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

Recommended Posts

Hi, Pardon me if this is a ridiculously stupid question, but here goes. My character is controlled via the mouse. When you click with the left button, he follows it. What I need is a relatively simple way to detect the angle of movement. Say, the character is at 32, 32 and the mouse is at 48, 50, how can I go about finding a simple way to detect angle? Sorry, I'm sure this is stupid, but I don't really have many options. I'm looking up math information right now, but having no luck. Thanks a lot.

Share on other sites
atan() or atan2()

compare the mouse position to the characters position, rise over run = slope, remember? slope~angle

Why do you need to know the angle though?
Just use a vector approach, much more direct, saves the work of doing trig stuff.

Share on other sites
some trig:
float angleinradians = atan2( ( mousey - charactery ) / ( mousex - characterx ) )why? because:              mouse              .             /|            / | dy = mousey - charactery           /  |character .---            dx = mousex - characterxtan( theta ) = dy / dxthustheta = arc tangent( dy / dx )

Share on other sites
Thanks for the help! Got it figured out now.

Share on other sites
remember to check for mousex=charx first, otherwise you could divide by zero

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633723
• Total Posts
3013546
×

Important Information

GameDev.net 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!