CameraMoving.cs 3.03 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using Windows.Kinect;
using System;
using System.IO;

public class CameraMoving : MonoBehaviour
{
    private KinectSensor _Sensor;
    private BodyFrameReader _Reader;
    private Body[] _Data = null;

    public Text stepText;

    public Body[] GetData()
    {
        return _Data;
    }

    int step = 0;
    float speed = 2f;

    KinectManager manager;

    void Start()
    {
        _Sensor = KinectSensor.GetDefault();

        if (_Sensor != null)
        {
            _Reader = _Sensor.BodyFrameSource.OpenReader();

            if (!_Sensor.IsOpen)
            {
                _Sensor.Open();
                Debug.Log(_Sensor.IsOpen);

            }
        }

    }

    // Update is called once per frame
    void Update()
    {
        if (_Reader != null)
        {
            var frame = _Reader.AcquireLatestFrame();
            if (frame != null)
            {
                //Debug.Log("frame");

                if (_Data == null)
                {
                    //Debug.Log("data");

                    _Data = new Body[_Sensor.BodyFrameSource.BodyCount];
                    Debug.Log("2");

                    transform.Translate(Vector3.forward * Time.deltaTime * speed);
                    //Debug.Log("3");

                    step = manager.UpdateKinect(_Data);
                    Debug.Log(step);

                    stepText.text = "Step: " + step;

                }

                frame.GetAndRefreshBodyData(_Data);


                frame.Dispose();
                frame = null;
            }
        }

    }
}

public class KinectManager
{
    int step_num = 0;

    //var bodyIndexFrame = 

    public int UpdateKinect(Windows.Kinect.Body[] bodies)
    {
        //Debug.Log("1");
        var body = bodies[0];
        var joints = body.Joints;
        //Debug.Log("2");

        //if (body == null || !body.IsTracked) return 0;

        JointType left_knee = (Windows.Kinect.JointType)13;
        JointType right_knee = (Windows.Kinect.JointType)17;
        //Debug.Log("3");

        var left_knee_position = joints[left_knee].Position;
        var right_knee_position = joints[right_knee].Position;
        //Debug.Log("4");
        //Walking
        if (right_knee_position.X < left_knee_position.X && right_knee_position.Y < left_knee_position.Y)
        {
            Debug.Log(string.Format("{0} STEP", step_num.ToString()));
            //Console.WriteLine(string.Format("{0} STEP", step_num.ToString()));
            step_num++;
        }
        else if (right_knee_position.X > left_knee_position.X && right_knee_position.Y > left_knee_position.Y)
        {
            Debug.Log(string.Format("{0} STEP", step_num.ToString()));
            //Console.WriteLine(string.Format("{0} STEP", step_num.ToString()));
            step_num++;
        }
        else
        {
            Debug.Log("not walking");
            //Console.WriteLine("not walking");
        }
        return step_num;
    }

}