Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. fn reconcile_predictions(
  2. player: &mut PlayerComponent,
  3. player_predictions: &mut PlayerPredictionsComponent,
  4. prediction_id: u16,
  5. server_position: Point3<f32>,
  6. server_rotation: UnitQuaternion<f32>,
  7. ) {
  8. let predictions = &mut player_predictions.predictions;
  9.  
  10. // First, find the slot for this prediction
  11. let slot = predictions.iter().position(|v| v.id == prediction_id);
  12.  
  13. // Only do something if we did find a slot, if not just don't do anything
  14. if let Some(slot) = slot {
  15. // Calculate the differences from our prediction
  16. let position_difference = server_position - predictions[slot].position;
  17. let rotation_difference = predictions[slot].rotation.rotation_to(&server_rotation);
  18. println!("{}", predictions[slot].rotation.magnitude());
  19.  
  20. // Normalize the rotation. When this wasn't done rotations would quickly spin out of control
  21. // from floating point error.
  22. //player.rotation.as_mut_unchecked().normalize_mut();
  23.  
  24. // Apply this difference to the current prediction slot, and all following ones, so
  25. // that the difference doesn't get applied multiple times on future reconciliations
  26. let mut current_slot = slot;
  27. loop {
  28. // Patch this slot with the difference
  29. let current_id = predictions[current_slot].id;
  30. predictions[current_slot].position += position_difference;
  31. predictions[current_slot].rotation *= rotation_difference;
  32. //println!("{}", predictions[current_slot].rotation.magnitude());
  33. //predictions[current_slot].rotation.as_mut_unchecked().normalize_mut();
  34.  
  35. // Increment the slot
  36. current_slot += 1;
  37. if current_slot >= predictions.len() {
  38. current_slot = 0;
  39. }
  40.  
  41. // If the next slot isn't sequential in ID from this one, stop
  42. if !wrapping_sequence_check(current_id, predictions[current_slot].id) {
  43. break;
  44. }
  45. }
  46.  
  47. // Apply the difference to the current position
  48. player.position += position_difference;
  49. player.rotation *= rotation_difference;
  50.  
  51. // Normalize the rotation. When this wasn't done rotations would quickly spin out of control
  52. // from floating point error.
  53. player.rotation.as_mut_unchecked().normalize_mut();
  54.  
  55. // TODO: Log reconciliation peaks
  56. //println!("Difference: {}", difference.magnitude());
  57. let _ = 1; // Wtf rustfmt
  58. } else {
  59. warn!(
  60. "Could not find prediction with given ID, the prediction rolling buffer is likely too \
  61. small"
  62. );
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement