Skip to content

Commit d2debf2

Browse files
andriyDevjohanhelsing
authored andcommitted
Fix sprite picking backend not considering the viewport of the camera. (#16386)
# Objective - When picking sprites, the pointer is offset from the mouse, causing you to pick sprites you're not mousing over! ## Solution - Shift over the cursor by the minimum of the viewport. ## Testing - I was already using the bevy_mod_picking PR for my project, so it seems to work! - I tested this on the sprite_example (making the camera only render to part of the viewport), and it also works there. ## Notes - This is just aevyrie/bevy_mod_picking#365 but in Bevy form. - We don't need to renormalize the viewport in any way since the viewport is specified in pixels, so all that matters is that the origin is correct. Co-authored-by: johanhelsing <[email protected]>
1 parent e6fbcb7 commit d2debf2

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

crates/bevy_sprite/src/picking_backend.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,13 @@ pub fn sprite_picking(
7171
continue;
7272
};
7373

74-
let Ok(cursor_ray_world) = camera.viewport_to_world(cam_transform, location.position)
75-
else {
74+
let viewport_pos = camera
75+
.logical_viewport_rect()
76+
.map(|v| v.min)
77+
.unwrap_or_default();
78+
let pos_in_viewport = location.position - viewport_pos;
79+
80+
let Ok(cursor_ray_world) = camera.viewport_to_world(cam_transform, pos_in_viewport) else {
7681
continue;
7782
};
7883
let cursor_ray_len = cam_ortho.far - cam_ortho.near;

0 commit comments

Comments
 (0)