Advertisement
Guest User

Box2D stuff

a guest
Apr 24th, 2013
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <Box2D/Box2D.h>
  2.  
  3. int main (int argc, char * argv[])
  4. {
  5. //World/ground initialization code directly lifted from OpenGL project
  6.     b2World * world = new b2World(b2Vec2(-5.0f, -5.0f));
  7.    
  8.     b2BodyDef bd;
  9.     b2Body * ground = world->CreateBody(&bd);
  10.  
  11.     b2Vec2 vs[4];
  12.     vs[0].Set(-2640.0f, -2640.0f);
  13.     vs[1].Set(-2640.0f, 2640.0f);
  14.     vs[2].Set(2640.0f, 2640.0f);
  15.     vs[3].Set(2640.0f, -2640.0f);
  16.  
  17.     b2ChainShape loop;
  18.     loop.CreateLoop(vs, 4);
  19.     b2FixtureDef fd;
  20.     fd.shape = &loop;
  21.     fd.density = 0.0f;
  22.     ground->CreateFixture(&fd);
  23.  
  24. //Define two known circles whose values we can print out
  25. //The initialization code for each circle is directly lifted from the OpenGL project
  26.  
  27.     b2BodyDef circleDef;
  28.     b2Body * circleBody;
  29.     b2CircleShape circleShape;
  30.     b2FixtureDef circleFixtureDef;
  31.  
  32.     b2BodyDef circleDef2;
  33.     b2Body * circleBody2;
  34.     b2CircleShape circleShape2;
  35.     b2FixtureDef circleFixtureDef2;
  36.  
  37.     circleDef.type = b2_dynamicBody;
  38.     circleDef.position.Set(50.0f, 50.0f);
  39.     circleDef.bullet = true;
  40.     circleBody = world->CreateBody(&circleDef);
  41.     circleShape.m_p.Set(50.0f, 50.0f);
  42.     circleShape.m_radius = 50.0f;
  43.     circleFixtureDef.shape = &circleShape;
  44.     circleFixtureDef.density = 1.0f;
  45.     circleFixtureDef.friction = 0.1001f;
  46.  
  47.     circleDef2.type = b2_dynamicBody;
  48.     circleDef2.position.Set(150.0f, 150.0f);
  49.     circleDef2.bullet = true;
  50.     circleBody2 = world->CreateBody(&circleDef);
  51.     circleShape2.m_p.Set(50.0f, 50.0f);
  52.     circleShape2.m_radius = 50.0f;
  53.     circleFixtureDef2.shape = &circleShape;
  54.     circleFixtureDef2.density = 1.0f;
  55.     circleFixtureDef2.friction = 0.1001f;
  56.    
  57.     circleBody->CreateFixture(&circleFixtureDef);
  58.     circleBody2->CreateFixture(&circleFixtureDef2);
  59.  
  60. //Generate a bunch of other circles
  61.  
  62.     for (int i = 0; i < 40; i++)
  63.     {
  64.         b2BodyDef circleDef;
  65.         b2Body * circleBody;
  66.         b2CircleShape circleShape;
  67.         b2FixtureDef circleFixtureDef;
  68.  
  69.         circleDef.type = b2_dynamicBody;
  70.         circleDef.position.Set(50.0f * i, 50.0f * i);
  71.         circleDef.bullet = true;
  72.         circleBody = world->CreateBody(&circleDef);
  73.         circleShape.m_p.Set(50.0f * i, 50.0f * i);
  74.         circleShape.m_radius = 50.0f;
  75.         circleFixtureDef.shape = &circleShape;
  76.         circleFixtureDef.density = 1.0f;
  77.         circleFixtureDef.friction = 0.1001f;
  78.     circleBody->CreateFixture(&circleFixtureDef);
  79.     }
  80.  
  81. //Simulate the world for a while
  82.  
  83.     for (int i = 0; i < 10000; i++)
  84.     {
  85.         world->Step(0.01667f, 10, 10);
  86.         printf("x: %f, y: %f\n", circleBody->GetPosition().x, circleBody->GetPosition().y);
  87.         printf("x: %f, y: %f\n", circleBody2->GetPosition().x, circleBody2->GetPosition().y);
  88.     }
  89.  
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement