I've got some code that's giving me rather strange results. I'm not quite sure yet if it's a fault on my end, or a bug in Box2D.
I have an editor that lets you draw polygons. When you right click on one of them, it calls this function (where p is your mouse position)
If I draw one triangle, and then delete it, it disappears as expected, but if I click again, the Query still returns a count of 1, even though the object is gone, and it prints "destroying body" again. The two commented lines commented out above cause my program to crash when deleting a body that's already been deleted.
I'm drawing the shapes like this
Which directly uses the b2Bodies. If the shape is absent from the drawing algorithm, shouldn't it also be destroyed from the queries?
Edit: I'm using Box2D 1.4.3
I have an editor that lets you draw polygons. When you right click on one of them, it calls this function (where p is your mouse position)
Code:
void Editor::DestroyPoly(b2Vec2 p) // buggy!!
{
// Make a small box.
b2AABB aabb;
b2Vec2 d;
d.Set(0.001f, 0.001f);
aabb.minVertex = p - d;
aabb.maxVertex = p + d;
// Query the world for overlapping shapes.
const int32 k_maxCount = 10;
b2Shape* shapes[k_maxCount];
int32 count = m_world->Query(aabb, shapes, k_maxCount);
b2Body* body = NULL;
printf("%d\n",count);
for (int32 i = 0; i < count; ++i)
{
bool inside = shapes[i]->TestPoint(p);
if (inside)
{
body = shapes[i]->m_body;
//m_bodies.erase(*(list<b2BodyDef>::const_iterator*)body->m_userData);
//delete (list<b2BodyDef>::const_iterator*)body->m_userData;
printf("destroying body\n");
m_world->DestroyBody(body);
break;
}
}
}
{
// Make a small box.
b2AABB aabb;
b2Vec2 d;
d.Set(0.001f, 0.001f);
aabb.minVertex = p - d;
aabb.maxVertex = p + d;
// Query the world for overlapping shapes.
const int32 k_maxCount = 10;
b2Shape* shapes[k_maxCount];
int32 count = m_world->Query(aabb, shapes, k_maxCount);
b2Body* body = NULL;
printf("%d\n",count);
for (int32 i = 0; i < count; ++i)
{
bool inside = shapes[i]->TestPoint(p);
if (inside)
{
body = shapes[i]->m_body;
//m_bodies.erase(*(list<b2BodyDef>::const_iterator*)body->m_userData);
//delete (list<b2BodyDef>::const_iterator*)body->m_userData;
printf("destroying body\n");
m_world->DestroyBody(body);
break;
}
}
}
If I draw one triangle, and then delete it, it disappears as expected, but if I click again, the Query still returns a count of 1, even though the object is gone, and it prints "destroying body" again. The two commented lines commented out above cause my program to crash when deleting a body that's already been deleted.
I'm drawing the shapes like this
Code:
// draw other shapes
for (b2Body* b = m_world->m_bodyList; b; b = b->m_next)
{
for (b2Shape* s = b->m_shapeList; s; s = s->m_next)
{
drawShape(s, Color(0,1,0));
}
}
for (b2Body* b = m_world->m_bodyList; b; b = b->m_next)
{
for (b2Shape* s = b->m_shapeList; s; s = s->m_next)
{
drawShape(s, Color(0,1,0));
}
}
Which directly uses the b2Bodies. If the shape is absent from the drawing algorithm, shouldn't it also be destroyed from the queries?
Edit: I'm using Box2D 1.4.3