kripken / box2d.js

Port of Box2D to JavaScript using Emscripten
1.32k stars 198 forks source link

Destroyed bodies still exist in GetBodyList #66

Closed shannon closed 9 years ago

shannon commented 9 years ago

Perhaps there is something I am missing but when I destroy a body and then loop over the world bodies later using GetBodyList they are still all there. But GetBodyCount is the correct number. I created a jsfiddle showing the problem.

http://jsfiddle.net/n535chbx/2/

var world = new Box2D.b2World( new Box2D.b2Vec2(0.0, -10.0) );

var bodies = [];
for(var i = 0; i < 100; i++){
    bodies.push(world.CreateBody( new Box2D.b2BodyDef() ));
}

bodies.slice(50).forEach(function(body){
    world.DestroyBody(body);
})

var b2Body = world.GetBodyList();
var items = 0;
while(Box2D.getPointer(b2Body)){
    items++;
    b2Body = b2Body.GetNext();
}

var count = world.GetBodyCount();

document.body.innerHTML = 'count: ' + count + ' | items: ' + i;

//outputs: count: 50 | items: 100
shannon commented 9 years ago

Ignore me I am an idiot. i != items.

I am having trouble with what appears to be bodies still showing up in the list so clearly I have something else going on since this fiddle works fine now.

http://jsfiddle.net/n535chbx/3/

I may reopen if I can pinpoint my problem better.

shannon commented 9 years ago

I figured out my problem. It turns out if I was trying to destroy my body wrapper object instead of the b2Body object.

http://jsfiddle.net/n535chbx/4/

It doesn't error and for some reason it lowers the GetBodyCount(). It lead to some "interesting" results during debugging. I'll just leave my findings here for some other poor soul.