From 19ffb0dd75a207a81ee21ba7da7ae5c7cbdd87f3 Mon Sep 17 00:00:00 2001 From: bherbruck Date: Sun, 18 Oct 2026 23:12:55 -0400 Subject: [PATCH] Orbit drags turn from where they began, so erratic drags don't drift (#2842) An orbit drag now applies its whole turn so far to the camera it began with, instead of adding each pointer step to the camera of the step before. The view then depends only on where the pointer is: dragging past straight up or down and back no longer loses the pitch that the clamp swallowed, and returning to the starting point brings back the starting view. The pivot is still picked once, when the drag begins, and kept still on screen by the rigid turn about it. Co-authored-by: Claude Opus 5.5 --- crates/ui-egui/src/navigation.rs | 31 ++++++++++++++++--- crates/ui-egui/src/viewport.rs | 53 ++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/navigation.rs b/crates/ui-egui/src/navigation.rs index b8e4a674c..a8083ae15 100644 --- a/crates/ui-egui/src/navigation.rs +++ b/crates/ui-egui/src/navigation.rs @@ -169,6 +169,7 @@ pub(crate) fn modifier_motion( let previous = app.viewport.modifier_nav; if previous.map(|(m, _)| m) != next.map(|(m, _)| m) { app.viewport.orbit_pivot = None; + app.viewport.orbit_from = None; app.viewport.orbit_free = None; app.viewport.orbit_snap = None; } @@ -481,6 +482,7 @@ pub(crate) fn handle(app: &mut SolveApp, ui: &mut egui::Ui, rect: Rect, resp: &e } if !dragging && motion.is_none() { app.viewport.orbit_pivot = None; + app.viewport.orbit_from = None; } if app.viewport.spin { // A click on the navigation bar (its own button) is not a stop. @@ -699,11 +701,32 @@ pub fn orbit(app: &mut SolveApp, rect: egui::Rect, dx: f64, dy: f64, press: Opti } }), }; - let Some(pivot) = pivot else { - app.cam.orbit(dx, dy); - return; + // The drag's whole turn so far, applied to the camera it began with: the view depends on + // where the pointer is, not on the path it took, so fast or erratic drags (a pitch held at + // straight up or down for a while, many tiny steps) can't drift. Anything else that moves + // the camera meanwhile (a wheel zoom, a new pivot) starts the sum again from there. + let from = match app.viewport.orbit_from { + Some(f) if f.last == app.cam && f.pivot == pivot => f, + _ => OrbitFrom { start: app.cam, dx: 0.0, dy: 0.0, pivot, last: app.cam }, }; - app.cam.orbit_about(dx, dy, pivot); + let (dx, dy) = (from.dx + dx, from.dy + dy); + app.cam = from.start; + match pivot { + Some(p) => app.cam.orbit_about(dx, dy, p), + None => app.cam.orbit(dx, dy), + } + app.viewport.orbit_from = Some(OrbitFrom { dx, dy, last: app.cam, ..from }); +} + +/// An orbit drag so far: the camera it began with, the pointer's total motion since, the pivot +/// and the camera that produced. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct OrbitFrom { + start: Camera, + dx: f64, + dy: f64, + pivot: Option, + last: Camera, } #[cfg(test)] diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index bde68cac6..b36560580 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -96,6 +96,8 @@ pub struct ViewportState { pub hover_bodies: Vec, /// The model point an orbit turns around (Preferences: orbit around the cursor). pub orbit_pivot: Option, + /// The orbit drag under way, summed from where it began (see [`crate::navigation::orbit`]). + pub orbit_from: Option, /// Continuous orbit: the view turns about the vertical until the next click or key. pub spin: bool, /// The 3D mouse's motion this frame, for the page showing the camera to apply with what its @@ -5956,6 +5958,57 @@ mod tests { } } + /// An erratic orbit drag (big jumps, reversals, the pointer over the panels and back) keeps + /// the point picked when it began on its pixel the whole way, and coming back to where it + /// began brings back the view it began with (up to the rounding of the pointer's f32 steps). + #[test] + fn an_erratic_cursor_orbit_keeps_its_pivot_and_comes_back() { + for perspective in [false, true] { + let mut h = crate::scenario::Harness::new(); + h.app.preferences.orbit_around = crate::prefs::OrbitAround::Cursor; + h.app.ui.perspective = perspective; + h.app.run("solid.box", json!({"length":80,"width":50,"height":20})).unwrap(); + h.app.fit_view(); + h.frames(2); + let rect = h.app.viewport.rect.unwrap(); + let at = rect.center() + vec2(30.0, 20.0); + let pivot = crate::prefs::pivot_under(&h.app, &projection(&h.app, rect), at); + let start = h.app.cam; + let shift = egui::Modifiers { shift: true, ..Default::default() }; + let send = |h: &mut crate::scenario::Harness, e: egui::Event| { + h.app.synthetic.push(e); + h.frame(); + }; + send(&mut h, egui::Event::ModifiersChanged(egui::Modifiers::NONE)); + send(&mut h, egui::Event::PointerMoved(at)); + send(&mut h, egui::Event::PointerButton { pos: at, button: egui::PointerButton::Middle, pressed: true, modifiers: shift }); + let mut seed: u32 = 12345; + let mut next = move || { + seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + f32::from((seed >> 16) as u16) / 65535.0 + }; + for i in 0..300 { + // Mostly small moves, now and then a big jump, some of them over the panels. + let p = if i % 17 == 0 { pos2(next() * 1600.0, next() * 1000.0) } else { at + vec2((next() - 0.5) * 900.0, (next() - 0.5) * 200.0) }; + send(&mut h, egui::Event::PointerMoved(p)); + assert_eq!(h.app.viewport.orbit_pivot, Some(pivot), "move {i}: the pivot is picked once, when the drag begins"); + let s = projection(&h.app, rect).to_screen(pivot).unwrap(); + assert!(s.distance(at) < 1.0, "perspective={perspective}, move {i} to {p:?}: the pivot moved to {s:?}"); + } + send(&mut h, egui::Event::PointerMoved(at)); + let s = projection(&h.app, rect).to_screen(pivot).unwrap(); + assert!(s.distance(at) < 1.0, "{s:?}"); + let back = h.app.cam; + assert!( + (back.yaw - start.yaw).rem_euclid(std::f64::consts::TAU).min((start.yaw - back.yaw).rem_euclid(std::f64::consts::TAU)) < 1e-6, + "{start:?} -> {back:?}" + ); + assert!((back.pitch - start.pitch).abs() < 1e-6, "{start:?} -> {back:?}"); + assert!((back.target - start.target).len() < 1e-4, "back where it began: {start:?} -> {back:?}"); + send(&mut h, egui::Event::PointerButton { pos: at, button: egui::PointerButton::Middle, pressed: false, modifiers: shift }); + } + } + #[test] fn shift_orbit_keeps_cursor_pivot_and_default_middle_pan() { let mut h = crate::scenario::Harness::new();