Esempio n. 1
0
        private Boolean velocityHighEnough(MotionAverages averages)
        {
            if (this.possibleDirections.HasFlag(MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionRight))
            {
                if (averages.velocityAverage.x < -swipeMaximum * minimumSwipeThreshold)
                {
                    direction = MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionRight;
                    return(true);
                }
            }
            if (this.possibleDirections.HasFlag(MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionLeft))
            {
                if (averages.velocityAverage.x > swipeMaximum * minimumSwipeThreshold)
                {
                    direction = MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionLeft;
                    return(true);
                }
            }
            if (this.possibleDirections.HasFlag(MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionDown))
            {
                if (averages.velocityAverage.y < -swipeMaximum * minimumSwipeThreshold)
                {
                    direction = MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionDown;
                    return(true);
                }
            }
            if (this.possibleDirections.HasFlag(MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionUp))
            {
                if (averages.velocityAverage.y > swipeMaximum * minimumSwipeThreshold)
                {
                    direction = MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionUp;
                    return(true);
                }
            }
            if (this.possibleDirections.HasFlag(MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionIn))
            {
                if (averages.velocityAverage.z < -swipeMaximum * minimumSwipeThreshold)
                {
                    direction = MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionIn;
                    return(true);
                }
            }
            if (this.possibleDirections.HasFlag(MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionOut))
            {
                if (averages.velocityAverage.z > swipeMaximum * minimumSwipeThreshold)
                {
                    direction = MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionOut;
                    return(true);
                }
            }


            return(false);
        }
        //Returns the average position from all hands. Useful for things like swipe
        protected MotionAverages averageVectorForHands()
        {
            float totalXPosition = 0;
            float totalYPosition = 0;
            float totalZPosition = 0;

            float totalXVelocity = 0;
            float totalYVelocity = 0;
            float totalZVelocity = 0;

            int totalNumberOfFingers = 0;

            //Collect Totals
            foreach (Hand hand in this.hands)
            {
                foreach (Finger finger in hand.Fingers)
                {
                    totalNumberOfFingers += 1;

                    totalXPosition += finger.TipPosition.x;
                    totalYPosition += finger.TipPosition.y;
                    totalZPosition += finger.TipPosition.z;

                    totalXVelocity += finger.TipVelocity.x;
                    totalYVelocity += finger.TipVelocity.y;
                    totalZVelocity += finger.TipVelocity.z;
                }
            }


            //Calculate Averages
            float avgXPos = totalXPosition / totalNumberOfFingers;
            float avgYPos = totalYPosition / totalNumberOfFingers;
            float avgZPos = totalZPosition / totalNumberOfFingers;

            float avgXVel = totalXVelocity / totalNumberOfFingers;
            float avgYVel = totalYVelocity / totalNumberOfFingers;
            float avgZVel = totalZVelocity / totalNumberOfFingers;

            //Create Average Vectors

            Leap.Vector avgPositionVector = new Leap.Vector(avgXPos, avgYPos, avgZPos);
            Leap.Vector avgVelocityVector = new Leap.Vector(avgXVel, avgYVel, avgZVel);

            //Return averages
            MotionAverages averages = new MotionAverages(avgPositionVector, avgVelocityVector);

            return(averages);
        }
        private void processTouchVectors(MotionAverages averages)
        {
            switch (this.state)
            {
            case MotionGestureRecognizerState.MotionGestureRecognizerStatePossible:
                if (averages.velocityAverage.y < MotionTapDownBeginYThreshold && averages.velocityAverage.z < MotionTapDownBeginZThreshold)
                {
                    this.Direction = MotionTapGestureRecognizerDirection.MotionTapGestureRecognizerDirectionDown;
                    this.state     = MotionGestureRecognizerState.MotionGestureRecognizerStateBegan;
                }
                break;

            case MotionGestureRecognizerState.MotionGestureRecognizerStateBegan:
                this.state = MotionGestureRecognizerState.MotionGestureRecognizerStateChanged;
                if (averages.velocityAverage.y > MotionTapDownBeginYThreshold && averages.velocityAverage.z > MotionTapDownBeginZThreshold)
                {
                    this.Direction = MotionTapGestureRecognizerDirection.MotionTapGestureRecognizerDirectionUp;
                }
                break;

            case MotionGestureRecognizerState.MotionGestureRecognizerStateChanged:
                this.state = MotionGestureRecognizerState.MotionGestureRecognizerStateChanged;

                if (averages.velocityAverage.y > MotionTapDownBeginZThreshold && averages.velocityAverage.z > MotionTapDownBeginZEndThreshold)
                {
                    this.Direction = MotionTapGestureRecognizerDirection.MotionTapGestureRecognizerDirectionUp;
                    this.state     = MotionGestureRecognizerState.MotionGestureRecognizerStateEnded;
                }
                else if (averages.velocityAverage.y > MotionTapDownBeginYThreshold && averages.velocityAverage.z > MotionTapDownBeginZThreshold)
                {
                    this.Direction = MotionTapGestureRecognizerDirection.MotionTapGestureRecognizerDirectionUp;
                }
                break;

            case MotionGestureRecognizerState.MotionGestureRecognizerStateEnded:
                this.state = MotionGestureRecognizerState.MotionGestureRecognizerStatePossible;
                break;

            default:
                break;
            }
        }
        public override void positionDidUpdate(Leap.HandList hands)
        {
            this.hands = hands;

            if (this.hands.Count == this.NumberOfHandsRequired)
            {
                if (isDesiredNumberOfFingersPerHand())
                {
                    MotionAverages averages = averageVectorForHands();

                    if (averages != null)
                    {
                        centerPoint = averages.positionAverage;
                        processTouchVector();
                    }
                    else
                    {
                        processNonTouch();
                    }
                }
            }
        }
        private void processTouchVectors(MotionAverages averages)
        {
            switch (this.state)
            {
                case MotionGestureRecognizerState.MotionGestureRecognizerStatePossible:
                    if (averages.velocityAverage.y < MotionTapDownBeginYThreshold && averages.velocityAverage.z < MotionTapDownBeginZThreshold)
                    {
                        this.Direction = MotionTapGestureRecognizerDirection.MotionTapGestureRecognizerDirectionDown;
                        this.state = MotionGestureRecognizerState.MotionGestureRecognizerStateBegan;
                    }
                    break;
                case MotionGestureRecognizerState.MotionGestureRecognizerStateBegan:
                    this.state = MotionGestureRecognizerState.MotionGestureRecognizerStateChanged;
                    if (averages.velocityAverage.y > MotionTapDownBeginYThreshold && averages.velocityAverage.z > MotionTapDownBeginZThreshold)
                    {
                        this.Direction = MotionTapGestureRecognizerDirection.MotionTapGestureRecognizerDirectionUp;
                    }
                    break;
                case MotionGestureRecognizerState.MotionGestureRecognizerStateChanged:
                    this.state = MotionGestureRecognizerState.MotionGestureRecognizerStateChanged;

                    if (averages.velocityAverage.y > MotionTapDownBeginZThreshold && averages.velocityAverage.z > MotionTapDownBeginZEndThreshold)
                    {
                        this.Direction = MotionTapGestureRecognizerDirection.MotionTapGestureRecognizerDirectionUp;
                        this.state = MotionGestureRecognizerState.MotionGestureRecognizerStateEnded;
                    }
                    else if (averages.velocityAverage.y > MotionTapDownBeginYThreshold && averages.velocityAverage.z > MotionTapDownBeginZThreshold)
                    {
                        this.Direction = MotionTapGestureRecognizerDirection.MotionTapGestureRecognizerDirectionUp;
                    }
                    break;
                case MotionGestureRecognizerState.MotionGestureRecognizerStateEnded:
                    this.state = MotionGestureRecognizerState.MotionGestureRecognizerStatePossible;
                    break;
                default:
                    break;
            }
        }
        public override void positionDidUpdate(HandList hands)
        {
            this.hands = hands;

            if (this.hands.Count == this.NumberOfHandsRequired)
            {
                if (isDesiredNumberOfFingersPerHand())
                {
                    MotionAverages averages = averageVectorForHands();

                    if (averages != null)
                    {
                        this.TapPosition = averages.positionAverage;
                        this.TapVelocity = averages.velocityAverage;

                        processTouchVectors(averages);
                    }

                    //Callback
                    callback();
                }
            }
        }
Esempio n. 7
0
        public override void positionDidUpdate(HandList hands)
        {
            this.hands = hands;

            if (this.hands.Count == this.NumberOfHandsRequired)
            {
                if (isDesiredNumberOfFingersPerHand())
                {
                    MotionAverages averages = averageVectorForHands();

                    if (averages != null)
                    {
                        if (velocityHighEnough(averages))
                        {
                            switch (this.state)
                            {
                            case MotionGestureRecognizerState.MotionGestureRecognizerStatePossible:
                                //Helps with false positives due to change of direction
                                if (accelerationCounter > 3)
                                {
                                    accelerationCounter = 0;
                                    this.state          = MotionGestureRecognizerState.MotionGestureRecognizerStateBegan;
                                }
                                else
                                {
                                    accelerationCounter++;
                                }
                                break;

                            case MotionGestureRecognizerState.MotionGestureRecognizerStateBegan:
                                this.state = MotionGestureRecognizerState.MotionGestureRecognizerStateChanged;
                                break;

                            case MotionGestureRecognizerState.MotionGestureRecognizerStateChanged:
                                this.state = MotionGestureRecognizerState.MotionGestureRecognizerStateChanged;
                                break;

                            default:
                                break;
                            }
                        }
                        else
                        {
                            switch (this.state)
                            {
                            case MotionGestureRecognizerState.MotionGestureRecognizerStateBegan:
                                if (decelerationCounter > 1)
                                {
                                    decelerationCounter = 0;
                                    this.state          = MotionGestureRecognizerState.MotionGestureRecognizerStateEnded;
                                }
                                else
                                {
                                    decelerationCounter++;
                                }
                                break;

                            case MotionGestureRecognizerState.MotionGestureRecognizerStateChanged:
                                if (decelerationCounter > 1)
                                {
                                    decelerationCounter = 0;
                                    this.state          = MotionGestureRecognizerState.MotionGestureRecognizerStateEnded;
                                }
                                else
                                {
                                    decelerationCounter++;
                                }
                                break;

                            case MotionGestureRecognizerState.MotionGestureRecognizerStateEnded:
                                this.state = MotionGestureRecognizerState.MotionGestureRecognizerStatePossible;
                                break;

                            default:
                                break;
                            }
                        }

                        //Callback
                        callback();
                    }
                }
            }
        }
        private Boolean velocityHighEnough(MotionAverages averages)
        {
            if (this.possibleDirections.HasFlag(MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionRight))
            {
                if (averages.velocityAverage.x < -swipeMaximum * minimumSwipeThreshold)
                {
                    direction = MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionRight;
                    return true;
                }
            }
            if (this.possibleDirections.HasFlag(MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionLeft))
            {
                if (averages.velocityAverage.x > swipeMaximum * minimumSwipeThreshold)
                {
                    direction = MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionLeft;
                    return true;
                }
            }
            if (this.possibleDirections.HasFlag(MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionDown))
            {
                if (averages.velocityAverage.y < -swipeMaximum * minimumSwipeThreshold)
                {
                    direction = MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionDown;
                    return true;
                }
            }
            if (this.possibleDirections.HasFlag(MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionUp))
            {
                if (averages.velocityAverage.y > swipeMaximum * minimumSwipeThreshold)
                {
                    direction = MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionUp;
                    return true;
                }
            }
            if (this.possibleDirections.HasFlag(MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionIn))
            {
                if (averages.velocityAverage.z < -swipeMaximum * minimumSwipeThreshold)
                {
                    direction = MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionIn;
                    return true;
                }
            }
            if (this.possibleDirections.HasFlag(MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionOut))
            {
                if (averages.velocityAverage.z > swipeMaximum * minimumSwipeThreshold)
                {
                    direction = MotionSwipeGestureRecognizerDirection.MotionSwipeGestureRecognizerDirectionOut;
                    return true;
                }
            }
         

            return false;
        }