Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include liquidfun physics engine with nodejs

I have been making a game with box2dweb and node.js. Everything has been working fine until I decided to switch to Google's LiquidFun engine, which is also based on Box2d.

In box2dweb I could export the modules I needed by adding the following to the bottom of the box2d.js file.

module.exports = {
  b2Vec2: Box2D.Common.Math.b2Vec2,
  b2BodyDef: Box2D.Dynamics.b2BodyDef,
  b2Body: Box2D.Dynamics.b2Body
};

LiquidFun doesn't use the same namespacing in its compiled file so I tried:

module.exports = {
  b2Vec2,
  b2BodyDef,
  b2Body
};

I have had no luck in getting node.js to export any of the functions I need. Could someone please tell me how to properly export LiquidFun?

like image 482
two70 Avatar asked Nov 10 '22 21:11

two70


1 Answers

I am using Node.js v0.10.35 and installed the debug module (with "npm install debug"). The following is the "Hello LiquidFun" I used to test out the liquidfun.js as running on Node.js, as adapted from https://google.github.io/liquidfun/Programmers-Guide/html/md__chapter02__hello__box2_d.html

var lf=require('./liquidfun.js');
var debug=require('debug')('liquidfun');

var gravity = new lf.b2Vec2(0,-10);
var world = new lf.b2World(gravity);
lf.setWorld(world);
var groundBodyDef = new lf.b2BodyDef();
groundBodyDef.position.Set(0,-10);
var groundBody = world.CreateBody(groundBodyDef);
var groundBox = new lf.b2PolygonShape();
groundBox.SetAsBoxXY(50,10);
groundBody.CreateFixtureFromShape(groundBox,0);


var bodyDef = new lf.b2BodyDef();
bodyDef.type= lf.b2_dynamicBody;
bodyDef.position.Set(0,4);
var body=world.CreateBody(bodyDef);

var dynamicBox = new lf.b2PolygonShape;
dynamicBox.SetAsBoxXY(1,1);

fixtureDef = new lf.b2FixtureDef;
fixtureDef.shape = dynamicBox;
fixtureDef.density = 1;
fixtureDef.friction=0.3;
fixtureDef.restitution=0.5;

body.CreateFixtureFromDef(fixtureDef);
var timeStep=1/60;
var velocityIterations=6;
var positionIteration=2;

for (var i=0;i<60;i++)
{   world.Step(timeStep, velocityIterations, positionIteration);
    var position = body.GetPosition();
    var angle = body.GetAngle();
    debug(position.x+" "+position.y+" "+angle);
}

To make this work, add the following lines to liquidfun.js (I am using v1.1.0) to export all those constructor functions to the above program:

module.exports = {
    b2Vec2: b2Vec2,
    b2BodyDef: b2BodyDef,
    b2PolygonShape: b2PolygonShape,
    b2FixtureDef: b2FixtureDef,
    b2World: b2World,
    b2_dynamicBody: b2_dynamicBody,
    setWorld: function(_world){ world=_world;   }
};

Note that I have defined a method "setWorld(_world)" which is used for passing the world object from the nodejs script back into this module. The reason is that I found the liquidfun.js requires the variable "world" to be defined (which is a b2World object), while in my example, I created the "world" outside of the module and hence has to be passed back in to make it work. Alternatively, you may create the "world" inside the liquidfun.js module and export that to the nodejs script.

By the way, be reminded to set the environment "DEBUG=liquidfun" to see the simulated results. On windows, type the following to run

set DEBUG=liquidfun & node hello_liquidfun.js
like image 71
Clifford Choy Avatar answered Nov 14 '22 23:11

Clifford Choy