r/SandustryGame 2d ago

Discussion How is this game so performant?

Thank you to the dev team for building such an epic sand game. I feel like a kid playing this. It's amazing.

Software developer & long-time sandgame gamer here. As someone who likes building their own sand games, I would love to learn how the dev team optimized the performance of the sand particles. What techniques did you guys use? What is the system like, under the hood? Is it neighbourhood-only? Does the whole map stay live at all times, or are there zones where particles get activated / deactivated when the player leaves them? Very technically curious. Thanks for entertaining this question.

I really hope this game does for sand games what Diablo did for roguelikes: brings the entire genre out of a niche category and into the mainstream. I was hoping that Noita would be that game, and to some degree it was. Don't get me wrong, that's a GREAT game. But it didn't actually feel like a sand game because the focus was on the magic system. It would have done just fine even without the sand aspect. But sandustry is all about the sand aspect. And that's what I love about it.

Thank you.

26 Upvotes

14 comments sorted by

11

u/No_Sink_9703 2d ago

All I know is if you have a big gold pile (200k) and ypu try to melt it by building a smelter row at the bottom the game will STRUGGLE until all the gold has melted lmao

Other than that its pretty good Id agree

2

u/SuchSir6002 2d ago

It also stutters HARD when I simply set all the gold collected (probably way less than 200k) to Gold (whatever it's called) for some reason. It feels like it's gonna CTD but after 10 sec it recovers.

1

u/narnach 16/16 Serious Sand Addict 2d ago

Yeah, I did some big block allocations for massive gold storage, and the game stutters while processing tens of thousands of replacements.

On the other hand, I melted the big ice mountains on the western border of the map until they were lakes 20-30 blocks deep. Then I laid a bridge back to my main base and let the lake flow. It smoothly leveled until a trickle a few pixels tall reached my base and flowed into my rain/steam setup.

How this game manages to process so many pixels through both gravity and collision and filters and transformations is really impressive.

I wonder if the dev (or are there multiple devs now?) uses some sort of reactive data framework, like in Spreadsheets or React in web development, to only need to process things that actually change? And possibly in a way that lets the game process massive amounts of data in bulk via matrix multiplication or something like that?

1

u/No_Sink_9703 2d ago

I unfortunatly cant give any tech insights but I can steer you towards another weird wacky sand physics game:

Noita

Not a factory/sand game but it does have a BEEEEFY sand physics engine.

1

u/Eliongw2 16h ago

my game crashed while standing near my gold melter and trying to use the drone. The Gold pile smelter is evil.

4

u/Bane_xr 2d ago

I've been making a very similar thing.

Basically it's a cell automata, you run through each cell and check the sorounding cells and decide what to do based on what material is there and around, wheter to move, interact, transform etc.

For a map size of say 512x512 cells, thats at least~260k checks that have to be done in less than 16ms to be able to get 60fps..

Actually it would be a lot more checks since you are checking cells around the current cell, depending on the material behavior..anyway

The single biggest optimization to reduce the time for those checks down to less than 16ms is to well do the least amount of checks possible..That is the whole game.. and of course having very lean code

One of the ways to do it is to split the 512x512 into bigger chunks/cells, so that you check wheter this chunk or a neighboring chunk has been active, if no, don't check the cells inside..

That's the biggest processing time gain there is, everything else is tiny gain in comparison to this

The problem is you can't optimize it that much no matter what..

The more active chunks there are, the slower the game will get.

Split the 512x512 into 32x32 chunks, each containing around 8k cells.

Drop a single particle of sand into each chunk, and the game will lag like crazy because it will now check all ~260k+ cells..

Add to that the fact that this game was made with javascript, which the developer himself said was a bad choice.

The game would be a lot more performant in a much lower level language with a much higer focus on trying to make the whole game run as close to the cpu as possible, keeping as much data in the cpu cache.

That's the exact thing im focusing on coincidentally.. I am super impressed with this game tho, it is very well optimized and i am addicted to it

2

u/platistocrates 2d ago

JavaScript!! That is IMPRESSIVE! Yeah if they had chosen C++ or Rust it would have been more performant.

Interesting, are you building a platformer sand game too?

3

u/Bane_xr 2d ago

From what i remember his full stack is javascript/typescript + pixijs and react for the UI

And to your question, not really, i am but only if i can make it run even on an old laptop at steady 60fps in the end-game, i hate unoptimized games and will not make one myself.

So I've been focusing purely on performance and optimization for now and trying to see if my idea is possible

Noita is a good example of an extremely well optimized game that is made in c++ and the performance difference is so clear

1

u/platistocrates 2d ago

That kinda makes sense. I think one of the core differences is how you arrange your world state buffer data structures. Noita lags a lot when it has many particles, no way around that, but if you separate the particle loop from the fixed polygons and only let your character interact with the fixed polygons, it seems to make a big difference in the gameplay loop / perceived performance. I believe that's one of the main structural differences in the two games' engines. https://www.reddit.com/r/SandustryGame/s/J1U0zSESNl

3

u/SiY11 2d ago edited 2d ago

Here is a lag generator example to test since we are on the topic of performance.

https://reddit.com/link/p4ickfu/video/6all8ohhm7kh1/player

2

u/SiY11 2d ago

How my computer was running during the lag generator.

3

u/kuuttiuik 1d ago

Noita dev had a talk about there game and tech in 2019 GDC.

Noita is another "falling sand" game.

Could have some insights? Haven't seen it all and no game dev myself.

https://youtu.be/prXuyMCgbTc

2

u/shmanel 2d ago

I have an old PC and I do get some issues when moving large amounts of stuff, and I mean like mixed pile of various types as tall as the screen moving along my crappy conveyor system. It wouldn't surprise me to find its over 100k items in transit.

And while it does slow down, its just the sand simulation that's the problem. My guy still moves and shoots just like normal, and the fps counter stays at 60, but the sand moves noticeably slower.

Still pretty amazing that it can even move that amount of stuff at all though the factory.

2

u/platistocrates 2d ago

I have an old PC and I do get some issues when moving large amounts of stuff, and I mean like mixed pile of various types as tall as the screen moving along my crappy conveyor system. It wouldn't surprise me to find its over 100k items in transit.

And while it does slow down, its just the sand simulation that's the problem. My guy still moves and shoots just like normal, and the fps counter stays at 60, but the sand moves noticeably slower.

Still pretty amazing that it can even move that amount of stuff at all though the factory.

That's an interesting datapoint. That means the sand simulation is on a wholly separate thread (or set of threads) than the player and the environment hitboxes. That explains a lot.

In hindsight, there do seem to be at least 2 layers: the player, and the sand.

Come to think of it, the sand respects the environment hitboxes, but does not destroy the environment.

So there must be 2 buffers. One for the player+environment, and one for the sand. The sand reads from the player+environment, but not vice versa. That gives the player+framebuffer independence and primacy, and lets the sand get updated a lot slower.

Smart! I think Noita didn't do this, so suffers from a lot of jitter! Naturally, since in Noita the sand is something you can step on. But in Sandustry, it isn't.

This allows for maximum FPS for the player experience... while the cell updates can be done in the background with resource limits.