// Use this for initialization
    void Start()
    {
/*
        _gameObjectBM = BodyManager.BM;
        if (BodyManager == null)
        {
            return;
        }
*/
       // _bodyManager = BodyManager.GetComponent<BodyManager>();
        _bodyManager = BodyManager.BM;
        if (_bodyManager == null)
        {
            return;
        }
        _canvasExerciseManager = CanvasExerciseManager.GetComponent<CanvasExerciseManager>();

        _kinectSensor = _bodyManager.GetSensor();

        // initialize Body array with # of maximum bodies
        _bodies = _bodyManager.GetBodies();
        Debug.Log(_bodyManager + " | " + _bodies);
        //_bodyFrameReader = _kinectSensor.BodyFrameSource.OpenReader();

        // Initialize new GestureDetector list
        _gestureDetectorList = new List<GestureDetector>();
        // For every body add a new GestureDetector instance to the list

        for (int bodyIndex = 0; bodyIndex < _bodies.Length; bodyIndex++)
        {
            GestureTextGameObject.text = "none";
            _gestureDetectorList.Add(new GestureDetector(_kinectSensor));
        }
    }
Beispiel #2
0
    // check if BodyManager has data
    void Update()
    {
        int state = 0;

        if (BodyManager == null)
        {
            return;
        }

        _BodyManager = BodyManager.GetComponent <BodyManager> ();        // Store gameobject for usage
        if (_BodyManager == null)
        {
            return;
        }

        Kinect.Body[] data = _BodyManager.GetBodies();  // Get bodies of bodymanager
        if (data == null)
        {
            return;
        }

        List <ulong> trackedIds = new List <ulong>(); // List to store tracked body ids

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

            if (body.IsTracked)
            {
                trackedIds.Add(body.TrackingId);        // add body with its id into the list
            }
        }

        List <ulong> knownIds = new List <ulong> (_Bodies.Keys); // List with current bodies

        foreach (ulong trackingId in knownIds)
        {
            // If curent tracked body list contains not any tracking id --> Delete untracked body
            if (!trackedIds.Contains(trackingId))
            {
                Destroy(_Bodies [trackingId]);
                _Bodies.Remove(trackingId);
            }
        }

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

            if (body.IsTracked)
            {
                if (!_Bodies.ContainsKey(body.TrackingId))                         // if list of bodies contains not current tracking id
                {
                    _Bodies [body.TrackingId] = CreateBodyObject(body.TrackingId); // Create body object with id and connected joints
                    _Bodies[body.TrackingId].gameObject.tag = "Player";
                }
                RefreshBodyObject(body, _Bodies [body.TrackingId]);  // Refresh current body and its GameObject
            }

            // Tracking for UI Component
            if (body.IsTracked)
            {
                KinectInputModule.instance.TrackBody(body);
            }
        }
    } // Update
    // Use this for initialization
    void Start()
    {
        // -----------------------------------------
        // ------------ INITIALIZATIONS ------------
        // -----------------------------------------

        CanvasHandCursor = GameObject.Find("CanvasHandCursor");

        // Disable handcursor in execution mode TODO all disable in testmode, enable in production
        bodyManager = GameObject.Find("BodyManager");

        KinectManager = GameObject.Find("KinectManager");
        if (KinectManager == null)
        {
            return;
        }

        _kinectManager      = KinectManager.GetComponent <KinectManager>();
        _interactionManager = _kinectManager.GetComponent <InteractionManager>();

        if (CanvasHandCursor.activeSelf)
        {
            _interactionManager.enabled = false;
            CanvasHandCursor.gameObject.SetActive(false);
        }

        Debug.Log("IsUserDetected: " + _kinectManager.IsUserDetected());


        _exerciseExecutionValidationManager = ExerciseExecutionValidationManager.GetComponent <ExerciseExecutionValidationManager>();

        // Reference to exercise data of current user
//		_currentExerciseData = UserDataObject.currentUser.exerciseData[PlayerPrefs.GetInt("CurrentExerciseId")];
        _currentExerciseData = UserDataObject.GetCurrentExercise();
        _lastExercise        = UserDataObject.GetLastTierExercise();

        // Duration manager
        _durationManager = durationManager.GetComponent <DurationManager>();

        _minTimeAlreadyReached = false;
        _repsIterator          = 0;

        _methodCheckedArray = new bool[UserDataObject.GetCurrentChecksArray().Length];

        // -----------------------------------------
        // ------------- UI COMPONENTS -------------
        // -----------------------------------------

        // Exercise name
        titleText.text       = UserDataObject.GetCurrentExerciseName().ToUpper();
        standingLegText.text = UserDataObject.GetCurrentSide().direction;
        successPanel         = successPanel.GetComponent <CanvasGroup>(); // TODO implement success animation for rep

        foreach (var check in UserDataObject.GetCurrentChecksArray())
        {
            GameObject gameObjectCheckItem = Instantiate(checkItem);
            CheckItem  currentCheckItem    = gameObjectCheckItem.GetComponent <CheckItem>();

            currentCheckItem.methodName       = check.methodName;
            currentCheckItem.description.text = check.description;
            checkItemList.Add(currentCheckItem);

            gameObjectCheckItem.transform.SetParent(checkSpacer, false);
        }

        // Array of Toggles
        _toggleArray = new Toggle[UserDataObject.GetCurrentRepetitionsArray().Length];

        // Create and check toggles for each rep of current exercise
        foreach (var repetition in UserDataObject.GetCurrentRepetitionsArray())
        {
            GameObject gameObjectToggle = Instantiate(toggle);
            Toggle     currentToggle    = gameObjectToggle.GetComponent <Toggle>();

            _toggleArray[Array.IndexOf(UserDataObject.GetCurrentRepetitionsArray(), repetition)] = currentToggle;

            // Check if exercise not already accomplished
            if (!_currentExerciseData.accomplished)
            {
                // look for accomplished reps, check regarding toggles and set current rep
                if (repetition.accomplished)
                {
                    _repsIterator += 1;
                    currentToggle.GetComponent <Toggle>().isOn = true;
                }
                else if (_currentRepetition == null)
                {
                    _currentRepetition = repetition;
                }
            }
            else             // If exercise already accomplished
            {
                // Set first rep as current rep
                if (_currentRepetition == null)
                {
                    repetition.attempts   = 0;
                    repetition.confidence = 0.0f;
                    repetition.userTime   = 0.0f;

                    _currentRepetition = repetition;
                }
            }
            // Append GO to group
            gameObjectToggle.transform.SetParent(toggleGroup, false);
        }
        textReps.text = "Reps " + _repsIterator + "/" + UserDataObject.GetCurrentRepetitionsArray().Length;

        // Set ID of current repetition
        PlayerPrefs.SetInt("CurrentRepetitionId", Array.IndexOf(UserDataObject.GetCurrentRepetitionsArray(), _currentRepetition));

        // -----------------------------------------
        // ---------------- KINECT -----------------
        // -----------------------------------------

        _bodyManager = bodyManager.GetComponent <BodyManager>();
        if (_bodyManager == null)
        {
            return;
        }

        _kinectSensor = _bodyManager.GetSensor();

        _bodies = _bodyManager.GetBodies();
        Debug.Log(_bodyManager + " | " + _bodies);

        // Initialize gesture detector object
        _gestureDetectorList = new List <GestureDetector>();
        for (int bodyIndex = 0; bodyIndex < _bodies.Length; bodyIndex++)
        {
            _gestureDetectorList.Add(new GestureDetector(_kinectSensor));
        }

        // Foot joints for getting positions
        _jointFootRight           = KinectInterop.JointType.FootRight;
        _jointFootLeft            = KinectInterop.JointType.FootLeft;
        _startingHeightDifference = UserDataObject.GetCurrentExerciseStartingHeightDifference();
        heightTestIterator        = 0;

        // Initial foot position
        if (_kinectManager.IsUserDetected())
        {
            long userId = _kinectManager.GetPrimaryUserID();

            if (_kinectManager.IsJointTracked(userId, (int)_jointFootRight) &&
                _kinectManager.IsJointTracked(userId, (int)_jointFootLeft))
            {
                _initialStartingHeightLeft  = _kinectManager.GetJointKinectPosition(userId, (int)_jointFootLeft).y;
                _initialStartingHeightRight = _kinectManager.GetJointKinectPosition(userId, (int)_jointFootRight).y;

                initialLeftFootText.text  += _initialStartingHeightLeft.ToString();
                initialRightFootText.text += _initialStartingHeightRight.ToString();
            }
        }
        _bothFeetUp         = false;
        _inStartingPosition = false;


        if (UserDataObject.GetCurrentExerciseFileName() == "WalkForward")
        {
            _progressMinConfidence = 0.8f;
        }
        if (UserDataObject.GetCurrentExerciseFileName() == "BobOne")
        {
            _progressMinConfidence = 0.4f;
        }
        Debug.Log("_progressMinConfidence: " + _progressMinConfidence);
    }