Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            position = new Vector3(0, 0.54f, 0);
            ignoreX  = false;
        }

        prevAcceleration = acceleration;

        acceleration = synthesizer.GetAcceleration();
        rotation     = synthesizer.GetRotation();
        rotationRate = synthesizer.GetRotationRate();
        compass      = synthesizer.GetCompass();

//		acceleration = -Move(0.6f);

        //Invert y readings to reflect device movement correctly
        acceleration.x = -acceleration.x;
        acceleration.y = -acceleration.y;

        //If using calculation
//		acceleration.z = -(acceleration.z);

//		//If using Android Linear Acceleration
        if (synthesizer.isBroadcasterStarted)
        {
            acceleration.z = -(acceleration.z + 0.15f);
        }
        else
        {
            acceleration.z = -acceleration.z;
        }

        acceleration = LowPassFilter(prevAcceleration, acceleration);

        prevHighPassValue = HighPassValue;
        HighPassValue     = HighPassFilter(prevAcceleration, acceleration);
        acceleration      = HighPassValue;


        prevVelocity = velocity;
//		velocity = prevVelocity + acceleration * Time.deltaTime;
//		distance = prevVelocity * Time.deltaTime + 0.5f * acceleration * Time.deltaTime * Time.deltaTime;

        // Rotation Rate Limit------------------------------------------------------------------

        //Rotation Rate Limit
        if (Mathf.Abs(rotationRate.y) >= 1f)
        {
            newX     = prevNewX;
            xTooMuch = true;
            moveRate = 0f;
            StartCoroutine(Wait(0f));
            xTooMuch = false;
        }
        if (Mathf.Abs(rotationRate.x) >= 1f)
        {
            newY     = prevNewY;
            yTooMuch = true;
            moveRate = 0f;
            StartCoroutine(Wait(0f));
            yTooMuch = false;
            //			moveRate = maxMoveRate;
        }
        if (Mathf.Abs(rotationRate.z) >= 1f)
        {
            newZ     = prevNewZ;
            zTooMuch = true;
            moveRate = 0f;
            StartCoroutine(Wait(0f));
            zTooMuch = false;
        }

//Kalman Filter------------------------------------------------------------------

        if (!xTooMuch)
        {
            prevOriX = oriX;
            prevNewX = newX;
            oriX     = acceleration.x;
            newX     = kalman[0].GetAngle(acceleration.x, 0f, Time.deltaTime);
        }

        if (!yTooMuch)
        {
            prevOriY = oriY;
            prevNewY = newY;
            oriY     = acceleration.y;
            newY     = kalman[1].GetAngle(acceleration.y, 0f, Time.deltaTime);
        }

        if (!zTooMuch)
        {
            prevOriZ = oriZ;
            prevNewZ = newZ;
            oriZ     = acceleration.z;
            newZ     = kalman[2].GetAngle(acceleration.z, 0f, Time.deltaTime);
        }


//		//Limit moveRate depending on gradient
//		if (((newY - prevNewY) / Time.deltaTime) > 50f)
//		{
//			moveRate = 0f;
//			newY = prevNewY;
//		}
//		else if (((newZ - prevNewZ) / Time.deltaTime) > 50f)
//		{
//			moveRate = 0f;
//			newZ = prevNewZ;
//		}
//		else if (((newX - prevNewX) / Time.deltaTime) > 50f)
//		{
//			moveRate = 0f;
//			newX = prevNewX;
//		}
//		else
//		{
//			moveRate = maxMoveRate;
//		}
//
//
        //Find resultant vector to compensate accel data when device is tilted
        //FAILED!!!!//
//		if (Mathf.Abs (rotation.x) >= 0.4f)
//		{
//			float angle = rotation.x * 180f / Mathf.PI;
//			newY = newY * Mathf.Cos (angle) - newZ * Mathf.Sin (angle);
//			newZ = newY * Mathf.Sin (angle) + newZ * Mathf.Cos (angle);
//		}
//		if (Mathf.Abs (rotation.y) >= 0.4f)
//		{
//			float angle = rotation.y * 180f / Mathf.PI;
//			newZ = newZ * Mathf.Cos (angle) - newX * Mathf.Sin (angle);
//			newX = newZ * Mathf.Sin (angle) + newX * Mathf.Cos (angle);
//		}
//		if (Mathf.Abs (rotation.z) >= 0.4f)
//		{
//			float angle = rotation.z * 180f / Mathf.PI;
//			newX = newX * Mathf.Cos (angle) - newY * Mathf.Sin (angle);
//			newY = newX * Mathf.Sin (angle) + newY * Mathf.Cos (angle);
//		}


        if (plotter != null)
        {
            plotter.SetColor(1, Color.red);
            plotter.SetColor(2, Color.magenta);
            plotter.SetColor(3, Color.green);
            plotter.SetColor(4, Color.yellow);
            plotter.SetColor(5, Color.blue);
            plotter.SetColor(6, Color.cyan);
            plotter.SetColor(0, Color.blue);



            //X value
            plotter.Plot(prevOriX, oriX, 1, showLines[1]);

            plotter.Plot(prevNewX, newX, 2, showLines[2]);

            //Y value
            plotter.Plot(prevOriY, oriY, 3, showLines[3]);

            plotter.Plot(prevNewY, newY, 4, showLines[4]);

            //Z value
            plotter.Plot(prevOriZ, oriZ, 5, showLines[5]);

            plotter.Plot(prevNewZ, newZ, 6, showLines[6]);

            plotter.Plot(0f, 0f, 0, showLines[0]);
        }

        if (Mathf.Abs(newX) > threshold && Mathf.Abs(newX) < limit * 2f)
        {
            velocity.x = prevVelocity.x + newX * Time.deltaTime;
            distance.x = prevVelocity.x * Time.deltaTime + 0.5f * newX * Time.deltaTime * Time.deltaTime;
        }
        else
        {
            velocity.x = 0f;
            distance.x = 0f;
            newX       = 0f;
        }

        if (Mathf.Abs(newY) > threshold && Mathf.Abs(newY) < limit)
        {
            velocity.z = prevVelocity.z + newY * Time.deltaTime;
            distance.z = prevVelocity.z * Time.deltaTime + 0.5f * newY * Time.deltaTime * Time.deltaTime;
        }
        else
        {
            velocity.z = 0f;
            distance.z = 0f;
            newY       = 0f;
        }

        if (Mathf.Abs(newZ) > threshold && Mathf.Abs(newZ) < limit)
        {
            velocity.y = prevVelocity.y + newZ * Time.deltaTime;
            distance.y = prevVelocity.y * Time.deltaTime + 0.5f * newZ * Time.deltaTime * Time.deltaTime;
        }
        else
        {
            velocity.y = 0f;
            distance.y = 0f;
            newZ       = 0f;
        }

//------------------------------------------------------------------------------

//----------------------------------------------------------------

        if (Input.GetKeyDown(KeyCode.C))
        {
            plotter.ClearAllList();
        }

        if (Mathf.Abs(rotation.x) <= threshold && Mathf.Abs(rotation.y) <= threshold)
        {
            position.x += distance.x * 5f;
            position.z += distance.z * 5f;
            position.y += distance.y * 5f;
        }


        float dist        = Vector3.Distance(controlObject.transform.position, position);
        float distCovered = (Time.time - startTime);
        float ratio       = dist / distCovered;


////Move parent gameobject according to child axis (Scene AndroidBroadcaster)
//		Transform child = controlObject.transform.GetChild (0);

////Move backupCamParent according to headCamera axis (Scene Test)
        Transform child = controlObject.transform.GetChild(0).GetChild(0);
        Vector3   tem   = child.transform.TransformDirection(distance);

//		controlObject.transform.position =  Vector3.Lerp (controlObject.transform.position, tem, ratio);
//		controlObject.transform.localPosition =  Vector3.Lerp (controlObject.transform.localPosition, position, ratio);
//		Vector3 a = controlObject.transform.TransformVector (controlObject.transform.localPosition);
//		controlObject.transform.position = Vector3.Lerp (controlObject.transform.position, a, ratio);

//		controlObject.transform.position =  Vector3.Lerp (controlObject.transform.position, position, ratio);
//				Debug.Log(child.transform.localPosition + "::" + distance.x + ":" + distance.y + ":" + distance.z);
//		Debug.Log (tem.x + ":" + tem.y + ":" + tem.z);
//		Debug.Log(distance.x + ":" + distance.y + ":" + distance.z);
//		Debug.Log (velocity.x + ":" + velocity.y + ":" + velocity.z);

        controlObject.GetComponent <Rigidbody>().velocity = tem * moveRate;
        if (controlObject.transform.position.y >= 1.5f)
        {
            Vector3 newPos = controlObject.transform.position;
            newPos.y = 1.5f;
            controlObject.transform.position = newPos;
        }

//----------------------------------------------------------------------------------------------------

        //controlObject.transform.rotation = Quaternion.Lerp (controlObject.transform.rotation, synthesizer.GetRotation(), ratio);

        startTime = Time.time;
    }
Ejemplo n.º 2
0
    private void OnNewPoses(TrackedDevicePose_t[] poses)
    {
        SteamVR_TrackedObject.EIndex trackerIndex = (SteamVR_TrackedObject.EIndex)SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.First, ETrackedDeviceClass.GenericTracker);
        SteamVR_TrackedObject[]      index        = GameObject.FindObjectsOfType <SteamVR_TrackedObject> ();

        if (trackerIndex == SteamVR_TrackedObject.EIndex.None)
        {
//		Debug.Log ("Couldn't find tracker.Please turn on the tracker.");
        }
        foreach (SteamVR_TrackedObject obj in index)
        {
            if (obj.index == trackerIndex)
            {
                tracker = obj.gameObject;
            }
        }
//		SteamVR.instance.hmd.GetDeviceToAbsoluteTrackingPose()


        if (mainCam == null || backupCam == null)
        {
            Debug.Log("VR Cam is null");
        }

        //****For Testing purpose******
        if (!lostTrack)
        //Check whether hmd pose is valid (hmd is trackable)
//		if(poses[0].bPoseIsValid)
        {
            hmdTracking = true;
            aligned     = false;

            //headCamera.transform.SetParent (mainSteamVRObject.transform);
            if (backupCamera.activeSelf)
            {
                backupCamera.GetComponent <Camera> ().depth   = -1;
                headCamera.GetComponent <Camera> ().depth     = 1;
                backupCamera.GetComponent <Camera> ().enabled = true;
                backupCamera.SetActive(true);
                headCamera.gameObject.SetActive(false);
                backupCamera.tag = "BackupCamera";
                mainCam.enabled  = false;
            }
            prevCamRot = headCamera.transform.rotation;
            prevCamPos = headCamera.transform.position;

            Vector3 v1         = Vector3.zero - prevCamPos;
            Vector3 unitVector = v1 / v1.magnitude;
            Vector3 v2         = new Vector3(-unitVector.x, 0f, -unitVector.z);
            alignVector = 4f * v2;

            Debug.DrawLine(Vector3.zero, backupCamParent.transform.position, Color.red);
            Debug.DrawLine(Vector3.zero, alignVector, Color.green);
            Debug.DrawLine(backupCamParent.transform.position, alignVector, Color.blue);


            if (Input.GetKeyDown(KeyCode.Space) && synthesizer.isBroadcasterStarted)
            {
                Quaternion gyro    = synthesizer.GetRotation();
                Quaternion gyroRot = new Quaternion(gyro.y, -gyro.z, -gyro.x, gyro.w);

//				Quaternion onlyRot = new Quaternion (0f, -gyro.z, 0f, gyro.w);
                Quaternion onlyRot = new Quaternion(0f, backupCamera.transform.rotation.z, 0f, backupCamera.transform.rotation.w);
                initialRot = onlyRot;

                Vector3 compass = synthesizer.GetCompass();
                compassHeading = compass.x;

                Debug.Log("Initial Rotation : " + initialRot + " ~ Compass Heading : " + compassHeading);
            }
        }
        else
        {
            Vector3    acc     = synthesizer.GetAcceleration();
            Quaternion gyro    = synthesizer.GetRotation();
            Vector3    compass = synthesizer.GetCompass();


            GetRotatingDirection();
            hmdTracking = false;
            backupCamera.SetActive(true);
            if (headCamera.activeSelf)
            {
                backupCamera.GetComponent <Camera> ().depth   = 1;
                headCamera.GetComponent <Camera> ().depth     = -1;
                backupCamera.GetComponent <Camera> ().enabled = true;
                headCamera.gameObject.SetActive(false);
            }


//			tracker.transform.position = Vector3.Lerp (tracker.transform.position, prevCamPos, 1.0f);
            if (useGyroscope)
            {
                Quaternion newRot = new Quaternion();
//
                newRot = new Quaternion(gyro.y, -gyro.z, -gyro.x, gyro.w) * Quaternion.Euler(euler);
                tracker.transform.rotation = Quaternion.Lerp(tracker.transform.rotation, newRot, 0.1f);
            }
            else
            {
                Quaternion newRot = new Quaternion(acc.x, acc.y, -acc.z, 0.4f);
                tracker.transform.rotation = Quaternion.Lerp(tracker.transform.rotation, newRot, 0.1f);
            }

            //Use intermediary (parent) to compensate misaligned camera localposition & localrotation
            Transform  parent     = backupCamParent.transform.GetChild(0);
            Quaternion compensate = Quaternion.Inverse(backupCam.transform.localRotation);
            parent.transform.localRotation = Quaternion.Lerp(parent.transform.localRotation, compensate, 0.1f);
            parent.transform.localPosition = -backupCam.transform.localPosition;

//			Quaternion r = tracker.transform.rotation * Quaternion.Inverse (prevCamRot) ;
            Quaternion r = tracker.transform.rotation;
            backupCamParent.transform.rotation = Quaternion.Lerp(backupCamParent.transform.rotation, r, 0.1f);

            if (!aligned)
            {
//				AlignCamera ();
                aligned = true;

                Quaternion onlyRot = new Quaternion(0f, -gyro.z, 0f, gyro.w);
//				Quaternion onlyRot = new Quaternion(0f, -backupCamera.transform.rotation.z, 0f, backupCamera.transform.rotation.w);
                float angle = Quaternion.Angle(initialRot, onlyRot);
                if (compass.x - compassHeading < 0f)
                {
                    //anti-clockwise
                    if (angle > 0f)
                    {
                        angle = -angle;
                    }
                }
                else
                {
                    //clockwise
                    if (angle < 0f)
                    {
                        angle = -angle;
                    }
                }
                environment.transform.rotation       = Quaternion.AngleAxis(-angle, environment.transform.up);
                mainSteamVRObject.transform.rotation = Quaternion.AngleAxis(-angle, mainSteamVRObject.transform.up);
                mainSteamVRObject.transform.position = GameObject.Find("CameraRig Marker 1").transform.position;
                Debug.Log("Angle : " + (-angle));
            }

            Debug.DrawRay(initialEnvironmentPosition, Vector3.forward * 10f + initialEnvironmentPosition, Color.red);
            Debug.DrawRay(environment.transform.position, environment.transform.forward * 10f, Color.blue);

//			Vector3 newPos = backupCam.transform.position;
//			newPos.y = height;
//			backupCamParent.transform.position = newPos;
        }
    }