/// <summary> /// Destroys the internal collider representation. /// </summary> private void DestroyNative() { if (parent != null) parent.RemoveCollider(this); parent = null; if (native != null) { native.Destroy(); native = null; } }
public void SetRigidbody(JointBody body, Rigidbody rigidbody) { IntPtr rigidbodyPtr = IntPtr.Zero; if (rigidbody != null) rigidbodyPtr = rigidbody.native.GetCachedPtr(); Internal_SetBody(mCachedPtr, body, rigidbodyPtr); }
/// <summary> /// Changes the rigidbody parent of the collider. Meant to be called from the Rigidbody itself. /// </summary> /// <param name="rigidbody">New rigidbody to assign as the parent to the collider.</param> /// <param name="isInternal">If true the rigidbody will just be changed internally, but parent rigidbody will not be /// notified.</param> internal void SetRigidbody(Rigidbody rigidbody, bool isInternal = false) { if (rigidbody == parent) return; if (native != null && !isInternal) { if (parent != null) parent.RemoveCollider(this); NativeRigidbody nativeRigidbody = null; if (rigidbody != null) nativeRigidbody = rigidbody.native; native.Rigidbody = nativeRigidbody; if (rigidbody != null) rigidbody.AddCollider(this); } parent = rigidbody; UpdateCollisionReportMode(); }
/// <summary> /// Checks is the provided rigidbody a valid parent for this collider. /// /// This is required because certain colliders are limited in how they can be used. /// </summary> /// <param name="parent">Rigidbody that is the potential parent.</param> /// <returns>True if collider can be a part of the rigidbody.</returns> protected internal virtual bool IsValidParent(Rigidbody parent) { return true; }
/// <summary> /// Checks can the provided rigidbody be used for initializing the joint. /// </summary> /// <param name="body">Body to check.</param> /// <returns>True if the body can be used for initializing the joint, false otherwise.</returns> private bool IsBodyValid(Rigidbody body) { if (body == null) return false; if (body.native == null) return false; return true; }
/// <summary> /// Notifies the joint that one of the attached rigidbodies moved and that its transform needs updating. /// </summary> /// <param name="body">Rigidbody that moved.</param> internal void NotifyRigidbodyMoved(Rigidbody body) { if (native == null) return; // If physics update is in progress do nothing, as its the joint itself that's probably moving the body if (Physics.IsUpdateInProgress) return; if (commonData.bodies[0] == body) UpdateTransform(JointBody.Target); else if (commonData.bodies[1] == body) UpdateTransform(JointBody.Anchor); }
/// <summary> /// Sets a body managed by the joint. One of the bodies must be movable (non-kinematic). /// </summary> /// <param name="body">Which of the rigidbodies to set.</param> /// <param name="rigidbody">Rigidbody to managed by the joint, or null. If one of the bodies is null the other /// one will be anchored globally to the position/rotation set by <see cref="SetPosition"/> /// and <see cref="SetRotation"/>.</param> public void SetRigidbody(JointBody body, Rigidbody rigidbody) { if (commonData.bodies[(int)body] == rigidbody) return; if (commonData.bodies[(int)body] != null) commonData.bodies[(int)body].SetJoint(null); commonData.bodies[(int)body] = rigidbody; if (rigidbody != null) commonData.bodies[(int)body].SetJoint(this); // If joint already exists, destroy it if we removed all bodies, otherwise update its transform if (native != null) { if (!IsBodyValid(commonData.bodies[0]) && !IsBodyValid(commonData.bodies[0])) DestroyNative(); else { native.SetRigidbody(body, rigidbody); UpdateTransform(body); } } else // If joint doesn't exist, check if we can create it { // Must be an active component and at least one of the bodies must be non-null if (SceneObject.Active && (IsBodyValid(commonData.bodies[0]) || IsBodyValid(commonData.bodies[0]))) { RestoreNative(); } } }