// Update is called once per frame
    void Update()
    {
        // get 1st player
        uint playerID = KinectManager.Instance != null?KinectManager.Instance.GetPlayer1ID() : 0;

        if (playerID <= 0)
        {
            return;
        }

        // set the position in space
        Vector3 posPointMan = KinectManager.Instance.GetUserPosition(playerID);

        posPointMan.z = !MirroredMovement ? -posPointMan.z : posPointMan.z;

        // store the initial position
        if (!initialPosInitialized)
        {
            posInitialOffset      = transform.position - (MoveVertically ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z));
            initialPosInitialized = true;
        }

        transform.position = posInitialOffset + (MoveVertically ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z));

        // update the local positions of the bones
        int jointsCount = (int)KinectWrapper.NuiSkeletonPositionIndex.Count;

        for (int i = 0; i < jointsCount; i++)
        {
            if (_bones[i] != null)
            {
                if (KinectManager.Instance.IsJointTracked(playerID, i))
                {
                    _bones[i].gameObject.SetActive(true);

                    int     joint    = MirroredMovement ? KinectWrapper.GetSkeletonMirroredJoint(i): i;
                    Vector3 posJoint = KinectManager.Instance.GetJointPosition(playerID, joint);
                    posJoint.z = !MirroredMovement ? -posJoint.z : posJoint.z;
                    Quaternion rotJoint = KinectManager.Instance.GetJointOrientation(playerID, joint, !MirroredMovement);

                    posJoint  -= posPointMan;
                    posJoint.z = -posJoint.z;

                    if (MirroredMovement)
                    {
                        posJoint.x = -posJoint.x;
                    }

                    _bones[i].transform.localPosition = posJoint;
                    _bones[i].transform.localRotation = rotJoint;
                }
                else
                {
                    _bones[i].gameObject.SetActive(false);
                }
            }
        }
    }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        KinectManager manager = KinectManager.Instance;

        // get 1st player
        uint playerID = manager != null?manager.GetPlayer1ID() : 0;

        if (playerID <= 0)
        {
            // reset the pointman position and rotation
            if (transform.position != initialPosition)
            {
                transform.position = initialPosition;
            }

            if (transform.rotation != initialRotation)
            {
                transform.rotation = initialRotation;
            }

            for (int i = 0; i < bones.Length; i++)
            {
                bones[i].gameObject.SetActive(true);

                bones[i].transform.localPosition = Vector3.zero;
                bones[i].transform.localRotation = Quaternion.identity;

                if (SkeletonLine)
                {
                    lines[i].gameObject.SetActive(false);
                }
            }

            return;
        }

        // set the user position in space
        Vector3 posPointMan = manager.GetUserPosition(playerID);

        posPointMan.z = !MirroredMovement ? -posPointMan.z : posPointMan.z;

        // store the initial position
        if (initialPosUserID != playerID)
        {
            initialPosUserID = playerID;
            initialPosOffset = transform.position - (MoveVertically ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z));
        }

        transform.position = initialPosOffset + (MoveVertically ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z));

        // update the local positions of the bones
        for (int i = 0; i < bones.Length; i++)
        {
            if (bones[i] != null)
            {
                int joint = MirroredMovement ? KinectWrapper.GetSkeletonMirroredJoint(i): i;

                if (manager.IsJointTracked(playerID, joint))
                {
                    bones[i].gameObject.SetActive(true);

                    Vector3 posJoint = manager.GetJointPosition(playerID, joint);
                    posJoint.z = !MirroredMovement ? -posJoint.z : posJoint.z;

                    Quaternion rotJoint = manager.GetJointOrientation(playerID, joint, !MirroredMovement);
                    rotJoint = initialRotation * rotJoint;

                    posJoint -= posPointMan;

                    if (MirroredMovement)
                    {
                        posJoint.x = -posJoint.x;
                        posJoint.z = -posJoint.z;
                    }

                    bones[i].transform.localPosition = posJoint;
                    bones[i].transform.rotation      = rotJoint;
                }
                else
                {
                    bones[i].gameObject.SetActive(false);
                }
            }
        }

        if (SkeletonLine)
        {
            for (int i = 0; i < bones.Length; i++)
            {
                bool bLineDrawn = false;

                if (bones[i] != null)
                {
                    if (bones[i].gameObject.activeSelf)
                    {
                        Vector3 posJoint = bones[i].transform.position;

                        int     parI      = parIdxs[i];
                        Vector3 posParent = bones[parI].transform.position;

                        if (bones[parI].gameObject.activeSelf)
                        {
                            lines[i].gameObject.SetActive(true);

                            //lines[i].SetVertexCount(2);
                            lines[i].SetPosition(0, posParent);
                            lines[i].SetPosition(1, posJoint);

                            bLineDrawn = true;
                        }
                    }
                }

                if (!bLineDrawn)
                {
                    lines[i].gameObject.SetActive(false);
                }
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        KinectManager manager = KinectManager.Instance;

        // get 1st player
        uint playerID = manager != null?manager.GetPlayer1ID() : 0;


        if (playerID <= 0)
        {
            Debug.Log(playerID);
            // reset the pointman position and rotation
            if (transform.position != initialPosition)
            {
                transform.position = initialPosition;
            }

            if (transform.rotation != initialRotation)
            {
                transform.rotation = initialRotation;
            }

            for (int i = 0; i < bones.Length; i++)
            {
                bones[i].gameObject.SetActive(true);

                bones[i].transform.localPosition = Vector3.zero;
                bones[i].transform.localRotation = Quaternion.identity;

                if (SkeletonLine)
                {
                    lines[i].gameObject.SetActive(false);
                }
            }

            return;
        }


        // set the user position in space
        Vector3 posPointMan = manager.GetUserPosition(playerID);

        posPointMan.z = !MirroredMovement ? -posPointMan.z : posPointMan.z;

        // store the initial position
        if (initialPosUserID != playerID)
        {
            initialPosUserID = playerID;
            initialPosOffset = transform.position - (MoveVertically ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z));
        }

        transform.position = initialPosOffset + (MoveVertically ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z));

        // update the local positions of the bones
        for (int i = 0; i < bones.Length; i++)
        {
            if (bones[i] != null)
            {
                int joint = MirroredMovement ? KinectWrapper.GetSkeletonMirroredJoint(i): i;

                if (manager.IsJointTracked(playerID, joint))
                {
                    //bones[i].gameObject.SetActive(true);

                    Vector3 posJoint = manager.GetJointPosition(playerID, joint);
                    posJoint.z = !MirroredMovement ? -posJoint.z : posJoint.z;

                    Quaternion rotJoint = manager.GetJointOrientation(playerID, joint, !MirroredMovement);
                    rotJoint = initialRotation * rotJoint;

                    posJoint -= posPointMan;

                    if (MirroredMovement)
                    {
                        posJoint.x = -posJoint.x;
                        posJoint.z = -posJoint.z;
                    }

                    bones[i].transform.localPosition = posJoint;
                    bones[i].transform.rotation      = rotJoint;
                }
                else
                {
                    bones[i].gameObject.SetActive(false);
                }

                if (bones[11].transform.position.x != 0)
                {
                    //Debug.Log(bones[11].transform.position.x);
                    HandData = bones[11].transform.position.x;
                }


                dis = HandData - PreHandData;
                if (Mathf.Abs(dis) > 0.2f)
                {
                    Debug.Log(dis);
                }

                dis = 0.0f;
            }
        }
        playerID = 0;
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            returnpage.SetActive(true);
            SceneManager.LoadScene("welcome");
        }
        if (gameover)
        {
            Invoke("gamerestart", 3f);
            return;
        }

        KinectManager manager = KinectManager.Instance;

        // get 1st player
        uint playerID = manager != null?manager.GetPlayer1ID() : 0;

        if (playerID <= 0)
        {
            // reset the pointman position and rotation
            //if(transform.position != initialPosition)
            //{
            //	transform.position = initialPosition;
            //}

            //if(transform.rotation != initialRotation)
            //{
            //	transform.rotation = initialRotation;
            //}

            //for(int i = 0; i < bones.Length; i++)
            //{
            //	bones[i].gameObject.SetActive(true);

            //	bones[i].transform.localPosition = Vector3.zero;
            //	bones[i].transform.localRotation = Quaternion.identity;

            //	if(SkeletonLine)
            //	{
            //		lines[i].gameObject.SetActive(false);
            //	}
            //}

            return;
        }

        // set the user position in space
        Vector3 posPointMan = manager.GetUserPosition(playerID);

        posPointMan.z = !MirroredMovement ? -posPointMan.z : posPointMan.z;

        // store the initial position
        if (initialPosUserID != playerID)
        {
            initialPosUserID = playerID;
            initialPosOffset = transform.position - (MoveVertically ? posPointMan : new Vector3(posPointMan.x, 0, 0));
        }

        transform.position = initialPosOffset + (MoveVertically ? posPointMan : new Vector3(posPointMan.x * moverate, 0, 0));

        // update the local positions of the bones
        for (int i = 0; i < bones.Length; i++)
        {
            if (bones[i] != null)
            {
                int joint = MirroredMovement ? KinectWrapper.GetSkeletonMirroredJoint(i): i;

                if (manager.IsJointTracked(playerID, joint))
                {
                    gamestart       = true;
                    rg.gravityScale = 1;
                    bones[i].gameObject.SetActive(true);

                    Vector3 posJoint = manager.GetJointPosition(playerID, joint);
                    posJoint.z = !MirroredMovement ? -posJoint.z : posJoint.z;

                    Quaternion rotJoint = manager.GetJointOrientation(playerID, joint, !MirroredMovement);
                    rotJoint = initialRotation * rotJoint;

                    posJoint -= posPointMan;

                    if (MirroredMovement)
                    {
                        posJoint.x = -posJoint.x;
                        posJoint.z = -posJoint.z;
                    }
                    posJoint.z = 0;
                    rotJoint.x = 0;
                    rotJoint.y = 0;
                    //float degree = (bones[i + 1].transform.localPosition.y - bones[i].transform.localPosition.y) /
                    //    (bones[i + 1].transform.localPosition.x - bones[i].transform.localPosition.x);
                    //double zrotation = Math.Atan(degree)/Math.PI;
                    //rotJoint.z = (float)zrotation;
                    bones[i].transform.localPosition = posJoint;
                    bones[i].transform.rotation      = rotJoint;
                }
                else
                {
                    bones[i].gameObject.SetActive(false);
                }
            }
        }

        if (SkeletonLine)
        {
            for (int i = 0; i < bones.Length; i++)
            {
                bool bLineDrawn = false;

                if (bones[i] != null)
                {
                    if (bones[i].gameObject.activeSelf)
                    {
                        Vector3 posJoint = bones[i].transform.position;

                        int     parI      = parIdxs[i];
                        Vector3 posParent = bones[parI].transform.position;

                        if (bones[parI].gameObject.activeSelf)
                        {
                            lines[i].gameObject.SetActive(true);

                            //lines[i].SetVertexCount(2);
                            lines[i].SetPosition(0, posParent);
                            lines[i].SetPosition(1, posJoint);

                            bLineDrawn = true;
                        }
                    }
                }

                if (!bLineDrawn)
                {
                    lines[i].gameObject.SetActive(false);
                }
            }
        }
    }
    // `te the avatar each frame.
    public void UpdateUnit(uint UserID)
    {
        if (!isLocalPlayer)
        {
            return;
        }

        setLog("Update Unit");

        if (!transform.gameObject.activeInHierarchy)
        {
            return;
        }

        // Get the KinectManager instance
        if (kinectManager == null)
        {
            kinectManager = KinectManager.Instance;
        }

        // move the avatar to its Kinect position
        //MoveAvatar(UserID);


        KinectManager manager = KinectManager.Instance;

        // get 1st player
        uint playerID = manager != null?manager.GetPlayer1ID() : 0;

        if (playerID <= 0)
        {
            // reset the pointman position and rotation
            if (transform.position != initialPosition)
            {
                transform.position = initialPosition;
            }

            if (transform.rotation != initialRotation)
            {
                transform.rotation = initialRotation;
            }


            // 위치초기화
            // initialRotation = MoveObject.rotation;

//            for (int i = 0; i < bones.Length; i++)
//            {
//                bones[i].gameObject.SetActive(true);
//
//                bones[i].transform.localPosition = Vector3.zero;
//                bones[i].transform.localRotation = Quaternion.identity;
//
//                if (SkeletonLine)
//                {
//                    lines[i].gameObject.SetActive(false);
//                }
//            }

            return;
        }

        // set the user position in space
        Vector3 posPointMan = manager.GetUserPosition(playerID);

        // [원본] posPointMan.z = !mirroredMovement ? -posPointMan.z : posPointMan.z;
        posPointMan.z = mirroredMovement ? -posPointMan.z : posPointMan.z;
        // [원본] posPointMan.x = !mirroredMovement ? -posPointMan.x : posPointMan.x;
        posPointMan.x = mirroredMovement ? -posPointMan.x : posPointMan.x;

        // 움직임 반영
        //if (MoveObject != null)
        //{
        //    if (InitMovePosition != null)
        //    {
        //        InitMovePosition = MoveObject.position;
        //    }
        //    Vector3 moveResultPos = MoveObject.position;
        //    moveResultPos.x = InitMovePosition.x + posPointMan.x * MoveScale;
        //    // MoveObject.position = moveResultPos;
        //}

        // store the initial position
        if (initialPosUserID != playerID)
        {
            initialPosUserID = playerID;
            initialPosOffset = transform.position - (verticalMovement ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z));
        }

        transform.position = initialPosOffset + (verticalMovement ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z));

        // Debug.Log("update the local positions of the bones");
        // update the local positions of the bones

        transform.rotation = initialRotation;
        for (int i = 0; i < bones.Length; i++)
        {
            if (bones[i] != null || true)
            {
                //Debug.Log("bones[i] != null");

                int joint = mirroredMovement ? KinectWrapper.GetSkeletonMirroredJoint(i) : i;

                if (manager.IsJointTracked(playerID, joint))
                {
                    //bones[i].gameObject.SetActive(true);

                    Vector3 posJoint = manager.GetJointPosition(playerID, joint);
                    posJoint.z = !mirroredMovement ? -posJoint.z : posJoint.z;

                    // [원본] Quaternion rotJoint = manager.GetJointOrientation(playerID, joint, !mirroredMovement);
                    // 몸통의 축이 반대로 논다...
                    Quaternion rotJoint = manager.GetJointOrientation(playerID, joint, mirroredMovement);
                    rotJoint = initialRotation * rotJoint;


                    // 위치 인식과 관절인식을 달리하기 위해 구분
                    posJoint -= posPointMan;

                    if (mirroredMovement)
                    {
                        posJoint.x = -posJoint.x;
                        posJoint.z = -posJoint.z;
                    }


                    foreach (DictionaryEntry Item in ht_bones)
                    {
                        Transform trans = (Transform)Item.Value;
                        if (i == (int)Item.Key && trans != null)
                        {
                            float scaleFactor = 1;
                            if (scaleFactor >= 0.1)
                            {
                                scaleFactor = ModelScale;
                            }

                            Vector3 result;
                            Vector3 origin = (Vector3)ht_origin[i];
                            result.x = origin.x * 0.3f + posJoint.x * 0.7f;
                            result.y = posJoint.y * 0.7f;
                            result.z = posJoint.z * 0.7f;

                            result.x *= scaleFactor;
                            result.y *= scaleFactor;
                            result.z *= scaleFactor;

                            result.y += 1.0F * scaleFactor;


                            if (i == (int)KinectWrapper.NuiSkeletonPositionIndex.HandRight || i == (int)KinectWrapper.NuiSkeletonPositionIndex.HandLeft)
                            {
                            }
                            else if (i == (int)KinectWrapper.NuiSkeletonPositionIndex.KneeLeft || i == (int)KinectWrapper.NuiSkeletonPositionIndex.KneeRight)
                            {
                                result.y -= 0.3f * scaleFactor;
                            }
                            else
                            {
                                // trans.rotation = rotJoint;
                            }
                            if (MoveObject != null)
                            {
                                trans.position = result + MoveObject.position;
                            }
                            if (horizonalMovement)
                            {
                                var tempVector = trans.position;
                                tempVector.x  += posPointMan.x * MoveScale;
                                trans.position = tempVector;
                            }

                            if (i != (int)KinectWrapper.NuiSkeletonPositionIndex.HandRight && i != (int)KinectWrapper.NuiSkeletonPositionIndex.HandLeft && i != (int)KinectWrapper.NuiSkeletonPositionIndex.Spine)
                            {
                                // trans.rotation = rotJoint;
                            }

                            break;
                        }
                    }
                }
                else
                {
                    //bones[i].gameObject.SetActive(false);
                }
            }
            if (MoveObject != null)
            {
                transform.rotation = MoveObject.rotation;
            }
        }
        for (var boneIndex = 0; boneIndex < bones.Length; boneIndex++)
        {
            //string debugFormat = string.Format("boneIndex: {0}   value: {1}", boneIndex, bones[boneIndex]);
            //Debug.Log(debugFormat);

            if (!bones[boneIndex])
            {
                continue;
            }

            Debug.Log("진입");

            //if (boneIndex2JointMap.ContainsKey(boneIndex))
            //{
            //    KinectWrapper.NuiSkeletonPositionIndex joint = !mirroredMovement ? boneIndex2JointMap[boneIndex] : boneIndex2MirrorJointMap[boneIndex];
            //    TransformBone(UserID, joint, boneIndex, !mirroredMovement);
            //}
            //else if (specIndex2JointMap.ContainsKey(boneIndex))
            //{
            //    // special bones (clavicles)
            //    List<KinectWrapper.NuiSkeletonPositionIndex> alJoints = !mirroredMovement ? specIndex2JointMap[boneIndex] : specIndex2MirrorJointMap[boneIndex];

            //    if (alJoints.Count >= 2)
            //    {
            //        //Vector3 baseDir = alJoints[0].ToString().EndsWith("Left") ? Vector3.left : Vector3.right;
            //        //TransformSpecialBone(UserID, alJoints[0], alJoints[1], boneIndex, baseDir, !mirroredMovement);
            //    }
            //}



            // 디버깅이 가능해야 한다. 좌표 출력 부분
            //if (boneIndex == ((int)HumanBodyBones.Spine))
            //{
            //    Vector3 spinePos = bones[boneIndex].position;
            //    string str_debug = string.Format("Spine({0},{1},{2})", spinePos.x, spinePos.y, spinePos.z);

            //    Debug.Log(str_debug);
            //}
        }
        CmdsyncPlayer();
    }
    // Update is called once per frame
    void Update()
    {
        KinectManager manager = KinectManager.Instance;

        // get 1st player
        uint playerID = manager != null?manager.GetPlayer1ID() : 0;

        if (playerID <= 0)
        {
            // reset the pointman position and rotation
            if (transform.position != initialPosition)
            {
                transform.position = initialPosition;
            }

            if (transform.rotation != initialRotation)
            {
                transform.rotation = initialRotation;
            }

            /*
             * for (int i = 0; i < bones.Length; i++)
             * {
             *  bones[i].gameObject.SetActive(true);
             *
             *  bones[i].transform.localPosition = Vector3.zero;
             *  bones[i].transform.localRotation = Quaternion.identity;
             *
             *  if (SkeletonLine)
             *  {
             *      lines[i].gameObject.SetActive(false);
             *  }
             * }
             */

            return;
        }

        // set the user position in space
        Vector3 posPointMan = manager.GetUserPosition(playerID);

        posPointMan.z = !MirroredMovement ? -posPointMan.z : posPointMan.z;

        // store the initial position
        if (initialPosUserID != playerID)
        {
            initialPosUserID = playerID;
            initialPosOffset = transform.position - (MoveVertically ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z));
        }

        transform.position = initialPosOffset + (MoveVertically ? posPointMan : new Vector3(posPointMan.x, 0, posPointMan.z));

        // update the local positions of the bones

        int joint = MirroredMovement ? KinectWrapper.GetSkeletonMirroredJoint(3) : 3;

        if (manager.IsJointTracked(playerID, 3))
        {
            //bones[i].gameObject.SetActive(true);

            Vector3 posJoint = manager.GetJointPosition(playerID, joint);
            posJoint.z = !MirroredMovement ? -posJoint.z : posJoint.z;

            Quaternion rotJoint = manager.GetJointOrientation(playerID, joint, !MirroredMovement);
            rotJoint = initialRotation * rotJoint;

            posJoint -= posPointMan;

            if (MirroredMovement)
            {
                posJoint.x = -posJoint.x;
                posJoint.z = -posJoint.z;
            }

            //bones[i].transform.localPosition = posJoint;
            // bones[i].transform.rotation = rotJoint;

            UnityEngine.Debug.Log("head : " + posJoint + " " + rotJoint);


            //else
            //{
            //bones[i].gameObject.SetActive(false);
            //}
        }
    }