Move.cs 29.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using Kinect = Windows.Kinect;
using System.Runtime.InteropServices;
using System.IO;
using System;
using System.Threading;
using UnityEngine.SceneManagement;
public class Move : MonoBehaviour
{
    //    [DllImport("NtKinectDll")]
    //    private static extern System.IntPtr getKinect();
    //    [DllImport("NtKinectDll")]
    //    private static extern int setSkeleton(System.IntPtr kinect, System.IntPtr data, System.IntPtr state, System.IntPtr id);

    int bodyCount = 6;
    int jointCount = 25;

    //public Material BoneMaterial;
    public GameObject BodySourceManager;
    private Dictionary<ulong, GameObject> _Bodies = new Dictionary<ulong, GameObject>();
    private BodySourceManager _BodyManager;

    public GameObject Trainnner;
    public GameObject Trainnne;
    CharacterSkeleton skeleton_Trainnner;
    CharacterSkeleton skeleton_Trainnne;
    public bool mirror = true;
    public bool move = true;
    private System.IntPtr kinect;

    //lsy
    public string[] lines;
    public string[] lines_right;
    public int trainer_i = -1;
    public int trainer_j = 0;
    public float[] trainer_data1;
    public int[] trainer_state;
    public int speed = 0;
    public int trainer_count = 0;
    public bool trainer_check = true;
    public bool trainer_check_outside = true;
    public float trainer_timer = 0;
    public bool trainer_other_side = false;
    public int trainer_speed = 0;
    //lsy end

    public Text AngleLeftKnee;
    public Text AngleRightKnee;
    public Text KneeToeLeft;
    public Text KneeToeRight;
    public Text LeftLegUp;
    public Text RightLegUp;

    public Text IfSpineIsStraight;
    public Text HipBalance;
    public Text PullHipBack;
    public Text Great;

    string hipbal, leftkneewarning, rightkneewarning, leftsidehighkick, rightsidehighkick;
    string spinestraight, pullhipback, great;

    string exercise;
    int workout_flag, workout_count, workout_flag2;
    bool workout;

    int sidehk_flag, lunge_flag_l, lunge_flag_r;

    private float timer;
    private float flag_timer;
    int flag;

    public GameObject count3;
    public GameObject count2;
    public GameObject count1;
    public GameObject HipBalance1, PullHipBack2, LegUp3, SpineStraight4, Great5, KneeDown6;
    public GameObject ex_start;
    public GameObject Squat_Instruction, SideHighKick_Instruction, Lunge_Left_Instruction, Lunge_Right_Instruction;
    public GameObject Right_Start;

    //lsy
    void Trainer_Run()
    {
        if (!trainer_check)//운동 끝났을 때
        {
            trainer_timer += Time.deltaTime;
            if (trainer_timer > 3)
            {
                Debug.Log("3초 됨 운동 끝!!!!!"); //결과씬이나 다른씬으로
                trainer_check_outside = false;
                SceneManager.LoadScene("Result"); // Result씬으로 이동
            }
            return;
        }
        //Thread.Sleep(50);
        if (trainer_j >= lines.Length - 3)
        {
            if (trainer_count < 20 && exercise == "Squat")
            {
                trainer_count++;
                Debug.Log("카운트 : " + trainer_count);
            }
            if (trainer_count < 10 && exercise == "SideHiKick")
            {
                trainer_count++;
                Debug.Log("카운트 : " + trainer_count);
            }
            if (trainer_count < 10 && exercise == "Lunge")
            {
                trainer_count++;
                Debug.Log("카운트 : " + trainer_count);
            }
            if (trainer_count >= 20 && exercise == "Squat")
            {
                trainer_check = false;//스쿼트일때 운동 끝나는 시점!!!!!
            }
            if (trainer_count >= 10 && exercise == "SideHiKick")
            {
                if (!trainer_other_side)
                {
                    if (trainer_timer > 3)
                    {
                        Right_Start.SetActive(false);

                        trainer_count = 0;
                        lines = lines_right;
                        trainer_other_side = true;
                        trainer_timer = 0;
                    }
                    else
                    {
                        Right_Start.SetActive(true);

                        trainer_timer += Time.deltaTime;
                        return;
                    }
                }
                else
                {
                    trainer_check = false;//사이드하이킥일때 운동 끝나는 시점!!!!!
                }
            }
            if (trainer_count >= 10 && exercise == "Lunge")
            {
                if (!trainer_other_side)
                {
                    if (trainer_timer > 3)
                    {
                        Lunge_Right_Instruction.SetActive(false);

                        trainer_speed = 7;
                        trainer_count = 0;
                        lines = lines_right;
                        trainer_other_side = true;
                        trainer_timer = 0;
                    }
                    else
                    {
                        Lunge_Right_Instruction.SetActive(true);

                        trainer_timer += Time.deltaTime;
                        return;
                    }
                }
                else
                {
                    trainer_check = false;//런지일때 운동 끝나는 시점!!!!!
                }
            }
            trainer_j = 0;
            return;
        }
        trainer_i = -1;
        trainer_data1 = new float[bodyCount * jointCount * 3];
        trainer_state = new int[bodyCount * jointCount];
        for (int k = 0; k < 25; k++)
        {
            trainer_i++;
            trainer_data1[trainer_i] = float.Parse(lines[trainer_j]);
            trainer_i++;
            trainer_data1[trainer_i] = float.Parse(lines[trainer_j + 1]);
            trainer_i++;
            trainer_data1[trainer_i] = float.Parse(lines[trainer_j + 2]);
            if ((float.Parse(lines[trainer_j]) + float.Parse(lines[trainer_j + 1]) + float.Parse(lines[trainer_j + 2])) != 0)
            {
                trainer_state[trainer_i - 2] = 1;
            }
            skeleton_Trainnner.dasomset_trainer(trainer_data1, trainer_state, 0, true, true);
            trainer_j = trainer_j + 3;
        }
        if (speed < trainer_speed)
        {
            trainer_j -= 75;
            speed++;
        }
        else
        {
            //    Debug.Log("다음");
            //   Debug.Log(trainer_j);
            speed = 0;

        }
    }
    //lsy end

    void Trainee_Count()
    {

        if (exercise == "Squat")
        {

            workout_flag = 0;
            workout_flag2 = 0;
        }
        else if (exercise == "SideHiKick")
        {
            workout_flag = 0;

        }
        else if (exercise == "Lunge")
        {
            workout_flag = 0;
        }

        workout_count++;
        workout = false;
        Debug.Log("트레이니: " + workout_count);

    }

    void Start()
    {
        timer = 0;

        skeleton_Trainnner = new CharacterSkeleton(Trainnner);
        skeleton_Trainnne = new CharacterSkeleton(Trainnne);

        //lsy
        trainer_state = new int[bodyCount * jointCount];
        trainer_data1 = new float[bodyCount * jointCount * 3];
        //lsy end

        exercise = ClickExercise.selected_exercise; //ClickExercise에서 선택한 운동이 무엇인지 String으로 넘어옴.
        Debug.Log(exercise);    //Squat, SideHiKick, Lunge 에 따라서 Trainer움직이고, 사용자에게 instruction주기 

        if (exercise == "Squat")
        {
            lines = File.ReadAllLines(@"Trainer_txt/squart.txt");
            Debug.Log("읽음");
        }
        else if (exercise == "SideHiKick")
        {
            lines = File.ReadAllLines(@"Trainer_txt/leg_right.txt");
            lines_right = File.ReadAllLines(@"Trainer_txt/leg_left.txt");
            Debug.Log("읽음");
        }
        else if (exercise == "Lunge")
        {
            lines = File.ReadAllLines(@"Trainer_txt/runzi_right.txt");
            lines_right = File.ReadAllLines(@"Trainer_txt/runzi_left.txt"); 
            Debug.Log("읽음");
        }
        else
        {
            lines = File.ReadAllLines(@"Trainer_txt/squart.txt");
            Debug.Log("읽음");
        }

        workout_count = 0;
        workout_flag = 0;
        workout_flag2 = 0;
        workout = false;

        sidehk_flag = 0;
        lunge_flag_l = 0;
        lunge_flag_r = 0;
        flag = 0;
        flag_timer = 0;

        if (exercise == "Squat")
        {
            Squat_Instruction.SetActive(true);
        }
        else if (exercise == "SideHiKick")
        {
            SideHighKick_Instruction.SetActive(true);
        }
        else if (exercise == "Lunge")
        {
            Lunge_Left_Instruction.SetActive(true);
        }

        //HipBalance1.SetActive(false);
        //PullHipBack2.SetActive(false);
        //LegUp3.SetActive(false);
        //SpineStraight4.SetActive(false);
        //Great5.SetActive(false);
        //KneeDown6.SetActive(false);
    }

    private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
    {
        { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
        { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
        { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
        { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },

        { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
        { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
        { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
        { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },

        { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
        { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
        { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
        { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
        { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
        { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },

        { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
        { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
        { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
        { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
        { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
        { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },

        { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
        { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
        { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
        { Kinect.JointType.Neck, Kinect.JointType.Head },
    };

    void Update()
    {

        timer += Time.deltaTime;
        //Debug.Log("Timer: " + timer);
        if (timer < 3)
        {
            if (exercise == "Squat")
            {
                Squat_Instruction.SetActive(true);
            }
            else if (exercise == "SideHiKick")
            {
                SideHighKick_Instruction.SetActive(true);
            }
            else if (exercise == "Lunge")
            {
                Lunge_Left_Instruction.SetActive(true);
            }
        }
        else if (timer < 4)
        {
            Squat_Instruction.SetActive(false);
            SideHighKick_Instruction.SetActive(false);
            Lunge_Left_Instruction.SetActive(false);

            count3.SetActive(true);
            count2.SetActive(false);
            count1.SetActive(false);
            ex_start.SetActive(true);

        }
        else if (timer < 5) {
            count3.SetActive(false);
            count2.SetActive(true);

        }
        else if (timer < 6)
        {
            count2.SetActive(false);
            count1.SetActive(true);
            ex_start.SetActive(true);

        }
        else
        {
            count1.SetActive(false);
            ex_start.SetActive(false);

            //lsy
            if (trainer_check_outside)
            {

                Trainer_Run();

            }
            //lsy end

            float[] data1 = new float[bodyCount * jointCount * 3];
            int[] state = new int[bodyCount * jointCount];
            int[] id = new int[bodyCount];
            GCHandle gch = GCHandle.Alloc(data1, GCHandleType.Pinned);
            GCHandle gch2 = GCHandle.Alloc(state, GCHandleType.Pinned);
            GCHandle gch3 = GCHandle.Alloc(id, GCHandleType.Pinned);

            if (BodySourceManager == null)
            {
                return;
            }

            _BodyManager = BodySourceManager.GetComponent<BodySourceManager>();
            if (_BodyManager == null)
            {
                return;
            }

            Kinect.Body[] data = _BodyManager.GetData();
            if (data == null)
            {
                return;
            }

            List<ulong> trackedIds = new List<ulong>();
            foreach (var body in data)
            {
                if (body == null)
                {
                    continue;
                }

                if (body.IsTracked)
                {

                    trackedIds.Add(body.TrackingId);
                }
            }

            List<ulong> knownIds = new List<ulong>(_Bodies.Keys);

            // First delete untracked bodies
            foreach (ulong trackingId in knownIds)
            {
                if (!trackedIds.Contains(trackingId))
                {
                    Destroy(_Bodies[trackingId]);
                    _Bodies.Remove(trackingId);
                }
            }

            foreach (var body in data)
            {
                if (body == null)
                {
                    continue;
                }

                if (body.IsTracked)
                {
                    if (!_Bodies.ContainsKey(body.TrackingId))
                    {

                        _Bodies[body.TrackingId] = CreateBodyObject(body.TrackingId);

                    }
                    RefreshBodyObject(body, _Bodies[body.TrackingId]);
                }
            }
        }

    }

    private GameObject CreateBodyObject(ulong id)
    {

        GameObject body = new GameObject("Body:" + id);

        for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
        {
            GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
            //LineRenderer lr = jointObj.AddComponent<LineRenderer>();
            //lr.SetVertexCount(2);
            //lr.material = BoneMaterial;
            //lr.SetWidth(0.05f, 0.05f);

            jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
            jointObj.name = jt.ToString();
            jointObj.transform.parent = body.transform;
        }

        return body;
    }

    private void RefreshBodyObject(Kinect.Body body, GameObject bodyObject)
    {
        float[] data1 = new float[bodyCount * jointCount * 3];
        int[] state = new int[bodyCount * jointCount];
        int[] id1 = new int[bodyCount];
        GCHandle gch = GCHandle.Alloc(data1, GCHandleType.Pinned);
        GCHandle gch2 = GCHandle.Alloc(state, GCHandleType.Pinned);
        GCHandle gch3 = GCHandle.Alloc(id1, GCHandleType.Pinned);

        int i = -1;
        for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
        {
            Kinect.Joint sourceJoint = body.Joints[jt];
            Kinect.Joint? targetJoint = null;

            

            if (_BoneMap.ContainsKey(jt))
            {
                targetJoint = body.Joints[_BoneMap[jt]];
            }
            Transform jointObj = bodyObject.transform.Find(jt.ToString());
            jointObj.localPosition = GetVector3FromJoint(sourceJoint);

            //Debug.Log(i);

            i++;
            data1[i] = sourceJoint.Position.X;
            i++;
            data1[i] = sourceJoint.Position.Y;
            i++;
            data1[i] = sourceJoint.Position.Z;
            if ((sourceJoint.Position.X + sourceJoint.Position.Y + sourceJoint.Position.Z) != 0)
            {
                state[i - 2] = 1;
            }
            skeleton_Trainnne.dasomset(data1, state, 0, true, true, body.Joints[jt], jointObj);

            /*
            LineRenderer lr = jointObj.GetComponent<LineRenderer>();
            if (targetJoint.HasValue)
            {
                //Debug.Log(jointObj.localPosition);
                //Debug.Log(GetVector3FromJoint(targetJoint.Value));
                lr.SetPosition(0, jointObj.localPosition);
                lr.SetPosition(1, GetVector3FromJoint(targetJoint.Value));
                lr.SetColors(GetColorForState(sourceJoint.TrackingState), GetColorForState(targetJoint.Value.TrackingState));
            }
            else
            {
                lr.enabled = false;
            }
            */
        }
        
        ///////////////////////////
        Angles MyAngles = new Angles();
        byte[] ReadAngles = MyAngles.GetVector(body);

        if (exercise == "Squat")
        {
            if (ReadAngles[1] - ReadAngles[2] <= 5)
            {
                HipBalance1.SetActive(false);
            }
            else
            {
                //"양쪽 힘이 동일하지 않습니다. \n균형을 잡으세요!";
                HipBalance1.SetActive(true);
            }

            if (ReadAngles[5] > 95 || ReadAngles[6] > 95)
            {
                //"엉덩이를 뒤로 더 빼세요!";
                PullHipBack2.SetActive(true);
            }
            else
            {
                PullHipBack2.SetActive(false);
            }

            if (ReadAngles[3] < 95 && ReadAngles[4] < 95)
            {
                Great5.SetActive(true);
            }
            else
            {
                great = "";
                Great5.SetActive(false);
            }

            ////////////운동 횟수 카운트//////////////////////
            if (ReadAngles[3] < 95 && ReadAngles[4] < 95 && workout
                && workout_flag >= 95 && workout_flag2 >= 95)
            {
                Trainee_Count();
            }
            else
            {
                workout_flag = ReadAngles[3];
                workout_flag2 = ReadAngles[4];
                workout = true;
            }

        }
        else if (exercise == "SideHiKick")
        {
            if ((ReadAngles[7] < 45) && (ReadAngles[7] > 10)
                && (sidehk_flag < ReadAngles[7]))
            {
                LegUp3.SetActive(true); //다리를 더 높이 들어올리세요
                Great5.SetActive(false);

            }
            else
            {
                LegUp3.SetActive(false);
                Great5.SetActive(false);

                //이전각도
                sidehk_flag = ReadAngles[7];
            }

            if (ReadAngles[7] >= 45)
            {
                Great5.SetActive(true); //Great
            }
            else
            {
                Great5.SetActive(false);
            }

            ////////운동 횟수 카운트////////////////////
            if (ReadAngles[7] > 45 && workout && workout_flag <= 45)
            {
                Trainee_Count();
            }
            else
            {
                workout_flag = ReadAngles[7];
                workout = true;
            }



        }
        else if (exercise == "Lunge")
        {
            if (workout_count < 10)
            {
                if (flag == 0)
                {
                    if (lunge_flag_l < 120 && ReadAngles[3] >= 91 && lunge_flag_l > ReadAngles[3])
                    {
                        great = "";
                        //Great5.SetActive(false);
                        KneeDown6.SetActive(true); //무릎을 더 굽혀주세요.  
                        flag = 1;

                    }
                    else if (ReadAngles[3] < 91)
                    {
                        great = "GREAT!";
                        Great5.SetActive(true);
                        //KneeDown6.SetActive(false);

                        flag = 1;
                    }
                }
                else
                {

                    flag_timer += Time.deltaTime;
                    //Debug.Log(flag_timer);
                    if (flag_timer > 5)
                    {
                        flag = 0;
                        flag_timer = 0;
                        Debug.Log("5초넘음");

                        Great5.SetActive(false);
                        KneeDown6.SetActive(false);
                    }
                }


                //////////////운동 횟수 카운트//////////////////
                //왼쪽
                if (ReadAngles[3] < 95 && workout && workout_flag >= 95)
                {
                    Trainee_Count();
                }
                else
                {
                    workout_flag = ReadAngles[3];
                    workout = true;
                }
            }
            else
            {
                if (flag == 0)
                {
                    if (lunge_flag_r < 120 && ReadAngles[4] >= 91 && lunge_flag_r > ReadAngles[4])
                    {
                        great = "";
                        //Great5.SetActive(false);
                        KneeDown6.SetActive(true); //무릎을 더 굽혀주세요.  
                        flag = 1;

                    }
                    else if (ReadAngles[4] < 91)
                    {
                        great = "GREAT!";
                        Great5.SetActive(true);
                        //KneeDown6.SetActive(false);

                        flag = 1;
                    }
                }
                else
                {

                    flag_timer += Time.deltaTime;
                    //Debug.Log(flag_timer);
                    if (flag_timer > 5)
                    {
                        flag = 0;
                        flag_timer = 0;
                        Debug.Log("5초넘음");

                        Great5.SetActive(false);
                        KneeDown6.SetActive(false);
                    }
                }

                //////////////운동 횟수 카운트//////////////////
                //오른쪽
                if (workout_count >= 10 && ReadAngles[4] < 95 && workout && workout_flag >= 95)
                {
                    Trainee_Count();
                }
                else
                {
                    workout_flag = ReadAngles[4];
                    workout = true;
                }
            }


            lunge_flag_l = ReadAngles[3];
            lunge_flag_r = ReadAngles[4];
        }

        //공통된 사항
        if (ReadAngles[0] < 170 || ReadAngles[0] > 190)
        {
            spinestraight = "허리를 곧게 펴세요!";
            SpineStraight4.SetActive(true);
        }
        else
        {
            spinestraight = "";
            SpineStraight4.SetActive(false);
        }
        
        ///////////////////////////
    }

    private static Color GetColorForState(Kinect.TrackingState state)
    {
        switch (state)
        {
            case Kinect.TrackingState.Tracked:
                return Color.green;

            case Kinect.TrackingState.Inferred:
                return Color.red;

            default:
                return Color.black;
        }
    }

    private static Vector3 GetVector3FromJoint(Kinect.Joint joint)
    {
        return new Vector3(joint.Position.X * 10, joint.Position.Y * 10, joint.Position.Z * 10);
    }

    public class Angles
    {
        public double AngleBetweenTwoVectors(Vector3 Va, Vector3 Vb)
        {
            double dotProduct;

            Va.Normalize();
            Vb.Normalize();
            dotProduct = Vector3.Dot(Va, Vb);

            return (double)Math.Acos(dotProduct) * (180 / Math.PI);

        }

        public byte[] GetVector(Kinect.Body body)
        {
            Vector3 SpineShoulder = new Vector3(body.Joints[Kinect.JointType.SpineShoulder].Position.X, body.Joints[Kinect.JointType.SpineShoulder].Position.Y, body.Joints[Kinect.JointType.SpineShoulder].Position.Z);
            Vector3 RightShoulder = new Vector3(body.Joints[Kinect.JointType.ShoulderRight].Position.X, body.Joints[Kinect.JointType.ShoulderRight].Position.Y, body.Joints[Kinect.JointType.ShoulderRight].Position.Z);
            Vector3 LeftShoulder = new Vector3(body.Joints[Kinect.JointType.ShoulderLeft].Position.X, body.Joints[Kinect.JointType.ShoulderLeft].Position.Y, body.Joints[Kinect.JointType.ShoulderLeft].Position.Z);
            Vector3 RightElbow = new Vector3(body.Joints[Kinect.JointType.ElbowRight].Position.X, body.Joints[Kinect.JointType.ElbowRight].Position.Y, body.Joints[Kinect.JointType.ElbowRight].Position.Z);
            Vector3 LeftElbow = new Vector3(body.Joints[Kinect.JointType.ElbowLeft].Position.X, body.Joints[Kinect.JointType.ElbowLeft].Position.Y, body.Joints[Kinect.JointType.ElbowLeft].Position.Z);
            Vector3 RightWrist = new Vector3(body.Joints[Kinect.JointType.WristRight].Position.X, body.Joints[Kinect.JointType.WristRight].Position.Y, body.Joints[Kinect.JointType.WristRight].Position.Z);
            Vector3 LeftWrist = new Vector3(body.Joints[Kinect.JointType.WristLeft].Position.X, body.Joints[Kinect.JointType.WristLeft].Position.Y, body.Joints[Kinect.JointType.WristLeft].Position.Z);
            Vector3 UpVector = new Vector3((float)0.0, (float)1.0, (float)0.0);
            //////////////////////////////////////
            Vector3 SpineMid = new Vector3(body.Joints[Kinect.JointType.SpineMid].Position.X, body.Joints[Kinect.JointType.SpineMid].Position.Y, body.Joints[Kinect.JointType.SpineMid].Position.Z);
            Vector3 SpineBase = new Vector3(body.Joints[Kinect.JointType.SpineBase].Position.X, body.Joints[Kinect.JointType.SpineBase].Position.Y, body.Joints[Kinect.JointType.SpineBase].Position.Z);
            Vector3 HipLeft = new Vector3(body.Joints[Kinect.JointType.HipLeft].Position.X, body.Joints[Kinect.JointType.HipLeft].Position.Y, body.Joints[Kinect.JointType.HipLeft].Position.Z);
            Vector3 KneeLeft = new Vector3(body.Joints[Kinect.JointType.KneeLeft].Position.X, body.Joints[Kinect.JointType.KneeLeft].Position.Y, body.Joints[Kinect.JointType.KneeLeft].Position.Z);
            Vector3 AnkleLeft = new Vector3(body.Joints[Kinect.JointType.AnkleLeft].Position.X, body.Joints[Kinect.JointType.AnkleLeft].Position.Y, body.Joints[Kinect.JointType.AnkleLeft].Position.Z);
            Vector3 HipRight = new Vector3(body.Joints[Kinect.JointType.HipRight].Position.X, body.Joints[Kinect.JointType.HipRight].Position.Y, body.Joints[Kinect.JointType.HipRight].Position.Z);
            Vector3 KneeRight = new Vector3(body.Joints[Kinect.JointType.KneeRight].Position.X, body.Joints[Kinect.JointType.KneeRight].Position.Y, body.Joints[Kinect.JointType.KneeRight].Position.Z);
            Vector3 AnkleRight = new Vector3(body.Joints[Kinect.JointType.AnkleRight].Position.X, body.Joints[Kinect.JointType.AnkleRight].Position.Y, body.Joints[Kinect.JointType.AnkleRight].Position.Z);
            Vector3 FootRight = new Vector3(body.Joints[Kinect.JointType.FootRight].Position.X, body.Joints[Kinect.JointType.FootRight].Position.Y, body.Joints[Kinect.JointType.FootRight].Position.Z);
            Vector3 FootLeft = new Vector3(body.Joints[Kinect.JointType.FootLeft].Position.X, body.Joints[Kinect.JointType.FootLeft].Position.Y, body.Joints[Kinect.JointType.FootLeft].Position.Z);


            double StraightSpine = AngleBetweenTwoVectors(SpineShoulder - SpineMid, SpineBase - SpineMid); //스쿼트, 런지 - Joint 3개로도 가능

            double AngleLeftHip = AngleBetweenTwoVectors(SpineBase - SpineShoulder, HipLeft - KneeLeft); //스쿼트, 런지
            double AngleRightHip = AngleBetweenTwoVectors(SpineBase - SpineShoulder, HipRight - KneeRight);  //스쿼트, 런지
            //double AngleLeftKnee = AngleBetweenTwoVectors(KneeLeft - HipLeft, KneeLeft - AnkleLeft);    //스쿼트, 런지, 사이드하이킥 - Joint 3개로도 가능
            double AngleLeftKnee = AngleBetweenTwoVectors(HipLeft - KneeLeft, AnkleLeft - KneeLeft);    //스쿼트, 런지, 사이드하이킥 - Joint 3개로도 가능
            //double AngleRightKnee = AngleBetweenTwoVectors(KneeRight - HipRight, KneeRight - AnkleRight);    //스쿼트, 런지, 사이드하이킥 - Joint 3개로도 가능
            double AngleRightKnee = AngleBetweenTwoVectors(HipRight - KneeRight, AnkleRight - KneeRight);    //스쿼트, 런지, 사이드하이킥 - Joint 3개로도 가능

            double KneeToeLeft = AngleBetweenTwoVectors(AnkleLeft - FootLeft, KneeLeft - FootLeft); //스쿼트 - Joint 3개로도 가능
            double KneeToeRight = AngleBetweenTwoVectors(AnkleRight - FootRight, KneeRight - FootRight); //스쿼트 - Joint 3개로도 가능
            //double LeftLegUp = AngleBetweenTwoVectors(SpineMid - SpineShoulder, KneeLeft - HipLeft);    //사이드 하이킥
            //double RightLegUp = AngleBetweenTwoVectors(SpineMid - SpineShoulder, KneeRight - HipRight);    //사이드 하이킥
            double LeftLegUp = AngleBetweenTwoVectors(KneeRight - HipRight, KneeLeft - HipLeft);    //사이드 하이킥
            double RightLegUp = AngleBetweenTwoVectors(KneeLeft - HipLeft, KneeRight - HipRight);    //사이드 하이킥
            //LeftLegUp이랑 RightLegUp이랑 각도 동일


            byte[] Angles = { Convert.ToByte(StraightSpine),
                              Convert.ToByte(AngleLeftHip), Convert.ToByte(AngleRightHip),
                              Convert.ToByte(AngleLeftKnee), Convert.ToByte(AngleRightKnee),
                              Convert.ToByte(KneeToeLeft), Convert.ToByte(KneeToeRight),
                              Convert.ToByte(LeftLegUp), Convert.ToByte(RightLegUp)};

            return Angles;
        }
    }
}