Search Unity

Best approach for handling physics objects in a networked game?

Discussion in 'Multiplayer' started by JoeStrout, May 2, 2020.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'm working with a team on a VR MMO. The world will include objects that can be picked up, tossed around, and banged into each other, which means I have to deal with physics. What's the best approach to handling this?

    I don't think having all the physics done on the server will be responsive enough — there's always a little lag between the server and the client, and if object seem to react before (or after) you hit them, it's going to be jarring. Plus I suspect running all that physics on the server might bog it down.

    So I'm imagining something like, each physics object that's away has one particular client that simulates it. For all the others, it's strictly kinetic, and we do some transform synchronization from the "owning" client, to the server, to all other clients. The owning client would be whichever player is closest to it, or last touched it.

    But there are all sorts of complications maintaining that as players come and go, as objects bump into other objects and wake them up, etc. For something like a hockey or soccer game where there is only one non-kinetic object (the puck/ball), it could certainly work. But for a complex world of arbitrary objects banging around, it seems pretty thorny.

    Any recommendations? (If it matters, we're currently using Mirror, though I wouldn't be opposed to switching to some other solution if necessary.)
     
    Iron-Warrior likes this.
  2. voncarp

    voncarp

    Joined:
    Jan 3, 2012
    Posts:
    188
    I think much if it depends on how much and what kind of physics you are talking about.

    For something like throwing a grenade or kicking a ball you script the results.

    Example: You send the launch position, direction, angle, speed gravity, and whatever from a projectile equation in your network message. The network cost will be 1 message and whatever bandwidth for the variables you send.

    https://en.m.wikipedia.org/wiki/Projectile_motion

    The client should now should have the launch variables to plug into the exact equation. So the grenade hit point is now deterministic. Follow the hit point normal and you have a new direction. Also, deterministic. Slow down the speed per hit and repeat.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yes, I know that, but we're not talking about projectiles here — we're talking about dropping a mug on a table and having it bounce off the plate and land on its side, knocking the candlestick onto the floor. That's too complex for scripted motion.
     
  4. ph4ntomz

    ph4ntomz

    Joined:
    Jul 22, 2016
    Posts:
    37
  5. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    Gaffer On Games wrote a pretty in-depth article covering the subject. The general idea is the same as what you outlined in your OP (client authority based on who grabbed/touched objects last), and it has full source available.
     
    Joe-Censored and JoeStrout like this.