From d79bb13f4d5a92a26d2a798f706429f60f96a521 Mon Sep 17 00:00:00 2001 From: Joona Aalto Date: Mon, 23 Sep 2024 01:46:46 +0300 Subject: [PATCH] Fix 3D rotation update in `transform_to_position` (#520) # Objective Fixes #516. Rotation is currently updated incorrectly in 3D by `transform_to_position`. It uses addition and subtraction instead of multiplication for quaternions. This causes clear desync between `Transform` rotation and `Rotation` when `SyncConfig::position_to_transform` is `false` and the `Transform` is changed: https://github.com/user-attachments/assets/5b51480e-6f55-4317-9ed1-ec02f9c4d640 ## Solution Fix the rotation update. We can also remove the normalization, because individual quaternion multiplications should remain normalized, assuming the inputs are normalized (and there aren't too many successive rotations). Now, the desync is fixed: https://github.com/user-attachments/assets/8922832f-21b4-4ed9-947d-bd4b77786647 --- src/sync/mod.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/sync/mod.rs b/src/sync/mod.rs index ca5579e2..1bed6cc5 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs @@ -263,10 +263,9 @@ pub fn transform_to_position( #[cfg(feature = "3d")] { rotation.0 = (previous_transform.rotation - + (transform.rotation - previous_transform.rotation) - + (rotation.f32() - previous_transform.rotation)) - .normalize() - .adjust_precision(); + * (transform.rotation * previous_transform.rotation.inverse()) + * (rotation.f32() * previous_transform.rotation.inverse())) + .adjust_precision(); } } }