private static void DrawDistanceJoint(DistanceJoint joint) { Vector3 anchorA = GetAnchor(joint, JointBody.Target); Vector3 anchorB = GetAnchor(joint, JointBody.Anchor); Gizmos.Color = Color.White; Gizmos.DrawSphere(anchorA, 0.05f); Gizmos.DrawSphere(anchorB, 0.05f); Gizmos.Color = Color.Red; Vector3 diff = anchorB - anchorA; float length = diff.Length; Vector3 normal = diff.Normalized; float min = 0.0f; float max = length; if (joint.EnableMinDistanceLimit) { min = MathEx.Max(0.0f, joint.MinDistance); if (joint.EnableMaxDistanceLimit) min = MathEx.Min(min, MathEx.Min(10000.0f, joint.MaxDistance)); Gizmos.DrawLine(anchorA, anchorA + normal * min); } if (joint.EnableMaxDistanceLimit) { max = MathEx.Min(10000.0f, joint.MaxDistance); if (joint.EnableMinDistanceLimit) max = MathEx.Max(max, min); if (length > max) Gizmos.DrawLine(anchorA + normal * max, anchorA + normal * length); } Gizmos.Color = Color.Green; Gizmos.DrawLine(anchorA + normal * min, anchorA + normal * MathEx.Min(max, length)); }
/// <summary> /// Creates GUI elements for fields specific to the distance joint. /// </summary> protected void BuildGUI(DistanceJoint joint) { enableMinLimitField.OnChanged += x => { joint.EnableMinDistanceLimit = x; MarkAsModified(); ConfirmModify(); minLimitField.Active = x; }; minLimitField.OnChanged += x => { joint.MinDistance = x; MarkAsModified(); }; minLimitField.OnFocusLost += ConfirmModify; minLimitField.OnConfirmed += ConfirmModify; enableMaxLimitField.OnChanged += x => { joint.EnableMaxDistanceLimit = x; MarkAsModified(); ConfirmModify(); maxLimitField.Active = x; }; maxLimitField.OnChanged += x => { joint.MaxDistance = x; MarkAsModified(); }; maxLimitField.OnFocusLost += ConfirmModify; maxLimitField.OnConfirmed += ConfirmModify; toleranceField.OnChanged += x => { joint.Tolerance = x; MarkAsModified(); }; toleranceField.OnFocusLost += ConfirmModify; toleranceField.OnConfirmed += ConfirmModify; enableSpringField.OnChanged += x => { joint.EnableSpring = x; MarkAsModified(); ConfirmModify(); springLayout.Active = x; }; Layout.AddElement(enableMinLimitField); GUILayoutX minLimitLayout = Layout.AddLayoutX(); { minLimitLayout.AddSpace(10); minLimitLayout.AddElement(minLimitField); } Layout.AddElement(enableMaxLimitField); GUILayoutX maxLimitLayout = Layout.AddLayoutX(); { maxLimitLayout.AddSpace(10); maxLimitLayout.AddElement(maxLimitField); } Layout.AddElement(toleranceField); Layout.AddElement(enableSpringField); springLayout = Layout.AddLayoutX(); { springLayout.AddSpace(10); springGUI = new SpringGUI(joint.Spring, springLayout); springGUI.OnChanged += x => { joint.Spring = x; MarkAsModified(); }; springGUI.OnConfirmed += ConfirmModify; } minLimitField.Active = joint.EnableMinDistanceLimit; maxLimitField.Active = joint.EnableMaxDistanceLimit; springLayout.Active = joint.EnableSpring; base.BuildGUI(joint, true); }
/// <summary> /// Updates all GUI elements from current values in the joint. /// </summary> /// <param name="joint">Joint to update the GUI from.</param> protected void Refresh(DistanceJoint joint) { if (enableMinLimitField.Value != joint.EnableMinDistanceLimit) { enableMinLimitField.Value = joint.EnableMinDistanceLimit; minLimitField.Active = joint.EnableMinDistanceLimit; } minLimitField.Value = joint.MinDistance; if (enableMaxLimitField.Value != joint.EnableMaxDistanceLimit) { enableMaxLimitField.Value = joint.EnableMaxDistanceLimit; maxLimitField.Active = joint.EnableMaxDistanceLimit; } maxLimitField.Value = joint.MaxDistance; toleranceField.Value = joint.Tolerance; if (enableSpringField.Value != joint.EnableSpring) { enableSpringField.Value = joint.EnableSpring; springLayout.Active = joint.EnableSpring; } springGUI.Spring = joint.Spring; base.Refresh(joint); }