Box2D Forums

It is currently Sun May 19, 2013 1:13 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Jun 12, 2012 8:01 am 
Offline

Joined: Tue Jun 12, 2012 7:43 am
Posts: 3
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.


Top
 Profile  
 
PostPosted: Thu Jun 14, 2012 6:02 am 
Offline

Joined: Tue Jun 12, 2012 7:43 am
Posts: 3
I create this program witch is similar to my big one:

Code:
#include <conio.h>
#include <vector>
#include <time.h>
#include <Box2D/Box2D.h>

b2World *world;
b2BodyDef bd;
b2Body* obstacles;
b2EdgeShape shape;

b2Body* body;


void main(void)
{
 int i,j;

 b2Vec2 gravity(0.0f, -1.0f);
 world = new b2World(gravity);
 obstacles = world->CreateBody(&bd);
 
 //we setup a plenty of horizontal lines starting at y==0 and we go lower with every step
 for (int i=0;i<5000;i++)
  {
   shape.Set(b2Vec2(-10.0,0.0-(2.0*i)), b2Vec2(10.0,0.0-(2.0*i)));
   obstacles->CreateFixture(&shape, 0.0f);
  }

 //we set up dynamic body
 b2BodyDef myBodyDef;
 myBodyDef.type = b2_dynamicBody;
 myBodyDef.position.Set(0.0,2.0);
 body = world->CreateBody(&myBodyDef);

 //and we add circle fixture
 b2CircleShape circleShape;
 circleShape.m_p.Set(0, 0);
 circleShape.m_radius = 0.5;
 b2FixtureDef myFixtureDef;
 myFixtureDef.shape = &circleShape;
 myFixtureDef.density = 1.0;
 body->CreateFixture(&myFixtureDef);

 //now we simulate until few steps - ball will land on first horizontal lines
 for (int i=0;i<12;i++)
 {
  for (int j=0;j<30;j++) world->Step(0.01, 6, 2);
  printf("%f %f\n",body->GetPosition().x,body->GetPosition().y);
 }

 printf("ball landed... recreating map\n");

 //so we delete obstacles and create new ones:

 world->DestroyBody(obstacles);
 obstacles = NULL;
 obstacles = world->CreateBody(&bd);

 //notice that we start from i==2 now - so ball should have space to continue some falling
 for (int i=2;i<5000;i++)
  {
   shape.Set(b2Vec2(-10.0,0.0-(2.0*i)), b2Vec2(10.0,0.0-(2.0*i)));
   obstacles->CreateFixture(&shape, 0.0f);
  }

 //let's check what happened:
 for (int i=0;i<12;i++)
 {
  for (int j=0;j<30;j++) world->Step(0.01, 6, 2);
  printf("%f %f\n",body->GetPosition().x,body->GetPosition().y);
 }

 delete world;
 printf("done");
 getch();
}


And this program above work :x I got no idea why my original program not working :cry:h Do anyone see any potential danger in code above? Or it's perfectly proper form Box2D point of view?


Top
 Profile  
 
PostPosted: Fri Jun 15, 2012 1:38 am 
Offline

Joined: Tue Jun 12, 2012 7:43 am
Posts: 3
OK. Moderator can remove this topic. I got bug in my class constructor and in "load_map" method that make program read the same file again ... Funny part is that it was the first thing that I checked and somehow (how?!?!?) I got conclusion that this is not the case....


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group