From 887ec37b6777620e197c40bcb0c44adcc871b4fd Mon Sep 17 00:00:00 2001 From: Alexander Filippov Date: Tue, 22 Oct 2024 05:05:54 +0200 Subject: [PATCH] Remove 2D physics support. --- Runtime/Components/NetworkRigidBodyBase.cs | 409 ++++----------------- Runtime/Components/NetworkRigidbody.cs | 2 +- package.json | 5 +- 3 files changed, 69 insertions(+), 347 deletions(-) diff --git a/Runtime/Components/NetworkRigidBodyBase.cs b/Runtime/Components/NetworkRigidBodyBase.cs index 7e88081..97930a9 100644 --- a/Runtime/Components/NetworkRigidBodyBase.cs +++ b/Runtime/Components/NetworkRigidBodyBase.cs @@ -1,15 +1,15 @@ -#if COM_UNITY_MODULES_PHYSICS || COM_UNITY_MODULES_PHYSICS2D +#if COM_UNITY_MODULES_PHYSICS using System.Runtime.CompilerServices; using UnityEngine; namespace Unity.Netcode.Components { /// - /// NetworkRigidbodyBase is a unified and integration that helps to synchronize physics motion, collision, and interpolation + /// NetworkRigidbodyBase is a integration that helps to synchronize physics motion, collision, and interpolation /// when used with a . /// /// - /// For a customizable netcode Rigidbody, create your own component from this class and use + /// For a customizable netcode Rigidbody, create your own component from this class and use /// during instantiation (i.e. invoked from within the Awake method). You can re-initialize after having initialized but only when the is not spawned. /// public abstract class NetworkRigidbodyBase : NetworkBehaviour @@ -21,7 +21,7 @@ namespace Unity.Netcode.Components #endif /// - /// When enabled, the associated will use the Rigidbody/Rigidbody2D to apply and synchronize changes in position, rotation, and + /// When enabled, the associated will use the Rigidbody to apply and synchronize changes in position, rotation, and /// allows for the use of Rigidbody interpolation/extrapolation. /// /// @@ -44,13 +44,10 @@ namespace Unity.Netcode.Components /// public bool AutoSetKinematicOnDespawn = true; - // Determines if this is a Rigidbody or Rigidbody2D implementation - private bool m_IsRigidbody2D => RigidbodyType == RigidbodyTypes.Rigidbody2D; // Used to cache the authority state of this Rigidbody during the last frame private bool m_IsAuthority; protected internal Rigidbody m_InternalRigidbody { get; private set; } - protected internal Rigidbody2D m_InternalRigidbody2D { get; private set; } internal NetworkTransform NetworkTransform; private float m_TickFrequency; @@ -64,18 +61,6 @@ namespace Unity.Netcode.Components } private InterpolationTypes m_OriginalInterpolation; - /// - /// Used to define the type of Rigidbody implemented. - /// - /// - public enum RigidbodyTypes - { - Rigidbody, - Rigidbody2D, - } - - public RigidbodyTypes RigidbodyType { get; private set; } - /// /// Initializes the networked Rigidbody based on the /// passed in as a parameter. @@ -83,10 +68,8 @@ namespace Unity.Netcode.Components /// /// Cannot be initialized while the associated is spawned. /// - /// type of rigid body being initialized - /// (optional) The to be used /// (optional) The to be used - protected void Initialize(RigidbodyTypes rigidbodyType, NetworkTransform networkTransform = null, Rigidbody2D rigidbody2D = null, Rigidbody rigidbody = null) + protected void Initialize(NetworkTransform networkTransform = null, Rigidbody rigidbody = null) { // Don't initialize if already spawned if (IsSpawned) @@ -94,17 +77,10 @@ namespace Unity.Netcode.Components Debug.LogError($"[{name}] Attempting to initialize while spawned is not allowed."); return; } - RigidbodyType = rigidbodyType; - m_InternalRigidbody2D = rigidbody2D; m_InternalRigidbody = rigidbody; NetworkTransform = networkTransform; - if (m_IsRigidbody2D && m_InternalRigidbody2D == null) - { - m_InternalRigidbody2D = GetComponent(); - - } - else if (m_InternalRigidbody == null) + if (m_InternalRigidbody == null) { m_InternalRigidbody = GetComponent(); } @@ -145,11 +121,7 @@ namespace Unity.Netcode.Components // Finally, we adjust the threshold based on the body's current velocity perTickVelocity.x = Mathf.Clamp(Mathf.Abs(perTickVelocity.x), minThreshold, thresholdMax); perTickVelocity.y = Mathf.Clamp(Mathf.Abs(perTickVelocity.y), minThreshold, thresholdMax); - // 2D Rigidbody only moves on x & y axis - if (!m_IsRigidbody2D) - { - perTickVelocity.z = Mathf.Clamp(Mathf.Abs(perTickVelocity.z), minThreshold, thresholdMax); - } + perTickVelocity.z = Mathf.Clamp(Mathf.Abs(perTickVelocity.z), minThreshold, thresholdMax); return perTickVelocity; } @@ -164,12 +136,8 @@ namespace Unity.Netcode.Components var rotationPerTick = (GetAngularVelocity() * Mathf.Rad2Deg) * m_TickFrequency; var minThreshold = NetworkTransform.RotAngleThreshold * m_TickFrequency; - // 2D Rigidbody only rotates around Z axis - if (!m_IsRigidbody2D) - { - rotationPerTick.x = Mathf.Clamp(Mathf.Abs(rotationPerTick.x), minThreshold, thresholdMax); - rotationPerTick.y = Mathf.Clamp(Mathf.Abs(rotationPerTick.y), minThreshold, thresholdMax); - } + rotationPerTick.x = Mathf.Clamp(Mathf.Abs(rotationPerTick.x), minThreshold, thresholdMax); + rotationPerTick.y = Mathf.Clamp(Mathf.Abs(rotationPerTick.y), minThreshold, thresholdMax); rotationPerTick.z = Mathf.Clamp(Mathf.Abs(rotationPerTick.z), minThreshold, thresholdMax); return rotationPerTick; @@ -178,88 +146,39 @@ namespace Unity.Netcode.Components /// /// Sets the linear velocity of the Rigidbody. /// - /// - /// For , only the x and y components of the are applied. - /// public void SetLinearVelocity(Vector3 linearVelocity) { - if (m_IsRigidbody2D) - { -#if COM_UNITY_MODULES_PHYSICS2D_LINEAR - m_InternalRigidbody2D.linearVelocity = linearVelocity; -#else - m_InternalRigidbody2D.velocity = linearVelocity; -#endif - } - else - { - m_InternalRigidbody.linearVelocity = linearVelocity; - } + m_InternalRigidbody.linearVelocity = linearVelocity; } /// /// Gets the linear velocity of the Rigidbody. /// - /// - /// For , the velocity returned is only applied to the x and y components. - /// /// as the linear velocity [MethodImpl(MethodImplOptions.AggressiveInlining)] public Vector3 GetLinearVelocity() { - if (m_IsRigidbody2D) - { -#if COM_UNITY_MODULES_PHYSICS2D_LINEAR - return m_InternalRigidbody2D.linearVelocity; -#else - return m_InternalRigidbody2D.velocity; -#endif - } - else - { - return m_InternalRigidbody.linearVelocity; - } + return m_InternalRigidbody.linearVelocity; } /// /// Sets the angular velocity for the Rigidbody. /// - /// - /// For , the z component of is only used to set the angular velocity. - /// A quick way to pass in a 2D angular velocity component is: * angularVelocity (where angularVelocity is a float) - /// /// the angular velocity to apply to the body [MethodImpl(MethodImplOptions.AggressiveInlining)] public void SetAngularVelocity(Vector3 angularVelocity) { - if (m_IsRigidbody2D) - { - m_InternalRigidbody2D.angularVelocity = angularVelocity.z; - } - else - { - m_InternalRigidbody.angularVelocity = angularVelocity; - } + m_InternalRigidbody.angularVelocity = angularVelocity; } /// /// Gets the angular velocity for the Rigidbody. /// - /// - /// For , the z component of the returned is the angular velocity of the object. - /// /// angular velocity as a [MethodImpl(MethodImplOptions.AggressiveInlining)] public Vector3 GetAngularVelocity() { - if (m_IsRigidbody2D) - { - return Vector3.forward * m_InternalRigidbody2D.angularVelocity; - } - else - { - return m_InternalRigidbody.angularVelocity; - } + return m_InternalRigidbody.angularVelocity; } /// @@ -269,14 +188,7 @@ namespace Unity.Netcode.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] public Vector3 GetPosition() { - if (m_IsRigidbody2D) - { - return m_InternalRigidbody2D.position; - } - else - { - return m_InternalRigidbody.position; - } + return m_InternalRigidbody.position; } /// @@ -286,18 +198,7 @@ namespace Unity.Netcode.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] public Quaternion GetRotation() { - if (m_IsRigidbody2D) - { - var quaternion = Quaternion.identity; - var angles = quaternion.eulerAngles; - angles.z = m_InternalRigidbody2D.rotation; - quaternion.eulerAngles = angles; - return quaternion; - } - else - { - return m_InternalRigidbody.rotation; - } + return m_InternalRigidbody.rotation; } /// @@ -307,14 +208,7 @@ namespace Unity.Netcode.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] public void MovePosition(Vector3 position) { - if (m_IsRigidbody2D) - { - m_InternalRigidbody2D.MovePosition(position); - } - else - { - m_InternalRigidbody.MovePosition(position); - } + m_InternalRigidbody.MovePosition(position); } /// @@ -324,14 +218,7 @@ namespace Unity.Netcode.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] public void SetPosition(Vector3 position) { - if (m_IsRigidbody2D) - { - m_InternalRigidbody2D.position = position; - } - else - { - m_InternalRigidbody.position = position; - } + m_InternalRigidbody.position = position; } /// @@ -340,16 +227,8 @@ namespace Unity.Netcode.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] public void ApplyCurrentTransform() { - if (m_IsRigidbody2D) - { - m_InternalRigidbody2D.position = transform.position; - m_InternalRigidbody2D.rotation = transform.eulerAngles.z; - } - else - { - m_InternalRigidbody.position = transform.position; - m_InternalRigidbody.rotation = transform.rotation; - } + m_InternalRigidbody.position = transform.position; + m_InternalRigidbody.rotation = transform.rotation; } // Used for Rigidbody only (see info on normalized below) @@ -362,29 +241,18 @@ namespace Unity.Netcode.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] public void MoveRotation(Quaternion rotation) { - if (m_IsRigidbody2D) + // Evidently we need to check to make sure the quaternion is a perfect + // magnitude of 1.0f when applying the rotation to a rigid body. + m_QuaternionCheck.x = rotation.x; + m_QuaternionCheck.y = rotation.y; + m_QuaternionCheck.z = rotation.z; + m_QuaternionCheck.w = rotation.w; + // If the magnitude is greater than 1.0f (even by a very small fractional value), then normalize the quaternion + if (m_QuaternionCheck.magnitude != 1.0f) { - var quaternion = Quaternion.identity; - var angles = quaternion.eulerAngles; - angles.z = m_InternalRigidbody2D.rotation; - quaternion.eulerAngles = angles; - m_InternalRigidbody2D.MoveRotation(quaternion); - } - else - { - // Evidently we need to check to make sure the quaternion is a perfect - // magnitude of 1.0f when applying the rotation to a rigid body. - m_QuaternionCheck.x = rotation.x; - m_QuaternionCheck.y = rotation.y; - m_QuaternionCheck.z = rotation.z; - m_QuaternionCheck.w = rotation.w; - // If the magnitude is greater than 1.0f (even by a very small fractional value), then normalize the quaternion - if (m_QuaternionCheck.magnitude != 1.0f) - { - rotation.Normalize(); - } - m_InternalRigidbody.MoveRotation(rotation); + rotation.Normalize(); } + m_InternalRigidbody.MoveRotation(rotation); } /// @@ -394,14 +262,7 @@ namespace Unity.Netcode.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] public void SetRotation(Quaternion rotation) { - if (m_IsRigidbody2D) - { - m_InternalRigidbody2D.rotation = rotation.eulerAngles.z; - } - else - { - m_InternalRigidbody.rotation = rotation; - } + m_InternalRigidbody.rotation = rotation; } /// @@ -410,47 +271,23 @@ namespace Unity.Netcode.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] private void SetOriginalInterpolation() { - if (m_IsRigidbody2D) + switch (m_InternalRigidbody.interpolation) { - switch (m_InternalRigidbody2D.interpolation) - { - case RigidbodyInterpolation2D.None: - { - m_OriginalInterpolation = InterpolationTypes.None; - break; - } - case RigidbodyInterpolation2D.Interpolate: - { - m_OriginalInterpolation = InterpolationTypes.Interpolate; - break; - } - case RigidbodyInterpolation2D.Extrapolate: - { - m_OriginalInterpolation = InterpolationTypes.Extrapolate; - break; - } - } - } - else - { - switch (m_InternalRigidbody.interpolation) - { - case RigidbodyInterpolation.None: - { - m_OriginalInterpolation = InterpolationTypes.None; - break; - } - case RigidbodyInterpolation.Interpolate: - { - m_OriginalInterpolation = InterpolationTypes.Interpolate; - break; - } - case RigidbodyInterpolation.Extrapolate: - { - m_OriginalInterpolation = InterpolationTypes.Extrapolate; - break; - } - } + case RigidbodyInterpolation.None: + { + m_OriginalInterpolation = InterpolationTypes.None; + break; + } + case RigidbodyInterpolation.Interpolate: + { + m_OriginalInterpolation = InterpolationTypes.Interpolate; + break; + } + case RigidbodyInterpolation.Extrapolate: + { + m_OriginalInterpolation = InterpolationTypes.Extrapolate; + break; + } } } @@ -460,19 +297,9 @@ namespace Unity.Netcode.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] public void WakeIfSleeping() { - if (m_IsRigidbody2D) + if (m_InternalRigidbody.IsSleeping()) { - if (m_InternalRigidbody2D.IsSleeping()) - { - m_InternalRigidbody2D.WakeUp(); - } - } - else - { - if (m_InternalRigidbody.IsSleeping()) - { - m_InternalRigidbody.WakeUp(); - } + m_InternalRigidbody.WakeUp(); } } @@ -482,27 +309,13 @@ namespace Unity.Netcode.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] public void SleepRigidbody() { - if (m_IsRigidbody2D) - { - m_InternalRigidbody2D.Sleep(); - } - else - { - m_InternalRigidbody.Sleep(); - } + m_InternalRigidbody.Sleep(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool IsKinematic() { - if (m_IsRigidbody2D) - { - return m_InternalRigidbody2D.bodyType == RigidbodyType2D.Kinematic; - } - else - { - return m_InternalRigidbody.isKinematic; - } + return m_InternalRigidbody.isKinematic; } /// @@ -524,14 +337,7 @@ namespace Unity.Netcode.Components [MethodImpl(MethodImplOptions.AggressiveInlining)] public void SetIsKinematic(bool isKinematic) { - if (m_IsRigidbody2D) - { - m_InternalRigidbody2D.bodyType = isKinematic ? RigidbodyType2D.Kinematic : RigidbodyType2D.Dynamic; - } - else - { - m_InternalRigidbody.isKinematic = isKinematic; - } + m_InternalRigidbody.isKinematic = isKinematic; // If we are not spawned, then exit early if (!IsSpawned) @@ -574,38 +380,17 @@ namespace Unity.Netcode.Components { case InterpolationTypes.None: { - if (m_IsRigidbody2D) - { - m_InternalRigidbody2D.interpolation = RigidbodyInterpolation2D.None; - } - else - { - m_InternalRigidbody.interpolation = RigidbodyInterpolation.None; - } + m_InternalRigidbody.interpolation = RigidbodyInterpolation.None; break; } case InterpolationTypes.Interpolate: { - if (m_IsRigidbody2D) - { - m_InternalRigidbody2D.interpolation = RigidbodyInterpolation2D.Interpolate; - } - else - { - m_InternalRigidbody.interpolation = RigidbodyInterpolation.Interpolate; - } + m_InternalRigidbody.interpolation = RigidbodyInterpolation.Interpolate; break; } case InterpolationTypes.Extrapolate: { - if (m_IsRigidbody2D) - { - m_InternalRigidbody2D.interpolation = RigidbodyInterpolation2D.Extrapolate; - } - else - { - m_InternalRigidbody.interpolation = RigidbodyInterpolation.Extrapolate; - } + m_InternalRigidbody.interpolation = RigidbodyInterpolation.Extrapolate; break; } } @@ -688,12 +473,10 @@ namespace Unity.Netcode.Components // Alternately, users can affix the fixed joint to a child GameObject (without a rigid body) of the parent NetworkObject // and then add a NetworkTransform to that in order to get the parented child NetworkObject to move around in "local space" public FixedJoint FixedJoint { get; private set; } - public FixedJoint2D FixedJoint2D { get; private set; } internal System.Collections.Generic.List NetworkRigidbodyConnections = new System.Collections.Generic.List(); internal NetworkRigidbodyBase ParentBody; - private bool m_FixedJoint2DUsingGravity; private bool m_OriginalGravitySetting; private float m_OriginalGravityScale; @@ -706,44 +489,6 @@ namespace Unity.Netcode.Components } - /// - /// When using a custom , this virtual method is invoked when the - /// is created in the event any additional adjustments are needed. - /// - protected virtual void OnFixedJoint2DCreated() - { - - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ApplyFixedJoint2D(NetworkRigidbodyBase bodyToConnect, Vector3 position, float connectedMassScale = 0.0f, float massScale = 1.0f, bool useGravity = false, bool zeroVelocity = true) - { - transform.position = position; - m_InternalRigidbody2D.position = position; - m_OriginalGravitySetting = bodyToConnect.m_InternalRigidbody.useGravity; - m_FixedJoint2DUsingGravity = useGravity; - - if (!useGravity) - { - m_OriginalGravityScale = m_InternalRigidbody2D.gravityScale; - m_InternalRigidbody2D.gravityScale = 0.0f; - } - - if (zeroVelocity) - { -#if COM_UNITY_MODULES_PHYSICS2D_LINEAR - m_InternalRigidbody2D.linearVelocity = Vector2.zero; -#else - m_InternalRigidbody2D.velocity = Vector2.zero; -#endif - m_InternalRigidbody2D.angularVelocity = 0.0f; - } - - FixedJoint2D = gameObject.AddComponent(); - FixedJoint2D.connectedBody = bodyToConnect.m_InternalRigidbody2D; - OnFixedJoint2DCreated(); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ApplyFixedJoint(NetworkRigidbodyBase bodyToConnectTo, Vector3 position, float connectedMassScale = 0.0f, float massScale = 1.0f, bool useGravity = false, bool zeroVelocity = true) { @@ -769,7 +514,6 @@ namespace Unity.Netcode.Components /// When invoked and not already attached to a fixed joint, this will connect two rigid bodies with enabled. /// Invoke this method on the rigid body you wish to attach to another (i.e. weapon to player, sticky bomb to player/object, etc). /// - /// /// /// /// Parenting relative: @@ -777,7 +521,7 @@ namespace Unity.Netcode.Components /// - The can be viewed as the parent. ///
/// This is the recommended way, as opposed to parenting, to attached/detatch two rigid bodies to one another when is enabled. - /// For more details on using and . + /// For more details on using . ///
/// This provides a simple joint solution between two rigid bodies and serves as an example. You can add different joint types by creating a customized/derived /// version of . @@ -806,14 +550,7 @@ namespace Unity.Netcode.Components if (objectToConnectTo != null) { - if (m_IsRigidbody2D) - { - ApplyFixedJoint2D(objectToConnectTo, positionOfConnection, connectedMassScale, massScale, useGravity, zeroVelocity); - } - else - { - ApplyFixedJoint(objectToConnectTo, positionOfConnection, connectedMassScale, massScale, useGravity, zeroVelocity); - } + ApplyFixedJoint(objectToConnectTo, positionOfConnection, connectedMassScale, massScale, useGravity, zeroVelocity); ParentBody = objectToConnectTo; ParentBody.NetworkRigidbodyConnections.Add(this); @@ -835,7 +572,7 @@ namespace Unity.Netcode.Components /// /// Authority Only: - /// When invoked and already connected to an object via or (depending upon the type of rigid body), + /// When invoked and already connected to an object via , /// this will detach from the fixed joint and destroy the fixed joint component. /// /// @@ -849,32 +586,14 @@ namespace Unity.Netcode.Components } if (UseRigidBodyForMotion) { - if (m_IsRigidbody2D) + if (FixedJoint != null) { - if (FixedJoint2D != null) - { - if (!m_FixedJoint2DUsingGravity) - { - FixedJoint2D.connectedBody.gravityScale = m_OriginalGravityScale; - } - FixedJoint2D.connectedBody = null; - Destroy(FixedJoint2D); - FixedJoint2D = null; - ResetInterpolation(); - RemoveFromParentBody(); - } - } - else - { - if (FixedJoint != null) - { - FixedJoint.connectedBody = null; - m_InternalRigidbody.useGravity = m_OriginalGravitySetting; - Destroy(FixedJoint); - FixedJoint = null; - ResetInterpolation(); - RemoveFromParentBody(); - } + FixedJoint.connectedBody = null; + m_InternalRigidbody.useGravity = m_OriginalGravitySetting; + Destroy(FixedJoint); + FixedJoint = null; + ResetInterpolation(); + RemoveFromParentBody(); } } } diff --git a/Runtime/Components/NetworkRigidbody.cs b/Runtime/Components/NetworkRigidbody.cs index a157d26..7d3d21e 100644 --- a/Runtime/Components/NetworkRigidbody.cs +++ b/Runtime/Components/NetworkRigidbody.cs @@ -17,7 +17,7 @@ namespace Unity.Netcode.Components protected virtual void Awake() { - Initialize(RigidbodyTypes.Rigidbody); + Initialize(); } } } diff --git a/package.json b/package.json index ab43e49..0ddc180 100644 --- a/package.json +++ b/package.json @@ -26,5 +26,8 @@ "description": "A lightweight sample to get started", "path": "Samples~/Bootstrap" } - ] + ], + "author": { + "name": "Unity" + } }