如何在Node.js中包含LiquidFun物理引擎 [英] How to include liquidfun physics engine with nodejs

查看:189
本文介绍了如何在Node.js中包含LiquidFun物理引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 box2dweb 和node.js.在我决定切换到Google的 LiquidFun之前,一切工作正常引擎,该引擎也基于Box2d.

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.

在box2dweb中,我可以通过将以下内容添加到box2d.js文件的底部来导出所需的模块.

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在其编译文件中没有使用相同的命名空间,所以我尝试了:

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

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

我没有运气让node.js导出我需要的任何功能.有人可以告诉我如何正确导出LiquidFun吗?

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?

推荐答案

我正在使用Node.js v0.10.35并安装了调试模块(带有"npm install debug").以下是我用来测试在Node.js上运行的liquidfun.js的"Hello LiquidFun",它改编自

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);
}

要执行此操作,请将以下几行添加到liquidfun.js(我正在使用v1.1.0),以将所有那些构造函数导出到上述程序中:

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;   }
};

请注意,我已经定义了一种方法"setWorld(_world)",该方法用于将world对象从nodejs脚本传递回此模块.原因是我发现liquidfun.js要求定义变量"world"(这是一个b2World对象),而在我的示例中,我在模块外部创建了"world",因此必须将其传递回使它工作.或者,您可以在liquidfun.js模块内部创建世界",并将其导出到nodejs脚本.

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.

顺便提一下,请注意将环境设置为"DEBUG = liquidfun",以查看模拟结果.在Windows上,键入以下内容以运行

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

这篇关于如何在Node.js中包含LiquidFun物理引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆