Esempio n. 1
0
    void Update()
    {
        m_elapsedTime += Time.deltaTime;
        if (m_elapsedTime < updateIntervalSec)
        {
            return;
        }
        m_elapsedTime = 0f;

        // Map to correct axis

        // TODO - I really need to fix this. I have the UI of this app oriented facing down the -x axis which results in the
        // Z axis being treated as the X axis, which is why they're swapped here.
        Vector3 acceleration = Input.acceleration;

        acceleration = new Vector3(acceleration.z, acceleration.y, acceleration.x);

        /*
         *  The device's "Reference Frame" is the axis of the phone:
         *  The phone's z-axis points through the screen.
         *  The phone's x-axis points from top through bottom of the phone (speaker through home button)
         *  The phone's y-axis points from side to side through the phone (volume through power button)
         */
        Quaternion attitude      = m_gyroscope.attitude;
        Quaternion deltaAttitude = m_zeroInverseRotation * attitude;

        // Get Axis Angle changes from Zero Vector
        Vector3 deltaAngles = Vector3.zero;

        deltaAngles.x = Vector3.Angle(Vector3.up, Vector3.ProjectOnPlane(deltaAttitude * Vector3.up, Vector3.right));
        deltaAngles.y = Vector3.Angle(Vector3.forward, Vector3.ProjectOnPlane(deltaAttitude * Vector3.forward, Vector3.up));
        deltaAngles.z = Vector3.Angle(Vector3.right, Vector3.ProjectOnPlane(deltaAttitude * Vector3.right, Vector3.forward));

        // Write Values to Output
        m_angles = new Vector3(Calculator.ClampAngle(deltaAngles.x, 1), Calculator.ClampAngle(deltaAngles.y, 1), Calculator.ClampAngle(deltaAngles.z, 1));

        m_angles   = m_weightedMean.Add(m_angles).Mean();
        m_angles.x = Units.Round(m_angles.x, 1);
        m_angles.y = Units.Round(m_angles.y, 1);
        m_angles.z = Units.Round(m_angles.z, 1);


        int maxAxis = 0;

        for (int axis = 1; axis < 3; ++axis)
        {
            if (Mathf.Abs(m_angles[axis]) > Mathf.Abs(m_angles[maxAxis]))
            {
                maxAxis = axis;
            }
        }

        if (maxAxis == 0)
        {
            // X
            m_protractor.transform.rotation = Quaternion.Euler(0f, 180f, 90f);
        }
        else if (maxAxis == 1)
        {
            // Y
            m_protractor.transform.rotation = Quaternion.Euler(0f, 180f, 0f);
        }
        else
        {
            // Z
            m_protractor.transform.rotation = Quaternion.Euler(-90f, 180f, 0f);
        }

        //------------------------------------------------------------------------
        // NOTE: Keep This. It's Accurate and Useful for Debugging
        //------------------------------------------------------------------------
        // Vector3    deltaQuaternionAngles = deltaAttitude.eulerAngles;
        // Quaternion Representing Amount of Rotation Between Two Quaternions:
        //      deltaQuaternion = Quaternion.Inverse( fromQuaternion ) * toQuaternion;
        //debugText.text = "Quaternion Delta Angles:"
        //    + "\nX - " + ClampAngle( deltaQuaternionAngles.x, 1 )
        //    + "\nY - " + ClampAngle( deltaQuaternionAngles.y, 1 )
        //    + "\nZ - " + ClampAngle( deltaQuaternionAngles.z, 1 );
        //------------------------------------------------------------------------

        // Fire event
        onAngleChange();
    }
Esempio n. 2
0
    void Update()
    {
        m_elapsedTime += Time.deltaTime;
        if (m_elapsedTime < updateIntervalSec)
        {
            return;
        }
        m_elapsedTime = 0f;

        // Map to correct axis

        // TODO - I really need to fix this. I have the UI of this app oriented facing down the -x axis which results in the
        // Z axis being treated as the X axis, which is why they're swapped here.
        Vector3 acceleration = Input.acceleration;

        acceleration = new Vector3(acceleration.z, acceleration.y, acceleration.x);

        // Is device mostly flat, or mostly landscape?
        // Flat = acceleration along -X axis  [Display Y and X axis]
        // Landscape = acceleration along Y axis  [Display Z axis only]
        if (Vector3.Angle(acceleration, new Vector3(-1, 0, 0)) < 75f)
        {
            SetOrientation(Orientation.Flat);

            var yAccel = Vector3.ProjectOnPlane(acceleration, Vector3.forward);
            var xAccel = Vector3.ProjectOnPlane(acceleration, Vector3.up);
            // Because Acceleration along the x axis IS a rotation along the Y axis, and vice-versa
            var yAngle = 180f - Vector3.Angle(xAccel, Vector3.right);
            var xAngle = 180f - Vector3.Angle(yAccel, Vector3.right);

            m_angles   = m_weightedMean.Add(new Vector3(xAngle, yAngle, 0f)).Mean();
            m_angles.x = Units.Round(angles.x, 1);
            m_angles.y = Units.Round(angles.y, 1);

            // Update Bubble
            var bubblePos = new Vector2(
                m_bubbleRange.x * Mathf.Min(yAngle / 30f, 1f) * Mathf.Sign(acceleration.z) * -1f,
                m_bubbleRange.y * Mathf.Min(xAngle / 30f, 1f) * Mathf.Sign(acceleration.y) * -1f
                );
            bubblePos = Vector3.ClampMagnitude(bubblePos, m_bubbleRadius);

            m_bubbleLevelBubble.anchoredPosition = bubblePos;
        }
        else
        {
            SetOrientation(Orientation.Landscape);

            var zAccel = Vector3.ProjectOnPlane(acceleration, Vector3.right);
            var zAngle = 180f - Vector3.Angle(zAccel, Vector3.up);

            m_angles   = m_weightedMean.Add(new Vector3(0f, 0f, zAngle)).Mean();
            m_angles.z = Units.Round(angles.z, 1);

            // Update Bubble
            var bubblePos = new Vector2(
                Mathf.Sin(zAngle * Mathf.Deg2Rad) * m_bubbleRadius * Mathf.Sign(acceleration.z) * -1f,
                Mathf.Cos(zAngle * Mathf.Deg2Rad) * m_bubbleRadius
                );

            m_bubbleLevelBubble.anchoredPosition = bubblePos;
        }
        // Fire event
        onAngleChange();
    }