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?
I might consider something like this:
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.
The general idea would be something like this:
On mouse down:
(x0, y0)
.slope = 0
.start time = current time
.On mouse move:
(x, y)
.newSlope = abs((y - y0) / (x - x0))
. (this indicates the direction of mouse movement with respect to the starting position)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
.slope = newSlope
and reset start time = current time
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With