Skip to content
A port of the box2djs library into a nodejs module
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


@author Josh Adell

A port of the box2djs library into a node module.


Two options here:

  1. Grab the code from its

  2. Use npm: npm install box2d

Option 2 is the recommended way.

Code example

Here is the box2d "Hello World" program from written for node:

var sys = require("sys"),
    b2d = require("./box2dnode");

// Define world
var worldAABB = new b2d.b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(100.0, 100.0);

var gravity = new b2d.b2Vec2(0.0, -10.0);
var doSleep = true;

var world = new b2d.b2World(worldAABB, gravity, doSleep);

// Ground Box
var groundBodyDef = new b2d.b2BodyDef();
groundBodyDef.position.Set(0.0, -10.0);

var groundBody = world.CreateBody(groundBodyDef);

var groundShapeDef = new b2d.b2PolygonDef();
groundShapeDef.SetAsBox(50.0, 10.0);


// Dynamic Body
var bodyDef = new b2d.b2BodyDef();
bodyDef.position.Set(0.0, 4.0);

var body = world.CreateBody(bodyDef);

var shapeDef = new b2d.b2PolygonDef();
shapeDef.SetAsBox(1.0, 1.0);
shapeDef.density = 1.0;
shapeDef.friction = 0.3;

// Run Simulation!
var timeStep = 1.0 / 60.0;

var iterations = 10;

for (var i=0; i < 60; i++) {
    world.Step(timeStep, iterations);
    var position = body.GetPosition();
    var angle = body.GetAngle();
    sys.puts(i+": <"+position.x+", "+position.y+"> @"+angle);
You can’t perform that action at this time.