Detecting a mouse shake?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Inner party member
Posts: 3651
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA

Detecting a mouse shake?

Post by Jasoco » Thu Oct 29, 2015 8:57 pm

Trying to work out a way to detect when the mouse is being shaken. i.e. moved back and forth rapidly in a relatively small space. I thought I had figured it out but not quite.

I was using all three mouse callbacks. Pressed, Moved and Released to store the information. And was basing my detection on how far the mouse has moved compared to how much total distance it has traveled and while it worked, it also sends false positives if the shake timer reaches the end when you happen to be moving the mouse in the area of its initial press.

I'm probably missing a pretty simple solution involving maths or something.

Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Detecting a mouse shake?

Post by davisdude » Thu Oct 29, 2015 10:43 pm

This is how I imagine it working:
The 2 circles represent boundaries of mouse movement. Obviously these could be experimented with and resized.
The red one represents an error boundary- this way, small, unintentional movements won't trigger anything.
Once the mouse is moved outside of the red circle, add a count. Redo this process on each update. Once the count reaches a certain number (say, 3) then a shake could be detected. The blue circle could be a max distance.

I have no idea if this will work, but that's how I would try it.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim

Posts: 80
Joined: Wed Sep 29, 2010 8:14 pm

Re: Detecting a mouse shake?

Post by Xugro » Thu Oct 29, 2015 11:49 pm

I wrote a small program, that should do the trick.

The idea behind it: If you are in an small enough area, then you are really shaking and not just moving the mouse around and getting back to the starting point. In my case I just made an rectangular approximation, but it does the job.
(1.57 KiB) Downloaded 50 times

User avatar
Party member
Posts: 2995
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary

Re: Detecting a mouse shake?

Post by zorg » Fri Oct 30, 2015 5:13 am

I'd personally base shake detection on 3 parameters;
1. the speed of the mouse,
2. the direction of the movement, (coupled with the above would make a velocity vector)
3. time restrictions. (too fast jitters and too slow movement wouldn't count as shaking)
(4. and another that basically says at least how many movements constitute a shake)

In mousemove, compute dx,dy, from those a magnitude and angle, and save those with a timestamp into a table, acting as a buffer with some length (#4).
Check between all values in the table, whether the timestamp difference is between your bounds for detection (#3), whether the mouse moved enough (#1), and whether the difference between angles is big enough, let's say >90° and <270°(#2).
If all of these three criteria are met for all value pairs, you did a shake.

Of course, tweaking it is the hard part, to suit your needs :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 27 guests