Advertisement
veeableful

Box2D Test

Feb 16th, 2012
6,117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. #include <SFML\Graphics.hpp>
  2. #include <Box2D\Box2D.h>
  3.  
  4. /** We need this to easily convert between pixel and real-world coordinates*/
  5. static const float SCALE = 30.f;
  6.  
  7. /** Create the base for the boxes to land */
  8. void CreateGround(b2World& World, float X, float Y);
  9.  
  10. /** Create the boxes */
  11. void CreateBox(b2World& World, int MouseX, int MouseY);
  12.  
  13. int main()
  14. {
  15.     /** Prepare the window */
  16.     sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "Test");
  17.     Window.SetFramerateLimit(60);
  18.  
  19.     /** Prepare the world */
  20.     b2Vec2 Gravity(0.f, 9.8f);
  21.     b2World World(Gravity);
  22.     CreateGround(World, 400.f, 500.f);
  23.  
  24.     /** Prepare textures */
  25.     sf::Texture GroundTexture;
  26.     sf::Texture BoxTexture;
  27.     GroundTexture.LoadFromFile("ground.png");
  28.     BoxTexture.LoadFromFile("box.png");
  29.  
  30.     while (Window.IsOpened())
  31.     {
  32.         if (sf::Mouse::IsButtonPressed(sf::Mouse::Left))
  33.         {
  34.             int MouseX = sf::Mouse::GetPosition(Window).x;
  35.             int MouseY = sf::Mouse::GetPosition(Window).y;
  36.             CreateBox(World, MouseX, MouseY);
  37.         }
  38.         World.Step(1/60.f, 8, 3);
  39.  
  40.         Window.Clear(sf::Color::White);
  41.         int BodyCount = 0;
  42.         for (b2Body* BodyIterator = World.GetBodyList(); BodyIterator != 0; BodyIterator = BodyIterator->GetNext())
  43.         {
  44.             if (BodyIterator->GetType() == b2_dynamicBody)
  45.             {
  46.                 sf::Sprite Sprite;
  47.                 Sprite.SetTexture(BoxTexture);
  48.                 Sprite.SetOrigin(16.f, 16.f);
  49.                 Sprite.SetPosition(SCALE * BodyIterator->GetPosition().x, SCALE * BodyIterator->GetPosition().y);
  50.                 Sprite.SetRotation(BodyIterator->GetAngle() * 180/b2_pi);
  51.                 Window.Draw(Sprite);
  52.                 ++BodyCount;
  53.             }
  54.             else
  55.             {
  56.                 sf::Sprite GroundSprite;
  57.                 GroundSprite.SetTexture(GroundTexture);
  58.                 GroundSprite.SetOrigin(400.f, 8.f);
  59.                 GroundSprite.SetPosition(BodyIterator->GetPosition().x * SCALE, BodyIterator->GetPosition().y * SCALE);
  60.                 GroundSprite.SetRotation(180/b2_pi * BodyIterator->GetAngle());
  61.                 Window.Draw(GroundSprite);
  62.             }
  63.         }
  64.         Window.Display();
  65.     }
  66.  
  67.     return 0;
  68. }
  69.  
  70. void CreateBox(b2World& World, int MouseX, int MouseY)
  71. {
  72.     b2BodyDef BodyDef;
  73.     BodyDef.position = b2Vec2(MouseX/SCALE, MouseY/SCALE);
  74.     BodyDef.type = b2_dynamicBody;
  75.     b2Body* Body = World.CreateBody(&BodyDef);
  76.  
  77.     b2PolygonShape Shape;
  78.     Shape.SetAsBox((32.f/2)/SCALE, (32.f/2)/SCALE);
  79.     b2FixtureDef FixtureDef;
  80.     FixtureDef.density = 1.f;
  81.     FixtureDef.friction = 0.7f;
  82.     FixtureDef.shape = &Shape;
  83.     Body->CreateFixture(&FixtureDef);
  84. }
  85.  
  86. void CreateGround(b2World& World, float X, float Y)
  87. {
  88.     b2BodyDef BodyDef;
  89.     BodyDef.position = b2Vec2(X/SCALE, Y/SCALE);
  90.     BodyDef.type = b2_staticBody;
  91.     b2Body* Body = World.CreateBody(&BodyDef);
  92.  
  93.     b2PolygonShape Shape;
  94.     Shape.SetAsBox((800.f/2)/SCALE, (16.f/2)/SCALE);
  95.     b2FixtureDef FixtureDef;
  96.     FixtureDef.density = 0.f;
  97.     FixtureDef.shape = &Shape;
  98.     Body->CreateFixture(&FixtureDef);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement