I am writing a web based game, using websockets. its pretty simple, two players shooting at each other with bullets that have acceleration from a starting point at an angle.

The client side (frontend in web dev terms) uses phaser.io with p2js for physics.

However after reading this post, i believe that it's smarter to do some of the physics for the players on the server side. Such as collision detections, and update the player positions at regular intervals to keep the two players at sync.

Now i have two questions

  • if i use the Box2d javascript port on nodejs on the server side to simulate server side physics, than is it just as efficient as using the native c++ one? seeing as from what i have read so far it seems the js ports aren't that well maintained, and they are js ported - on the other hand the js codes might be easier to deal with than the c++ ones.
  • using p2js on the client side and using box2d on the server. The server being authoritative, and sends regular updates (once every second) as to the actual positions of the players. will the physics simulation be in sync (atleast to the user)? Theoretically i don't see any reason for it to not, given that both user has the exact same latency (this is assumed for sake of simplicity).
  • \$\begingroup\$ If you need to write a prototype for a C++ server there's good info on BeeJ's Guide But for playtesting purposes, any working implementation should be fine. \$\endgroup\$ – AturSams Nov 11 '14 at 13:10
  • \$\begingroup\$ The rule of thumb is not to optimize unless you have to. \$\endgroup\$ – AturSams Nov 11 '14 at 13:17
  • Obviously, c++ is faster than JavaScript but that is not an actual question. You could simply run some tests and check.
  • The second part of the question isn't clear at all. The physics simulation will be in sync if you run the simulation on the server side and use the results from the server to "guide" the clients. You can only extrapolate (or whatever you want to call it on the client) but you need to actually use the server as the "common grounds" that decide what actually happened at every point in time.

The clients can't see each other, definitely not in real time! You can't coordinate between them unless there is a very sophisticated algorithm or in your case a "ruling entity" dictating what actually happened at any point in time where the server is that mediator in your case and the clients "just" tell the server what each player is up to (handling input and control).

So yes, the clients will be in sync because the server tells them to be (in the same state); and no it's impossible to have the "exact same" latency unless you control the environment (your own network w/o using the Internet).

| improve this answer | |
  • \$\begingroup\$ you actually understood both my questions correctly, however my question may be a little unclear, i updated it. regarding the c++ question, i knew the answer before asking, but i was wondering if i missed out some part about whether there really is much of a gain compared to the js one \$\endgroup\$ – mercy Nov 11 '14 at 9:16
  • \$\begingroup\$ I think you should check, it could depend on your server's load and hardware since it's a finite task (each frame). If the server is sufficiently powerful, it might work. The question is, how many players would need this service at the same time? Obviously, if a lot of players are going to be using the service at the same time, you may see a considerable boost. I am not deeply familiar with JavaScript interpretation but I know that with PHP (for instance) a lot has been done (namely by FaceBook) to allow PHP to run at amazing speeds. \$\endgroup\$ – AturSams Nov 11 '14 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.