How to enable a fluid physics engine with nodes

I am making a game with box2dweb and node.js. Everything worked fine until I decided to switch to Google LiquidFun , which is also based on Box2d.

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

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

LiquidFun does not use the same namespace in the compiled file, so I tried:

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

I was unable to get node.js to export any of the functions that I need. Can someone tell me how to export LiquidFun properly?

+5
source share
1 answer

I am using Node.js v0.10.35 and installed the debugging module (with "npm install debug"). The following is the "Hello LiquidFun" that I used to test liquidfun.js as running on Node.js, according to https://google.imtqy.com/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 use v1.1.0) to export all these 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 the "setWorld (_world)" method, which is used to pass the world object from the js script node back to this module. The reason is that I found that liquidfun.js requires the world variable to be defined (which is a b2World object), while in my example I created a world outside the module and therefore should be returned to for it to work. Alternatively, you can create a β€œworld” inside the liquidfun.js module and export it to a nodejs script.

By the way, recall to set the environment to "DEBUG = liquidfun" to see simulated results. In the window, enter the following command:

 set DEBUG=liquidfun & node hello_liquidfun.js 
+1
source

Source: https://habr.com/ru/post/1210152/


All Articles