Remove 2D physics support.

This commit is contained in:
2024-10-22 05:05:54 +02:00
parent 016788c21e
commit 887ec37b67
3 changed files with 69 additions and 347 deletions

View File

@@ -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
{
/// <summary>
/// NetworkRigidbodyBase is a unified <see cref="Rigidbody"/> and <see cref="Rigidbody2D"/> integration that helps to synchronize physics motion, collision, and interpolation
/// NetworkRigidbodyBase is a <see cref="Rigidbody"/> integration that helps to synchronize physics motion, collision, and interpolation
/// when used with a <see cref="NetworkTransform"/>.
/// </summary>
/// <remarks>
/// For a customizable netcode Rigidbody, create your own component from this class and use <see cref="Initialize(RigidbodyTypes, NetworkTransform, Rigidbody2D, Rigidbody)"/>
/// For a customizable netcode Rigidbody, create your own component from this class and use <see cref="Initialize(NetworkTransform, Rigidbody)"/>
/// during instantiation (i.e. invoked from within the Awake method). You can re-initialize after having initialized but only when the <see cref="NetworkObject"/> is not spawned.
/// </remarks>
public abstract class NetworkRigidbodyBase : NetworkBehaviour
@@ -21,7 +21,7 @@ namespace Unity.Netcode.Components
#endif
/// <summary>
/// When enabled, the associated <see cref="NetworkTransform"/> will use the Rigidbody/Rigidbody2D to apply and synchronize changes in position, rotation, and
/// When enabled, the associated <see cref="NetworkTransform"/> will use the Rigidbody to apply and synchronize changes in position, rotation, and
/// allows for the use of Rigidbody interpolation/extrapolation.
/// </summary>
/// <remarks>
@@ -44,13 +44,10 @@ namespace Unity.Netcode.Components
/// </summary>
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;
/// <summary>
/// Used to define the type of Rigidbody implemented.
/// <see cref=""/>
/// </summary>
public enum RigidbodyTypes
{
Rigidbody,
Rigidbody2D,
}
public RigidbodyTypes RigidbodyType { get; private set; }
/// <summary>
/// Initializes the networked Rigidbody based on the <see cref="RigidbodyTypes"/>
/// passed in as a parameter.
@@ -83,10 +68,8 @@ namespace Unity.Netcode.Components
/// <remarks>
/// Cannot be initialized while the associated <see cref="NetworkObject"/> is spawned.
/// </remarks>
/// <param name="rigidbodyType">type of rigid body being initialized</param>
/// <param name="rigidbody2D">(optional) The <see cref="Rigidbody2D"/> to be used</param>
/// <param name="rigidbody">(optional) The <see cref="Rigidbody"/> to be used</param>
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<Rigidbody2D>();
}
else if (m_InternalRigidbody == null)
if (m_InternalRigidbody == null)
{
m_InternalRigidbody = GetComponent<Rigidbody>();
}
@@ -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
/// <summary>
/// Sets the linear velocity of the Rigidbody.
/// </summary>
/// <remarks>
/// For <see cref="Rigidbody2D"/>, only the x and y components of the <see cref="Vector3"/> are applied.
/// </remarks>
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;
}
/// <summary>
/// Gets the linear velocity of the Rigidbody.
/// </summary>
/// <remarks>
/// For <see cref="Rigidbody2D"/>, the <see cref="Vector3"/> velocity returned is only applied to the x and y components.
/// </remarks>
/// <returns><see cref="Vector3"/> as the linear velocity</returns>
[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;
}
/// <summary>
/// Sets the angular velocity for the Rigidbody.
/// </summary>
/// <remarks>
/// For <see cref="Rigidbody2D"/>, the z component of <param name="angularVelocity"/> is only used to set the angular velocity.
/// A quick way to pass in a 2D angular velocity component is: <see cref="Vector3.forward"/> * angularVelocity (where angularVelocity is a float)
/// </remarks>
/// <param name="angularVelocity">the angular velocity to apply to the body</param>
[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;
}
/// <summary>
/// Gets the angular velocity for the Rigidbody.
/// </summary>
/// <remarks>
/// For <see cref="Rigidbody2D"/>, the z component of the <see cref="Vector3"/> returned is the angular velocity of the object.
/// </remarks>
/// <returns>angular velocity as a <see cref="Vector3"/></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector3 GetAngularVelocity()
{
if (m_IsRigidbody2D)
{
return Vector3.forward * m_InternalRigidbody2D.angularVelocity;
}
else
{
return m_InternalRigidbody.angularVelocity;
}
return m_InternalRigidbody.angularVelocity;
}
/// <summary>
@@ -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;
}
/// <summary>
@@ -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;
}
/// <summary>
@@ -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);
}
/// <summary>
@@ -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;
}
/// <summary>
@@ -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);
}
/// <summary>
@@ -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;
}
/// <summary>
@@ -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;
}
/// <summary>
@@ -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<NetworkRigidbodyBase> NetworkRigidbodyConnections = new System.Collections.Generic.List<NetworkRigidbodyBase>();
internal NetworkRigidbodyBase ParentBody;
private bool m_FixedJoint2DUsingGravity;
private bool m_OriginalGravitySetting;
private float m_OriginalGravityScale;
@@ -706,44 +489,6 @@ namespace Unity.Netcode.Components
}
/// <summary>
/// When using a custom <see cref="NetworkRigidbodyBase"/>, this virtual method is invoked when the
/// <see cref="FixedJoint2D"/> is created in the event any additional adjustments are needed.
/// </summary>
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>();
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 <see cref="UseRigidBodyForMotion"/> 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).
/// <seealso cref="FixedJoint"/>
/// <seealso cref="FixedJoint2D"/>
/// </summary>
/// <remarks>
/// Parenting relative:
@@ -777,7 +521,7 @@ namespace Unity.Netcode.Components
/// - The <param name="objectToConnectTo"/> can be viewed as the parent.
/// <br/>
/// This is the recommended way, as opposed to parenting, to attached/detatch two rigid bodies to one another when <see cref="UseRigidBodyForMotion"/> is enabled.
/// For more details on using <see cref="UnityEngine.FixedJoint"/> and <see cref="UnityEngine.FixedJoint2D"/>.
/// For more details on using <see cref="UnityEngine.FixedJoint"/>.
/// <br/>
/// 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 <see cref="NetworkRigidbodyBase"/>.
@@ -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
/// <summary>
/// Authority Only:
/// When invoked and already connected to an object via <see cref="FixedJoint"/> or <see cref="FixedJoint2D"/> (depending upon the type of rigid body),
/// When invoked and already connected to an object via <see cref="FixedJoint"/>,
/// this will detach from the fixed joint and destroy the fixed joint component.
/// </summary>
/// <remarks>
@@ -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();
}
}
}

View File

@@ -17,7 +17,7 @@ namespace Unity.Netcode.Components
protected virtual void Awake()
{
Initialize(RigidbodyTypes.Rigidbody);
Initialize();
}
}
}

View File

@@ -26,5 +26,8 @@
"description": "A lightweight sample to get started",
"path": "Samples~/Bootstrap"
}
]
],
"author": {
"name": "Unity"
}
}