-
-
Notifications
You must be signed in to change notification settings - Fork 21.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[3.x] Support for Dynamic BVH as 2D Physics broadphase #48314
Conversation
0db4c85
to
a7f371e
Compare
On the whole getting it to work in 2d looks pretty straightforward and this looks great. 👍 A couple of points
|
Also note that the expansion parameters are actually set here (bvh_structs.inc, 169) rather than the section you quoted. That section is
If in 2d the unit is pixels then you may find that boosting these works a bit better. Incidentally, feel free to have a play with redefining the auto expansion and seeing if you can come up with a good algorithm, I think I just ran out of time on that so I fixed it for the RCs for safety. I originally had all the expansion margins / auto expansion figures editable in the project settings, but was figuring it was overkill for most users. |
@lawnjelly Thanks for your feedback!
It makes sense, I will make a separate PR for this since it's an independent fix.
It's a good idea, I can change it to make the code more readable and easier to maintain.
No wonder I wasn't seeing any difference :D |
Yup absolutely. 👍 My thinking was that an AI system to choose the best values might be easiest for beginners, having them available would be good for advanced guys. I just wanted to keep things simple in the first rollout (and also iron out any bugs). |
List of changes: - Modified bvh class to handle 2D and 3D as a template - Changes in Rect2, Vector2, Vector3 interface to uniformize template calls - New option in Project Settings to enable BVH for 2D Physics (enabled by default like in 3D)
a7f371e
to
d8f6810
Compare
I've just pushed all the changes from previous comments and opened #48337 for the bvh tree optimization on Concerning expansion margins: |
Assuming you've done the testing this looks good to me. A couple of points to note:
So at the moment we have a bit of error prone +1 / -1 indexing hopscotch that may not be necessary. This can in some cases be resolved by adding a reserved dummy element at the beginning of arrays and taking account for this. But as there are quite a few arrays I never got around to changing this / evaluating whether it should be changed. On the other hand using +1 indexing is quite widely used, so there's no great problem. I think quake uses this, which is where I originally picked up this technique many years ago (presumably they also did it because checking zero can be faster on processors than comparing to a number).
In the old days the The problem imo with Also in extreme cases it can cause compilation to fail (we have had that in godot, see #41231). There are in 99% of cases no positive outcomes from using Actually there is a possible exception, if you are using it for a function defined in the cpp it could possibly be useful with whole program optimization - I don't tend to rely on that myself, and haven't tested the effect. I'm primarily referring to the use of |
Thanks! |
Might be worth doing some benchmarks after running |
Adding support to use @lawnjelly's dynamic BVH implementation in 2D physics.
I'm starting with a PR on 3.x as an exception, since the dynamic BVH has been implemented only on this branch for 3D. Once everything is validated, it can be all ported to master.
List of changes:
set_pairable
with no parameter changeRect2
,Vector2
,Vector3
interface to uniformize template callsNote 1: I've tested #45824 with the bvh enabled, and the results are similar to the hash grid with #47979 applied, so the dynamic bvh seems to handle collision layers/masks properly and doesn't need extra optimization in 2D and 3D.
Note 2: I've tested with different values for the expansion margin in the BVH to see if 2D needs different parameters, but it didn't seem to make a difference in test results:
godot/core/math/bvh_public.inc
Lines 402 to 408 in 3f5c106
Note 3: Performance tests have been done with a 3.x port of #48221, after which the 2D hash grid broadphase is the main bottleneck for the 2D physics step. Results would be different without multithreading as more frame time would be spent in the narrowphase and contact solver.
Test configuration:
CPU: AMD Ryzen 9 4900HS with Radeon Graphics, 3000 Mhz, 8 Core(s), 16 Logical Processor(s)
RAM: 16.0 GB
Display: GeForce RTX 2060 with Max-Q Design/PCIe/SSE2
Test scenes
2D - Simple contacts (1 island - 500 rigid bodies)
https://github.com/godotengine/godot-demo-projects/tree/master/2d/physics_tests/tests/performance/test_perf_contacts.tscn
~35% improvement (physics ticks)
Hashgrid - 500 bodies
BVH - 500 bodies
2D - Advanced contacts (multiple islands - 9 x 300 rigid bodies)
https://github.com/godotengine/godot-demo-projects/tree/master/2d/physics_tests/tests/performance/test_perf_contact_islands.tscn
~30% improvement (physics ticks)
Hashgrid - 9 x 300 bodies
BVH - 9 x 300 bodies
2D - Broadphase with no contact (125 000 rigid bodies)
https://github.com/godotengine/godot-demo-projects/tree/master/2d/physics_tests/tests/performance/test_perf_broadphase.tscn
~30% improvement on adding objects (physics ticks)
Hashgrid - 125 000 bodies
BVH - 125 000 bodies