[SOLVED] How do I find the direction mouse is moving

Before proceeding, please use the forum search feature at the top of the page to check if your question has already been answered.

How do I…

I’m trying to work out how to find which direction (up, down, left, right) the mouse is travelling in but I can’t seem to get an accurate result.

Because how fast you can move mouse (you can move it faster then a frame skips)
I can’t think of any accurate way to do it

My 1st stupid idea is to create sprite object and apply force to it toward CursorX() CursorY()
Like very high INSTANT force something above 500 or even 1000

And now you can check angle of movement with forces of that object
This would tell you more or less which direction cursor is moving

Up would be 270° angle with 45° tolerance
Left 180°
Right 0°
Down 90°
ALL with 45° tolerance

Hi Inferno, you could try something like this. I did not test it thoroughly, so not sure if it’s bulletproof, and to make it work, the readout is done with a delay of 0.1sec.

But here’s one way to do it:

  • you write the position of the mouse to a variable (I write it to an array - one child for the X and one for Y)
  • add a “Wait 0.1 sec” before writing down the variable
  • then you compare the current mouse X and Y position to the values in the array
  • if X is bigger than the variable, it’s Right, else it’s Left
  • if Y is bigger than the variable, it’s Down, else it’s Up

I think @Rebel-Red-Rabbit’s suggestion is along the right lines, but I don’t like the idea of using a wait.

This would be my solution:

1 Like

Wow, I don’t fully understand how, but it works great.

@Inferno, definitely use this solution :sweat_smile:

In a nutshell, it gets the difference between the mouse position in this frame and the last frame, and determining the direction of movement.

btw, abs() is a function that returns the absolute value of a number; it drops the negative sign so all numbers are positive.

Thanks, but the main part I don’t understand is why we need to compare the X and Y values.

Clearly it makes sense, because it works, but 'm not great at math, so I don’t know how those calculations work :sweat_smile:

That’s just to figure out the direction of the biggest mouse movement - horizontally or vertically.

So if the mouse moves vertically 20 pixels and horizontally 30 pixels, then the overall movement is taken as being left- right because the absolute X difference is greater than that of the Y difference. The sub events then determine whether the horizontal movement is to the left or right.

It’s the same steps for vertical movements, but using the up-down change in mouse position.

Thank you all so much for your help! :heart: