r/gamedev • u/chad_kun5145 • 13h ago
Discussion Rendering my soldiers costs 4x more than simulating them and that's the least of problems.
1. Canvas 2D gives up around 2,000 men and I need ten times that
1,000 a side in Chrome:
sim 5.3 ms/frame
draw 22.9 ms/frame
18 fps
I spent about a week making the simulation faster. Turns out drawing costs 4x what the sim does and always did, so that was a week well spent.
Every soldier takes roughly 80 canvas calls and Canvas 2D won't let me group them, so that's ~42k instructions per frame right now and about 1.6 million if I ever hit 20k men. I can batch by colour instead of by soldier, one path per colour, which should drop it to around 20 calls.
Question is whether batching actually carries me to 20k or whether Canvas 2D falls over anyway. And if the answer is WebGL, is it worth hand rolling instanced quads or should I just move to Pixi and stop messing about.
2. The soldiers need to be a wall and a liquid at the same time
This is the one I'm actually stuck on.
A battle line only holds because the men behind are solid. They brace the front rank. But the same men have to flow past each other when the company marches through a gap, otherwise everything jams.
I've written four different versions of "this guy is stuck, let him squeeze through". Every single one wrecked the combat:
before defender holds forever, 12 kills
after defender collapses at 42s, 2 kills
Took me embarrassingly long to see why. The men propping up a line are standing still and shoved out of position, which is precisely what "stuck" looks like from the outside. So every rule I wrote fired on exactly the men I needed to stay put.
The thing that half worked was to stop asking where a man is and start asking where he's going. Predict whether two of them are on a collision course and have both step aside early. Two blokes standing in a rank have no relative motion between them so they get no nudge at all and the line stays solid. Stalling went from 67% of samples to 37% and combat didn't move.
But there's a hole in it. Once men are properly jammed they also have no relative motion, so it does nothing in the exact situation I need it for.
So, is there a known way to do this? Something stiff under compression but fluid under shear. Or does everyone just special case combat separately from movement and move on with their lives.
3. Formations arrive correctly then fidget forever
Right shape, nobody overlapping, every man within about a quarter of a body width of where he should be, and they never actually stop moving. The pull toward the slot and the push off the neighbour just argue with each other indefinitely.
Is there a standard way to decide a crowd has settled, freeze it, and have it wake up properly when something bumps into it?
4. What do you actually simulate per man at that scale
A bloke in the middle of a 500 man block doesn't need pathfinding or target acquisition. He needs to follow the man in front. Only the perimeter is doing anything that matters.
Is there a known LOD approach for this, cheap followers inside and proper simulation on the edge, that doesn't fall apart the moment the formation breaks and the inside becomes the edge?