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

Convert cursor to world 2D coordinates with custom viewport physical position #278

Open
erictuvesson opened this issue Apr 9, 2024 · 0 comments

Comments

@erictuvesson
Copy link

Page: Convert cursor to world coordinates - 2D games

The viewport_to_world and viewport_to_world_2d functions take in viewport_position which is not relative to the screen corner. To get the correct viewport_position it has to be adjusted to the viewport logical position.

I don't think this should replace the current code on the page, but it would be helpful to have something that points in this direction as well. When using a crate like bevy_pixel_camera which modifies the viewport position it's not very straightforward with physical and logical viewport positions.

Here is a code snippet that takes the viewport logical position into account:

if let Some(world_position) = window.cursor_position().and_then(|cursor| {
    if let Some(logical) = camera.logical_viewport_rect() {
        camera.viewport_to_world_2d(camera_transform, cursor - logical.min)
    } else {
        camera.viewport_to_world_2d(camera_transform, cursor)
    }
}) {
    // ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant