I just started playing around with Box2D recently, and I have to say I'm impressed. It works really well!
However, I did notice one issue when following code from the examples. The following line of code appears to be the recommended way to loop through the bodyList:
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
...
}
However, I found this loop can cause bugs in the following scenario:
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
//We process bb and realize it should be destroyed, so we call:
world.DestroyBody(bb);
}
While bb does get destroyed, this code inadvertently causes the loop to abort and anything else in the bodyList fails to get processed on that step. This is because the DestroyBody method reassigns body's "m_next" property.
Switching my loop to something like the following allowed me to preserve whatever was next in the body list regardless of what happens to the current body:
var next:b2Body = m_world.m_bodyList;
var current:b2Body;
while (next != null) {
current = next;
next = next.m_next;
//PROCESS CURRENT HERE
}
While this does seem a little convoluted, it seemed safer than attempting to alter the Box2D internals.
However, I did notice one issue when following code from the examples. The following line of code appears to be the recommended way to loop through the bodyList:
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
...
}
However, I found this loop can cause bugs in the following scenario:
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
//We process bb and realize it should be destroyed, so we call:
world.DestroyBody(bb);
}
While bb does get destroyed, this code inadvertently causes the loop to abort and anything else in the bodyList fails to get processed on that step. This is because the DestroyBody method reassigns body's "m_next" property.
Switching my loop to something like the following allowed me to preserve whatever was next in the body list regardless of what happens to the current body:
var next:b2Body = m_world.m_bodyList;
var current:b2Body;
while (next != null) {
current = next;
next = next.m_next;
//PROCESS CURRENT HERE
}
While this does seem a little convoluted, it seemed safer than attempting to alter the Box2D internals.