![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | jarlowrey |
In game I am spawning 11 bullets every second and a new enemy every 2 s. The bullets are staying on screen, and the enemies I killed immediately. Thus everything should be the same layer/mask for most of the time, but collision pairs still increases, seemingly exponentially.
Why is that? Why doesn’t killing an enemy decrease the number of collision pairs more (especially since there is typically only 1 enemy on the screen at a time)?
For example, the following screen shot has 27.5k collision pairs, 26 fps, 3.1k nodes and 4k objects, but nothing has colliding layers/masks (the red stuff is not colliding). Why could that be?
I believe collision pairs is an algorithm term for two colliders being compared to see if they’re colliding. The simple version is that an object will create a list of neighbors to compare with one at a time, each pair would also allow the other object to avoid duplicating the collision check when it does its comparisons.
Without knowing more, your scene doesn’t look like it has thousands of nodes. You may have a leak somewhere.
If not, there is an option in Project Settings > Physics > 2d, for Cell Size.
A collision system will be exponentially slower with each addition, as each collider has to compare itself against every other. Each time you add one, that is one more that every collider has to test for. To optimize, Godot uses a grid, and colliders will only look within a certain area of that grid for neighbors to collision test. The objects that are far away in other grid cells can never be hit, and are ignored. Less pairs to tests.
When you have small objects that cluster, more can fit into a grid cell, resulting in more tests. So you may try experimenting with shrinking the grid cell size by small amounts.
avencherus | 2018-05-10 03:36
I have shrunk the grid size down to zero and it didnt seem to have much improvement. I’ve looked over the code and tested it (shoot mines, wait for them to be destroyed, destroy enemies, etc) to see if the nodes can reduce back down or if they keep increasing, and it seems to reduce itself (ie no leak). I will keep looking into ways to reduce node count, it does seem high but it may be right.
Thanks!
jarlowrey | 2018-05-10 05:38
Thus everything should be the same layer/mask for most of the time, but collision pairs still increases
Collision pairs only exist for things on the same layer/mask. For all the physics objects, what are their collision layers and masks set to be? are all your bullets set to collide with each other? That would explain the exponential-ish increase in pairs.
markopolo | 2018-05-11 16:11
What I mean is that in the screenshot with 27.5k pairs, nothing is set to collide with each other.
jarlowrey | 2018-05-12 14:02
Oh, shows what I know -_-
markopolo | 2018-05-12 17:14
Just my poor wording, no worries!
jarlowrey | 2018-05-14 23:30