Unity3D+kinect2 实现关节位置到UGUI坐标的转换,以及判断是否在某个UGUI中

Unity3D+kinect2 实现关节位置到UGUI坐标的转换,以及判断是否在某个UGUI中_第1张图片Unity3D+kinect2 实现关节位置到UGUI坐标的转换,以及判断是否在某个UGUI中_第2张图片基本思路是:

1、先判断是否检测到玩家

2、是否检测到对应玩家的对应关节点

3、该关节点的左边转换为当前屏幕坐标

4、判断该屏幕坐标在不在某个按钮或者图片中

但是,需要注意的是:场景中的Main Camera需要设置成Perspective,然后角度大约50-60度


这里有个水果忍者游戏的开始界面中,关于右手是否握拳选中三个按钮中的某个的case,大家可以参考其中的坐标转换的代码。


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class lesson16 : MonoBehaviour {

    public Canvas canvas;
    public Image rightHand;
    public Sprite[] handStateSprites;

    public Image btn1;
    public Image btn2;
    public Image btn3;
    public Image circle1;
    public Image circle2;
    public Image circle3;

    public int upForce = 8000;
    public int gravityScale = 10;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        //print ("dw = "+KinectManager.Instance.GetDepthImageWidth() + "dy = " + KinectManager.Instance.GetDepthImageHeight());
        if (KinectManager.Instance.IsUserDetected ()) 
        {

            //jian ce 到玩家
            long userId = KinectManager.Instance.GetPrimaryUserID ();
            int jointType = (int)KinectInterop.JointType.HandRight;//表示右手

            if(KinectManager.Instance.IsJointTracked(userId,jointType))
            {
                //关jie点被追踪到
                Vector3 handPos =KinectManager.Instance.GetJointKinectPosition(userId, jointType);
                Vector3 handScreenPosV3 = Camera.main.WorldToScreenPoint (handPos);
                Vector2 handSenPos = new Vector2 (handScreenPosV3.x, handScreenPosV3.y);
                Vector2 uguiPos;

                //zhi xing 屏幕 zuo biao  UGUI zuo biao  zhuan huan
                bool changeSuccess = RectTransformUtility.ScreenPointToLocalPointInRectangle ((RectTransform)canvas.transform, handSenPos, Camera.main, out uguiPos);

                if (changeSuccess) 
                {
                    //表示右手在canvas所表示的矩形范wei内
                    RectTransform rightRectTf = rightHand.transform as RectTransform;
                    rightRectTf.anchoredPosition = uguiPos;//Update the image of right hand
                }

                bool isHandClose = false;
                rightHand.sprite = handStateSprites [0];
                KinectInterop.HandState rightHandState = KinectManager.Instance.GetRightHandState (userId);
                if (rightHandState == KinectInterop.HandState.Closed) 
                {
                    isHandClose = true;
                    rightHand.sprite = handStateSprites [1];

                }

                if(isHandClose == true)
                {    //has handclosed, judge whether over the btns
                    if (RectTransformUtility.RectangleContainsScreenPoint (circle1.rectTransform, handSenPos, Camera.main) && circle1.enabled == true) 
                    {
                        //over but1
                        handClickFruit(btn1);
                        print ("1");
                    } 
                    else if (RectTransformUtility.RectangleContainsScreenPoint (circle2.rectTransform, handSenPos, Camera.main) && circle2.enabled == true) 
                    {
                        //over but2
                        handClickFruit(btn2);
                        print ("2");
                    } 
                    else if (RectTransformUtility.RectangleContainsScreenPoint (circle3.rectTransform, handSenPos, Camera.main) && circle3.enabled == true) 
                    {
                        //over but3    
                        handClickFruit(btn3);
                        print ("3");
                    } 

            }
        }

    }
}
    private void handClickFruit(Image clickFruit)
    {
        Rigidbody2D r1 = btn1.GetComponent<Rigidbody2D> ();
        Rigidbody2D r2 = btn2.GetComponent<Rigidbody2D> ();
        Rigidbody2D r3 = btn3.GetComponent<Rigidbody2D> ();
        if (clickFruit == btn1) 
        {

            r1.AddForce (new Vector2 (0,upForce));
        }
        else if(clickFruit == btn2)
        {
            r2.AddForce (new Vector2 (0,upForce));
        }
        else if(clickFruit == btn3)
        {
            r3.AddForce (new Vector2 (0,upForce));
        }
        r1.gravityScale = gravityScale;
        r2.gravityScale = gravityScale;
        r3.gravityScale = gravityScale;

        circle1.enabled = false;
        circle2.enabled = false;
        circle3.enabled = false;


   }

}


另一种转换更准确:如上两张图片所示


你可能感兴趣的:(UNITY3D)