Skip to content

Commit 1854d2d

Browse files
fix: half float position encoding manufactures motion on resting objects
NetworkDeltaPosition carries the half float rounding loss of each update into the next one, which keeps the average transmitted position accurate while a value is moving. The loss alone is enough to change the encoded delta, so once the value stops moving that mechanism keeps changing what is sent even though the position has not moved. The encoded value alternates between neighbouring representable values and a stationary object is transmitted as one that oscillates. The rounding loss is now only carried forward while the value moves by at least one representable step. MaxDeltaBeforeAdjustment also determined the transmitted resolution, since a half float's step size grows with its magnitude. At 64 the coarsest step was 31.25mm, so objects away from their base position were reproduced in ~3cm increments. At 2 it is 0.977mm. Folding the delta into the base more often costs no bandwidth with reliable deltas because both sides apply the same rule to the same value, and the reconstructed position is unchanged by the fold. UseUnreliableDeltas forces a full precision base synchronization per fold, so those projects will send those more often. Measured on 10 settling physics objects with half float enabled: 28-42mm of oscillation before, none after, matching the same scene with half float disabled. Objects in motion improve as well, peak error dropping from 12.5mm to 0.587mm. Sender and receiver must agree on MaxDeltaBeforeAdjustment, so this is not compatible across builds. NetworkConstants.PROTOCOL_VERSION already participates in the connection config hash, so mismatched versions cannot connect. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 981c809 commit 1854d2d

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,21 @@ Additional documentation and release notes are available at [Multiplayer Documen
1313

1414
### Changed
1515

16+
- Changed `NetworkTransform.UseHalfFloatPrecision` to synchronize position with a resolution of approximately 1mm regardless of how far an object has travelled. Previously the resolution could degrade to approximately 3cm. This does not increase bandwidth, but projects using `NetworkTransform.UseUnreliableDeltas` will send full precision position updates more often.
17+
1618
- All editor assembly definitions are renamed with `Unity.Netcode.GameObjects.x` variants
1719
- `Unity.Netcode.Editor``Unity.Netcode.GameObjects.Editor`
1820
- `Unity.Netcode.Editor.CodeGen``Unity.Netcode.GameObjects.Editor.CodeGen`
1921
- `Unity.Netcode.Editor.PackageChecker``Unity.Netcode.GameObjects.Editor.PackageChecker`
2022
- `Unity.Netcode.Editor.Tests``Unity.Netcode.GameObjects.Editor.Tests`
2123

22-
23-
2424
### Deprecated
2525

26-
2726
### Removed
2827

29-
3028
### Fixed
3129

30+
- Issue where objects using `NetworkTransform.UseHalfFloatPrecision` appeared to jitter on non-authority instances while they were stationary or coming to rest, even though the authority was not moving them.
3231
- Issue with not being able to spawn initially disabled in-scene placed objects. (#4093)
3332
- Issue with pre-instantiated network prefab instances being marked as in-scene placed. Now pre-instantiated network prefabs are dynamically spawned. (#4093)
3433
- Issue where a user could spawn runtime created `NetworkObject` that has a GlobalObjectIdHash of zero. These are not valid instances and will no longer be allowed to spawn. (#4093)

com.unity.netcode.gameobjects/Runtime/Components/NetworkDeltaPosition.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ namespace Unity.Netcode.Components
1111
[Serializable]
1212
public struct NetworkDeltaPosition : INetworkSerializable
1313
{
14-
internal const float MaxDeltaBeforeAdjustment = 64f;
14+
/// <summary>
15+
/// How far the delta may grow before it is folded into the base position.
16+
/// </summary>
17+
/// <remarks>
18+
/// This determines the transmitted position resolution, since a half float's step size grows with its
19+
/// magnitude. Keeping the delta small keeps that step small: at 2 the coarsest step is roughly 1mm.
20+
/// </remarks>
21+
internal const float MaxDeltaBeforeAdjustment = 2f;
1522

1623
/// <summary>
1724
/// The HalfVector3 used to synchronize the delta in position
@@ -138,14 +145,29 @@ public void UpdateFrom(ref Vector3 vector3, int networkTick)
138145
{
139146
CollapsedDeltaIntoBase = false;
140147
NetworkTick = networkTick;
141-
DeltaPosition = (vector3 + PrecisionLossDelta) - CurrentBasePosition;
142148
for (int i = 0; i < HalfVector3.Length; i++)
143149
{
144150
if (HalfVector3.AxisToSynchronize[i])
145151
{
152+
var rawDelta = vector3[i] - CurrentBasePosition[i];
153+
154+
// Adding the previous rounding loss back in keeps the average position accurate while the
155+
// value is moving, but it also changes the value being sent. Once the value stops moving
156+
// that is all it does, which makes a stationary object appear to oscillate.
157+
var movedSinceLastSend = Mathf.Abs(vector3[i] - PreviousPosition[i]);
158+
var applyPrecisionLoss = movedSinceLastSend >= HalfPrecisionQuantum(rawDelta);
159+
160+
DeltaPosition[i] = applyPrecisionLoss ? rawDelta + PrecisionLossDelta[i] : rawDelta;
161+
146162
HalfVector3.Axis[i] = math.half(DeltaPosition[i]);
147163
HalfDeltaConvertedBack[i] = Mathf.HalfToFloat(HalfVector3.Axis[i].value);
148-
PrecisionLossDelta[i] = DeltaPosition[i] - HalfDeltaConvertedBack[i];
164+
165+
// Left unchanged when skipped so it is still applied once movement resumes.
166+
if (applyPrecisionLoss)
167+
{
168+
PrecisionLossDelta[i] = DeltaPosition[i] - HalfDeltaConvertedBack[i];
169+
}
170+
149171
if (Mathf.Abs(HalfDeltaConvertedBack[i]) >= MaxDeltaBeforeAdjustment)
150172
{
151173
CurrentBasePosition[i] += HalfDeltaConvertedBack[i];
@@ -165,6 +187,26 @@ public void UpdateFrom(ref Vector3 vector3, int networkTick)
165187
}
166188
}
167189

190+
/// <summary>
191+
/// The smallest change a half float can represent at the magnitude of the value passed in.
192+
/// </summary>
193+
/// <param name="value">The value to get the step size for.</param>
194+
/// <returns>The distance to the next representable half float value.</returns>
195+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
196+
internal static float HalfPrecisionQuantum(float value)
197+
{
198+
// The step size is symmetric about zero, so the sign is dropped.
199+
var magnitude = (ushort)(math.half(value).value & 0x7FFF);
200+
201+
// Guard only: stepping past the largest finite half float would give infinity.
202+
if (magnitude >= 0x7BFF)
203+
{
204+
return MaxDeltaBeforeAdjustment;
205+
}
206+
207+
return Mathf.HalfToFloat((ushort)(magnitude + 1)) - Mathf.HalfToFloat(magnitude);
208+
}
209+
168210
/// <summary>
169211
/// Constructor
170212
/// </summary>

0 commit comments

Comments
 (0)