Correct implementation of collisions for large number of objects

I’m making a survivor-type game and I need help handling multiple objects collision.

Basically, I have three problems:

  1. I run into performance issues when the number of objects is getting high (around +400/500) The most performance intense events are the collisions which are implemented like this:

Is there better way to implement this? Or maybe that’s the engine limit and I need to use some tricks like enabling collisions only for objects in X distance from player? Maybe I should compress object sprites as much as I can and that should help?

  1. The second problem is that the objects are “shaking” and “teleporting” based on collision rules above and the object move event as seen below:

I think this problem could be solved if I managed to find a solution to the third problem.

  1. The third problem is that the objects keep pushing themselves which breaks the game logic (some objects move faster). Is there a way to prevent this?

For starters, add some conditions to the event. Otherwise GDevelop will have to test every combination and permutation of monster collisions and try to move them apart. It should only work with the objects that are colliding, not everything. So get rid of that first event in your screen snip and replace it with the suggestions below.

Now, I’m assuming that Monsters is an object group that has Monster1, Monster2 and Monster3 as members. If so, create an event that checks for collision between Monsters and Monsters, and has the action to move Monsters from Monsters. This should help the processing time.

I’s also suggest you create an object group for the impassable objects (say called “Impassables”). Then create an event that checks for collision between Monsters and Impassables, moving Monsters away from Impassables.

And instead of updating the monster’s Z position in that second event of the screen snip, consider adding the Y-Sort extension and behaviour to the monsters. It may be a tad faster.

Ok. So first of all, I refactored the code to use objects groups with condition instead of permutation on objects, but I guess the implementation of this isn’t really different under the hood because I didn’t notice any improvement in performance.

I tested disabling setting dynamic z-order and I saw a slight raise in performance, so I will take a look at the Y-Sort extension.

You could try to use the Boids extension. It’s more efficient than the “move away” action.

2 Likes

First of all, based on this thread Question about collision detection events and performance - #5 by Keith_1357, I think the @mrnen suggestion seems to not be optimal.

The “Move object away from object” action already checks for distance, so adding a basic check “Object is in collision with object” is basically making the same check twice (because they both start with checking distance). I’m not sure, but I think the distance checking is not as taxing as the actual collision calculations which are making the most hit on performance.

As for the @davy suggestions, using the flock extension can be about 50% more efficient, but only if it fits your use case. I did some testing and I wasn’t satisfied with the results for my use case (+ the documentation is somewhat lacking).

For now I’m gonna stick to optimisation by interfering with game logic simply by applying actions to less objects.