2
0
Эх сурвалжийг харах

Better distance, slider and hinge joint inspectors

BearishSun 9 жил өмнө
parent
commit
46cf3e5474

+ 10 - 22
Source/MBansheeEditor/Scene/Gizmos/JointGizmos.cs

@@ -74,14 +74,13 @@ namespace BansheeEditor
             float min = 0.0f;
             float min = 0.0f;
             float max = length;
             float max = length;
 
 
-            Vector3 center = anchorA + diff*0.5f;
             if (joint.EnableMinDistanceLimit)
             if (joint.EnableMinDistanceLimit)
             {
             {
                 min = MathEx.Max(0.0f, joint.MinDistance);
                 min = MathEx.Max(0.0f, joint.MinDistance);
                 if (joint.EnableMaxDistanceLimit)
                 if (joint.EnableMaxDistanceLimit)
                     min = MathEx.Min(min, MathEx.Min(10000.0f, joint.MaxDistance));
                     min = MathEx.Min(min, MathEx.Min(10000.0f, joint.MaxDistance));
 
 
-                Gizmos.DrawLine(center - normal*min*0.5f, center + normal*min*0.5f);
+                Gizmos.DrawLine(anchorA, anchorA + normal * min);
             }
             }
 
 
             if (joint.EnableMaxDistanceLimit)
             if (joint.EnableMaxDistanceLimit)
@@ -91,15 +90,11 @@ namespace BansheeEditor
                     max = MathEx.Max(max, min);
                     max = MathEx.Max(max, min);
 
 
                 if (length > max)
                 if (length > max)
-                {
-                    Gizmos.DrawLine(center - normal*max*0.5f, center - normal*length*0.5f);
-                    Gizmos.DrawLine(center + normal*max*0.5f, center + normal*length*0.5f);
-                }
+                    Gizmos.DrawLine(anchorA + normal * max, anchorA + normal * length);
             }
             }
 
 
             Gizmos.Color = Color.Green;
             Gizmos.Color = Color.Green;
-            Gizmos.DrawLine(center - normal * min * 0.5f, center - normal * max * 0.5f);
-            Gizmos.DrawLine(center + normal * min * 0.5f, center + normal * max * 0.5f);
+            Gizmos.DrawLine(anchorA + normal * min, anchorA + normal * max);
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -125,7 +120,6 @@ namespace BansheeEditor
             float min = 0.0f;
             float min = 0.0f;
             float max = length;
             float max = length;
 
 
-            Vector3 center = anchorA + diff * 0.5f;
             if (joint.EnableLimit)
             if (joint.EnableLimit)
             {
             {
                 LimitLinearRange limit = joint.Limit;
                 LimitLinearRange limit = joint.Limit;
@@ -134,18 +128,14 @@ namespace BansheeEditor
                 min = MathEx.Clamp(limit.Lower, 0.0f, max);
                 min = MathEx.Clamp(limit.Lower, 0.0f, max);
                 max = MathEx.Max(max, min);
                 max = MathEx.Max(max, min);
 
 
-                Gizmos.DrawLine(center - normal * min * 0.5f, center + normal * min * 0.5f);
+                Gizmos.DrawLine(anchorA, anchorA + normal * min);
 
 
                 if (length > max)
                 if (length > max)
-                {
-                    Gizmos.DrawLine(center - normal*max*0.5f, center - normal*length*0.5f);
-                    Gizmos.DrawLine(center + normal*max*0.5f, center + normal*length*0.5f);
-                }
+                    Gizmos.DrawLine(anchorA + normal * max, anchorA + normal * length);
             }
             }
 
 
             Gizmos.Color = Color.Green;
             Gizmos.Color = Color.Green;
-            Gizmos.DrawLine(center - normal * min * 0.5f, center - normal * max * 0.5f);
-            Gizmos.DrawLine(center + normal * min * 0.5f, center + normal * max * 0.5f);
+            Gizmos.DrawLine(anchorA + normal * min, anchorA + normal * max);
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -220,25 +210,23 @@ namespace BansheeEditor
                     Degree upper = MathEx.WrapAngle(limit.Upper);
                     Degree upper = MathEx.WrapAngle(limit.Upper);
 
 
                     lower = MathEx.Min(lower, upper);
                     lower = MathEx.Min(lower, upper);
-                    upper = MathEx.Max(lower, upper);
+                    upper = MathEx.Max(upper, lower);
 
 
                     // Arc zero to lower limit
                     // Arc zero to lower limit
                     Gizmos.Color = Color.Red;
                     Gizmos.Color = Color.Red;
-                    Gizmos.DrawWireArc(Vector3.XAxis * x, Vector3.XAxis, radius, lower * -0.5f, lower);
+                    Gizmos.DrawWireArc(Vector3.XAxis * x, Vector3.XAxis, radius, new Degree(0.0f), lower);
 
 
                     // Arc lower to upper limit
                     // Arc lower to upper limit
                     Degree validRange = upper - lower;
                     Degree validRange = upper - lower;
 
 
                     Gizmos.Color = Color.Green;
                     Gizmos.Color = Color.Green;
-                    Gizmos.DrawWireArc(Vector3.XAxis * x, Vector3.XAxis, radius, (lower + validRange) * -0.5f, validRange * 0.5f);
-                    Gizmos.DrawWireArc(Vector3.XAxis * x, Vector3.XAxis, radius, lower * 0.5f, validRange * 0.5f);
+                    Gizmos.DrawWireArc(Vector3.XAxis * x, Vector3.XAxis, radius, lower, validRange);
 
 
                     // Arc upper to full circle
                     // Arc upper to full circle
                     Degree remainingRange = new Degree(360) - upper;
                     Degree remainingRange = new Degree(360) - upper;
 
 
                     Gizmos.Color = Color.Red;
                     Gizmos.Color = Color.Red;
-                    Gizmos.DrawWireArc(Vector3.XAxis * x, Vector3.XAxis, radius, (upper + remainingRange) * -0.5f, remainingRange * 0.5f);
-                    Gizmos.DrawWireArc(Vector3.XAxis * x, Vector3.XAxis, radius, upper * 0.5f, remainingRange * 0.5f);
+                    Gizmos.DrawWireArc(Vector3.XAxis * x, Vector3.XAxis, radius, upper, remainingRange);
                 };
                 };
 
 
                 drawLimitedArc(-height);
                 drawLimitedArc(-height);

+ 21 - 3
Source/MBansheeEngine/Math/MathEx.cs

@@ -555,6 +555,24 @@ namespace BansheeEngine
             return value;
             return value;
         }
         }
 
 
+        /// <summary>
+        /// Clamps a value between two other values.
+        /// </summary>
+        /// <param name="value">Value to clamp.</param>
+        /// <param name="min">Minimum value of the range to clamp. Must be lower than <paramref name="max"/></param>
+        /// <param name="max">Maximum value of the range to clamp. Must be higher than <paramref name="min"/></param>
+        /// <returns>Returns unchanged value if it is in valid range, otherwise returns value clamped to the range
+        /// extremes. </returns>
+        public static Radian Clamp(Radian value, Radian min, Radian max)
+        {
+            if (value < min)
+                value = min;
+            else if (value > max)
+                value = max;
+
+            return value;
+        }
+
         /// <summary>
         /// <summary>
         /// Clamps a value between zero and one.
         /// Clamps a value between zero and one.
         /// </summary>
         /// </summary>
@@ -598,12 +616,12 @@ namespace BansheeEngine
         {
         {
             const float inv360 = 1.0f/360.0f;
             const float inv360 = 1.0f/360.0f;
             float angleVal = angle.Degrees;
             float angleVal = angle.Degrees;
-            float wrapCount = (float)MathEx.Floor(MathEx.Abs(angleVal * inv360));
+            float wrapCount = (float)MathEx.Floor(MathEx.Abs(angleVal) * inv360);
 
 
             if (angleVal > 0.0f)
             if (angleVal > 0.0f)
                 angleVal -= 360.0f * wrapCount;
                 angleVal -= 360.0f * wrapCount;
-            else
-                angleVal += 360.0f * wrapCount;
+            else if(angleVal < 0.0f)
+                angleVal += 360.0f * (wrapCount + 1);
 
 
             return new Degree(angleVal);
             return new Degree(angleVal);
         }
         }