At a certain point during the development of Tiny Tanks, we decided that the game would need Online Multiplayer. So we set out to add a high quality multiplayer to the game, not knowing how many hurdles we would have to overcome. In this article, I want to go over how I tamed one of the beasts of the game: The fully destructible environment.
We want to play online, what do we have to do?
Tiny Tanks features a massive amount of destruction. You can blow up the whole map into small pieces and drive around on them. They influence the gameplay and therefore, we have to somehow synchronize them when you want to play online. But why is that important?
Why online multiplayer + physics is a horrible idea
We can we not just run the physics simulation on everyone’s PC and just watch the game run fine? That is because of a thing called determinism, or to be more precise: non-determinism. What does that mean? When we run a deterministic physics simulation a few times, we expect the same things to happen exactly each time. No matter if we run it on a different PC or not. The problem is NVIDIA PhysX is only deterministic under very, very special conditions. Now the problem we face is the following: If we just let the simulation run on everyone’s PC without checking if all the simulations are running exactly the same, we will have great chaos very quickly. For one player half of the map could be gone and another player could still have most of the arena intact. And therefore, we have to make sure that everything is running the same for everyone and if not, we have to correct it to be the same again. Below you can see what it looks like when we force all the players to do the same thing, even if the physics may say something different.
But how much bandwidth do I need?
So, how do we do that? The solution is to send so-called snapshots or states from one of the players – the host – to all the other players. However, if we just send the raw states to the other players, we quickly run into trouble. Let’s imagine how much data the host would have to send to the other players. Our standard level consists of 65 blocks, which in turn each consist of 10 debris pieces. The worst-case scenario would be to send updates for each debris 60 times per second, consisting of the position and rotation, taking 16 bytes, totalling roughly 10 Mbits/s having to be sent. Now it doesn’t stop there. This data has to be sent to all the players. For 8 players that would mean the host would have to send 70 Mbits/s, which of course, is not really desirable.
Let's tackle this behemoth one step at a time
There are a few ways to counter this. The first step is to lower the rate at which the data is sent. We can go down to 20 times per second, that way we can – with the snip of a finger – cut the data by over 60%. Now we just have to add some smoothing, called interpolation to the movement so the debris looks nice and smooth when moving around.
Next, we can use delta compression. That means, we only send data when the values are different. When debris is not moving, we don’t send data. As most of the time the debris are actually not moving, we can save a huge amount of data.
Another technique we can use is to compress the position and rotation. We know that the debris will not move too far away from the centre and we do not have to know the position down to the micrometre. A few centimetres of accuracy is plenty enough. Doing this, we can reduce the size of the individual data from 16 bytes to roughly 6 bytes. Again, we reduced the sent data by roughly over 60%.
Looks like this could actually work!
Of course, there are a few more tricks, but using just these techniques we can reduce the traffic maximum from 70 Megabits/s to just under 10 Mbits/s. This still sounds like a ton, but remember that this is the traffic we would use up if all debris was to move at the same time. Due to delta compression, we only move 5-20% of the debris at a time. Meaning we realistically need 2 Mbits/s MAX as the host. The rest of the players will need even less.
Where do we go from here?
All of this took us a few weeks to get right and there are more things we had to incorporate. This is just to give you a glimpse at what we are actually doing to make the game run smoothly. We are still working hard towards a satisfying online experience, but we are very close to finishing it.
If you want to stay updated, make sure to follow us on our social media platforms.
Hope you enjoyed the article and thank you for reading,