I create program that change labyrinth during run-time. My program go like this:
Code:
b2World *world;
b2BodyDef bd;
b2Body* obstacles;
b2EdgeShape shape;
(constructor):
Code:
obstacles = world->CreateBody(&bd);
for (i=0;i<x;i++)
{
fscanf(in,"%d %d %d %d\n",&x0,&y0,&x1,&y1);
shape.Set(b2Vec2((float)x0*size_scale_factor_x, (float)y0*size_scale_factor_y), b2Vec2((float)x1*size_scale_factor_x, (float)y1*size_scale_factor_y));
obstacles->CreateFixture(&shape, 0.0f);
}
so what I do is basically reading „walls” from file. At some point I wish to change this walls setup (load_new_map method). I try everything. One of my approach was to delete body (not inside callbacks ofc!), create it again:
Code:
world->DestroyBody(obstacles);
obstacles = NULL;
obstacles = world->CreateBody(&bd);
and then read new walls:
Code:
for (i=0;i<x;i++)
{
fscanf(in,"%d %d %d %d\n",&x0,&y0,&x1,&y1);
shape.Set(b2Vec2((float)x0*size_scale_factor_x, (float)y0*size_scale_factor_y), b2Vec2((float)x1*size_scale_factor_x, (float)y1*size_scale_factor_y));
obstacles->CreateFixture(&shape, 0.0f);
}
but after this operation Box2D still detect collision with „old” obstacle object. What surprise me is a fact that if I removed „obstacles = world->CreateBody(&bd);” line (and reading new walls from file ofc) it work as expected („walls” disappears and objects don't collide with “obstacles”).
Any idea what I do wrong?
PS. Sorry for my poor English.