Skip to content

Instantly share code, notes, and snippets.

@LaylBongers
Created September 8, 2019 18:20
Show Gist options
  • Save LaylBongers/6f832c50037ad4adb8c0d0f45f7dd4ce to your computer and use it in GitHub Desktop.
Save LaylBongers/6f832c50037ad4adb8c0d0f45f7dd4ce to your computer and use it in GitHub Desktop.
fn reconcile_predictions(
player: &mut PlayerComponent,
player_predictions: &mut PlayerPredictionsComponent,
prediction_id: u16,
server_position: Point3<f32>,
server_rotation: UnitQuaternion<f32>,
) {
let predictions = &mut player_predictions.predictions;
// First, find the slot for this prediction
let slot = predictions.iter().position(|v| v.id == prediction_id);
// Only do something if we did find a slot, if not just don't do anything
if let Some(slot) = slot {
// Calculate the differences from our prediction
let position_difference = server_position - predictions[slot].position;
let rotation_difference = predictions[slot].rotation.rotation_to(&server_rotation);
println!("{}", predictions[slot].rotation.magnitude());
// Normalize the rotation. When this wasn't done rotations would quickly spin out of control
// from floating point error.
//player.rotation.as_mut_unchecked().normalize_mut();
// Apply this difference to the current prediction slot, and all following ones, so
// that the difference doesn't get applied multiple times on future reconciliations
let mut current_slot = slot;
loop {
// Patch this slot with the difference
let current_id = predictions[current_slot].id;
predictions[current_slot].position += position_difference;
predictions[current_slot].rotation *= rotation_difference;
//println!("{}", predictions[current_slot].rotation.magnitude());
//predictions[current_slot].rotation.as_mut_unchecked().normalize_mut();
// Increment the slot
current_slot += 1;
if current_slot >= predictions.len() {
current_slot = 0;
}
// If the next slot isn't sequential in ID from this one, stop
if !wrapping_sequence_check(current_id, predictions[current_slot].id) {
break;
}
}
// Apply the difference to the current position
player.position += position_difference;
player.rotation *= rotation_difference;
// Normalize the rotation. When this wasn't done rotations would quickly spin out of control
// from floating point error.
player.rotation.as_mut_unchecked().normalize_mut();
// TODO: Log reconciliation peaks
//println!("Difference: {}", difference.magnitude());
let _ = 1; // Wtf rustfmt
} else {
warn!(
"Could not find prediction with given ID, the prediction rolling buffer is likely too \
small"
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment