Prediction: syntax

This commit is contained in:
mischa 2024-03-13 19:33:43 +08:00 committed by MrGadget
parent 7cc63de0ec
commit 8b86e709be

View File

@ -92,7 +92,7 @@ public static bool Sample<T>(
// readjust the deltas of the states after the inserted one. // readjust the deltas of the states after the inserted one.
// returns the corrected final position. // returns the corrected final position.
public static T CorrectHistory<T>( public static T CorrectHistory<T>(
SortedList<double, T> stateHistory, SortedList<double, T> history,
int stateHistoryLimit, int stateHistoryLimit,
T corrected, // corrected state with timestamp T corrected, // corrected state with timestamp
T before, // state in history before the correction T before, // state in history before the correction
@ -102,11 +102,11 @@ public static T CorrectHistory<T>(
{ {
// respect the limit // respect the limit
// TODO unit test to check if it respects max size // TODO unit test to check if it respects max size
if (stateHistory.Count >= stateHistoryLimit) if (history.Count >= stateHistoryLimit)
stateHistory.RemoveAt(0); history.RemoveAt(0);
// insert the corrected state into the history, or overwrite if already exists // insert the corrected state into the history, or overwrite if already exists
stateHistory[corrected.timestamp] = corrected; history[corrected.timestamp] = corrected;
// the entry behind the inserted one still has the delta from (before, after). // the entry behind the inserted one still has the delta from (before, after).
// we need to correct it to (corrected, after). // we need to correct it to (corrected, after).
@ -146,27 +146,27 @@ public static T CorrectHistory<T>(
after.rotationDelta = Quaternion.Slerp(Quaternion.identity, after.rotationDelta, (float)multiplier).normalized; after.rotationDelta = Quaternion.Slerp(Quaternion.identity, after.rotationDelta, (float)multiplier).normalized;
// changes aren't saved until we overwrite them in the history // changes aren't saved until we overwrite them in the history
stateHistory[after.timestamp] = after; history[after.timestamp] = after;
// second step: readjust all absolute values by rewinding client's delta moves on top of it. // second step: readjust all absolute values by rewinding client's delta moves on top of it.
T last = corrected; T last = corrected;
for (int i = afterIndex; i < stateHistory.Count; ++i) for (int i = afterIndex; i < history.Count; ++i)
{ {
double key = stateHistory.Keys[i]; double key = history.Keys[i];
T entry = stateHistory.Values[i]; T value = history.Values[i];
// correct absolute position based on last + delta. // correct absolute position based on last + delta.
entry.position = last.position + entry.positionDelta; value.position = last.position + value.positionDelta;
entry.velocity = last.velocity + entry.velocityDelta; value.velocity = last.velocity + value.velocityDelta;
entry.angularVelocity = last.angularVelocity + entry.angularVelocityDelta; value.angularVelocity = last.angularVelocity + value.angularVelocityDelta;
// Quaternions always need to be normalized in order to be a valid rotation after operations // Quaternions always need to be normalized in order to be a valid rotation after operations
entry.rotation = (entry.rotationDelta * last.rotation).normalized; // quaternions add delta by multiplying in this order value.rotation = (value.rotationDelta * last.rotation).normalized; // quaternions add delta by multiplying in this order
// save the corrected entry into history. // save the corrected entry into history.
stateHistory[key] = entry; history[key] = value;
// save last // save last
last = entry; last = value;
} }
// third step: return the final recomputed state. // third step: return the final recomputed state.