Example #1
0
        /// <summary>
        /// Attempt to get a frame & track bodies each frame
        /// </summary>
        private bool GetAndRefreshBodyData(BodyFrameArrivedEventArgs e, bool dataReceived)
        {
            // Represents a frame that contains all the computed real-time tracking information
            // about people that are in view of the sensor.
            using (BodyFrame bodyFrame = e.FrameReference.AcquireFrame())
            {
                // Not able to get a frame?
                //if (bodyFrame == null) return !dataReceived;
                //TODO: Can probably get rid of this
                if (bodyFrame == null)
                {
                    kinectState = KinectState.notTracked;
                    Debug.WriteLine("Cannot get Frame Reference");
                }

                // Able to get a frame, but bodies is currently null
                if (bodyFrame != null && this.bodies == null)
                {
                    this.bodies = new Body[bodyFrame.BodyCount];
                    // TODO: Determine the correct Kinect State
                    Debug.WriteLine("Current _body count: " + bodyFrame.BodyCount);
                }
                // Return current status of bodies to Kinect
                bodyFrame?.GetAndRefreshBodyData(this.bodies);
                kinectState = KinectState.bodyTrackedAndIsTracking;
            }
            return(false);
        }
Example #2
0
 void detector_OnGestureDetected(string obj)
 {
     if (obj == "SwipeToRight")
     {
         kinectState = KinectState.MovingCursor;
         if (movingButton != null)
         {
             movingButton.Release();
             movingButton = null;
         }
     }
 }
Example #3
0
 void voiceCommander_OrderDetected(string obj)
 {
     if (obj == "stop")
     {
         kinectState = KinectState.MovingCursor;
         if (movingButton != null)
         {
             movingButton.Release();
             movingButton = null;
         }
     }
 }
Example #4
0
        private void CheckBodies(object sender, BodyFrameArrivedEventArgs e)
        {
            switch (kinectState)
            {
            case KinectState.bodyTrackedAndIsTracking:
                SaveCameraFrame(_body);
                break;

            case KinectState.notTracked:
                //GetAndRefreshBodyData(e, false);
                _body = TrackBodies(_body);
                break;

            default:
                // Set default state
                kinectState = KinectState.notTracked;
                break;
            }
        }
Example #5
0
        private void CheckForCollision()
        {
            //get stackpanel top and left compared to the canvas
            double stackPanelLeft = Canvas.GetLeft(lettersStackPanel);
            double stackPanelTop  = Canvas.GetTop(lettersStackPanel);

            //get moving letter's top and left
            double letterLeft = Canvas.GetLeft(movingButton);
            double letterTop  = Canvas.GetTop(movingButton);

            int width = 0;

            //check for collision
            for (int i = 0; i < lettersStackPanel.Children.Count; i++)
            {
                Border b = lettersStackPanel.Children[i] as Border;
                if (staticLetters[i].IsMatched)
                {
                    width += (int)b.Width;
                    continue; //already matched
                }


                double distance = Math.Sqrt(Math.Pow(stackPanelLeft + width - letterLeft, 2) + Math.Pow(stackPanelTop - letterTop, 2));

                StaticLetter   staticLetter   = staticLetters.Where(x => x.Border == b).Single();
                MoveableLetter moveableLetter = movingLetters.Where(x => x.HoverButton == movingButton).Single();

                if (distance < Utilities.DistanceForSucking && staticLetter.Index == moveableLetter.Index)  //if it's the same letter
                {
                    kinectState = KinectState.MovingCursor;
                    PiecesMatched(staticLetter, moveableLetter);
                    return;
                }
                width += (int)b.Width;
            }
        }
Example #6
0
 void HoButton_Click(object sender, RoutedEventArgs e)
 {
     //user has captured a letter
     kinectState  = KinectState.MovingObject;
     movingButton = sender as HoverButton;
 }
        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            if (this.skeleton.TrackingState == SkeletonTrackingState.Tracked)
            {
                // 内積を計算して類似度を取得
                int SetSimilarity = (int)(MyMath.Dot(JointType.ShoulderLeft, JointType.HandLeft, this.skeleton, this.poseSet) * 100);
                int ChargeSimilarity = (int)(MyMath.Dot(JointType.ShoulderLeft, JointType.HandLeft, this.skeleton, this.poseCharge) * 100);
                int AimSimilarity = (int)(MyMath.Dot(JointType.ShoulderLeft, JointType.HandLeft, this.skeleton, this.poseAim) * 100);
                int ShootSimilarity = (int)(MyMath.Dot(JointType.ShoulderLeft, JointType.HandLeft, this.skeleton, this.poseShoot) * 100);

                // 状態遷移
            #if false
                switch (this.nowState)
                {
                    case State.None:

                        if (kamaeSimilarity >= this.slider1.Value)
                        {
                            this.counter++;

                            if (this.counter >= slider4.Value)
                            {
                                this.counter = 0;
                                this.nowState = State.Kamae;
                            }
                        }
                        break;

                    case State.Kamae:

                        if (pose1Similarity >= this.slider2.Value)
                            this.nowState = State.Up;

                        if (pose2Similarity >= this.slider3.Value)
                            this.nowState = State.Down;
                        break;

                    case State.Up:
                    case State.Down:

                        this.counter++;

                        if (this.counter > this.slider4.Value)
                        {
                            this.counter = 0;
                            this.nowState = State.None;
                        }

                        break;

                    default:
                        break;
                }

                // 表示
                this.label4.Content = kamaeSimilarity + " / " + this.slider1.Value.ToString("F0");
                this.label5.Content = pose1Similarity + " / " + this.slider1.Value.ToString("F0");
                this.label6.Content = pose2Similarity + " / " + this.slider1.Value.ToString("F0");
            #endif

                // 骨格を描画
                for (int i = 0; i < this.skeleton.Joints.Count; i++)
                {
                    // 3次元座標を2次元座標に変換
                    cip[i] = this.sensor.CoordinateMapper.MapSkeletonPointToColorPoint(this.skeleton.Joints[(JointType)i].Position, ColorImageFormat.RgbResolution640x480Fps30);

                    // 描画範囲に合わせ収まるように縮小
                    cip[i].X /= 4;
                    cip[i].Y /= 4;
                }

                // 骨格情報をセット
                // 今は上半身のみ
                this.SetBonePoint(0, 3, 2);
                this.SetBonePoint(1, 2, 4);
                this.SetBonePoint(2, 4, 5);
                this.SetBonePoint(3, 5, 6);
                this.SetBonePoint(4, 6, 7);
                this.SetBonePoint(5, 2, 8);
                this.SetBonePoint(6, 8, 9);
                this.SetBonePoint(7, 9, 10);
                this.SetBonePoint(8, 10, 11);
            }
            else
            {
            #if false
                this.label4.Content = this.slider1.Value.ToString("F0");
                this.label5.Content = this.slider2.Value.ToString("F0");
                this.label6.Content = this.slider3.Value.ToString("F0");
                this.label8.Content = this.slider4.Value.ToString("F0") + "[ms]";
            #endif
                //MainWindow.getInstance.Shoot(800, 420, -50, -4);
            }

            //            this.label9.Content = "認識状態:" + this.nowState.ToString();
        }
        public void Reset()
        {
            nowState = KinectState.Other;

            RemoveBone();
            counter = 0;
        }
        public KinectState input()
        {
            if (this.skeleton.TrackingState == SkeletonTrackingState.Tracked)
            {
                // 内積を計算して類似度を取得
                // 構え1左肩→左手・左肘→左手・右肩→右手・右肘→右手
                int SetSimilarity    = (int)(
                    MyMath.Dot(JointType.ShoulderLeft, JointType.HandLeft, this.skeleton, this.poseSet) * 30
                    + MyMath.Dot(JointType.ElbowLeft , JointType.HandLeft, this.skeleton, this.poseSet) * 33
                    + MyMath.Dot(JointType.ShoulderRight, JointType.HandRight, this.skeleton, this.poseSet) * 20
                    + MyMath.Dot(JointType.ElbowRight   , JointType.HandRight, this.skeleton, this.poseSet) * 17
                    );
                // 構え2(引くまで)たぶん使わない
                int ChargeSimilarity = (int)(MyMath.Dot(JointType.ShoulderLeft, JointType.HandLeft, this.skeleton, this.poseCharge) * 100);
                // 狙いをつける これもたぶん使わない
                int AimSimilarity    = (int)(MyMath.Dot(JointType.ShoulderLeft, JointType.HandLeft, this.skeleton, this.poseAim) * 100);
                // 放つ たぶんこいつも・・・
                int ShootSimilarity  = (int)(MyMath.Dot(JointType.ShoulderLeft, JointType.HandLeft, this.skeleton, this.poseShoot) * 100);

                // 骨格を描画
                for (int i = 0; i < this.skeleton.Joints.Count; i++)
                {
                    // 3次元座標を2次元座標に変換
                    cip[i] = this.sensor.CoordinateMapper.MapSkeletonPointToColorPoint(this.skeleton.Joints[(JointType)i].Position, ColorImageFormat.RgbResolution640x480Fps30);

                    // 描画範囲に合わせ収まるように縮小
                    cip[i].X /= 4;
                    cip[i].Y /= 4;
                }

                // 骨格情報をセット
                // 今は上半身のみ
                this.SetBonePoint(0, 3, 2);   // 頭→肩中
                this.SetBonePoint(1, 2, 4);   // 肩中→左肩
                this.SetBonePoint(2, 4, 5);   // 左肩→左肘
                this.SetBonePoint(3, 5, 6);   // 左肘→左手
                this.SetBonePoint(4, 6, 7);   // 左手→?
                this.SetBonePoint(5, 2, 8);   // 肩中→右肩
                this.SetBonePoint(6, 8, 9);   // 右肩→右肘
                this.SetBonePoint(7, 9, 10);  // 右肘→右手
                this.SetBonePoint(8, 10, 11); // 右手→?
                this.SetBonePoint(9, 2, 1);   // 肩中→腰
                this.SetBonePoint(10, 1, 0);  // 腰→尻
                this.SetBonePoint(11, 0, 12); // 尻→左尻
                this.SetBonePoint(12, 12, 13);// 左尻→左膝
                this.SetBonePoint(13, 13, 14);// 左膝→左足
                this.SetBonePoint(14, 14, 15);//
                this.SetBonePoint(15, 0, 16); // 尻→右尻
                this.SetBonePoint(16, 16, 17);// 右尻→右膝
                this.SetBonePoint(17, 17, 18);// 右膝→右足
                this.SetBonePoint(18, 18, 19);//

                this.bones[19].Stroke = alphaColor;
                // 状態遷移
            #if false
                switch (this.nowState)
                {
                    case KinectState.Other:
                        this.nowState = KinectState.None;
                        Bow.getInstance.Idling(new Point(bones[4].X1, bones[4].Y1));
                        break;
                    case KinectState.None:
                        Bow.getInstance.Idling(new Point(bones[4].X1, bones[4].Y1));

                        if (SetSimilarity >= 90)
                        {
                            this.counter++;

                            if (this.counter >= 20)
                            {
                                this.counter = 5;
                                this.nowState = KinectState.Set;
                            }
                        }
                        else {
                            this.counter = 0;
                        }
                        break;

                    case KinectState.Set:
                        Bow.getInstance.Drawing(new Point(bones[4].X1, bones[4].Y1), new Point(bones[8].X1, bones[8].Y1));
                        // 左肘が伸びているか
                        if (MyMath.Dot(new Point(bones[3].X1, bones[3].Y1), new Point(bones[4].X1, bones[4].Y1), new Point(bones[2].X1, bones[2].Y1)) < -0.4f) {
                            // 右肘が曲がっているか
                            if (MyMath.Dot(new Point(bones[7].X1, bones[7].Y1), new Point(bones[8].X1, bones[8].Y1), new Point(bones[4].X1, bones[4].Y1)) > 0.1f) {
                                // 現在の位置を保存する
                                fromShoot.X = bones[8].X1; fromShoot.Y = bones[8].Y1;
                                toShoot.X = bones[4].X1; toShoot.Y = bones[4].Y1;

                                //this.SetBonePoint(19, 6, 10);
                                this.SetBonePoint(19, toShoot, fromShoot);

                                counter = (counter < 10) ? 10 : counter;
                            }
                        }
                        else {
                            counter--;
                            if (counter < 0) {
                                counter = 0;
                                this.nowState = KinectState.None;
                            }
                        }

            #if false
                        if (MyMath.PointToLength(fromShoot, toShoot) > 34) {
                            counter++;
                            if (counter > 30) {
                                this.counter = 5;
                                this.nowState = State.Charge;
                                //MainWindow.getInstance.Shoot((float)toShoot.X, (float)toShoot.Y, (float)(toShoot.X - fromShoot.X) * 2.0f, (float)(toShoot.Y - fromShoot.Y) * 2.0f);
                            }
                        }
            #else
                        if (MyMath.PointToLength(fromShoot, toShoot) >
                            MyMath.PointToLength(
                            new Point(bones[3].X1, bones[3].Y1)
                            , new Point(bones[3].X2, bones[3].Y2)
                            ) * 3.1f)
                        {
                            counter++;
                            if (counter > 30) {
                                this.counter = 5;
                                this.nowState = KinectState.Charge;
                                //MainWindow.getInstance.Shoot((float)toShoot.X, (float)toShoot.Y, (float)(toShoot.X - fromShoot.X) * 2.0f, (float)(toShoot.Y - fromShoot.Y) * 2.0f);
                            }
                        }
            #endif

                        break;

                    case KinectState.Charge:
                        Bow.getInstance.Drawing(new Point(bones[4].X1, bones[4].Y1), new Point(bones[8].X1, bones[8].Y1));
                        //float d = -1.0f;

                        //d = MyMath.PointToLength(fromShoot, new Point(bones[8].X1, bones[8].Y1));
                        float fsL, tsL;
                        tsL = MyMath.PointToLength(toShoot, new Point(bones[4].X1, bones[4].Y1));
                        fsL = MyMath.PointToLength(fromShoot, new Point(bones[8].X1, bones[8].Y1));

                        if (MyMath.Dot(new Point(bones[3].X1, bones[3].Y1), new Point(bones[4].X1, bones[4].Y1), new Point(bones[2].X1, bones[2].Y1)) < -0.5f) {
                            // 右肘が曲がっているか
                            if (MyMath.Dot(new Point(bones[7].X1, bones[7].Y1), new Point(bones[8].X1, bones[8].Y1), new Point(bones[4].X1, bones[4].Y1)) > 0.26f) {
                                // 現在の位置を保存する
                                //float fsL, tsL;
                                if (tsL > 0.9f && fsL > 0.6f && fsL < 6.5f) {
                                    // 動いた場合に移動する処理を行う
                                    fromShoot.X = bones[8].X1; fromShoot.Y = bones[8].Y1;
                                    toShoot.X = bones[4].X1; toShoot.Y = bones[4].Y1;
                                    counter = 20;
                                }
                                else {
                                    // 動かなかった場合にインクリメント
                                    counter++;
                                }
                                //this.SetBonePoint(19, 6, 10);
                                //this.SetBonePoint(19, toShoot, fromShoot);

                                counter = (counter < 20) ? 20 : counter;
                            }
                        }
                        else {
                            counter--;
                            if (counter < 0) {
                                counter = 0;
                                this.nowState = KinectState.None;
                            }
                        }

                        if (counter > 100) {
                            this.nowState = KinectState.Aim;
                        }
                        if (fsL > 4.9f && tsL < 3.0f) {
                            this.nowState = KinectState.Shoot;
                        }

                        break;
                    case KinectState.Aim:
                        Bow.getInstance.Drawing(new Point(bones[4].X1, bones[4].Y1), new Point(bones[8].X1, bones[8].Y1));
                        float a = -1.0f;

                        a = MyMath.PointToLength(fromShoot, new Point(bones[8].X1, bones[8].Y1));

                        if (MyMath.Dot(new Point(bones[2].X1, bones[2].Y1), new Point(bones[2].X1, bones[2].Y1 + 2.0f), new Point(bones[4].X1, bones[4].Y1)) < -0.5f) {
                            counter = 0;
                            this.nowState = KinectState.None;
                        }

                        // 左肘が伸びているか
                        if (MyMath.Dot(new Point(bones[3].X1, bones[3].Y1), new Point(bones[4].X1, bones[4].Y1), new Point(bones[2].X1, bones[2].Y1)) < -0.5f) {
                            // 右肘が曲がっているか
                            if (MyMath.Dot(new Point(bones[7].X1, bones[7].Y1), new Point(bones[8].X1, bones[8].Y1), new Point(bones[4].X1, bones[4].Y1)) > 0.26f) {
                                // 現在の位置を保存する
                                //float fsL, tsL;
                                fsL = a;
                                if ((tsL = MyMath.PointToLength(toShoot, new Point(bones[4].X1, bones[4].Y1))) > 3.2f) {
                                    fromShoot.X = bones[8].X1; fromShoot.Y = bones[8].Y1;
                                    toShoot.X = bones[4].X1; toShoot.Y = bones[4].Y1;
                                    this.nowState = KinectState.Charge;
                                }
                                else {
                                    counter++;
                                }
                                //this.SetBonePoint(19, 6, 10);
                                //this.SetBonePoint(19, toShoot, fromShoot);

                                //counter++;
                            }
                        }
                        else {
                            counter = 0;
                            this.nowState = KinectState.None;
                        }

            #if false
                        if (this.counter > 60)
                        {
                            this.counter = 0;
                            this.nowState = State.None;
                            MainWindow.getInstance.Shoot((float)toShoot.X, (float)toShoot.Y, (float)(toShoot.X - fromShoot.X) * 2.0f, (float)(toShoot.Y - fromShoot.Y) * 2.0f);
                        }
            #else
                        if (counter > 1 && a < 1.8f) {
                            this.counter = 0;
                            this.nowState = KinectState.Shoot;
                            //MainWindow.getInstance.Shoot((float)toShoot.X, (float)toShoot.Y, (float)(toShoot.X - fromShoot.X) * 2.0f, (float)(toShoot.Y - fromShoot.Y) * 2.0f);
                        }
            #endif

                        break;

                    case KinectState.Shoot:
                        Bow.getInstance.Drawing(new Point(bones[4].X1, bones[4].Y1), new Point(bones[8].X1, bones[8].Y1));
                        this.counter = 0;
                        this.nowState = KinectState.None;
                        MainWindow.getInstance.Shoot((float)toShoot.X, (float)toShoot.Y, (float)(toShoot.X - fromShoot.X) * 2.0f, (float)(toShoot.Y - fromShoot.Y) * 2.0f);
                        break;

                    default:
                        break;
                }
            #else
                recastTime--;
                float cal = 0.0f; // 雑用変数
                // 一部の状態を除いて腕が肩より上か手が腰より下にある場合はIdleに戻す
                if (!(nowState == KinectState.Other || nowState == KinectState.MuscleAtack || nowState == KinectState.AraburuTakaNoPose))
                if (
                    (bones[7].Y1 < bones[6].Y1 && bones[3].Y1 < bones[2].Y1)
                    || (bones[8].Y1 > bones[11].Y1 + 8 || bones[4].Y1 > bones[11].Y1 + 8)
                    )
                {
                    nowState = KinectState.Idle;
                }
                switch (nowState) {
                    case KinectState.Other:
                        nowState = KinectState.Idle;
                        counter = 0;
                        break;
                    case KinectState.Idle:
                        // -> move
                        if (
                            (bones[7].Y1 > bones[1].Y1 && bones[3].Y1 > bones[1].Y1) // 肘が肩より下
                            && (bones[8].Y1 > bones[7].Y1 && bones[4].Y1 > bones[3].Y1) // 両手が肘より下
                            && ((cal = MyMath.Dot(new Point(bones[8].X1, bones[8].Y1), new Point(bones[1].X1, bones[1].Y1), new Point(bones[7].X1, bones[7].Y1))) < 0.8f && cal > -0.5f) // 右腕の肘を上げて手を腰に当ててる状態
                            && ((cal = MyMath.Dot(new Point(bones[4].X1, bones[4].Y1), new Point(bones[1].X1, bones[1].Y1), new Point(bones[3].X1, bones[3].Y1))) < 0.8f && cal > -0.5f) // 左腕の肘を上げて手を腰に当ててる状態
                            )
                        {
                            counter++;
                            if (counter > 32) {
                                nowState = KinectState.MoveSet;
                                counter = 0;
                            }
                        }
                        else {
                            counter = 0;
                        }
                        // -> guard
                        if (
                            (bones[7].Y1 > bones[1].Y1 && bones[3].Y1 > bones[1].Y1) // 肘が肩より下
                            && (bones[8].Y1 < bones[7].Y1 + 3 && bones[4].Y1 < bones[3].Y1 + 5) // 両手が肘よりやや上
                            && (bones[8].X1 < bones[1].X1 && bones[4].X1 > bones[1].X1) // 腕が交差している
                            )
                        {
                            nowState = KinectState.Guard;
                            counter = 0;
                        }
                        // -> Bomb
                        // -> fire
                        if (
                            (bones[7].Y1 < bones[6].Y1 && bones[3].Y1 < bones[2].Y1)
                            && (bones[8].Y1 < bones[7].Y1 && bones[4].Y1 < bones[3].Y1)
                            && (MyMath.Dot(new Point(bones[7].X1, bones[7].Y1), new Point(bones[8].X1, bones[8].Y1), new Point(bones[6].X1, bones[8].Y1)) > -0.4f)
                            && (MyMath.Dot(new Point(bones[3].X1, bones[3].Y1), new Point(bones[4].X1, bones[4].Y1), new Point(bones[2].X1, bones[4].Y1)) > -0.4f)
                            )
                        {
                            if (bones[8].Y1 < bones[0].Y1 && bones[4].Y1 < bones[0].Y1 && bones[14].Y1 < bones[18].Y1 && recastTime < 0)
                            {
                                nowState = KinectState.AraburuTakaNoPose;
                                recastTime = 300;
                            }
                            else
                            {
                                nowState = KinectState.MuscleAtack;
                            }
                        }
                        break;
                    case KinectState.MoveSet:
                        // -> idle ↑のあれで処理
                        // -> move RL
                        if (
                            (bones[7].X1 > bones[6].X1 && bones[8].X1 > bones[7].X1) // 右腕が外に開いている
                            && (bones[8].Y1 < bones[7].Y1) // 右手が肘より上
                            )
                        {
                            nowState = KinectState.MoveRight;
                            counter = 0;
                        }
                        if (
                            (bones[3].X1 < bones[2].X1 && bones[4].X1 < bones[3].X1) // 左腕が外に開いている
                            && (bones[4].Y1 < bones[3].Y1) // 右手が肘より上
                            )
                        {
                            nowState = KinectState.MoveLeft;
                            counter = 0;
                        }
                        break;
                    case KinectState.MoveRight:
                        // -> idle ↑のあれで処理
                        counter++;
                        if (counter > 150) {
                            nowState = KinectState.Idle;
                            counter = 0;
                        }
                        break;
                    case KinectState.MoveLeft:
                        // -> idle ↑のあれで処理
                        counter++;
                        if (counter > 150) {
                            nowState = KinectState.Idle;
                            counter = 0;
                        }
                        break;
                    case KinectState.Guard:
                        // -> idle ↑のあれで処理
                        // -> parge
                        if (
                            (bones[7].X1 > bones[6].X1 && bones[8].X1 > bones[7].X1) // 右腕が外に開いている
                            && (bones[3].X1 < bones[2].X1 && bones[4].X1 < bones[3].X1) // 左腕が外に開いている
                            && (bones[8].Y1 < bones[7].Y1 && bones[4].Y1 < bones[3].Y1) // 左右の手が肘より上
                            )
                        {
                            nowState = KinectState.GuardParge;
                            counter = 0;
                        }
                        break;
                    case KinectState.GuardParge:
                        // -> idle
                        counter++;
                        if (counter > 100) {
                            nowState = KinectState.Idle;
                            counter = 0;
                        }
                        // -> fire
                        // -> bomb
                        if (
                            (bones[7].Y1 < bones[6].Y1 && bones[3].Y1 < bones[2].Y1)
                            && (bones[8].Y1 < bones[7].Y1 && bones[4].Y1 < bones[3].Y1)
                            && (MyMath.Dot(new Point(bones[7].X1, bones[7].Y1), new Point(bones[8].X1, bones[8].Y1), new Point(bones[6].X1, bones[8].Y1)) > -0.4f)
                            && (MyMath.Dot(new Point(bones[3].X1, bones[3].Y1), new Point(bones[4].X1, bones[4].Y1), new Point(bones[2].X1, bones[4].Y1)) > -0.4f)
                            )
                        {
                            if (bones[8].Y1 < bones[0].Y1 && bones[4].Y1 < bones[0].Y1 && bones[14].Y1 < bones[18].Y1 && recastTime < 0) {
                                nowState = KinectState.AraburuTakaNoPose;
                                counter = 0;
                                recastTime = 300;
                            }
                            else {
                                nowState = KinectState.MuscleAtack;
                                counter = 0;
                            }
                        }
                        break;
                    case KinectState.MuscleAtack:
                        //counter++;
                        if (
                            (bones[7].Y1 < bones[6].Y1 && bones[3].Y1 < bones[2].Y1)
                            && (bones[8].Y1 < bones[7].Y1 && bones[4].Y1 < bones[3].Y1)
                            && (MyMath.Dot(new Point(bones[7].X1, bones[7].Y1), new Point(bones[8].X1, bones[8].Y1), new Point(bones[6].X1, bones[8].Y1)) > -0.4f)
                            && (MyMath.Dot(new Point(bones[3].X1, bones[3].Y1), new Point(bones[4].X1, bones[4].Y1), new Point(bones[2].X1, bones[4].Y1)) > -0.4f)
                            )
                        {
                            if (bones[8].Y1 < bones[0].Y1 && bones[4].Y1 < bones[0].Y1 && bones[14].Y1 < bones[18].Y1 && recastTime < 0) {
                                nowState = KinectState.AraburuTakaNoPose;
                                counter = 0;
                                recastTime = 300;
                            }
                            else {
                                Player.getInstance.VectorPath(new Point(bones[4].X1 - bones[1].X1, bones[4].Y1 - bones[1].Y1), new Point(bones[8].X1 - bones[1].X1, bones[8].Y1 - bones[1].Y1));
                            }
                        }
                        else {
                            nowState = KinectState.Idle;
                            counter = 0;
                        }
                        break;
                    case KinectState.AraburuTakaNoPose:
                        counter++;
                        if (counter > 120) {
                            nowState = KinectState.Idle;
                            counter = 0;
                        }
                        break;
                }
            #endif

            }
            else
            {
            #if false
                this.label4.Content = this.slider1.Value.ToString("F0");
                this.label5.Content = this.slider2.Value.ToString("F0");
                this.label6.Content = this.slider3.Value.ToString("F0");
                this.label8.Content = this.slider4.Value.ToString("F0") + "[ms]";
            #endif
            //                MainWindow.getInstance.Shoot(800, 420, -50, -4);

                RemoveBone();
                Bow.getInstance.unDraw();
                return KinectState.Other;
            }

            //            this.label9.Content = "認識状態:" + this.nowState.ToString();
            return nowState;
        }
        public bool Initialize()
        {
            // インスタンスを代入
            getInstance = this;

            // 接続されているセンサを捜索
            foreach (var potentialSensor in KinectSensor.KinectSensors)
            {
                if (potentialSensor.Status == KinectStatus.Connected)
                {
                    this.sensor = potentialSensor;
                    break;
                }
            }

            if (null != this.sensor)
            {
                // SkeletonStreamを有効にする
                this.sensor.SkeletonStream.Enable();

                // SkeletonStreamにイベントを追加
                this.sensor.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(sensor_SkeletonFrameReady);

                // Turn on the depth stream to receive depth frames
                this.sensor.DepthStream.Enable(DepthFormat);

                this.depthWidth = this.sensor.DepthStream.FrameWidth;

                this.depthHeight = this.sensor.DepthStream.FrameHeight;

                this.sensor.ColorStream.Enable(ColorFormat);

                int colorWidth = this.sensor.ColorStream.FrameWidth;
                int colorHeight = this.sensor.ColorStream.FrameHeight;

                this.colorToDepthDivisor = colorWidth / this.depthWidth;

                // Turn on to get player masks
                this.sensor.SkeletonStream.Enable();

                // Allocate space to put the depth pixels we'll receive
                this.depthPixels = new DepthImagePixel[this.sensor.DepthStream.FramePixelDataLength];

                // Allocate space to put the color pixels we'll create
                this.colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength];

                this.playerPixelData = new int[this.sensor.DepthStream.FramePixelDataLength];

                this.colorCoordinates = new ColorImagePoint[this.sensor.DepthStream.FramePixelDataLength];

                // This is the bitmap we'll display on-screen
                this.colorBitmap = new WriteableBitmap(colorWidth, colorHeight, 96.0, 96.0, PixelFormats.Bgr32, null);

                // Set the image we display to point to the bitmap where we'll put the image data
                // Image に設定する
                //this.MaskedImage.Source = this.colorBitmap;

                // Add an event handler to be called whenever there is new depth frame data
                this.sensor.AllFramesReady += this.SensorAllFramesReady;

                // センサを起動
                try
                {
                    this.sensor.Start();
                }
                catch (IOException)
                {
                    this.sensor = null;
                }
            }
            else {
                //CompositionTarget.Rendering += UpdateImage;
            }

            // インスタンス化
            this.skeleton = new Skeleton();

            // スレッドでの呼び出し優先度指定
            this.dispatcherTimer = new DispatcherTimer();

            // 1秒ごとに処理()
            this.dispatcherTimer.Interval = new TimeSpan(100000);

            // イベント追加
            this.dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

            // タイマー動作開始
            //this.dispatcherTimer.Start();

            this.poseSet    = new Vector4[20];
            this.poseCharge = new Vector4[20];
            this.poseAim    = new Vector4[20];
            this.poseShoot  = new Vector4[20];

            try
            {
                this.poseSet    = MotionIO.LoadJoint("poseSet");
                this.poseCharge = MotionIO.LoadJoint("poseCharge");
                this.poseAim    = MotionIO.LoadJoint("poseAim");
                this.poseShoot  = MotionIO.LoadJoint("poseShoot");
            }
            catch (Exception ex)
            {
                Console.WriteLine("読み込み失敗");
            }

            playerColor = Brushes.ForestGreen;
            alphaColor = new SolidColorBrush(Color.FromArgb((byte)0, (byte)255, (byte)255, (byte)255));

            this.bones = new Line[20];
            for (int i = 0; i < this.bones.Length; i++)
            {
                this.bones[i] = new Line();

                // 描画設定
                this.bones[i].HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                this.bones[i].VerticalAlignment = System.Windows.VerticalAlignment.Top;
                this.bones[i].StrokeThickness = 4;
                this.bones[i].Stroke = playerColor;

                // Canvasに追加
                MainWindow.mainCanvas.Children.Add(this.bones[i]);
            }

            nowState = KinectState.Other;

            fromShoot = new Point();
            toShoot = new Point();

            recastTime = -1;

            return true;
        }
        public KinectState input()
        {
            if (this.skeleton.TrackingState == SkeletonTrackingState.Tracked)
            {
                // 内積を計算して類似度を取得
                // 構え1左肩→左手・左肘→左手・右肩→右手・右肘→右手
                int SetSimilarity    = (int)(
                    MyMath.Dot(JointType.ShoulderLeft, JointType.HandLeft, this.skeleton, this.poseSet) * 30
                    + MyMath.Dot(JointType.ElbowLeft , JointType.HandLeft, this.skeleton, this.poseSet) * 33
                    + MyMath.Dot(JointType.ShoulderRight, JointType.HandRight, this.skeleton, this.poseSet) * 20
                    + MyMath.Dot(JointType.ElbowRight   , JointType.HandRight, this.skeleton, this.poseSet) * 17
                    );
                // 構え2(引くまで)たぶん使わない
                int ChargeSimilarity = (int)(MyMath.Dot(JointType.ShoulderLeft, JointType.HandLeft, this.skeleton, this.poseCharge) * 100);
                // 狙いをつける これもたぶん使わない
                int AimSimilarity    = (int)(MyMath.Dot(JointType.ShoulderLeft, JointType.HandLeft, this.skeleton, this.poseAim) * 100);
                // 放つ たぶんこいつも・・・
                int ShootSimilarity  = (int)(MyMath.Dot(JointType.ShoulderLeft, JointType.HandLeft, this.skeleton, this.poseShoot) * 100);

                // 骨格を描画
                for (int i = 0; i < this.skeleton.Joints.Count; i++)
                {
                    // 3次元座標を2次元座標に変換
                    cip[i] = this.sensor.CoordinateMapper.MapSkeletonPointToColorPoint(this.skeleton.Joints[(JointType)i].Position, ColorImageFormat.RgbResolution640x480Fps30);

                    // 描画範囲に合わせ収まるように縮小
                    cip[i].X /= 4;
                    cip[i].Y /= 4;
                }

                // 骨格情報をセット
                // 今は上半身のみ
                this.SetBonePoint(0, 3, 2);   // 頭→肩中
                this.SetBonePoint(1, 2, 4);   // 肩中→左肩
                this.SetBonePoint(2, 4, 5);   // 左肩→左肘
                this.SetBonePoint(3, 5, 6);   // 左肘→左手
                this.SetBonePoint(4, 6, 7);   // 左手→?
                this.SetBonePoint(5, 2, 8);   // 肩中→右肩
                this.SetBonePoint(6, 8, 9);   // 右肩→右肘
                this.SetBonePoint(7, 9, 10);  // 右肘→右手
                this.SetBonePoint(8, 10, 11); // 右手→?
                this.SetBonePoint(9, 2, 1);   // 肩中→腰
                this.SetBonePoint(10, 1, 0);  // 腰→尻
                this.SetBonePoint(11, 0, 12); // 尻→左尻
                this.SetBonePoint(12, 12, 13);// 左尻→左膝
                this.SetBonePoint(13, 13, 14);// 左膝→左足
                this.SetBonePoint(14, 14, 15);//
                this.SetBonePoint(15, 0, 16); // 尻→右尻
                this.SetBonePoint(16, 16, 17);// 右尻→右膝
                this.SetBonePoint(17, 17, 18);// 右膝→右足
                this.SetBonePoint(18, 18, 19);//

                this.bones[19].Stroke = alphaColor;
                // 状態遷移
            #if true
                switch (this.nowState)
                {
                    case KinectState.Other:
                        this.nowState = KinectState.None;
                        Bow.getInstance.Idling(new Point(bones[4].X1, bones[4].Y1));
                        break;
                    case KinectState.None:
                        Bow.getInstance.Idling(new Point(bones[4].X1, bones[4].Y1));

                        if (SetSimilarity >= 90)
                        {
                            this.counter++;

                            if (this.counter >= 20)
                            {
                                this.counter = 5;
                                this.nowState = KinectState.Set;
                            }
                        }
                        else {
                            this.counter = 0;
                        }
                        break;

                    case KinectState.Set:
                        Bow.getInstance.Drawing(new Point(bones[4].X1, bones[4].Y1), new Point(bones[8].X1, bones[8].Y1));
                        // 左肘が伸びているか
                        if (MyMath.Dot(new Point(bones[3].X1, bones[3].Y1), new Point(bones[4].X1, bones[4].Y1), new Point(bones[2].X1, bones[2].Y1)) < -0.4f) {
                            // 右肘が曲がっているか
                            if (MyMath.Dot(new Point(bones[7].X1, bones[7].Y1), new Point(bones[8].X1, bones[8].Y1), new Point(bones[4].X1, bones[4].Y1)) > 0.1f) {
                                // 現在の位置を保存する
                                fromShoot.X = bones[8].X1; fromShoot.Y = bones[8].Y1;
                                toShoot.X = bones[4].X1; toShoot.Y = bones[4].Y1;

                                //this.SetBonePoint(19, 6, 10);
                                this.SetBonePoint(19, toShoot, fromShoot);

                                counter = (counter < 10) ? 10 : counter;
                            }
                        }
                        else {
                            counter--;
                            if (counter < 0) {
                                counter = 0;
                                this.nowState = KinectState.None;
                            }
                        }

            #if false
                        if (MyMath.PointToLength(fromShoot, toShoot) > 34) {
                            counter++;
                            if (counter > 30) {
                                this.counter = 5;
                                this.nowState = State.Charge;
                                //MainWindow.getInstance.Shoot((float)toShoot.X, (float)toShoot.Y, (float)(toShoot.X - fromShoot.X) * 2.0f, (float)(toShoot.Y - fromShoot.Y) * 2.0f);
                            }
                        }
            #else
                        if (MyMath.PointToLength(fromShoot, toShoot) >
                            MyMath.PointToLength(
                            new Point(bones[3].X1, bones[3].Y1)
                            , new Point(bones[3].X2, bones[3].Y2)
                            ) * 3.1f)
                        {
                            counter++;
                            if (counter > 30) {
                                this.counter = 5;
                                this.nowState = KinectState.Charge;
                                //MainWindow.getInstance.Shoot((float)toShoot.X, (float)toShoot.Y, (float)(toShoot.X - fromShoot.X) * 2.0f, (float)(toShoot.Y - fromShoot.Y) * 2.0f);
                            }
                        }
            #endif

                        break;

                    case KinectState.Charge:
                        Bow.getInstance.Drawing(new Point(bones[4].X1, bones[4].Y1), new Point(bones[8].X1, bones[8].Y1));
                        //float d = -1.0f;

                        //d = MyMath.PointToLength(fromShoot, new Point(bones[8].X1, bones[8].Y1));
                        float fsL, tsL;
                        tsL = MyMath.PointToLength(toShoot, new Point(bones[4].X1, bones[4].Y1));
                        fsL = MyMath.PointToLength(fromShoot, new Point(bones[8].X1, bones[8].Y1));

                        if (MyMath.Dot(new Point(bones[3].X1, bones[3].Y1), new Point(bones[4].X1, bones[4].Y1), new Point(bones[2].X1, bones[2].Y1)) < -0.5f) {
                            // 右肘が曲がっているか
                            if (MyMath.Dot(new Point(bones[7].X1, bones[7].Y1), new Point(bones[8].X1, bones[8].Y1), new Point(bones[4].X1, bones[4].Y1)) > 0.26f) {
                                // 現在の位置を保存する
                                //float fsL, tsL;
                                if (tsL > 0.9f && fsL > 0.6f && fsL < 6.5f) {
                                    // 動いた場合に移動する処理を行う
                                    fromShoot.X = bones[8].X1; fromShoot.Y = bones[8].Y1;
                                    toShoot.X = bones[4].X1; toShoot.Y = bones[4].Y1;
                                    counter = 20;
                                }
                                else {
                                    // 動かなかった場合にインクリメント
                                    counter++;
                                }
                                //this.SetBonePoint(19, 6, 10);
                                //this.SetBonePoint(19, toShoot, fromShoot);

                                counter = (counter < 20) ? 20 : counter;
                            }
                        }
                        else {
                            counter--;
                            if (counter < 0) {
                                counter = 0;
                                this.nowState = KinectState.None;
                            }
                        }

                        if (counter > 100) {
                            this.nowState = KinectState.Aim;
                        }
                        if (fsL > 4.9f && tsL < 3.0f) {
                            this.nowState = KinectState.Shoot;
                        }

                        break;
                    case KinectState.Aim:
                        Bow.getInstance.Drawing(new Point(bones[4].X1, bones[4].Y1), new Point(bones[8].X1, bones[8].Y1));
                        float a = -1.0f;

                        a = MyMath.PointToLength(fromShoot, new Point(bones[8].X1, bones[8].Y1));

                        if (MyMath.Dot(new Point(bones[2].X1, bones[2].Y1), new Point(bones[2].X1, bones[2].Y1 + 2.0f), new Point(bones[4].X1, bones[4].Y1)) < -0.5f) {
                            counter = 0;
                            this.nowState = KinectState.None;
                        }

                        // 左肘が伸びているか
                        if (MyMath.Dot(new Point(bones[3].X1, bones[3].Y1), new Point(bones[4].X1, bones[4].Y1), new Point(bones[2].X1, bones[2].Y1)) < -0.5f) {
                            // 右肘が曲がっているか
                            if (MyMath.Dot(new Point(bones[7].X1, bones[7].Y1), new Point(bones[8].X1, bones[8].Y1), new Point(bones[4].X1, bones[4].Y1)) > 0.26f) {
                                // 現在の位置を保存する
                                //float fsL, tsL;
                                fsL = a;
                                if ((tsL = MyMath.PointToLength(toShoot, new Point(bones[4].X1, bones[4].Y1))) > 3.2f) {
                                    fromShoot.X = bones[8].X1; fromShoot.Y = bones[8].Y1;
                                    toShoot.X = bones[4].X1; toShoot.Y = bones[4].Y1;
                                    this.nowState = KinectState.Charge;
                                }
                                else {
                                    counter++;
                                }
                                //this.SetBonePoint(19, 6, 10);
                                //this.SetBonePoint(19, toShoot, fromShoot);

                                //counter++;
                            }
                        }
                        else {
                            counter = 0;
                            this.nowState = KinectState.None;
                        }

            #if false
                        if (this.counter > 60)
                        {
                            this.counter = 0;
                            this.nowState = State.None;
                            MainWindow.getInstance.Shoot((float)toShoot.X, (float)toShoot.Y, (float)(toShoot.X - fromShoot.X) * 2.0f, (float)(toShoot.Y - fromShoot.Y) * 2.0f);
                        }
            #else
                        if (counter > 1 && a < 1.8f) {
                            this.counter = 0;
                            this.nowState = KinectState.Shoot;
                            //MainWindow.getInstance.Shoot((float)toShoot.X, (float)toShoot.Y, (float)(toShoot.X - fromShoot.X) * 2.0f, (float)(toShoot.Y - fromShoot.Y) * 2.0f);
                        }
            #endif

                        break;

                    case KinectState.Shoot:
                        Bow.getInstance.Drawing(new Point(bones[4].X1, bones[4].Y1), new Point(bones[8].X1, bones[8].Y1));
                        this.counter = 0;
                        this.nowState = KinectState.None;
                        MainWindow.getInstance.Shoot((float)toShoot.X, (float)toShoot.Y, (float)(toShoot.X - fromShoot.X) * 2.0f, (float)(toShoot.Y - fromShoot.Y) * 2.0f);
                        break;

                    default:
                        break;
                }
            #endif

            }
            else
            {
            #if false
                this.label4.Content = this.slider1.Value.ToString("F0");
                this.label5.Content = this.slider2.Value.ToString("F0");
                this.label6.Content = this.slider3.Value.ToString("F0");
                this.label8.Content = this.slider4.Value.ToString("F0") + "[ms]";
            #endif
            //                MainWindow.getInstance.Shoot(800, 420, -50, -4);

                RemoveBone();
                Bow.getInstance.unDraw();
                return KinectState.Other;
            }

            //            this.label9.Content = "認識状態:" + this.nowState.ToString();
            return nowState;
        }