title: Multiplayer / Networking options for a 2D game with physics
body: <p><strong>Summary:</strong></p>
  
  <p>My 50% finished 2D sidescroller with Box2D as physics engine should have multiplayer support in the final version. However, the current code is just a singleplayer game.</p>
  
  <ul>
  <li>What should I do now?  </li>
  <li>And more important, how should I implement multiplayer and combine it with singleplayer?   </li>
  <li>Is it a bad idea to code the singleplayer mode separated from multiplayer mode (<em>like Notch did it with Minecraft</em>)?</li>
  </ul>
  
  <p>The performance in singleplayer should be as good as possible (Simulating physics with using a loopback server to implement singleplayer mode would be a problem there)</p>
  
  <p><strong>Full background / questions:</strong></p>
  
  <p>I'm working on a relatively large 2D game project in C++, with physics as a core element of it. (I use Box2D for that)</p>
  
  <p>The finished game should have full multiplayer support, however I made the mistake that I didn't plan the networking part properly and basically worked on a singleplayer game until now.</p>
  
  <p>I thought that multiplayer support could be added to the almost finished singleplayer game in a relatively easy and clear way, but apparently, from what I have read this is wrong. </p>
  
  <p>I even read that a multiplayer game should be programmed as one from the beginning, with the singleplayer mode actually just consisting of hosting an invisible local server and connecting to it via loopback. (I found out that most FPS game engines do it that way, an example would be Source)</p>
  
  <p>So here I am, with my half finished 2D sidescroller game, and I don't really know how to go on.</p>
  
  <p>Simply continueing to work on the singleplayer / client seems useless to me now, as I'd have to recode and refactor even more later.</p>
  
  <p>First, a general question to anybody who possibly found himself in a situation like this:</p>
  
  <ul>
  <li>How should I proceed?</li>
  </ul>
  
  <p>Then, the more specific one - I have been trying to find out how I can approach the networking part for my game:</p>
  
  <ul>
  <li>Invisible / loopback server for singleplayer</li>
  </ul>
  
  <p>This would have the advantage that there basically is no difference between singleplayer and multiplayer mode. Not much additional code would be needed.</p>
  
  <p>A big disadvantage: Performance and other limitations in singleplayer. There would be two physics simulations running. One for the client and one for the loopback server.</p>
  
  <p>Even if you work around by providing a direct path for the data from the loopback server, through direct communcation by the threads for example, the singleplayer would be limited.</p>
  
  <p>This is a problem because people should be allowed to play around with masses of objects at once.</p>
  
  <ul>
  <li>Separated singleplayer / Multiplayer mode</li>
  </ul>
  
  <p>There would be no server involved in singleplayer mode.</p>
  
  <p>I'm not really sure how this would work. But at least I think that there would be a lot of additional work, because all of the singleplayer features would have to be re-implemented or glued to multiplayer mode.</p>
  
  <ul>
  <li>Multiplayer mode as a module for singleplayer</li>
  </ul>
  
  <p>This is merely a quick thought I had. Multiplayer could consist of a singleplayer game, with an additional networking module loaded and connected to a server, which sends and receives data and updates the singleplayer world.</p>
  
  <p>In the retrospective, I regret not having planned the multiplayer mode earlier. I'm really stuck at this point and I hope that somebody here is able to help me!</p>