0
\$\begingroup\$

I have a player in the center of the screen, where I also have my mouse position origin. (0,0) I would like to know the direction of the mouse, imagine splitting the screen with a X, so the top quadrant would be north, the bottom would be south... and so on. So if my mouse was at 5,-100 it should log ("North") as that is just a bit to the left and up 100 pixels.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Come on people. This is a valid question, even though it's simple. Explain your down votes if you're going to down vote. \$\endgroup\$
    – House
    Commented Dec 21, 2012 at 19:39
  • \$\begingroup\$ Without the accepted answer it is difficult to understand exactly what is being asked. This question reads more like a statement. Since this site strives to be a knowledge base for future users consider rephrasing as a clearer question. \$\endgroup\$ Commented Dec 21, 2012 at 19:46
  • \$\begingroup\$ -1 Severe lack of research \$\endgroup\$
    – Anko
    Commented Dec 22, 2012 at 2:25
  • \$\begingroup\$ I would say this question which reads as how to track the mouse direction so I can find if I move north, south, east and west. It is pretty hard to google for that, atleast I fail to do so. \$\endgroup\$
    – OMG-1
    Commented Aug 8, 2019 at 18:55

1 Answer 1

10
\$\begingroup\$
if (Abs(Y) > Abs(x)) {
  if (Y < 0) {
    return "North";
  } else {
    return "South";
  }
} else {
  if (X < 0) {
    return "West";
  } else {
    return "East";
  }
}
\$\endgroup\$
2
  • \$\begingroup\$ I thought for sure some trig would be needed, I guess not! \$\endgroup\$
    – FreshJays
    Commented Dec 21, 2012 at 13:59
  • 2
    \$\begingroup\$ Might want a slight amendment to the code to handle the X == Y == 0 case. \$\endgroup\$ Commented Dec 21, 2012 at 23:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .