Skip to content

Commit 9ce2f73

Browse files
rileylymannot-fl3
authored andcommitted
respect viewport in Camera::screen_to_world
1 parent 6d72a37 commit 9ce2f73

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/camera.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,24 @@ impl Camera2D {
135135
///
136136
/// Point is a screen space position, often mouse x and y.
137137
pub fn screen_to_world(&self, point: Vec2) -> Vec2 {
138+
let dims = self
139+
.viewport()
140+
.map(|(vx, vy, vw, vh)| Rect {
141+
x: vx as f32,
142+
y: screen_height() - (vy + vh) as f32,
143+
w: vw as f32,
144+
h: vh as f32,
145+
})
146+
.unwrap_or(Rect {
147+
x: 0.0,
148+
y: 0.0,
149+
w: screen_width(),
150+
h: screen_height(),
151+
});
152+
138153
let point = vec2(
139-
point.x / screen_width() * 2. - 1.,
140-
1. - point.y / screen_height() * 2.,
154+
(point.x - dims.x) / dims.w * 2. - 1.,
155+
1. - (point.y - dims.y) / dims.h * 2.,
141156
);
142157
let inv_mat = self.matrix().inverse();
143158
let transform = inv_mat.transform_point3(vec3(point.x, point.y, 0.));

0 commit comments

Comments
 (0)