From f1e67967102ce81841cb4d6ae347debb79c3a334 Mon Sep 17 00:00:00 2001 From: carrascomj Date: Thu, 16 Jan 2025 17:13:56 +0100 Subject: [PATCH] enhance: remove flickering mirror behavior on 360 mouse rotation --- src/picking.rs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/picking.rs b/src/picking.rs index 28c42ea..18d9a5b 100644 --- a/src/picking.rs +++ b/src/picking.rs @@ -187,19 +187,25 @@ pub fn rotate_or_scale_on_right_drag( trans.scale.x += scale; } else if drag.rotating { let pos = trans.translation; - trans.rotate_around(pos, Quat::from_axis_angle(Vec3::Z, -ev.delta.y * 0.05)); - // clamping of angle to rect angles - let (_, angle) = trans.rotation.to_axis_angle(); - const TOL: f32 = 0.06; - if f32::abs(angle) < TOL { - trans.rotation = Quat::from_axis_angle(Vec3::Z, 0.); - } else if f32::abs(angle - std::f32::consts::PI) < TOL { - trans.rotation = Quat::from_axis_angle(Vec3::Z, std::f32::consts::PI); - } else if f32::abs(angle - std::f32::consts::PI / 2.) < TOL { - trans.rotation = Quat::from_axis_angle(Vec3::Z, std::f32::consts::PI / 2.); - } else if f32::abs(angle - 3. * std::f32::consts::PI / 2.) < TOL { - trans.rotation = Quat::from_axis_angle(Vec3::Z, 3. * std::f32::consts::PI / 2.); - } + // calculate current angle in radians + let current_angle = trans.rotation.to_axis_angle().1; + // add the new rotation + let new_angle = current_angle - ev.delta.y * 0.05; + // snap to nearest 90 degrees (π/2 radians) if within threshold + const SNAP_THRESHOLD: f32 = 0.1; + let snapped_angle = { + let nearest_right = (new_angle / (std::f32::consts::PI / 2.0)).round() + * (std::f32::consts::PI / 2.0); + if (new_angle - nearest_right).abs() < SNAP_THRESHOLD { + nearest_right + } else { + new_angle + } + }; + trans.rotate_around( + pos, + Quat::from_axis_angle(Vec3::Z, snapped_angle - current_angle), + ); } } }