# Server-side Babylon.js

## Recommended Posts

I'm wondering if there's an implementation of Babylon.js that runs as a server, or more specifically, its physics engine.  Making a networked game without physics is easy.  No "engine" is required.  The only calculations the server has to do is figuring out a new position when moving an object in a certain direction and collision.  That's just simple trigonometry and can be done by hand.  The tricky part is if you have objects that react to physics.  Both the client and server need to use the same engine to calculate movement via physics.  If such thing does not exist, can someone point me in the direction of how I could implement the same physics that Babylon.js uses for my server?  For now I just want gravity.  I don't need joints, bounce, cloth, etc..

##### Share on other sites

Let me take a leap without looking. My knowledge of coding for networked games using a server is non-existent.  However if in a game without physics the only calculations the server has to do is figure out new positions for an object then these calculations could include changes in position due to gravity.

These calculations are based on Newton's equations of motion - extract below from https://en.wikipedia.org/wiki/Equations_of_motion

Here a is constant acceleration, or in the case of bodies moving under the influence of gravity, the standard gravity g is used. Note that each of the equations contains four of the five variables, so in this situation it is sufficient to know three out of the five variables to calculate the remaining two.

In elementary physics the same formulae are frequently written  as:

They are often referred to as the "SUVAT" equations, where "SUVAT" is an acronym from the variables: s = displacement, u = initial velocity, v = final velocity, a = acceleration, t = time

Now it may be that you knew this already, if so I apologise, if not and you want some help with the maths let me know.

##### Share on other sites

These look familiar! Going all the way back to my high school physics. Thanks for the tip!  I'm happy to start with something like this then eventually add more if the client/server architecture holds up.

##### Share on other sites

I've been looking at the physics engines for Babylon.js.  My server is in C# so if I use one of those engines I'd need to port it to C#.  Typescript is pretty similar to C#.  What would be the best way to do this?  Should I use babylon.oimoJSPlugin.ts ?  This could be weird because there is no Babylon.js in my server whatsoever.  All I have are position and rotation vectors.

The other option is to write my own from scratch with just the basics.  This website seems to be a good resource: http://buildnewgames.com/gamephysics/

##### Share on other sites

Hi oz,

both engines that are being used in Babylon are written in JavaScript and are not ports of any engine you can run on a .NET/C# server (Oimo is a port from an ActionScript engine, but that doesn't really matter in your case:) ).

My best suggestion would probably be - use node.js fas your server. Then you have JavaScript context/engine and you can use either one of the engines to calculate anything you want.

##### Share on other sites

@RaananW has provided the best solution we've found. Buiding upon node.js allows us to overcome ANY conflicts. And we use WebsocketIO to maintain an open connection - or as many connections as you wish when pairing with node.js!

Not a simple task by any means, but if you aren't familiar with node.js, there is loads of code out there to get you started. And if you sign onto a node.js web provider, they give a framework to get up and running almost immediately - and it's open source of course. But the real power is in the modifications and extensions to the node.js server.

DB

##### Share on other sites

yes, I did consider nodejs.  I've used it before for other projects.  I was tossing up between nodejs and C# for a server and all the benchmarks I saw showed that C# was faster for more intensive projects

##### Share on other sites

Of course any compiled code will run faster, however, it takes allot of work to write routines for threading processes descretly. In our benchmark tests, we found practically no difference whatsoever for the end user. However, what we're able to support using node.js is endless, rather than running into all kinds of issues with OS and browser as far as compatability. So my personal advice is to use node.js if you want complete versatility and compliance without limits; and without a whole lot of coding and debugging.

DB

##### Share on other sites

I'll consider it depending on how much physics I want to get into cos physics is the only tricky area.  My back-end server doesn't serve HTML so there's no browser issues.  It just keeps track of the assets and players in the virtual world.  It communicates with the website by sending JSON through web sockets.  Also, C# is cross-platform . i work on it on my Windows computer, compile it, copy the .exe and any extra DLLs to my Linux server and it just runs.  Its 1 .exe and 3 DLLs.

##### Share on other sites

@ozRocker It all depends on your needs. From your description, it appears that you are so very close to making an easy transition to node.js and already implemented WebsocetsIO - so to move to a node,js server should be an easy task - perhaps easier than you might think. And I can promise that once you go node, you'll optimize code - to be able to send any process across threads and cpu priorities can multitask to the nth degree. But I can ony tell you about the past 4 years of testing what ihas been the best server configuration for us to use for complete versatility. But of course there are always other factors we all have to prioritize.

So perhaps your current server is able to handle all operations you might now need seamlessly. However, I bet that within 2 years you are using node.js. Just my own opinion, and I love it when I'm wrong - but I can't see any scenario where this might not be the case in the near future for most WebGL development considering the new devices coming online in the next 2 years.. Please let us know how you proceed, although it appears that you've already made up your mind. If so, we'll look at both solutions in another year and compare performance and versatility between our node.js server and your C# server. I don't believe I can add anything more to this subject. We all make it work the best way we can.

DB

##### Share on other sites

We might be on different pages here, but yes, I will switch to another language if its necessary to proceed.  It sounds like you're talking about a web server.  I have a web server which is serving javascript / PHP / Babylon.js.  That's the client that deals with webGL, browsers and devices.  The C# server is an asset manager that keeps track of in-world objects and players.  It simply tells the clients where things are and which direction they are facing.  Its get more complicated when I add gravity.  There are a few good C# physics libraries available, however, the server and the client need to have the same calculations (well, with error rate of 0.01).  As I see it I have 3 options:

1 - change server to nodejs / javascript so I can use the exact code Babylon.js uses for physics.  This could get very weird though because there's no GUI, so no actual canvas / engine / scene.  I'm not sure anyone has actually used Babylon.js purely as a server or if it can even work like that without much hacking

2 - come up with my own algorithms and port it to both Javascript and C#.

3 - use Babylon.js physics on client and some other library such as Jitter for C# backend.  Doesn't matter that the code or even the process is different, as long as the results are within 0.01 of eachother

@dbawel if you know how to do no. 1 I'd be interested to see an example of Babylon.js running in server mode.  Is it easy to do?

##### Share on other sites
1 hour ago, ozRocker said:

2 - come up with my own algorithms and port it to both Javascript and C#.

Before you do this, take a look at BabylonHx.  http://babylonhx.com/

##### Share on other sites
1 hour ago, ozRocker said:

3 - use Babylon.js physics on client and some other library such as Jitter for C# backend.  Doesn't matter that the code or even the process is different, as long as the results are within 0.01 of eachother

##### Share on other sites

Woh!  So with Haxe I can port Babylon.js functions to C#??  This is like magic!  Its the solution to my original question

##### Share on other sites

@gamestudiohx is the developer of BabylonHx

##### Share on other sites

I wonder how up-to-date it is.  That guy is going to be busy every time a new Babylon.js version comes out!

##### Share on other sites

@ozRocker - I believe I understood what you initially asked, however, as the post progressed I was thinking strictly server requests and threads. BabylonHX is interesting, however you nailed the issue - which is updates and support. As this is not a product, I would be afraid of working on code that can potentially be obsolete - unless you personally take control of it yourself. You can certainly run WebGL extensions on your node.js server, but to handle computations such as physics, if you're seeking any user reaction, then the delay to send the result to the user can be as long as 60ms - which is not practical unless it is strictly for visualization.

I hope I've understood the issue. Bottom line for us is that CPU speed is doubling every year, and right now the performance is good on practically every device made in the last year - unless you are computing highly complex simulations. Even then, you can generally optimize your devices to accellerate these processes.

DB

##### Share on other sites
9 hours ago, dbawel said:

You can certainly run WebGL extensions on your node.js server, but to handle computations such as physics, if you're seeking any user reaction, then the delay to send the result to the user can be as long as 60ms - which is not practical unless it is strictly for visualization.

yep, this is the lag problem that exists with all multiplayer games.  Everything the user sees on the screen is 60ms (or whatever lag time) ago.  That's why the client has to also run the same computations, so it can "predict" where the object will be and instantly move it there, then make any corrections if required when it receives the positions/rotations from the server.  Prediction is not perfect though, which is why in Call of Duty or Quake there's a small chance you can get shot in the head if you've jumped behind a wall within the lag timeframe.  You either get speed or accuracy, but not both.

##### Share on other sites

@dbawel so it looks like this guy is trying to do what I'm doing, but without any success :/ I'm still looking for a framework to let me handle 3D maths (local to absolute conversions) and bounding boxes and collisions.  I did find a Babylonjs DLL that runs with .NET but its also asking for a canvas and engine in order to create a scene

##### Share on other sites

@ozRocker - The only way this would be possible, is to use both node.js and WebsocketIO - otherwise you won't get the speed you're potentially looking for as you will require the constant open connection between the server and the user. Also, I highly advise that you calculate simple physics on the user's device, and then check the accuracy on the server. Using this method,you should have perhaps 90% of correct calculations, and the server can make the additional 10% corrections for any minor errors. And using Websocket will minimize any delay to 1 second or less over a normal connection.

There will be much for you to write in node.js to accomplish this, but it is a next step in gaming technology - especially WebGL. Some MMOs already do this, and you should find the right balance so that any correction shouldn't be noticed by the user.

Cheers,

DB

##### Share on other sites
On 19/05/2016 at 6:12 PM, ozRocker said:

It communicates with the website by sending JSON through web sockets.

Yes, I am using web sockets.  My server already does basic movement calculations, same with the client, so the client can move the character instantly then fix any corrections coming back from the server (maybe I didn't mention that before).  It is up and running here www.punkoffice.com/webiverse

The only issue is that I can't use the Babylon.js physics code on the server to match with the client because you can't run Babylon.js in headless mode.

##### Share on other sites

I've talked with developers who have run three.js in headless mode without much additional code. Although we shouldn't be permitted to run extensions, this post clearly shows progress with cannon.js.

Good luck - perhaps it's a little early to move in this direction. But certainly worth a try.

DB

##### Share on other sites
8 hours ago, ozRocker said:

because you can't run Babylon.js in headless mode

You could run Oimo or Cannon on the server and then sync the meshes with the bodies on the client side.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.