/// <summary> /// Creates GUI elements for fields common to all joints. /// </summary> protected virtual void BuildGUI(Joint joint, bool showOffsets) { this.showOffsets = showOffsets; targetField = new GUIGameObjectField(typeof(Rigidbody), new LocEdString("Target")); anchorField = new GUIGameObjectField(typeof(Rigidbody), new LocEdString("Anchor")); if (showOffsets) { targetOffsetField = new GUIVector3Field(new LocEdString("Target offset")); anchorOffsetField = new GUIVector3Field(new LocEdString("Anchor offset")); } breakForceField = new GUIFloatField(new LocEdString("Break force")); breakTorqueField = new GUIFloatField(new LocEdString("Break torque")); collisionField = new GUIToggleField(new LocEdString("Enable collision")); targetField.OnChanged += x => { joint.SetRigidbody(JointBody.Target, (Rigidbody)x); MarkAsModified(); ConfirmModify(); }; anchorField.OnChanged += x => { joint.SetRigidbody(JointBody.Anchor, (Rigidbody)x); MarkAsModified(); ConfirmModify(); }; if(showOffsets) { targetOffsetField.OnChanged += x => { joint.SetPosition(JointBody.Target, x); MarkAsModified(); }; targetOffsetField.OnFocusLost += ConfirmModify; targetOffsetField.OnConfirmed += ConfirmModify; anchorOffsetField.OnChanged += x => { joint.SetPosition(JointBody.Anchor, x); MarkAsModified(); }; anchorOffsetField.OnFocusLost += ConfirmModify; anchorOffsetField.OnConfirmed += ConfirmModify; } breakForceField.OnChanged += x => { joint.BreakForce = x; MarkAsModified(); }; breakForceField.OnFocusLost += ConfirmModify; breakForceField.OnConfirmed += ConfirmModify; breakTorqueField.OnChanged += x => { joint.BreakTorque = x; MarkAsModified(); }; breakTorqueField.OnFocusLost += ConfirmModify; breakTorqueField.OnConfirmed += ConfirmModify; collisionField.OnChanged += x => { joint.EnableCollision = x; MarkAsModified(); ConfirmModify(); }; Layout.AddElement(targetField); if(showOffsets) Layout.AddElement(targetOffsetField); Layout.AddElement(anchorField); if(showOffsets) Layout.AddElement(anchorOffsetField); Layout.AddElement(breakForceField); Layout.AddElement(breakTorqueField); Layout.AddElement(collisionField); }
/// <summary> /// Updates all GUI elements from current values in the joint. /// </summary> /// <param name="joint">Joint to update the GUI from.</param> protected virtual void Refresh(Joint joint) { targetField.Value = joint.GetRigidbody(JointBody.Target); anchorField.Value = joint.GetRigidbody(JointBody.Anchor); if (showOffsets) { targetOffsetField.Value = joint.GetPosition(JointBody.Target); anchorOffsetField.Value = joint.GetPosition(JointBody.Anchor); } breakForceField.Value = joint.BreakForce; breakTorqueField.Value = joint.BreakTorque; collisionField.Value = joint.EnableCollision; }
/// <summary> /// Sets that joint that this rigidbody is attached to. Allows the rigidbody to notify the joint when it moves. /// </summary> /// <param name="joint">Joint the rigidbody is attached to, or null if none.</param> internal void SetJoint(Joint joint) { parentJoint = joint; }
/// <summary> /// Returns the anchor position for the specified joint body. Anchor represents the world position of the rigidbody /// added to the offset provided by the joint. /// </summary> /// <param name="joint">Joint from which to retrieve the body.</param> /// <param name="body">Body to retrieve the anchor for.</param> /// <returns>Anchor position in world space.</returns> private static Vector3 GetAnchor(Joint joint, JointBody body) { Rigidbody rigidbody = joint.GetRigidbody(body); Vector3 anchor = joint.GetPosition(body); if (rigidbody != null) { Quaternion worldRot = rigidbody.SceneObject.Rotation; anchor = worldRot.Rotate(anchor) + rigidbody.SceneObject.Position; } else { Quaternion worldRot = joint.SceneObject.Rotation; anchor = worldRot.Rotate(anchor) + joint.SceneObject.Position; } return anchor; }