- Home /
Physics in websocket multiplayer 2d game
Hey guys. I am trying to learn more about multiplayer and networking on unity so i decided to do a replica of soccer starts.
In that game, player applies a force to a cup/coin and pushes a ball or other cups/coins.
Using anodejs websocket as backend i basically broadcast the force and the direction vector so when the other client listens it, he applies the foce to the "twin" cup/coin in his side ( rotated 180 degrees ).
As first aproach, its working profectly, all cups moves fine, tested on 2 browsers and it was OK, until i made some stronger collisions, specially with the ball(less mass), boths clients gets out of sync, and it all becomes a mess :(
I dont know why is this happening: ( because the math and movement is actually working). On resume: in client A, calculates a force , applies it and sends to client B, client B applies same force ( rotated 180 degrees) and have diferents colisions results. running in background is enabled.
How can i fix this?
1- storing position on a list and sending the list so the other client can "reproduces" it later on.
2- keep sending by the socket in the update and costantly uptating position on other client.
3- your idea? <3
Thanks for your time
Answer by kaarloew · Nov 27, 2018 at 07:50 AM
Most physics engines are not deterministic.
"The first problem here is that the PhysX SDK is not deterministic.Especially when running different hardware setups, bus latencies can vary between runs, or on different machines. Even without hardware in the machine, we do not guarantee any type of determinism." https://www.nvidia.com/object/physx_knowledge_base.html
So you have few options:
You write your own physics engine that is deterministic (VERY hard)
Calculate physics in server that then sends the positions to clients (requires server resources)
Calculate physics in client that then sends the positions to other clients (this opens door for cheating)
thanks for your answer. i kinda though something similar was going on.
isnt also calculating your own physics an open door for hacking?
When you have your own deter$$anonymous$$istic physics engine, you can quite easily verify results on server. Players can just send input to server, server checks that everything is O$$anonymous$$ and then sends input to other clients.
Your answer

Follow this Question
Related Questions
Why is my character not colliding with other objects. 3 Answers
Generate Tiles Programatically Over Player Position? 0 Answers
2D, collision with background 0 Answers
Load Scene on Either Edge of Camera Collision 2 Answers
Find out that player is not grounded 2 Answers