Search code examples
c#kinectnui

multiple hits in loop after the break command


I've got a strange problem. I'm creating a NUI for application and I binded some simple gestures to right and left arrow. The problem is when I start application. When I make gesture for the first time my application is hitting 2 times in a row. After that it works 100% as I want. Only the start is the problem.

I'm adding two Joints and timestamp to my history struct which is put into the ArrayList

        this._history.Add(new HistoryItem()
            {
                timestamp = timestamp,
                activeHand = hand,
                controlJoint = controlJoint
            }
        );

then in foreach loop I'm comparing data

if (Math.Abs((hand.Position.X - item.controlJoint.Position.X)) < MainWindow.treshold && Math.Abs((hand.Position.Y - item.controlJoint.Position.Y)) < MainWindow.verticalTreshold)

If it hits I instantly break the lopp with

break;

after that I clear the history ArrayList

this._history.Clear();

So I don't get it. Why after the start it hits two times in a row ?

// edit

history ArrayList initialization

private List<HistoryItem> _history = new List<HistoryItem>(16);

in loop

foreach (HistoryItem item in this._history)
        {
         if ((hand.Position.X - item.controlJoint.Position.X) < MainWindow.treshold)
                            {
                                float tmp = (hand.Position.X - controlJoint.Position.X);
                                MainWindow.slideNumber++;
                                this._logger.Log("Next slide: " + MainWindow.slideNumber);
                                this._logger.Log(hand.Position.X + " - " + controlJoint.Position.X + " = " + tmp + " | " + MainWindow.treshold);
                                this.startTime = 0;
                                this.gestureStart = false;
                                answerFlag = true;
                                System.Windows.Forms.SendKeys.SendWait("{Right}");
                                break;
                            }
                }

Now. As you can see i'm breaking here. So this code shouldn't be invoked second time in a row

How this clears something

// edit 2

I'll also add that to get into this part of code the gestureStart flag need to be set to true. As you can see after getting into the 'if' part here I'm setting it to false. So it is impossible that the code instantly can get to this part

// edit 3 WORKING WORKAROUND

I've created kind of workaround. I've added time control. I'm comparing timestamp of invoking the code and timestamp of last gesture recognition. If its too fast ( i meen couple of ms which it impossible to make ) I don't allow to hit an arrow. I'm not sure if it's a perfect solution but it is a working solution


Solution

  • Ok my problem was the code. Ofc a small bug untracable in the debug. I used one function to analyse a history of frames.

    The method was working in 2 modes. I`ve detached that and created 2 diffrent methods, each for each task and now it works great