Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Floating Point Determinism

Discussion in 'Scripting' started by ChrisH, Oct 8, 2010.

  1. ChrisH

    ChrisH

    Joined:
    Aug 17, 2009
    Posts:
    31
    I'm making a network RTS game, loosely following the Age of Empires network model. ( http://www.gamasutra.com/view/feature/3094/1500_archers_on_a_288_network_.php ). Basically this means that I send commands over the network, then each computer receives the commands and performs a simultaneous simulation.

    In order for this type of system to work, I have to guarantee that each computer runs the simulation exactly the same as the others. Lately I've been reading up on floating point determinism, and it seems that what I'm trying to do may be a tall order for Unity/C#.

    I wrote a small plugin that sets the floating point precision control and rounding control registers via a call to the _controlfp_s function. ( http://msdn.microsoft.com/en-us/library/c9676k6h(VS.80).aspx )

    From what I've read, that should make the basic floating point operations deterministic across platforms. But what about the transcendental functions like sin and cos? Is there a way I can guarantee that those operations will be equal on different machines, all the way down to the last bit of precision? Also, what about Unity colliders? If two colliders intersect on one machine, does that guarantee that they intersect on all machines? (assuming I place them consistently across all machines of course)

    Thanks in advance for your ideas.
     
  2. roberto_sc

    roberto_sc

    Joined:
    Dec 13, 2010
    Posts:
    144
    ChrisH, I have exactly the same problem and you are many steps ahead of me. Please tell me if your plugin worked.
    Did you find answers for your - very convenient - questions?

    Based on what Elijah from Gas Powered Games said on this article:
    ,I'm afraid that setting the FPU, besides all other problems, may not work.
     
  3. zty

    zty

    Joined:
    Jul 7, 2015
    Posts:
    4
    Is there any follow up on this?
     
  4. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    322
    Hey ChrisH. Unfortunately, you're barking up an impossible tree. You cannot guarantee deterministic floating point math on a software level - it's just a consequence of hardware. Even setting _controlfp_s variables aren't a guarantee, although they can help in some scenarios. Unity Physics is non-deterministic. If you want deterministic physics simulations, you need to simulate on the server and send that information to the client.

    The easiest solution, I think, is to just not use floating points in your simulation. That's not to say you can't use it in your whole game logic! I would look at interpolation for things like unit movement, or using very small cell sizes for your integer coordinates. But for the actual simulation, stick with integer math.

    The alternative - trickier, but you get to keep all your fancy floating points - is to simulate and interpolate. This is a networking model things like multiplayer FPSs use. While you do simulate on the client, it's not a trusted simulation. The server runs the cardinal simulation, and occasionally tells the client about the state of the system. The client then corrects their simulation so the two are back in sync if they ever got out of sync. Pros - much more secure, and follows the golden rule of "never trust a client". Cons - more bandwidth intensive, more complex code (although all the problems have been solved years ago).
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Just use ints instead of floats. Problem solved.
     
  6. CaoMengde777

    CaoMengde777

    Joined:
    Nov 5, 2013
    Posts:
    813
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    This thread is super old.
     
  8. CaoMengde777

    CaoMengde777

    Joined:
    Nov 5, 2013
    Posts:
    813
  9. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    322
    Oh snap, I did not see that! Sorry to assist in rezzing this.