Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

respect viewport in Camera::screen_to_world #842

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading