Sphere
Log in Join

Godot 4.3 dropping to 20fps with 2000 CharacterBody2D nodes — _physics_process

## The scene

Godot 4.3 stable, 2D top-down prototype with a crowd of roughly 2000 small enemies. Each one is a CharacterBody2D with a move_and_slide() call in _physics_process. In the editor I was getting 18-22fps, and around 30 in an export build. I opened the profiler expecting to see draw calls, and got this instead:

Physics 2D    31.4 ms
Script 22.8 ms
Rendering 6.1 ms

So it was not rendering. I had spent a whole afternoon baking the tilemap and combining sprites for nothing.

## The dumb thing I was doing

Every instance ran this:

func _physics_process(delta):
velocity = (target - global_position).normalized() * speed
move_and_slide()

Two thousand times per tick, each one allocating a temporary Vector2 and doing a square root, then move_and_slide doing collision work against the tilemap. Fine for fifty agents. Not fine for two thousand. I did not need pixel-perfect steering for a crowd that is five pixels wide, but I wrote it like I did.

## What I actually changed

Four things, in order of how much they helped:

- A single manager computes a PackedVector2Array of desired directions once per tick, and each body reads its index instead of recomputing a normalized vector. The loop looks like this:

for i in range(count):
dirs[i] = (targets[i] - bodies[i].global_position).normalized()

- Bodies more than two screens from the camera call set_physics_process(false), and re-enable when they come back near.
- The normalized() call only happens when the target is within a threshold; far away I use a raw direction, since nobody can see the difference at that scale.
- Physics > 2D > Physics Ticks/Second dropped from 60 to 30 for this prototype. The movement is loose and floaty anyway, so nobody noticed.

## What did not help

I first tried queue_free() on distant enemies and respawning them as they came back, thinking fewer nodes meant less work. It got worse. Allocating and freeing thousands of nodes caused churn, and the spawn loop itself was heavier than just leaving the nodes parked with physics disabled. I also tried raising the physics tick rate to 120, hoping the work would spread out more evenly. It did the opposite: double the callbacks per second and the frame time got worse. I wanted the fix to be a settings change, not a code change, so I was clutching at straws. Removing the CollisionShape2D on distant bodies helped a little, but the main win was the manager array.

## The numbers after

Physics 2D    7.2 ms
Script 4.9 ms
Rendering 6.1 ms

Steady 60fps in the editor, which was all I needed to keep iterating. The crowd still looks like a crowd. I am sure there is a cleverer approach using a single MultiMesh and computing positions on the GPU, but for a prototype this was enough and I did not want to rewrite the collision behaviour. If your crowd scene tanks, profile before you touch your sprites; mine was entirely self-inflicted in script.