Jump to content

Game Server to Verify Player Actions


nicktendo
 Share

Recommended Posts

Would it be possible to create a BabylonJS scene within the context of a NodeJS server simply to recreate the environment of a player and verify valid movement/collisions? The BabylonJS scene on the server would obviously not need to paint anything anywhere, but would simply need to calculate mesh positions/collisions/etc.

Link to comment
Share on other sites

@Deltakosh A quick update, I've successfully gotten BJS to run within a NodeJS app. The dependencies are as follows:

gl: https://www.npmjs.com/package/gl

canvas: https://www.npmjs.com/package/canvas

jsdom: https://www.npmjs.com/package/jsdom

A version of NodeJS that utilizes Node Module version 48 to be compatible with canvas: https://nodejs.org/en/download/releases/

A few modifications had to be made to Babylon.js:

1. Define the following variables at the top of Babylon.js:

var window = {};
window.location = {};
window.location.href = "";
var navigator = {};

2. Add an argument to the engine constructor to pass a window variable from jsdom:

function Engine(canvas, antialias, nodeWindow, options, adaptToDeviceRatio) {
            window = nodeWindow;
            document = window.document;
            navigator = window.navigator;

3. Comment out the following code inside Effect.prototype._loadVertexShader:

if (vertex instanceof HTMLElement) {
                var vertexCode = BABYLON.Tools.GetDOMTextContent(vertex);
                callback(vertexCode);
                return;
            }

Without this comment an error is thrown when attempting to resolve 'HTMLElement', I'm sure there's a more comprehensive solution to this problem.

Here is the NodeJS app:

var gl = require('gl')(1920, 1080, { preserveDrawingBuffer: true });
var canvas = require('canvas');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
var BABYLON = require('./babylon.max.js');
 
var dom = new JSDOM();
var window = dom.window;
 
canvas.getContext = function(thecontext,options){
return gl;
}
canvas.addEventListener = function (event,options){
return 1;
}
 
var engine = new BABYLON.Engine(canvas, true, window);
var scene = new BABYLON.Scene(engine);
// Enable Collisions
scene.collisionsEnabled = true;
var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
var sphere = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene);
sphere.checkCollisions = true;
sphere.position.y = 5;
var speed = 0.01;
 
scene.registerBeforeRender(function() {
console.log(sphere.position.x);
sphere.position.x += speed;
});
 
engine.runRenderLoop(function () {
scene.render();
});
 
console.log("ran")
 
As of now the default vertex shader isn't able to be loaded properly, but as this is a headless implementation shaders probably aren't very relevant. I'll ultimately find a permanent solution for this error. I'm sure there is a significant amount of simplification that could be done to the engine implementation overall since it's running in a headless configuration, but this seems to be working for now. I wanted to minimize my modifications to the engine so I could use as close to a duplicate implementation of the browser version as possible :) 
 
I've attached the NodeJS project files for reference.

babylon.max.js

index.js

 

package.json

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...