How to include liquidfun physics engine with nodejs

陌路散爱 提交于 2019-12-23 11:41:10

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/27715782/how-to-include-liquidfun-physics-engine-with-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!