Physics forces sync issue between players in multiplayer game

Hi everyone,

I’m working on a multiplayer game with Havok physics and Socket.IO.
The forces are exchanged between players through sockets, and the players are synced correctly.

But sometimes, the ball doesn’t behave the same on both clients (it desyncs).
Has anyone faced this issue before? What’s the best way to keep physics objects like a ball perfectly synced across clients?

//client

//onDragEnd
socket.emit('applyForce', { meshId: pickedMesh.id, force: force, position:meshPos, room: roomRef.current })
pickedMesh.physicsBody.applyImpulse(force, meshPos);

//on applyForce event
function onApplyForce(data: { meshId: string, force: Vector3, position: Vector3}{
    const { meshId, force, position } = data
    const mesh = currentSceneRef.current?.meshes.find(mesh => mesh.id === meshId)
    if (mesh && mesh.physicsBody)
        mesh.physicsBody.applyImpulse(force, position);
    }
    socket.on('applyForce', onApplyForce)
}
//server
socket.on("applyForce", (data) => {
    const { meshId, force, position, room }=data
    socket.to(room).emit("applyForce", { meshId, force, position });
    });

You should probably be running the physics simulation on the server, and only be sending input buffers from clients.

You can sill run the sims locally still but then you would have a server side authority to reference and use that information to extrapolate or interpolate the local clients physics to be in sync with the server side sim.

If you want to sync separate clients running simulations at different speeds with latency you are gonna have a bad time, its doable but there will always be some separation of state. The other option is you can choose one client to be the “authority” but even then you are going to do some sort of lag compensation.

1 Like

:+1:

1 Like