+1 vote

enter image description here

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?

enter image description here

asked May 10, 2018 in Engine by jarlowrey (561 points)

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.

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!

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.

What I mean is that in the screenshot with 27.5k pairs, nothing is set to collide with each other.

Oh, shows what I know -_-

Just my poor wording, no worries!

1 Answer

0 votes

One year later, I've found the answer. Collision Pairs indicate how many objects are in the same grid cell for the 2D physics engine. These cells are used to determine whether there might be a collision between two objects.

I probably had so many collision pairs due to pooling objects and placing them all on top of each other. Now when I pool I randomly spread them out over a large area.

Unfortunately the area cannot be toooo large, otherwise the number of grid cells will increase, causing more performance hits.

You can also decrease the cell size in Project Settings > Physics > 2D > Cell Size, though there is a similar tradeoff

answered May 6 by jarlowrey (561 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.