title:  work simple with mouse gestures
author: Ulrich Seiffert
eMail:  3dgs@ulrich-seiffert.de
policy: credits

////////////////////////////////////////////////
// detect easy mouse-gestures
// author: u.seiffert / gameus.de
// date: mai  2006
// copyright: credits: part. coding by gameus.de
////////////////////////////////////////////////

Sometimes it is more easy to use mouse-gestures to initiate an action within a game.
So often it may be nice to use a mouse-gesture to initiate a magic spell with the mouse.

Handling and recognise a mouse gesture is easy if we use normalised vectors of the mousmoves.

The idea: we watch the mousemoves and store the normalized directions (x: -1/1,  y: -1/1) 
if the direction change.

Examples: 
- the mouse moves from top left to bottom right, the normal vector of the move is: (x/y)(1/1) 
- the mouse moves from bottom left to top right, the normal vector of the move is: (x/y)(1/-1)

A 'S' would be defined by this vectors: 

             /\ _begin here
             \ 
 end here - \/

Vecotrs: (-1/-1), (-1/1), (1,1), (-1,1), (-1,-1)

Now if we store these vectors in an (filo) stack, we can compare the vectors with an array of 
predefined gesture-vectors.

var gest_s[10] = -1,-1, -1,1, 1,1, -1,1, -1,-1;   // the moves for a "S"

This easy method of detecting mouse-gestures works fine with more complex moves so a 'S' is 
often part of a more complex gesture and no good idea when working with many gestures...

The sourcecode is well-documented and easy to understand.
You can run the script and can watch the changes of then filo-stack.
The predefined vectors are 'S' and 'O'. 
Do use more complex gestures if you use this code.

Suggestion:
If you want to handle horizontal and vertical moves (means ignoring a part of a vector)
you can define 0-vectors and ignore the actual mouse-vector if there is a 0.

var gest_e[12] = -1,0, 0,1, 1,0, -1,0, 0,1, 1, 0;	// the moves for an "E"

// ignoring the 0-vector
if((gest_e[gest_e_check]==0 || gest_e[gest_e_check]==p_dx) && (gest_e[gest_e_check+1]==0 || gest_e[gest_e_check+1]==p_dy)){
	gest_e_check+=2;	// match -> next one
}else{
	gest_e_check=0; 	// failed -> reset
}


If you have questions about the idea/code please contact: 3dgs@ulrich-seiffert.de




