Efficient collision detection with 100 - 200 objects

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Yugo
:warning: Old Version Published before Godot 3 was released.

I would like to have particles, which fly around and if they enter a certain region a force should be added to them and I would also like to count the amount of particles, which entered a certain region.

I tried it with Particles2D and ParticlesAttractor2D, but it is not possible to count the particles with them.

So what is the most efficient way to do this? I would like it to run on mobile devices and also a little bit older ones.

Should I use Area2D and KinematicBodys or implement everything myself? If so how should I do it?

:bust_in_silhouette: Reply From: batmanasb

Look at the Bullet Shower demo if you want an efficient solution. But if you want to use a lot of collision shapes, there is a setting in Project Settings > Physics 2D > cell_size. It should always be a a number equal to 2^n and increasing it improves performance with a lot of large collision shapes, and decreasing it helps with small collision shapes. More info is in the 2D physics doc.

Thank you so much batman :slight_smile: Just one question, how could I test if one implementation is better than another one? I mean what if I don’t see a difference on my mobile device? Is there any way I could compare/benchmark both implementations/approaches?

Yugo | 2016-03-08 00:53

What I usually do is just add a label and set it’s text to OS.get_frames_per_second() to see the FPS.

batmanasb | 2016-03-08 00:56

Thanks a good idea! :slight_smile: But it’s always at 60. Why don’t I get more than 60?

Because what if both implementations are at 60 FPS. Which is better then?

Yugo | 2016-03-08 11:14

The more advanced solution (that I haven’t tried myself yet) is to use the debugger while the game is running. Look at the performance tab and pick the implementation with the lowest Process or Fixed Process time (depending on which of the two you used). Also, you can see how they scale by stress testing them by simply adding more and more objects.

batmanasb | 2016-03-08 21:29

Turn off Vsync to uncap the FPS

dezboyle | 2022-06-22 20:46