Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (!filterInFixedUpdate)
        {
            if (!inputTransform)
            {
                Debug.LogError("ERROR: inputTransform is None! Set it in the inspector.");
                return;
            }
            if (inputInLocalCoordinates)
            {
                inputPos = inputTransform.localPosition;
                inputRot = inputTransform.localRotation;
            }
            else
            {
                inputPos = inputTransform.position;
                inputRot = inputTransform.rotation;
            }

            measuredPos[0] = inputPos.x;
            measuredPos[1] = inputPos.y;
            measuredPos[2] = inputPos.z;
            filterPos.setR(Time.deltaTime * positionNoiseCovariance);
            filterPos.predict();
            filterPos.update(measuredPos);
            pos = filterPos.getState();

//			measuredRot[0] = inputRot.x;
//			measuredRot[1] = inputRot.y;
//			measuredRot[2] = inputRot.z;
//			measuredRot[3] = inputRot.w;
//			filterRot.setR(Time.deltaTime * rotationNoiseCovariance);
//		    filterRot.predict();
//		    filterRot.update(measuredRot);
//			rot = filterRot.getState();

            filterRot.rotationNoiseCovariance = rotationNoiseCovariance;
            filterRot.Update(inputRot, Time.deltaTime);

            if (GetComponent <Rigidbody>() == null)
            {
                if (inputInLocalCoordinates)
                {
                    outputTransform.localPosition = new Vector3((float)pos[0], (float)pos[1], (float)pos[2]);
                    outputTransform.localRotation = filterRot.rotationState;
                }
                else
                {
                    outputTransform.position = new Vector3((float)pos[0], (float)pos[1], (float)pos[2]);
                    outputTransform.rotation = filterRot.rotationState;
                }
            }
        }
    }
Exemplo n.º 2
0
    public void Update()
    {
        if (!isTracking && skeletonManager.skeletons[bodyTrackingDeviceID, playerId].isTracking)
        {
            PlayerFound();
        }
        else if (isTracking && !skeletonManager.skeletons[bodyTrackingDeviceID, playerId].isTracking)
        {
            PlayerLost();
        }
        else if (!skeletonManager.skeletons[bodyTrackingDeviceID, playerId].isTracking)
        {
            return;
        }

        if (!highlightStartObject && wandSelector.HighlightedObject)
        {
            highlightStartObject = wandSelector.HighlightedObject;
            gestureRecognizer.EnableGesture();
        }
        else if (!wandSelector.HighlightedObject)
        {
            highlightStartObject = null;

            if (!wandSelector.Selection)
            {
                gestureRecognizer.DisableGesture();
            }
        }

        visualizerThreshold = Mathf.Clamp01(visualizerThreshold);

        RUISSkeletonManager.JointData startData = skeletonManager.GetJointData(wandStart, playerId, bodyTrackingDeviceID);
        RUISSkeletonManager.JointData endData   = skeletonManager.GetJointData(wandEnd, playerId, bodyTrackingDeviceID);

        if (endData.positionConfidence >= 0.5f)
        {
            // TUUKKA: Original code
//            transform.localPosition = endData.position;
//
//            if (startData != null && startData.positionConfidence >= 0.5f)
//            {
//                transform.localRotation = Quaternion.LookRotation(endData.position - startData.position);
//            }
//            else if (endData.rotationConfidence >= 0.5f)
//            {
//                transform.localRotation = endData.rotation;
//            }

            // First calculate local rotation
            if (startData != null && startData.positionConfidence >= 0.5f)
            {
                tempVector = endData.position - startData.position;
                if (Vector3.Angle(startData.rotation * Vector3.up, tempVector) > 5)
                {
                    tempRotation = Quaternion.LookRotation(endData.position - startData.position, startData.rotation * Vector3.up);
                }
                else
                {
                    tempRotation = Quaternion.LookRotation(endData.position - startData.position, startData.rotation * Vector3.right);
                }

                filteredRotation = rotationFilter.Update(tempRotation, Time.deltaTime);                 // HACK with kinect2 filtering is done in SkeletonManager
            }
//            else if (endData.rotationConfidence >= 0.5f)
//            {
//				tempRotation = endData.rotation;
//				filteredRotation = rotationFilter.Update(tempRotation, Time.deltaTime);
//            }

            if (rigidbody)
            {
                // TUUKKA:
                if (transform.parent)
                {
                    // If the wand has a parent, we need to apply its transformation first
                    rigidbody.MovePosition(transform.parent.TransformPoint(endData.position));
                    rigidbody.MoveRotation(transform.parent.rotation * filteredRotation);
                }
                else
                {
                    rigidbody.MovePosition(endData.position);
                    rigidbody.MoveRotation(filteredRotation);
                }
            }
            else
            {
                // If there is no rigidBody, then just change localPosition & localRotation
                transform.localPosition = endData.position;
                transform.localRotation = filteredRotation;
            }
        }
    }