Jump to content
  • Advertisement
Sign in to follow this  
froghaus

8 Way Movement Via Mouse Control (2D Game)

This topic is 4157 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
Advertisement
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:

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

tan( theta ) = dy / dx
thus
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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!