Skip to content

Viewport physics_object_picking_sort does not always pick the top objects by z-index/scene tree order in an area with more than 64 objects #80769

Closed
@ryanabx

Description

@ryanabx

Godot version

4.2 dev 3 [013e8e3]

System information

Godot v4.2.dev3 - Windows 10.0.22621 - GLES3 (Compatibility) - AMD Radeon(TM) Graphics (Advanced Micro Devices, Inc.; 31.0.21023.2010) - AMD Ryzen 9 6900HS with Radeon Graphics (16 Threads), Web Browser: Chrome 116.0.5845.96

Issue description

The physics_object_picking_sort property on viewports does not work 100% of the time when a large amount of physics objects are in the same place.
EDIT: This appears to be due how the raycast to detect pickable physics objects works. Basically, the raycast has a 64 object limit, and the 64 objects it picks could be any random 64 objects in an area, not explicitly the top 64 objects in an area. All the physics_object_picking_sort property does is sort the random 64 objects hit by z-index and scene tree order. I suggest that if physics_object_picking_sort is set to true, the raycast should only call input_event on the top 64 objects hit by the raycast by z-index or scene tree order.

My current project, see how my selection is not always the top most item in the scene tree.
https://github.com/godotengine/godot/assets/56272643/c59ea07d-39a9-4650-b7d5-a10bc33e0eca

My minimal example project (the output is showing the index and name of the first item that has _on_area2d_input_event fired:
https://github.com/godotengine/godot/assets/56272643/dcc8f77e-9907-461a-834c-2613d376cfff

I believe this is happening due to how picking works. I remember hearing that picking uses a raycast to determine overlapping physics objects, and that ray is limited to 64 objects. The problem is, the 64 objects it chooses are not necessarily in scene tree order or z-index order even if physics_object_picking_sort=true.

Steps to reproduce

  • Create a project with over 64 overlapping Area2Ds with the pickable property set to on. Set the viewport's physics_object_picking_sort property to true.
  • Connect the input_event signal to each Area2D printing identifying info about the Area2D (name perhaps, or index).
  • Notice how the topmost object does not always fire an input event when picking.

Minimal reproduction project

godot-bug-report.zip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions