Big slowdown when KinematicBodies collide with each other

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TheFamousRat

Hello,

First off, apologies if this has already been answered before, I looked for a similar topic in 3d but nothing turned up from my searches.

I am building a 3d game, in which I would like a large amount of enemies to chase the player : ~50 enemies at the same time would already be a nice target. To guarantee quick pathfinding, I am using a custom made FlowField-based approach.

The pathfinding or collision detection with the terrain works really well with one enemy or large amount of isolated ones. However, when they collide with each other, or when the player collides with them, the game slows down to 10 fps with even as little as 5 enemies (and gets worse with more enemies).

The enemies themselves are for now represented each by a KinematicBody, with as children one Cylinder CollisionShape and a Capsule MeshInstance. Each body is moved using move_and_slide_with_snap.

The problem is only encountered in situations where they collide against each other continuously for a few frames (for example when they reached their goal, when they are crammed in a small space etc.).

Am I doing something obvious wrong here ?

I tried to be as straight to the point as possible because this doesn’t seem to be related to my project in particular, but if this is an uncommon issue I’ll happily recreate a thread with more specific details.

I’m not sure if this is normal to have such a drop in performance with only 5 enemies. Anyway, if you don’t find another solution, you could add rules to your enemy to keep them apart, avoiding the collision. They could have an area where they check the distance with close enemies, and try to stay away.

MrEliptik | 2021-03-20 21:31

I don’t think either. However while profiling I couldn’t come to the conclusion that my own GDNative code was the issue, so it seems to be up with Godot. I think a good course of action would be to test the code on another machine honestly.

Yes, I was already thinking about adding some flocking behaviors ! This averts the issue, since when not colliding with each other the enemies behave fine. I’ll probably add some logic on top, like the enemies following their path without physics when far the player’s field of view, to save on CPU time with lots of opponents.

TheFamousRat | 2021-03-21 23:07