/*	void FixedUpdate ()
 *      {
 *              if ( resetFlag )
 *              {
 *                      ResetOrientation ();
 *                      resetFlag = false;
 *              }
 *              CheckSetPose ();
 *
 *              rb.useGravity = UseGravity;
 *              CheckConstraints ();
 *
 *              if ( MotorsEnabled )
 *              {
 *                      if ( useTwist )
 *                      {
 *                              // just set linear and angular velocities, ignoring forces
 *                              rb.velocity = LinearVelocity;
 * //				rb.velocity = clampMaxSpeed ? Vector3.ClampMagnitude ( LinearVelocity, maxSpeedMS ) : LinearVelocity;
 *                              // new: flip angular velocity to generate CCW rotations
 * //				Vector3 angVel = -AngularVelocity;
 * //				if ( ConstrainTorqueX )
 * //					angVel.z = 0;
 * //				if ( ConstrainTorqueY )
 * //					angVel.x = 0;
 * //				if ( ConstrainTorqueZ )
 * //					angVel.y = 0;
 * //				rb.angularVelocity = angVel;
 *                              rb.angularVelocity = -AngularVelocity;
 *
 *                      } else
 *                      {
 *
 *                              // add force
 *                              if ( clampForce )
 *                                      force = Vector3.ClampMagnitude ( force, maxForce );
 *                              rb.AddRelativeForce ( force, forceMode );
 *
 *                              // add torque. but first clamp it
 *                              if ( maxTorqueDegrees != 0 )
 *                                      maxTorqueRadians = maxTorqueDegrees * Mathf.Deg2Rad;
 *
 *                              if ( clampTorque )
 *                                      torque = Vector3.ClampMagnitude ( torque, maxTorqueRadians );
 * //				rb.AddRelativeTorque ( newTorque, torqueMode );
 *                              rb.AddRelativeTorque ( -torque, torqueMode );
 *
 *                              // update acceleration
 *                              LinearAcceleration = ( rb.velocity - lastVelocity ) / Time.deltaTime;
 *                              lastVelocity = rb.velocity;
 *                              LinearVelocity = rb.velocity;
 *                              // new: flip angular velocity to match flipped torque
 * //				rb.angularVelocity = angVel;
 * //				AngularVelocity = -angVel;
 *                              AngularVelocity = -rb.angularVelocity;
 *                      }
 *              }
 *              curSpeed = rb.velocity.magnitude;
 *      }*/

    void OnGUI()
    {
        string       info = "";
        Vector2      size;
        Rect         r;
        GUIStyle     label    = GUI.skin.label;
        TextClipping clipping = label.clipping;

        label.clipping = TextClipping.Overflow;
        bool wrap = label.wordWrap;

        label.wordWrap = false;
        int fontSize = label.fontSize;

        label.fontSize = (int)(22f * Screen.height / 1080);

        if (showTelemetry)
        {
            info = @"Force: " + Force.ToRos().ToString() +
                   "\nTorque: " + Torque.ToRos().ToString() +
                   "\nPosition: " + Position.ToRos().ToString() +
                   "\nRPY: " + (-Rotation.eulerAngles).ToRos().ToString() +
                   "\nLinear Vel.: " + LinearVelocity.ToRos().ToString() +
                   "\nAngular Vel.: " + AngularVelocity.ToRos().ToString() +
                   "\nGravity " + (UseGravity ? "on" : "off") +
                   "\nLocal input " + (inputCtrl.localInput ? "on" : "off");
            if (ConstrainForceX)
            {
                info += "\nX Movement constrained";
            }
            if (ConstrainForceY)
            {
                info += "\nY Movement constrained";
            }
            if (ConstrainForceZ)
            {
                info += "\nZ Movement constrained";
            }
            if (ConstrainTorqueX)
            {
                info += "\nX Rotation constrained";
            }
            if (ConstrainTorqueY)
            {
                info += "\nY Rotation constrained";
            }
            if (ConstrainTorqueZ)
            {
                info += "\nZ Rotation constrained";
            }

//			GUIStyle label = GUI.skin.label;
//			TextClipping clipping = label.clipping;
//			label.clipping = TextClipping.Overflow;
//			bool wrap = label.wordWrap;
//			label.wordWrap = false;
//			int fontSize = label.fontSize;
//			label.fontSize = (int) ( 22f * Screen.height / 1080 );

            size = label.CalcSize(new GUIContent(info));
            r    = new Rect(10, 10, size.x + 10, size.y);
            GUI.Box(r, "");
            GUI.Box(r, "");
            r.x += 5;

            GUILayout.BeginArea(r);
            GUILayout.Label(info);
            GUILayout.EndArea();
        }         // telemetry

        // show axis arrows
        if (drawArrows)
        {
            bool showMovement = ConstrainForceX || ConstrainForceY || ConstrainForceZ || drawArrowsAlways;
            bool showRotation = ConstrainTorqueX || ConstrainTorqueY || ConstrainTorqueZ || drawArrowsAlways;
            // x arrow
            Camera  cam         = Camera.main;
            float   screenRatio = 1f * Screen.height / 1080;
            Vector2 texSize     = new Vector2(48, 8) * screenRatio;
            Vector2 texSize2    = new Vector2(16, 16) * screenRatio;
            float   arrowMag    = texSize.magnitude + 14;
            Vector3 pos         = transform.position;
            Vector2 screenPos   = cam.WorldToScreenPoint(pos);
            screenPos.y = Screen.height - screenPos.y;
            Vector2 top = cam.WorldToScreenPoint(pos + Up * 0.5f);
            top.y = Screen.height - top.y;
            Vector2 tip = cam.WorldToScreenPoint(pos + XAxis * 0.75f);
            tip.y = Screen.height - tip.y;
            Vector2 toTip   = (tip - screenPos).normalized;
            Rect    texRect = new Rect(screenPos - texSize, texSize * 2);
//			Rect texRect2 = new Rect ( screenPos + ( top - screenPos ).normalized * arrowMag - texSize2, texSize2 * 2 );
            Rect  texRect2 = new Rect(screenPos + toTip * arrowMag - texSize2, texSize2 * 2);
            float angle    = Vector2.Angle(Vector2.right, toTip);
            if (tip.y > screenPos.y)
            {
                angle = -angle;
            }
            GUIUtility.RotateAroundPivot(-angle, screenPos);
            GUI.color = axisColors [0];
            if (showMovement && !ConstrainForceX)
            {
                GUI.DrawTexture(texRect, axisArrows [0]);
            }
            GUIUtility.RotateAroundPivot(angle, screenPos);
            if (showRotation && !ConstrainTorqueX)
            {
                GUI.DrawTexture(texRect2, axisArrows [1]);
            }
//			GUI.DrawTexture ( new Rect ( tip.x - 2, tip.y - 2, 4, 4 ), dot );

            // y arrow
            tip   = cam.WorldToScreenPoint(pos + YAxis * 0.75f);
            tip.y = Screen.height - tip.y;
            toTip = (tip - screenPos).normalized;
            angle = Vector2.Angle(Vector2.right, toTip);
            if (tip.y > screenPos.y)
            {
                angle = -angle;
            }
            GUIUtility.RotateAroundPivot(-angle, screenPos);
            GUI.color = axisColors [1];
            if (showMovement && !ConstrainForceY)
            {
                GUI.DrawTexture(texRect, axisArrows [0]);
            }
            GUIUtility.RotateAroundPivot(angle, screenPos);
            texRect2.position = screenPos + toTip * arrowMag - texSize2;
            if (showRotation && !ConstrainTorqueY)
            {
                GUI.DrawTexture(texRect2, axisArrows [1]);
            }
//			GUI.DrawTexture ( new Rect ( tip.x - 2, tip.y - 2, 4, 4 ), dot );

            // z arrow
            tip   = cam.WorldToScreenPoint(pos + Up * 0.5f);
            tip.y = Screen.height - tip.y;
            toTip = (tip - screenPos).normalized;
            angle = Vector2.Angle(Vector2.right, toTip);
            if (tip.y > screenPos.y)
            {
                angle = -angle;
            }
            GUIUtility.RotateAroundPivot(-angle, screenPos);
            GUI.color = axisColors [2];
            if (showMovement && !ConstrainForceZ)
            {
                GUI.DrawTexture(texRect, axisArrows [0]);
            }
            GUIUtility.RotateAroundPivot(angle, screenPos);
            texRect2.position = screenPos + toTip * arrowMag - texSize2;
            if (showRotation && !ConstrainTorqueZ)
            {
                GUI.DrawTexture(texRect2, axisArrows [1]);
            }
//			GUI.DrawTexture ( new Rect ( tip.x - 2, tip.y - 2, 4, 4 ), dot );
//			GUI.color = Color.black;
//			GUI.DrawTexture ( new Rect ( screenPos.x - 2, screenPos.y - 2, 4, 4 ), dot );
        }         // axis arrows

        GUI.color = Color.white;

        // show controls legend
        if (showLegend)
        {
            info = @"F10: Legend on/off
H: Control on/off
WSAD/Arrows: Move around
Space/C: Thrust up/down
Q/E: Turn around
G: Reset Quad orientation
Scroll wheel: zoom in/out
RMB (drag): Rotate camera
RMB: Reset camera
MMB: Toggle patrol/follow
F5: Cycle quality settings
/: Path display on/off
Esc: Reload menu
Ctrl-Q: Quit";

            label.alignment = TextAnchor.MiddleLeft;
            size            = label.CalcSize(new GUIContent(info));
            r = new Rect(Screen.width - size.x - 20, 150, size.x + 10, size.y);
            GUI.Box(r, "");
            GUI.Box(r, "");
            r.x += 5;

            GUILayout.BeginArea(r);
            GUILayout.Label(info);
            GUILayout.EndArea();
        }
        else
        {
            label.alignment = TextAnchor.MiddleLeft;
            info            = "F10: Legend on/off";

            size = label.CalcSize(new GUIContent(info));
            r    = new Rect(Screen.width - size.x - 20, 150, size.x + 10, size.y);
            GUI.Box(r, "");
            GUI.Box(r, "");
            r.x += 5;

            GUILayout.BeginArea(r);
            GUILayout.Label(info);
            GUILayout.EndArea();
        }

        label.clipping = clipping;
        label.wordWrap = wrap;
        label.fontSize = fontSize;
    }