Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Is Unity physics is deterministic?

Discussion in 'General Discussion' started by Junaidali501, Sep 6, 2016.

  1. Junaidali501

    Junaidali501

    Joined:
    Dec 7, 2014
    Posts:
    8
    I am Currently working on snooker type game and developing multiplayer using google play realtime multiplayer and currently facing the game syc problem after exerting same force across different device. while doing short shots ball position often not syncing. Is the unity collision detection and physics is deterministic or not? if it is then please help me. I am using unity5.4 crack.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    It's not deterministic.

    --Eric
     
  3. Junaidali501

    Junaidali501

    Joined:
    Dec 7, 2014
    Posts:
    8
    thanks eric is there any way to make it deterministic?
     
  4. macdude2

    macdude2

    Joined:
    Sep 22, 2010
    Posts:
    686
  5. zoran404

    zoran404

    Joined:
    Jan 11, 2015
    Posts:
    520
    When doing multiplayer games you have 2 choices:
    • Only calculate physics on a single device (server) and send the states (the positions and velocities of all objects) to other devices (clients)
    • Or calculate the physic on all devices, but assign one device to act as server and continuously sync all other devices with it either by adjusting the state on the server to the state on the client or vice versa.
      The common solution when dealing with movement physics is to send to the server the position of the player on his own screen and then have the server move the virtual player in his memory to that position if possible.
    What you probably would want to do is the second thing, send to the other player how the ball moved on your own screen and the positions of all bolls afterwards.

    Alternatively if you want to make physics deterministic you can implement it yourself.
    This would normally be a time consuming task, but since we are talking about a snooker game that should be something that literally any (real) programmer could implement in a reasonable amount of time. But this is a considerably harder solution than the previous one.
     
    angrypenguin and Junaidali501 like this.
  6. ErisCaffee

    ErisCaffee

    Joined:
    Nov 26, 2014
    Posts:
    127
    I'm a little confused. When you say the physics is not deterministic, do you mean that the same starting conditions can produce different results on different clients? As in client A and client B both start with a ball on (0,0) that is given an initial velocity v and bounces off a wall at y=10 and the two clients calculate slightly different bounces?
     
  7. Junaidali501

    Junaidali501

    Joined:
    Dec 7, 2014
    Posts:
    8
    Zoran Thanks Worth knowing it.
     
  8. ErisCaffee

    ErisCaffee

    Joined:
    Nov 26, 2014
    Posts:
    127
    Ah. Should have googled first before posting. Differences in FPU calculations on different processors. Differences in timing affecting things like the number of times a random number generator is called. These can cause the physics to get out of sync. That makes sense now.
     
  9. Junaidali501

    Junaidali501

    Joined:
    Dec 7, 2014
    Posts:
    8
    Not Sure, but currently what i am doing is maintaining aspect ratio for every device and then send angle and force calculated from client a to client b using google play services. Most of the time game syc for long shots but when striker and ball are close together and you add force to striker then game not syn there are some position difference after collision. Any suggestions?
     
    Last edited: Sep 6, 2016
  10. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,301
    Yes.

    It means if you start the same simulation on different computers or even on the same computer (several times) and run it precisely for one second, then every time you'll get slightly different outcome.

    For deterministic physics you'll need fixed framerate, and you'll need a warranty that all computations produce exactly the same outcome on all target platforms (floating point).
     
    Kiwasi and Martin_H like this.
  11. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,738
    also thing to keep in mind, is a lot of multiplayer games have a both a physics for client side and server side. So if you have physics that is just for effect like debris or something you can do that client side for each client, since the result wont effect game play. While physics that can effect gameplay should take place on the server, and have the object state synced back to the clients.
     
  12. zoran404

    zoran404

    Joined:
    Jan 11, 2015
    Posts:
    520
    You will also have to send position of all the balls, after calculations, to the other device.
    This way in case there is a difference in outcome your game will be able to recover.
     
    Junaidali501 and angrypenguin like this.
  13. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,495
    To be honest if I were making a snooker game I think I'd implement my own physics rules for the balls. It's fairly simple stuff given how well defined it is, and it gives you the option of taking fine grained details like the grain of the table into account.

    Game physics libraries usually only take broad, generic cases into account. For example, see the way drag is handled in Unity's PhysX.

    Anyway, I'd (probably) make my own purpose-specific physics system, and have the server calculate the results of each shot based on that. Then each client would just animate based on the results.
     
    Junaidali501, Kiwasi and Ryiah like this.
  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The other option would be to precalculate physics. A significant part of the non-determinism comes from the steped nature of physics in games. A deterministic solution which is non step based wouldn't be to hard to build for the limited number of objects and inputs the on a billiards table.

    The only reason we even use a step based solution is to allow player input between steps. In Billards player input during the physics simulation is called cheating.