Jump to content
  • Advertisement
Sign in to follow this  

8 Way Movement Via Mouse Control (2D Game)

This topic is 4251 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, 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 this post

Link to post
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 this post

Link to post
Share on other sites
some trig:

float angleinradians = atan2( ( mousey - charactery ) / ( mousex - characterx ) )

why? because:

/ | dy = mousey - charactery
/ |
character .---
dx = mousex - characterx

tan( theta ) = dy / dx
theta = arc tangent( dy / dx )

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!