Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

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
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top