Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: C++  |  size: 2.24 KB  |  hits: 74  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <SFML/Graphics.hpp>
  2. #include <Box2D/Box2D.h>
  3. #include <iostream>
  4.  
  5. #define PPM 30 // Pixel Per Meters
  6.  
  7. using namespace sf;
  8.  
  9. b2World *myWorld;
  10. b2Body *boxBody;
  11. b2Body *lineBody;
  12.  
  13. void updatePhysics()
  14. {
  15.     float32 timeStep = 1.0f / 60.0f;
  16.         int32 velocityIterations = 6;
  17.         int32 positionIterations = 2;
  18.  
  19.     myWorld->Step(timeStep, velocityIterations, positionIterations);
  20.  
  21. }
  22.  
  23. void initPhysics()
  24. {
  25.     // set up the world
  26.     b2Vec2 gravity(0.0f,9.8f);
  27.     myWorld = new b2World(gravity,true);
  28.  
  29.     // box body definition
  30.     b2BodyDef boxBodyDef;
  31.     b2Vec2 boxVelocity;
  32.     boxVelocity.Set(0.0f,10.0f);
  33.     boxBodyDef.type = b2_dynamicBody;
  34.     boxBodyDef.position.Set(550.0f/PPM,45.0f/PPM);
  35.     boxBodyDef.linearVelocity = boxVelocity;
  36.  
  37.     // line body definition
  38.     b2BodyDef lineBodyDef;
  39.     lineBodyDef.type = b2_staticBody;
  40.     lineBodyDef.position.Set(400.0f/PPM,375.0f/PPM);
  41.  
  42.     // create the bodies
  43.     boxBody = myWorld->CreateBody(&boxBodyDef);
  44.     lineBody = myWorld->CreateBody(&lineBodyDef);
  45.  
  46.     // create the shapes
  47.     b2PolygonShape boxShape,lineShape;
  48.     boxShape.SetAsBox(50.0f/PPM,25.0f/PPM);
  49.     lineShape.SetAsEdge(b2Vec2(50.0f/PPM,550.0f/PPM),b2Vec2(750.0f/PPM,200.0f/PPM));
  50.  
  51.     // add fixtures
  52.     b2FixtureDef boxFixtureDef,lineFixtureDef;
  53.     boxFixtureDef.shape = &boxShape;
  54.     lineFixtureDef.shape = &lineShape;
  55.     boxFixtureDef.density = 1;
  56.  
  57.     boxBody->CreateFixture(&boxFixtureDef);
  58.     lineBody->CreateFixture(&lineFixtureDef);
  59. }
  60.  
  61. int main()
  62. {
  63.     bool running = true;
  64.  
  65.     initPhysics();
  66.  
  67.     RenderWindow app(VideoMode(800,600,32),"First Test",Style::Close);
  68.  
  69.     Shape box = Shape::Rectangle(500,20,600,70,Color::White);
  70.     Shape ground = Shape::Line(50,550,750,200,1,Color::Red);
  71.  
  72.  
  73.     while(running)
  74.     {
  75.         Event event;
  76.         while(app.GetEvent(event))
  77.         {
  78.             if(event.Type == Event::Closed)
  79.             running = false;
  80.         }
  81.  
  82.         updatePhysics();
  83.  
  84.         b2Vec2 pos = boxBody->GetPosition();
  85.         box.SetPosition(pos.x*PPM,pos.y*PPM);
  86.  
  87.         app.Clear();
  88.  
  89.         app.Draw(box);
  90.         app.Draw(ground);
  91.         app.Display();
  92.     }
  93.  
  94.     app.Close();
  95.     delete myWorld;
  96.  
  97.     return EXIT_SUCCESS;
  98. }