Skip to content

Make 2D picking Z plane configurable#982

Merged
Jondolf merged 8 commits into
avianphysics:mainfrom
Luracasmus:main
May 14, 2026
Merged

Make 2D picking Z plane configurable#982
Jondolf merged 8 commits into
avianphysics:mainfrom
Luracasmus:main

Conversation

@Luracasmus

@Luracasmus Luracasmus commented May 6, 2026

Copy link
Copy Markdown
Contributor

Objective

  • Even after 2D picking with a 3D camera #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 2D picking with a 3D camera #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.
    ---- 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 package from the cachyos-extra-znver4 distribution repository).

@Jondolf Jondolf added C-Feature A new feature, making something new possible A-Spatial-Query Relates to spatial queries, such as ray casting, shape casting, and intersection tests 2D Specific to two dimensions D-Straightforward Simple bug fixes and API improvements, docs, test and examples X-Uncontroversial This work is generally agreed upon labels May 14, 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! This seems like a reasonable addition to me. A bit of a shame that the shapes must be at the same z coordinate, but fixing that properly seems a bit tricky 🤔 so this is a fine compromise for now.

I tweaked the docs and migration guide a bit, just to improve clarity and wording at some parts

@Jondolf Jondolf merged commit 85de616 into avianphysics:main May 14, 2026
6 checks passed
@Jondolf Jondolf added this to the 0.7 milestone May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2D Specific to two dimensions A-Spatial-Query Relates to spatial queries, such as ray casting, shape casting, and intersection tests C-Feature A new feature, making something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples X-Uncontroversial This work is generally agreed upon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants