Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ env:
- CXX=g++-4.9
language: node_js
node_js:
- 4.2
- stable
- "6.11"
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.9
- g++-4.9
3 changes: 2 additions & 1 deletion Box2D.es6
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export default reduce(
}
) : box2d,
box2d
);
);
module.exports = exports.default;
18 changes: 11 additions & 7 deletions Box2D.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

## Details

Box2D compiled as static library, Node.js C++ addon produced by [swig](http://www.swig.org/).
Box2D compiled as static library, Node.js C++ addon produced by [SWIG](http://www.swig.org/).

## Install

Requires `curl`, `make`, `cmake`, `g++`. See also requirements for building [swig](http://www.swig.org/) and [node-gyp](https://www.npmjs.com/package/node-gyp)
Requires `curl`, `make`, `cmake`, `g++`. See also requirements for building [SWIG](http://www.swig.org/) and [node-gyp](https://www.npmjs.com/package/node-gyp)

Install with [NPM](https://www.npmjs.com/):

Expand All @@ -18,7 +18,7 @@ npm install node-gyp -g
npm install box2d-native
```

**IMPORTANT:** Installation tested only on Linux with Node.js >= 4.2.1 and gc++ 4.9.
**IMPORTANT:** Installation tested only with Node.js 6.11.2 and gc++ 4.9.

## Usage

Expand All @@ -35,12 +35,14 @@ Original "namespaced" Box2D classes (`b2Vec2`, `b2World`...) are also exposed.

## Demo

**IMPORTANT:** Runs only with Node.js v4.2.1
**IMPORTANT:** Runs only with Node.js v6.11.2

```
npm install node-gyp -g
git checkout https://github.com/zuker/box2d-native.git
cd box2d-native/demo
git clone https://github.com/zuker/box2d-native.git
cd box2d-native
npm install
cd demo
npm install
npm start
```
Expand Down
163 changes: 75 additions & 88 deletions demo/demo.js
Original file line number Diff line number Diff line change
@@ -1,183 +1,170 @@
'use strict';
let Box2D = require('box2d-native');
let World = Box2D.World;
let Vec2 = Box2D.Vec2;
let b2Vec2 = Box2D.b2Vec2;
let b2Draw = Box2D.b2Draw;
let b2Shape = Box2D.b2Shape;
let b2EdgeShape = Box2D.b2EdgeShape;
let b2CircleShape = Box2D.b2CircleShape;
let b2BodyDef = Box2D.b2BodyDef;
let b2_staticBody = Box2D.b2_staticBody;
let b2_kinematicBody = Box2D.b2_kinematicBody;
let b2_dynamicBody = Box2D.b2_dynamicBody;
let b2Mul = Box2D.b2Mul;
let b2Assert = Box2D.b2Assert;
let b2_maxPolygonVertices = Box2D.b2_maxPolygonVertices;
let b2Color = Box2D.b2Color;
let world = new World(new Vec2(0, -9.8));
let step;

var bd_ground = new Box2D.b2BodyDef();
var bd_circle = new Box2D.b2BodyDef();
bd_circle.position = new Vec2(20, 120);
bd_circle.type = Box2D.b2_dynamicBody;
var ground = world.CreateBody(bd_ground);
var circle = world.CreateBody(bd_circle);
let world = new World(new b2Vec2(0, -9.8));

var shape = new Box2D.b2EdgeShape();
let circleShape = new Box2D.b2CircleShape();
function CreateTestObjects() {
var bd_ground = new b2BodyDef();
var bd_circle = new b2BodyDef();
bd_circle.position = new b2Vec2(20, 120);
bd_circle.type = b2_dynamicBody;
var ground = world.CreateBody(bd_ground);
var circle = world.CreateBody(bd_circle);

circleShape.m_radius = 10;
shape.Set(new Box2D.b2Vec2(-40.0, 0.0), new Box2D.b2Vec2(40.0, 0.0));
var shape = new b2EdgeShape();
let circleShape = new b2CircleShape();

let fixture = ground.CreateFixture(shape, 0.0);
let circleFixture = circle.CreateFixture(circleShape, 0.0);
circleFixture.SetRestitution(0.5);
circleShape.m_radius = 10;
shape.Set(new b2Vec2(-40.0, 0.0), new b2Vec2(40.0, 0.0));

let fixture = ground.CreateFixture(shape, 0.0);
let circleFixture = circle.CreateFixture(circleShape, 0.0);
circleFixture.SetRestitution(0.5);
}

function DrawShape(fixture, xf, color, g_debugDraw) {
let shape = fixture.GetShape();
switch (fixture.GetType())
{
switch (fixture.GetType()) {
case b2Shape.e_circle:
{
let center = b2Mul(xf, shape.m_p);
let radius = shape.m_radius;
let axis = b2Mul(xf.q, new b2Vec2(1.0, 0.0));
{
let center = b2Mul(xf, shape.m_p);
let radius = shape.m_radius;
let axis = b2Mul(xf.q, new b2Vec2(1.0, 0.0));
g_debugDraw.DrawSolidCircle(center, radius, axis, color);
}
}
break;

case b2Shape.e_edge:
{
let v1 = b2Mul(xf, shape.m_vertex1);
let v2 = b2Mul(xf, shape.m_vertex2);
{
let v1 = b2Mul(xf, shape.m_vertex1);
let v2 = b2Mul(xf, shape.m_vertex2);
g_debugDraw.DrawSegment(v1, v2, color);
}
}
break;

case b2Shape.e_chain:
{
let count = shape.m_count;
const vertices = shape.m_vertices;

let v1 = b2Mul(xf, vertices[0]);
for (let i = 1; i < count; ++i)
{
let v2 = b2Mul(xf, vertices[i]);
let count = shape.m_count;
const vertices = shape.m_vertices;

let v1 = b2Mul(xf, vertices[0]);
for (let i = 1; i < count; ++i) {
let v2 = b2Mul(xf, vertices[i]);
g_debugDraw.DrawSegment(v1, v2, color);
g_debugDraw.DrawCircle(v1, 0.05, color);
v1 = v2;
v1 = v2;
}
}
}
break;

case b2Shape.e_polygon:
{
let vertexCount = shape.m_count;
b2Assert(vertexCount <= b2_maxPolygonVertices);
let vertices = new Array(b2_maxPolygonVertices);

for (let i = 0; i < vertexCount; ++i)
{
vertices[i] = b2Mul(xf, shape.m_vertices[i]);
}
let vertexCount = shape.m_count;
b2Assert(vertexCount <= b2_maxPolygonVertices);
let vertices = new Array(b2_maxPolygonVertices);

for (let i = 0; i < vertexCount; ++i) {
vertices[i] = b2Mul(xf, shape.m_vertices[i]);
}

g_debugDraw.DrawSolidPolygon(vertices, vertexCount, color);
}
}
break;

default:
break;
}
}


world.DrawDebugData = function () {
if (!this._debugDraw) {
return;
}
let flags = this._debugDraw.GetFlags();

if (flags & Box2D.b2Draw.e_shapeBit) {
for (let b = this.GetBodyList(); b; b = b.GetNext()) {
if (flags & b2Draw.e_shapeBit) {
for (let b = this.GetBodyList(); typeof b === 'object'; b = b.GetNext()) {
let xf = b.GetTransform();
for (let f = b.GetFixtureList(); f; f = f.GetNext()) {
if (b.IsActive() == false)
{
DrawShape(f, xf, b2Color(0.5, 0.5, 0.3), this._debugDraw);
}
else if (b.GetType() == Box2D.b2_staticBody)
{
for (let f = b.GetFixtureList(); typeof f === 'object'; f = f.GetNext()) {
if (b.IsActive() == false) {
DrawShape(f, xf, new b2Color(0.5, 0.5, 0.3), this._debugDraw);
} else if (b.GetType() == b2_staticBody) {
DrawShape(f, xf, new b2Color(0.5, 0.9, 0.5), this._debugDraw);
}
else if (b.GetType() == Box2D.b2_kinematicBody)
{
} else if (b.GetType() == b2_kinematicBody) {
DrawShape(f, xf, new b2Color(0.5, 0.5, 0.9), this._debugDraw);
}
else if (b.IsAwake() == false)
{
} else if (b.IsAwake() == false) {
DrawShape(f, xf, new b2Color(0.6, 0.6, 0.6), this._debugDraw);
}
else
{
} else {
DrawShape(f, xf, new b2Color(0.9, 0.7, 0.7), this._debugDraw);
}
}
}
}

if (flags & b2Draw.e_jointBit)
{
for (let j = m_jointList; j; j = j.GetNext())
{
if (flags & b2Draw.e_jointBit) {
for (let j = m_jointList; j; j = j.GetNext()) {
DrawJoint(j);
}
}

if (flags & b2Draw.e_aabbBit)
{
let color = new Box2D.b2Color(0.9, 0.3, 0.9);
if (flags & b2Draw.e_aabbBit) {
let color = new b2Color(0.9, 0.3, 0.9);
let bp = m_contactManager.m_broadPhase;

for (let b = this.m_bodyList; b; b = b.GetNext())
{
if (b.IsActive() == false)
{
for (let b = this.m_bodyList; b; b = b.GetNext()) {
if (b.IsActive() == false) {
continue;
}

for (let f = b.GetFixtureList(); f; f = f.GetNext())
{
for (let i = 0; i < f.m_proxyCount; ++i)
{
for (let f = b.GetFixtureList(); f; f = f.GetNext()) {
for (let i = 0; i < f.m_proxyCount; ++i) {
let proxy = f.m_proxies + i;
let aabb = bp.GetFatAABB(proxy.proxyId);
let vs = [new Vec2(), new Vec2(), new Vec2(), new Vec2()];
let vs = [new b2Vec2(), new b2Vec2(), new b2Vec2(), new b2Vec2()];
vs[0].Set(aabb.lowerBound.x, aabb.lowerBound.y);
vs[1].Set(aabb.upperBound.x, aabb.lowerBound.y);
vs[2].Set(aabb.upperBound.x, aabb.upperBound.y);
vs[3].Set(aabb.lowerBound.x, aabb.upperBound.y);

this._debugDraw.DrawPolygon(vs, 4, color);
this._debugDraw.DrawPolygon(vs, 4, color);
}
}
}
}

if (flags & Box2D.b2Draw.e_centerOfMassBit)
{
for (let b = this.m_bodyList; b; b = b.GetNext())
{
if (flags & b2Draw.e_centerOfMassBit) {
for (let b = this.m_bodyList; b; b = b.GetNext()) {
let xf = b.GetTransform();
xf.p = b.GetWorldCenter();
this._debugDraw.DrawTransform(xf);
this._debugDraw.DrawTransform(xf);
}
}
};

let lastTimestamp;
exports.step = function () {
let now = Date.now();
let stepRate = (now - lastTimestamp) / 1000;
let stepRate = (now - lastTimestamp) / 1000.0;
lastTimestamp = now;
world.Step(stepRate, 10, 10);
world.DrawDebugData();
};

exports.init = function (debugDraw) {
CreateTestObjects();
world._debugDraw = debugDraw;
};
};
Loading