#include <SFML/Graphics.hpp>
#include <Box2D/Box2D.h>
#include <stdio.h>
#define PPM 30 // Pixel Per Meters
using namespace sf;
int main()
{
bool running = true;
RenderWindow app(VideoMode(800,600,32),"First Test",Style::Close);
Shape box = Shape::Rectangle(500,20,600,70,Color::White);
Shape ground = Shape::Line(50,550,750,200,1,Color::Red);
// set up the world
b2Vec2 gravity(0.0f,9.8f);
b2World *myWorld = new b2World(gravity,true);
// box body definition
b2BodyDef boxBodyDef;
b2Vec2 boxVelocity;
boxVelocity.Set(0.0f,10.0f);
boxBodyDef.type = b2_dynamicBody;
boxBodyDef.position.Set(500,20);
boxBodyDef.linearVelocity = boxVelocity;
// line body definition
b2BodyDef lineBodyDef;
lineBodyDef.type = b2_staticBody;
lineBodyDef.position.Set(50,550);
// create the bodies
b2Body *boxBody = myWorld->CreateBody(&boxBodyDef);
b2Body *lineBody = myWorld->CreateBody(&lineBodyDef);
// create the shapes
b2PolygonShape boxShape,lineShape;
boxShape.SetAsBox(50.0f/PPM,25.0f/PPM);
lineShape.SetAsEdge(b2Vec2(50/PPM,550/PPM),b2Vec2(750/PPM,200/PPM));
// add fixtures
b2FixtureDef boxFixtureDef,lineFixtureDef;
boxFixtureDef.shape = &boxShape;
lineFixtureDef.shape = &lineShape;
boxFixtureDef.density = 1;
boxBody->CreateFixture(&boxFixtureDef);
lineBody->CreateFixture(&lineFixtureDef);
float timeStep = 1.0f / 20.0f; // 20fps
int velIter = 8;
int posIter = 3;
while(running)
{
Event event;
while(app.GetEvent(event))
{
if(event.Type == Event::Closed)
running = false;
}
myWorld->Step(timeStep, velIter, posIter);
b2Vec2 pos = boxBody->GetPosition();
printf("%f %f\n",pos.x, pos.y);
box.SetPointPosition(0,pos.x, pos.y);
box.SetPointPosition(1,pos.x+100,pos.y);
box.SetPointPosition(2,pos.x+100,pos.y+50);
box.SetPointPosition(3,pos.x,pos.y+50);
app.Clear();
app.Draw(box);
app.Draw(ground);
app.Display();
}
app.Close();
delete myWorld;
return EXIT_SUCCESS;
}