godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.93k stars 21.15k forks source link

PhysicsDirectSpaceState2D.intersect_shape stops working when there are lots of Area2Ds #94367

Open bogcow opened 3 months ago

bogcow commented 3 months ago

Tested versions

4.2.stable

System information

Windows 10

Issue description

Calling the function PhysicsDirectSpaceState2D.intersect_shape returns an empty array when there are many nearby overlapping Area2Ds. GIF 7-14-2024 5-39-03 PM In this GIF, the red circle is the intersect_shape call being made every physics frame. The lighter blue circle is the Area2D that we are trying to intersect. The darker blue circle is 3,000 Area2Ds on a separate collision layer. Notice how the size of the return results array goes to 0 as the mouse gets closer to the 3,000 Area2Ds. You would expect that the results size is 1 as long as the red circle is over the lighter blue circle.

Steps to reproduce

Minimal reproduction project (MRP)

IntersectShapeTest.zip

AThousandShips commented 3 months ago

Can you please try with 4.2.2 to make sure it's still occurring

rburing commented 3 months ago

This is because of the following:

https://github.com/godotengine/godot/blob/97b8ad1af0f2b4a216f6f1263bef4fbc69e56c7b/servers/physics_2d/godot_space_2d.h#L108

It should be documented and/or made configurable.

Making it configurable would require moving some memory allocations:

https://github.com/godotengine/godot/blob/97b8ad1af0f2b4a216f6f1263bef4fbc69e56c7b/servers/physics_2d/godot_space_2d.h#L111-L112