-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keep focus point under cursor when moving model
This doesn't work perfectly yet, probably due to #18.
- Loading branch information
1 parent
b6673cc
commit 8985bb9
Showing
3 changed files
with
46 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,57 @@ | ||
use nalgebra::Translation2; | ||
use nalgebra::{distance, Translation2}; | ||
use winit::dpi::PhysicalPosition; | ||
|
||
use crate::{camera::Camera, math::Point}; | ||
use crate::{camera::Camera, math::Point, window::Window}; | ||
|
||
pub struct Movement { | ||
focus_point: Option<Point>, | ||
cursor: Option<PhysicalPosition<f64>>, | ||
} | ||
|
||
impl Movement { | ||
pub fn new() -> Self { | ||
Self { focus_point: None } | ||
Self { | ||
focus_point: None, | ||
cursor: None, | ||
} | ||
} | ||
|
||
pub fn start(&mut self, focus_point: Option<Point>) { | ||
pub fn start( | ||
&mut self, | ||
focus_point: Option<Point>, | ||
cursor: Option<PhysicalPosition<f64>>, | ||
) { | ||
self.focus_point = focus_point; | ||
self.cursor = cursor; | ||
} | ||
|
||
pub fn stop(&mut self) { | ||
self.focus_point = None; | ||
} | ||
|
||
pub fn apply(&mut self, diff_x: f64, diff_y: f64, camera: &mut Camera) { | ||
if let Some(_) = self.focus_point { | ||
// TASK: Moving feels good, if you're dragging the model exactly | ||
// where your mouse goes. It feels weird, if the mouse cursor | ||
// moves faster or slower than the model you're moving. | ||
// | ||
// The following factor achieves this good-feeling move for | ||
// relatively small models at the default distance between | ||
// camera and model origin. It breaks down when moving the | ||
// camera closer or away from the model, which is the far more | ||
// common case. | ||
// | ||
// It would be nicer to have a zoom factor that depends on the | ||
// distance between camera and model origin, or even the | ||
// distance between the camera and the part of the model the | ||
// mouse is currently pointing at (or more precisely, the | ||
// distance between the camera and a plane that touches the | ||
// surface of the model where the mouse is pointing, and whose | ||
// normal is parallel to the camera's viewing direction). | ||
let f = 0.2; | ||
|
||
let trans_x = diff_x * f; | ||
let trans_y = -diff_y * f; | ||
|
||
let translation = Translation2::new(trans_x, trans_y); | ||
|
||
camera.translation = translation * camera.translation; | ||
pub fn apply( | ||
&mut self, | ||
cursor: Option<PhysicalPosition<f64>>, | ||
camera: &mut Camera, | ||
window: &Window, | ||
) { | ||
if let (Some(previous), Some(cursor)) = (self.cursor, cursor) { | ||
let previous = camera.cursor_to_model_space(previous, window); | ||
let cursor = camera.cursor_to_model_space(cursor, window); | ||
|
||
if let Some(focus_point) = self.focus_point { | ||
let d1 = distance(&camera.position(), &cursor); | ||
let d2 = distance(&camera.position(), &focus_point); | ||
|
||
let f = d2 / d1; | ||
|
||
let diff = (cursor - previous) * f; | ||
let translation = Translation2::new(diff.x, diff.y); | ||
|
||
camera.translation = translation * camera.translation; | ||
} | ||
} | ||
|
||
self.cursor = cursor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters