Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a shake event?

I'm making a gui API for games and one requested feature was a shake event. Essentially an event very similar to Windows 7's Aero Shake. When the mouse is down, if it is rapidly moved back and forth uni directionally, the event is fired. I'm just not sure what type of psedocode would go into this?

like image 495
jmasterx Avatar asked Nov 10 '10 23:11

jmasterx


3 Answers

I might consider something like this:

  1. always create a vector for the direction the mouse travelled on its last move
  2. always keep a record of the immediate prior vector, as you gather the next vector (which means you always have two vectors to evaluate).
  3. do a dot-product of the two vectors, as each mouse input arrives. If the dot-product is positive, then ignore it. If it is negative, then set a time value with it and store in an array tha stores perhaps up to five of these time stamps.
  4. repeat the above sequence. If the timestamp array has a consecutive set of timestamps that each differ by some epsilon, then you can presume someone is shaking something. :)
like image 171
Brent Arias Avatar answered Oct 24 '22 00:10

Brent Arias


You need to count the changes in mouse movement. If mouse changes direction more than 3 times in less than, say, 0.7 seconds then it's a shake. To detect the change in direction track forever last 5 mouse coordinates. If point P0 is last, and P5 fifth to last then calculate an angle made between P0-P3 and P3-P5. If the angle is lest that 5 degrees then the mouse changed direction.

like image 38
Dialecticus Avatar answered Oct 24 '22 01:10

Dialecticus


The general idea would be something like this:

On mouse down:

  1. Store the current position as (x0, y0).
  2. Set slope = 0.
  3. Set start time = current time.

On mouse move:

  1. Store the current position as (x, y).
  2. Set newSlope = abs((y - y0) / (x - x0)). (this indicates the direction of mouse movement with respect to the starting position)
  3. If abs(newSlope - slope) < some threshold then the user is still moving in the same direction: then check if current time - start time > shake time, if so, fire the shake event and reset start time.
  4. Otherwise, the user has changed direction. Set slope = newSlope and reset start time = current time.
like image 20
casablanca Avatar answered Oct 24 '22 01:10

casablanca