r/monogame • • 11d ago

Multi threaded Falling Sand simulator in a browser using MonoGame

Using MonoGame DesktopGL together with KNI to achieve multithreading in the browser.

In the browser, .NET’s threaded WebAssembly runtime lets workers share memory, so the simulation and terrain rasterization can run off the page thread. The renderer draws from a snapshot while the next simulation step runs. A small native bridge lets the game hand off the whole draw at once, keeping WebGL calls on the page thread.

My first attempt was running single threaded, but the game ran at a complete crawl. The render thread could have handled it, but all the simulation steps are very heavy (pixel level physics). This browser version is a Demo of the full game. I'm very happy with how well it ended up running, although it's not as performant as the full desktop version.

If interested, check out the browser demo at https://gravitonia.com/ and check out the Steam Store Page at https://store.steampowered.com/app/5272000/Gravitonia/

21 Upvotes

3 comments sorted by

1

u/Bockanator 6d ago

This is really cool! I'm imagining something similar to Particle Fleet: Emergence? I hope you change the AI store assets though because I don't think it's going to win you any favours πŸ˜… Regardless, I've wishlisted it!

1

u/jma-dev 5d ago

Thanks. I was not familiar with that game. Looks cool. It's certainly controversial with AI usage, but lying about it doesn't feel right. I'm actually a very seasoned developer, but in this day and age, the time boost is too big to ignore. It'll definitely going to consider your advice πŸ˜„

1

u/GladToBeAGuppy 5d ago

It looks cool! Wishlisted.