Skip to content

Commit

Permalink
respect viewport in Camera::screen_to_world
Browse files Browse the repository at this point in the history
  • Loading branch information
rileylyman authored and not-fl3 committed Oct 15, 2024
1 parent 6d72a37 commit 9ce2f73
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,24 @@ impl Camera2D {
///
/// Point is a screen space position, often mouse x and y.
pub fn screen_to_world(&self, point: Vec2) -> Vec2 {
let dims = self
.viewport()
.map(|(vx, vy, vw, vh)| Rect {
x: vx as f32,
y: screen_height() - (vy + vh) as f32,
w: vw as f32,
h: vh as f32,
})
.unwrap_or(Rect {
x: 0.0,
y: 0.0,
w: screen_width(),
h: screen_height(),
});

let point = vec2(
point.x / screen_width() * 2. - 1.,
1. - point.y / screen_height() * 2.,
(point.x - dims.x) / dims.w * 2. - 1.,
1. - (point.y - dims.y) / dims.h * 2.,
);
let inv_mat = self.matrix().inverse();
let transform = inv_mat.transform_point3(vec3(point.x, point.y, 0.));
Expand Down

0 comments on commit 9ce2f73

Please sign in to comment.