Skip to content

Sprite picking does not work correctly with custom viewport #19032

Closed as duplicate of#18005
@akimakinai

Description

@akimakinai

Bevy version

0.16.0

What you did

Used sprite picking with custom viewport

Camera {
    viewport: Some(Viewport {
        physical_position: (window_size * 0.125).as_uvec2(),
        physical_size: (window_size * 0.75).as_uvec2(),
        ..default()
    }),
    ..default()
},

What went wrong

  • pointer coordinate in sprite picking is incorrect. A sprite is picked in the wrong position.

Image

Image

Potential fix

Sprite picking code subtracts viewport min from cursor coordinate:

let viewport_pos = camera
.logical_viewport_rect()
.map(|v| v.min)
.unwrap_or_default();
let pos_in_viewport = location.position - viewport_pos;
let Ok(cursor_ray_world) = camera.viewport_to_world(cam_transform, pos_in_viewport) else {
continue;
};

However 2d_viewport_to_world example does not.

let Some(cursor_position) = window.cursor_position() else {
return;
};
// Calculate a world position based on the cursor's position.
let Ok(world_pos) = camera.viewport_to_world_2d(camera_transform, cursor_position) else {
return;
};

If I remove the subtraction from sprite picking code as well, it seems to work correctly. (it is redundant as Camera::viewport_to_world already does?)

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-BugAn unexpected or incorrect behaviorC-ExamplesAn addition or correction to our examples

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions