2D picking with a 3D camera#962
Merged
Jondolf merged 2 commits intoMar 20, 2026
Merged
Conversation
Jondolf
pushed a commit
that referenced
this pull request
Mar 23, 2026
# Objective Allow using perspective cameras with the 2D physics plugin ## Solution Follow the ray that `bevy_picking` gives to its intersection with the Z axis rather than truncating the value to 2D ## Testing (see below) --- ## Showcase I added a demo to the repository under the name `picking_2d_in_perspective.rs` (in `avian2d`); it looks a bit like this: [Screencast_20260320_083503.webm](https://github.com/user-attachments/assets/a2db47e8-0b60-47c7-9be7-05984c14981b) I had hoped to add a free camera to the demo so you could see rotations working as well; as of yet, I haven't gotten around to it. I may finish that in a separate PR if I have the time, or someone else can add it in :smile:
Contributor
|
Does this mean that colliders must be located at Z = 0 (world space?) to be picked properly with a camera using perspective projection, and if so, would it be possible to allow that Z level to be configured instead? |
Contributor
I made something that seems to work in my use case (3D camera with perspective projection and colliders located far away in negative Z, which was broken earlier) but I don't know if it's fully correct |
Jondolf
added a commit
that referenced
this pull request
May 14, 2026
# Objective - Even after #962, 2D picking only works reliably when pickable colliders are located on the Z = 0 plane when e.g. using perspective projection or a rotated camera. Allowing this Z coordinate to be configured could be useful. ## Solution - Make the Z level used for picking ray intersection configurable by adding a new field to `PhysicsPickingSettings`. - Extend the `picking_2d_in_perspective` example with controls for moving the physics picking Z plane, rotating the camera, and switching projection type, demonstrating this new functionality. - Write a migration guide entry for the changes in this PR and #962. ## Testing - The extended `picking_2d_in_perspective` example seems to work properly. - The solution works in my use case, picking from a 3D camera with perspective projection and colliders located far away in negative Z, which was previously broken. - `cargo test` returns 4 failures, but I get the same result on the latest commit before my changes. ```log ---- dynamics::rigid_body::mass_properties::tests::mass_properties_add_remove_collider stdout ---- thread 'dynamics::rigid_body::mass_properties::tests::mass_properties_add_remove_collider' (22023) panicked at crates/avian2d/../../src/dynamics/rigid_body/mass_properties/mod.rs:736:9: assertion `left == right` failed left: 15.707963 right: 15.707964 ---- dynamics::rigid_body::mass_properties::tests::mass_properties_no_auto_mass_add_remove stdout ---- thread 'dynamics::rigid_body::mass_properties::tests::mass_properties_no_auto_mass_add_remove' (22026) panicked at crates/avian2d/../../src/dynamics/rigid_body/mass_properties/mod.rs:890:9: assertion `left == right` failed left: 14.999999 right: 15.0 ---- dynamics::rigid_body::mass_properties::tests::mass_properties_move_child_collider stdout ---- thread 'dynamics::rigid_body::mass_properties::tests::mass_properties_move_child_collider' (22025) panicked at crates/avian2d/../../src/dynamics/rigid_body/mass_properties/mod.rs:843:9: assertion `left == right` failed left: 14.999999 right: 15.0 ---- dynamics::rigid_body::mass_properties::tests::mass_properties_rb_collider_with_set_mass_and_child_collider_with_set_mass stdout ---- thread 'dynamics::rigid_body::mass_properties::tests::mass_properties_rb_collider_with_set_mass_and_child_collider_with_set_mass' (22033) panicked at crates/avian2d/../../src/dynamics/rigid_body/mass_properties/mod.rs:629:9: assertion `left == right` failed left: 14.999999 right: 15.0 ``` All testing done was on x86_64-unknown-linux-gnu (latest CachyOS Desktop as of writing, using the [rust](https://packages.cachyos.org/package/cachyos-extra-znver4/x86_64_v4/rust) package from the `cachyos-extra-znver4` distribution repository). --------- Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Objective
Allow using perspective cameras with the 2D physics plugin
Solution
Follow the ray that
bevy_pickinggives to its intersection with the Z axis rather than truncating the value to 2DTesting
(see below)
Showcase
I added a demo to the repository under the name
picking_2d_in_perspective.rs(inavian2d); it looks a bit like this:Screencast_20260320_083503.webm
I had hoped to add a free camera to the demo so you could see rotations working as well; as of yet, I haven't gotten around to it. I may finish that in a separate PR if I have the time, or someone else can add it in 😄