Out of the box physics replication is horrible when you get past 50 ping. Especially around 100.
What I want to happen is simple. I want players to be able to push around a box (imagine the default first person template) in multiplayer without teleportation at medium ping. I want the game to be playable up to 150 ping.
The system I wrote is pretty simple. It simulates physics on the client and server.
The server sends correction data to the client about the cube such as location and velocity, with a timestamp (I synced the client and server clocks). So the client may receive a correction from the server that says “The cube had location(100, 50, 20), velocity(50, 0, 10) at the time of 1075ms”.
Every frame, the client cube stores the same information in an array (velocity, location, time).
When the client receives a correction from the server, it looks in the array for the data with the same timestamp as the server correction data. At this point, we have the server correction data at whatever time (let’s say 2000ms) and the client cube data that was at the same time.
I then apply an impulse correction to the cube using this calculation :
Mesh->AddImpulse((ServerVel - PastClientVel) * VelocityFactor + (ServerPos - PastClientPos) * PositionFactor);
I can adjust velocity factor and position factor to determine the strength of influence they have on the correction.
This solution in theory sounds great, but in practice it doesn’t work well. There are several problems that I haven’t been able to solve through adjusting the factors:
- The cube sometimes goes flying when the impulse is too great
- The cube’s constant correction towards the server position makes it harder for the client to push the cube
- The cube sometimes moves very slowly towards it’s intended goal.
I can provide a video of the problem and the full code solution if you want. It’s not very much code.