Why Factorio doesn’t burn everyone’s cpu?

795 views

Today i Fired up factorio again and i loaded up one of my older saves. And then, i had sudden realization. I know that object collisions are very taxing and take a lot of processing power. Then i zoomed out and watched Factorio handle more than 10k objects moving and colliding with each other on my screen and my cpu was resting at 30%. how is that even possible ?

In: Technology

4 Answers

Anonymous 0 Comments

To put it bluntly, most games are coded horribly inefficiently.

Consider a game with 1,000 different 3D boxes colliding. Boxes are one of the simplest shapes to test collisions for – you basically just need to check if the corner of one box is inside a second box. To check every box against every other box to see if any collide, that’s `1,000 * 1,000 = 1,000,000` different tests.

Now, that sounds like a lot, but it’s actually not *too* bad, because modern CPUs can perform tens of millions of calculations per frame while running at a cool 120 FPS. But what happens if you were to, say, go up to 10,000 boxes? Suddenly it’s `10,000 * 10,000 = 100,000,000` Oops, 10 times the boxes added *100 times* as much work for your CPU and now your PC has started to stutter.

That’s called an “N-squared” algorithm, because the amount of work goes up by the square of the number of objects. Those are bad, and you should avoid them.

There are a *lot* of tricks you can do to do better than checking every single box against every other box. For instance, Factorio isn’t checking every moving item against every other item. Instead, it only checks each item against the item in front of it. If you have 1,000 items on a belt, that’s only 1,000 checks, not a million.

Except — Factorio *also* knows that if items are already next to each other (compressed, in game lingo), it also doesn’t need to check them for collisions because they’re already colliding. So it skips them entirely — only separate clumps of items on the belt are checked. So you could have a million or more items on a belt, and yet the game is only checking for a few thousand collisions at any moment. That’s the exact opposite of what we saw before!

Factorio is full of these kinds of tricks, and that’s because it was made by a bunch of programmers who have a deep understanding of both computer science and the intricate details of how their own game works. Very few other game developers show both the ability and willingness to optimize their games to that degree.

You are viewing 1 out of 4 answers, click here to view all answers.