Skip to content

2D picking with a 3D camera#962

Merged
Jondolf merged 2 commits into
avianphysics:mainfrom
rdrpenguin04:rdr-2d-in-perspective-picking
Mar 20, 2026
Merged

2D picking with a 3D camera#962
Jondolf merged 2 commits into
avianphysics:mainfrom
rdrpenguin04:rdr-2d-in-perspective-picking

Conversation

@rdrpenguin04

Copy link
Copy Markdown
Contributor

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

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 😄

@Jondolf Jondolf added C-Bug Something isn't working C-Examples Improvements or additions to examples D-Straightforward Simple bug fixes and API improvements, docs, test and examples labels Mar 20, 2026
@Jondolf Jondolf added this to the 0.6.1 milestone Mar 20, 2026

@Jondolf Jondolf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@Jondolf Jondolf merged commit 25db6df into avianphysics:main Mar 20, 2026
6 checks passed
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:
@Luracasmus

Luracasmus commented May 2, 2026

Copy link
Copy Markdown
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?

@Luracasmus

Luracasmus commented May 2, 2026

Copy link
Copy Markdown
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?

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
Luracasmus@4c2e1e1

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>
@rdrpenguin04 rdrpenguin04 deleted the rdr-2d-in-perspective-picking branch May 31, 2026 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-Bug Something isn't working C-Examples Improvements or additions to examples D-Straightforward Simple bug fixes and API improvements, docs, test and examples

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants