2

I'm using the emscripten port of Box2D from here: https://github.com/kripken/box2d.js

It's working great, but I'm have some trouble interacting with emscripten.

Specifically I'm perform model-display sync in a loop like this:

function step() {
    world.Step(1/60);
    var body = this.world.GetBodyList();
    while(body != null) {
        readGeometry(body, body.data);
        body = body.GetNext();
    }
}

...but that doesn't seem to work. Although the C++ code returns NULL at the end of the linked list of body objects, body.GetNext() (return type in cpp is b2Body *) is never the native javascript null.

I've also tried:

body != Box2D.NULL

However, that is also never true. I'm guessing that emscripten is returning a wrapped pointer, and I have to do some specific operation on it to test for 'nullness'.

Inspecting the returned object I can see that the 'pointer' value in it for the null values is zero, and I can make it work with:

function step() {
    world.Step(1/60);
    var body = this.world.GetBodyList();
    while(body.a != 0) { // <--------------- This hack
        readGeometry(body, body.data);
        body = body.GetNext();
    }
}

So, it's clearly possible to test for NULL-ness, but I can't find any documentation on how to do it.

2 Answers 2

3

Try this

function step() {
  world.Step(1/60);
  var body = this.world.GetBodyList();
  while(Box2D.getPointer(body)) { // <-- will equal 0 for a Box2D.NULL object
    readGeometry(body, body.data);
    body = body.GetNext();
  }
}

I know this question is really old but I recently came across this problem and found the solution on github.

0

The accepted answer didn't work, but this did :

var next = World.m_bodyList;
var current;
while (next != null) {
    current = next; next = next.m_next;
    if(current.m_userData){
        var current_body = {};
        current_body.x = current.m_xf.position.x;
        current_body.y = current.m_xf.position.y
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.