SlideShare uma empresa Scribd logo
1 de 117
Networking for Physics
    Programmers
        Glenn Fiedler
      www.gafferongames.com



         @gafferongames
DEMO
“How do I network my
 physics simulation?”
Network Models
1.   Pure client/server
2.   Client side prediction
3.   Deterministic lockstep
4.   Authority scheme
1. Pure client/server
How does it work?
•   Client does not run any game logic
•   Client sends input to server
•   Server sends render state back to client
Examples
•   SSH
•   VNC
•   OnLive
Applied to physics
•   Client send inputs to server
•   Server runs sim and sends position and
    orientation of rigid bodies back to client
•   Client buffers and interpolates between
    nearest two samples to render
Advantages
•   Simple
•   Secure
•   Late join is easy
Disadvantages
•   Round trip delay between player input and
    physics response
2. Client side prediction
How does it work?
•   Special treatment of local player object
•   Client simulates local player object ahead
    without waiting for server round trip
•   Server sends correction to client if it
    disagrees with client state
server




         n



client
server


input and state (n)



            n



 client
server


input and state (n)



            n



 client
server


input and state (n)



            n



 client
server


input and state (n)



            n



 client
server


input and state (n)



            n



 client
server


input and state (n)
                               state (n)


            n



 client
server


input and state (n)
                               state (n)


            n



 client
server


input and state (n)
                               state (n)


            n



 client
server


input and state (n)
                               state (n)


            n



 client
server


input and state (n)
                               state (n)


            n                   n+t



 client
server


input and state (n)
                               state (n)


            n                   n+t



 client
server


input and state (n)



            n                        n+t



 client
                      Lag time ‘t’
server


             rewind
                               state (n)


         n                      n+t



client
server


                 rewind
                                   state (n)


             n                      n+t



client     apply
         correction
server


             rewind
                                      state (n)


         n                             n+t



client
                      replay using
                      stored inputs
server


             rewind
                                      state (n)


         n                             n+t



client
                      replay using
                      stored inputs
server


             rewind
                                      state (n)


         n                             n+t



client
                      replay using
                      stored inputs
server


             rewind
                                      state (n)


         n                             n+t



client
                      replay using
                      stored inputs
server


             rewind
                                      state (n)


         n                             n+t



client
                      replay using
                      stored inputs
server


             rewind
                                      state (n)


         n                             n+t



client
                      replay using
                      stored inputs
server


             rewind
                                      state (n)


         n                             n+t



client
                      replay using
                      stored inputs
server


             rewind
                                      state (n)


         n                             n+t



client
                      replay using
                      stored inputs
server


             rewind
                                      state (n)


         n                             n+t



client
                      replay using
                      stored inputs
server


             rewind
                                      state (n)


         n                             n+t



client
                      replay using
                      stored inputs
Examples
•   Halo (competitive)
•   Quake
•   Unreal
•   Counterstrike, TF2, Left4Dead, Portal 2?
Applied to physics
•   Determine player state (rigid body)
•   Advanced: May include objects player is
    interacting with
•   Rewind and replay all or part of simulation
    to apply correction from server
Advantages
•   No round trip delay for player actions
•   Almost as secure as pure client/server
•   Late join still relatively easy to implement
Disadvantages
•   Expensive to rewind and replay physics
•   Collision between player objects are poorly
    defined
3. Deterministic Lockstep
How does it work?
•   Each machine runs same game code
•   Wait for input from all players before
    advancing to next frame
•   Rely on determinism to stay in sync
Input Server




Peer to Peer
Input Server




         delay own input




Peer to Peer
Input Server




Peer to Peer
Input Server




Peer to Peer
Input Server




Peer to Peer
Input Server




Peer to Peer
Input Server




Peer to Peer
Input Server




Peer to Peer
Input Server




Peer to Peer
Input Server




Peer to Peer
Input Server




Peer to Peer
Input Server




Peer to Peer
Input Server




Peer to Peer
Input Server




Peer to Peer
Inputs(n)




            n
Inputs(n)




            n   n+1
n   n+1
Inputs(n+1)




        n     n+1
Inputs(n+1)




        n     n+1 n+2
n   n+1 n+2
Inputs(n+2)




  n   n+1 n+2
Inputs(n+2)




  n   n+1 n+2 n+3
n   n+1 n+2 n+3
n   n+1 n+2 n+3   ...
n   n+1 n+2 n+3   ...   ...
Examples
•   Every RTS ever made
•   Halo COOP story mode
•   Little Big Planet (soft body physics + fluids)
•   PixelJunk Shooter 2 (fluid sim)
•   Most fighting games (SF4, GGPO)
GGPO
•   Good Game Peace Out
•   Play Street Fighter 2 emulated ROM online
    without feeling latency
•   How does it do it?
Lag time ‘t’
...   n
Lag time ‘t’
      ...     n


Fork state!

              n   n+1    n+2   n+3   ...   n+t
Lag time ‘t’
...   n




      n   n+1    n+2   n+3   ...   n+t
Lag time ‘t’
...   n




      n   n+1    n+2   n+3   ...   n+t
Lag time ‘t’
...   n




      n   n+1    n+2   n+3   ...   n+t
Lag time ‘t’
...   n




      n   n+1    n+2   n+3   ...   n+t
Lag time ‘t’
...   n




      n   n+1    n+2   n+3   ...   n+t
Lag time ‘t’
...   n




      n   n+1    n+2   n+3   ...   n+t
Lag time ‘t’
...   n




      n   n+1    n+2   n+3   ...   n+t
Inputs(n)

                        Lag time ‘t’
        ...   n




              n   n+1    n+2   n+3   ...   n+t
Inputs(n)

                              Lag time ‘t’
        ...   n   n+1




              n   n+1   n+2   n+3   ...   n+t
Lag time ‘t’
...   n   n+1




      n   n+1    n+2   n+3   ...   n+t


                discarded
Lag time ‘t’
 ...   n     n+1




       n     n+1   n+2   n+3   ...   n+t


Fork state

             n+1   n+2   n+3   ...   n+t   n+t+1
Lag time ‘t’
...   n   n+1




      n   n+1   n+2   n+3   ...   n+t




          n+1   n+2   n+3   ...   n+t   n+t+1
Lag time ‘t’
...   n   n+1




      n   n+1   n+2   n+3   ...   n+t




          n+1   n+2   n+3   ...   n+t   n+t+1
Lag time ‘t’
...   n   n+1




      n   n+1   n+2   n+3   ...   n+t




          n+1   n+2   n+3   ...   n+t   n+t+1
Lag time ‘t’
...   n   n+1




      n   n+1   n+2   n+3   ...   n+t




          n+1   n+2   n+3   ...   n+t   n+t+1
Lag time ‘t’
...   n   n+1




      n   n+1   n+2   n+3   ...   n+t




          n+1   n+2   n+3   ...   n+t   n+t+1
Lag time ‘t’
...   n   n+1




      n   n+1   n+2   n+3   ...   n+t




          n+1   n+2   n+3   ...   n+t   n+t+1
Lag time ‘t’
...   n   n+1




      n   n+1   n+2   n+3   ...   n+t




          n+1   n+2   n+3   ...   n+t   n+t+1
Lag time ‘t’
...   n   n+1




      n   n+1   n+2   n+3   ...   n+t




          n+1   n+2   n+3   ...   n+t   n+t+1
Lag time ‘t’
...   n   n+1




      n   n+1   n+2   n+3   ...   n+t




          n+1   n+2   n+3   ...   n+t   n+t+1
Lag time ‘t’
...   n   n+1




      n   n+1   n+2   n+3   ...   n+t




          n+1   n+2   n+3   ...   n+t   n+t+1
Lag time ‘t’
...   n   n+1




      n   n+1   n+2   n+3   ...   n+t




          n+1   n+2   n+3   ...   n+t   n+t+1   :(
Applied to physics
•   Physics engine must be deterministic
•   Used fixed timestep decoupled from render
    framerate (eg. “Fix your timestep!”)
•   Fork all or part of simulation to hide
    latency
Advantages
•   Only need to send player inputs
•   Handles interactions between players well
•   Everything ‘just works’
Disadvantages
•   Low player counts only (2-4)
•   Floating point determinism world of pain
•   Late join can be difficult or impossible
•   Forking simulation is very expensive
4. Authority scheme
How does it work?
•   Split up the simulation and run parts of
    the world on different machines
•   Design the split to support networking
    goals, eg. latency hiding, convenience
Examples
•   Mercenaries 2
•   Insomniac games “Sync host”
•   Many, many other console games
Applied to physics
•   Take authority over objects you interact
    with. Become the server for these objects.
•   Resolve conflicts when multiple players
    want authority over the same object
DEMO
Advantages
•   Does not require 100% determinism
•   Does not wait for most lagged player
•   No need to rewind & replay or fork
    simulation to hide latency
Disadvantages
•   Trusting the client (cheating)
•   Difficult to handle interactions between
    multiple players
•   Late join is difficult
Network Models
1.   Pure client/server
2.   Client side prediction
3.   Deterministic lockstep
4.   Authority scheme
How to choose?
•   If latency is no problem, pure client/server
•   This is the simplest option
How to choose?
•   If you have too much state to send use
    deterministic lockstep
•   Beware of floating point determinism
•   Keep player count low (2-4)
•   Optional: Fork simulation to hide lag
How to choose?
•   If you aren’t deterministic, or have high
    player counts use client side prediction or
    authority scheme
How to choose?
•   Client side prediction is basically an
    anti-cheat measure for FPS
•   If you cannot afford it, do a COOP game with
    authority scheme instead
•   Advanced: Validate authority physics on
    dedicated server?
Thank you
Glenn Fiedler
www.gafferongames.com



   @gafferongames

Mais conteúdo relacionado

Mais procurados (12)

NS-2 Tutorial
NS-2 TutorialNS-2 Tutorial
NS-2 Tutorial
 
No Heap Remote Objects for Distributed real-time Java
No Heap Remote Objects for Distributed real-time JavaNo Heap Remote Objects for Distributed real-time Java
No Heap Remote Objects for Distributed real-time Java
 
Simple asynchronous remote invocations for distributed real-time Java
Simple asynchronous remote invocations for distributed real-time JavaSimple asynchronous remote invocations for distributed real-time Java
Simple asynchronous remote invocations for distributed real-time Java
 
Qt Widget In-Depth
Qt Widget In-DepthQt Widget In-Depth
Qt Widget In-Depth
 
Tut hemant ns2
Tut hemant ns2Tut hemant ns2
Tut hemant ns2
 
Ns 2 Network Simulator An Introduction
Ns 2 Network Simulator An IntroductionNs 2 Network Simulator An Introduction
Ns 2 Network Simulator An Introduction
 
Enhancing the region model of RTSJ
Enhancing the region model of RTSJEnhancing the region model of RTSJ
Enhancing the region model of RTSJ
 
2011.jtr.pbasanta.
2011.jtr.pbasanta.2011.jtr.pbasanta.
2011.jtr.pbasanta.
 
Working with NS2
Working with NS2Working with NS2
Working with NS2
 
Iterative Multilevel Empirical Bayes (IMEB): An efficient flexible and robust...
Iterative Multilevel Empirical Bayes (IMEB): An efficient flexible and robust...Iterative Multilevel Empirical Bayes (IMEB): An efficient flexible and robust...
Iterative Multilevel Empirical Bayes (IMEB): An efficient flexible and robust...
 
Ns2
Ns2Ns2
Ns2
 
Paxos building-reliable-system
Paxos building-reliable-systemPaxos building-reliable-system
Paxos building-reliable-system
 

Destaque

ข้อมูลสารสนเทศหลิวนะ
ข้อมูลสารสนเทศหลิวนะข้อมูลสารสนเทศหลิวนะ
ข้อมูลสารสนเทศหลิวนะNart-Anong Srinak
 
Curitiba - PMO - GP-19
Curitiba - PMO - GP-19Curitiba - PMO - GP-19
Curitiba - PMO - GP-19Marco Coghi
 
Fortaleza gp11-aq-praialimpa
Fortaleza gp11-aq-praialimpaFortaleza gp11-aq-praialimpa
Fortaleza gp11-aq-praialimpaMarco Coghi
 
Desayunos
DesayunosDesayunos
DesayunosCeeGe
 
Lisarb Hunka consultoria
Lisarb Hunka consultoriaLisarb Hunka consultoria
Lisarb Hunka consultoriaMarco Coghi
 
Planejamento Estratégico de Marketing
Planejamento Estratégico de MarketingPlanejamento Estratégico de Marketing
Planejamento Estratégico de MarketingLucas Alves
 
João Carreiro & Capataz e Munhoz & Mariano - Paulínia Arena Music - 20.10.2012
João Carreiro & Capataz e Munhoz & Mariano - Paulínia Arena Music - 20.10.2012João Carreiro & Capataz e Munhoz & Mariano - Paulínia Arena Music - 20.10.2012
João Carreiro & Capataz e Munhoz & Mariano - Paulínia Arena Music - 20.10.2012VocenoSertanejo
 
Projeto Mobile Service Care
Projeto Mobile Service CareProjeto Mobile Service Care
Projeto Mobile Service CareMarco Coghi
 
Уровень и образ жизни населения в 1989-2009 года
Уровень и образ жизни населения в 1989-2009 годаУровень и образ жизни населения в 1989-2009 года
Уровень и образ жизни населения в 1989-2009 годаIlia Malkov
 
SOROCABA-GP10-AQ-CONSTRUMAQ
SOROCABA-GP10-AQ-CONSTRUMAQSOROCABA-GP10-AQ-CONSTRUMAQ
SOROCABA-GP10-AQ-CONSTRUMAQMarco Coghi
 
Saligny2 layout 1
Saligny2 layout 1Saligny2 layout 1
Saligny2 layout 1unknown0408
 
cloudHQ presentation
cloudHQ presentationcloudHQ presentation
cloudHQ presentationSelim D
 
Berrini gp7-pmo-grupo7
Berrini gp7-pmo-grupo7Berrini gp7-pmo-grupo7
Berrini gp7-pmo-grupo7Marco Coghi
 

Destaque (20)

Gilvan Teste
Gilvan TesteGilvan Teste
Gilvan Teste
 
ข้อมูลสารสนเทศหลิวนะ
ข้อมูลสารสนเทศหลิวนะข้อมูลสารสนเทศหลิวนะ
ข้อมูลสารสนเทศหลิวนะ
 
Curitiba - PMO - GP-19
Curitiba - PMO - GP-19Curitiba - PMO - GP-19
Curitiba - PMO - GP-19
 
Fortaleza gp11-aq-praialimpa
Fortaleza gp11-aq-praialimpaFortaleza gp11-aq-praialimpa
Fortaleza gp11-aq-praialimpa
 
SisLogus
SisLogusSisLogus
SisLogus
 
Desayunos
DesayunosDesayunos
Desayunos
 
Lisarb Hunka consultoria
Lisarb Hunka consultoriaLisarb Hunka consultoria
Lisarb Hunka consultoria
 
Planejamento Estratégico de Marketing
Planejamento Estratégico de MarketingPlanejamento Estratégico de Marketing
Planejamento Estratégico de Marketing
 
João Carreiro & Capataz e Munhoz & Mariano - Paulínia Arena Music - 20.10.2012
João Carreiro & Capataz e Munhoz & Mariano - Paulínia Arena Music - 20.10.2012João Carreiro & Capataz e Munhoz & Mariano - Paulínia Arena Music - 20.10.2012
João Carreiro & Capataz e Munhoz & Mariano - Paulínia Arena Music - 20.10.2012
 
Brasília star sport bar & grill manual identidade
Brasília star sport bar & grill   manual identidadeBrasília star sport bar & grill   manual identidade
Brasília star sport bar & grill manual identidade
 
Rio Vivo
Rio VivoRio Vivo
Rio Vivo
 
Doce Vida
Doce VidaDoce Vida
Doce Vida
 
Industrial
IndustrialIndustrial
Industrial
 
Bulma 441
Bulma 441Bulma 441
Bulma 441
 
Projeto Mobile Service Care
Projeto Mobile Service CareProjeto Mobile Service Care
Projeto Mobile Service Care
 
Уровень и образ жизни населения в 1989-2009 года
Уровень и образ жизни населения в 1989-2009 годаУровень и образ жизни населения в 1989-2009 года
Уровень и образ жизни населения в 1989-2009 года
 
SOROCABA-GP10-AQ-CONSTRUMAQ
SOROCABA-GP10-AQ-CONSTRUMAQSOROCABA-GP10-AQ-CONSTRUMAQ
SOROCABA-GP10-AQ-CONSTRUMAQ
 
Saligny2 layout 1
Saligny2 layout 1Saligny2 layout 1
Saligny2 layout 1
 
cloudHQ presentation
cloudHQ presentationcloudHQ presentation
cloudHQ presentation
 
Berrini gp7-pmo-grupo7
Berrini gp7-pmo-grupo7Berrini gp7-pmo-grupo7
Berrini gp7-pmo-grupo7
 

Networked physics 2011

Notas do Editor

  1. \n
  2. \n
  3. Martijn het Hoofd & Matthijs Blaas emailed me and asked me\n\n“How do I network my physics simulation”\n\nI talked with them for many weeks over email explaining all that I know.\n\nDuring this conversation I discovered that the networking model that I use for my demo was probably not the best networking model for their game. \n\nThere’s more than one way to do it.\n
  4. So this talk is about how to choose the right network model for your physics simulation.\n\nA network model is the strategy you use for synchronizing your simulation over the network.\n\nIt’s what you put in your packets and what you do with it on the other side.\n\nEach network model presented here has it’s own tradeoffs, advantages and disadvantages.\n\nThis is the most important thing to understand about networking. The rest is implementation detail.\n
  5. \n
  6. First up is pure client server. \n\nAKA “dumb terminal”, “dumb client”, “thin client”\n
  7. The most important thing to understand is that the client does not run any game logic.\n\nThe client is dumb.\n\nThe client just sends the inputs to the server.\n\nThe server sends to the client whatever the client needs to show what’s happening on the server.\n
  8. Some examples:\n\nSecure shell. Key presses are sent to the machine you are connected to. It sends back the terminal codes so you can see what happens.\n\nVNC. Mouse and keyboard input sent to server. Compressed bitmaps sent back to client.\n\nOnLive. Controller input sent to server. Video stream sent back to client.\n
  9. How can we apply this to a physics simulation?\n\nEach client sends their inputs controlling the simulation to the server. eg. arrow keys.\n\nEach frame the server takes the last input from each client and steps the simulation forward\n\nThe server sends positions and orientations for each rigid body back to the client.\n\nThe client buffers these positions and orientations and interpolates between them to render.\n
  10. It’s simple.\n\nIt’s secure.\n\nAnd it’s easy to support late join (joining a game already in progress).\n
  11. But there is a delay before you see the result of your input.\n\neg. You press “up” then 250ms later you move forward.\n
  12. \n
  13. Client side prediction is the technique first person shooters use to hide this latency.\n
  14. Here is how it works:\n\nThe local player object is treated differently from other objects.\n\nThe client player runs the same simulation code that runs on the server for his object to advance himself forward without waiting for the input round trip to the server and back.\n\nThis is done while still keeping the server authoritative over the client player position, orientation etc \n\nThis is important on the PC because otherwise players could cheat.\n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. apply correction (in the past!)\n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. Some examples: \n\nHalo competitive mode. \n\nQuake. Unreal. All Valve games. I assume Portal 2 does as well, although I’m not 100% sure.\n\n\n
  41. How to apply client side prediction to physics?\n\nSend both the inputs and the player state to the server\n\neg. position, orientation, linear velocity, angular velocity.\n\nAs an advanced technique, you can also attempt to perform client side prediction on other objects the player is interacting with, eg. the vehicle they are in, or the objects they are pushing around\n\nValve has been doing this since Left4Dead 2.\n
  42. Advantages:\n\nNo delay between player input and actions.\n\nAlmost as secure as client server. \n\nLate join is still relatively easy to implement.\n
  43. Disadvantages:\n\nRewinding and replaying the simulation can get expensive (CPU)\n\nAlso because each player is predicting ahead according to their own inputs without considering the inputs of other players, inconsistencies occur when player objects collide with each other.\n\nThis is why first person shooters are usually static worlds where players interact at a distance\n
  44. \n
  45. Next is deterministic lockstep.\n
  46. Each machine runs the same game code and waits for all player inputs before simulating the frame.\n\nThe idea is that if the same initial state + the same inputs gives the same result, then all machines stay in sync.\n\nIMPORTANT: In order for this to work you need exactly the same result down to the floating point bits. Not close. Not near. Exactly the same.\n\nThis can be quite difficult to achieve in practice. Google “Floating point determinism” for details.\n
  47. \n
  48. Age of Empires “1500 Archers” article on Gamasutra.\n
  49. \n
  50. \n
  51. \n
  52. \n
  53. Problems with lag switches. One player can delay packets making other players wait.\n
  54. Input server.\n\nStarcraft 2 does something like this.\n\nBetter protection against lag switches.\n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. Every RTS game ever made. Too many units to possibly send over the network.\n\nHalo COOP mode uses deterministic lockstep. They have different network models for competitive and COOP play.\n\nLittle big planet has soft body physics and fluid simulation. Too much state to send.\n\nPixelJunk shooter has a fluid simulation with hundreds of thousand particles. Too much state to send.\n\nFighting games. SF4. GPPO typically networked deterministic lockstep because players interact with each other as a rule.\n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n
  81. \n
  82. \n
  83. \n
  84. \n
  85. \n
  86. \n
  87. \n
  88. \n
  89. \n
  90. \n
  91. \n
  92. \n
  93. \n
  94. \n
  95. \n
  96. \n
  97. \n
  98. \n
  99. \n
  100. Low player counts because you must wait for the most lagged player.\n\nStatistically speaking as the number of players increase the chance that at any moment some player is experiencing network problems approaches 1.\n\nThis is why there are no MMOs using deterministic lockstep :)\n\nFloating point determinism is difficult to achieve. Possible but difficult.\n\nLate join. Hard to capture deterministic checkpoint and restore it on another machine. What if you had 100,000 particles in your world? Too much state to send in any reasonable amount of time.\n
  101. \n
  102. \n
  103. \n
  104. \n
  105. \n
  106. Explain why I went with authority scheme.\n\nI don’t want latency when moving around so I can’t use pure client/server.\n\nI want to roll around in a big katamari ball without lag so client side prediction is a bit difficult, the simulation is very expensive and I cannot afford to rewind and replay it.\n\nMy demo is a large streaming world and in a real world situation the loading of assets from disk when objects activate would not be deterministic: eg. streaming from disk. I cannot use deterministic lockstep.\n
  107. \n
  108. \n
  109. \n
  110. \n
  111. \n
  112. \n
  113. \n
  114. \n
  115. \n
  116. \n
  117. \n