- Home /
ZigZag Gesture Recognition using JS
Hey there,
i'm having a hard time here figuring out how to actually implement a function that is able to recognize a zig zag gesture.
i painted a rough image of what i mean to give you a better understanding - yes im no van gogh.
in the picture, four changes of the finger movement are shown, but i'd be happy recognizing at least 3. please keep in mind that the gesture can be performed the other way round.
i aim for a function returning true if the user performs such a gesture.
What im asking for:
an actual implementation of this?
or maybe just u guys giving me a clue of how to properly implement this so i can code it myself.
Can u guys help me? Hope i made clear what i want to achieve. If you need any further information, just ask.
thx in advance!
flowinho
Answer by flowinho · Feb 06, 2013 at 06:02 PM
Solved.
case iPhoneTouchPhase.Moved: //Debug.Log("Moved");
touchPoints.Add(touch.position);
// Shift Array if too much touch coordinates
if(touchPoints.Count > maxPoints) touchPoints.Shift();
// Process touch distance
touchDistance = touchDistance + currentTouchDelta.magnitude;
// SwipeLength can be resetted
swipeLength = swipeLength + currentTouchDelta.magnitude;
// Gesture Recognizer
var predecessorTouch:Vector2 = touchPoints[1];
var tmpPos:Vector2 = touch.position;
var ang:float = Vector2.Angle(predecessorTouch, tmpPos);
var cross:Vector3 = Vector3.Cross(predecessorTouch, tmpPos);
if (cross.z > 0) ang = 360 - ang;
touchAngle = ang;
// Angle comparison
var absolute:float;
if(compareAngle!=0) absolute = Mathf.Abs(touchAngle - compareAngle);
if(absolute >= 50) directionChanged = true;
compareAngle = touchAngle;
if(directionChanged) {
directionChanges++;
Debug.Log("DirectionChanges: "+directionChanges);
directionChanged = false;
}
break;