Like a lot of people here I got it into my head that it would be a good idea to implement Box2D with SFML. So working with examples from this site I threw together this little ball bouncing program. The problem is that the movement of the ball is choppy, it will be smooth for a few moments, then get choppy, for a few moments, then back to smooth, its very aggravating. I was hoping that someone smarter then myself can take a look at the code here and let me know if its a SMFL graphics thing, or Box2D.
#include <SFML/Graphics.hpp>
#include <Box2D/Box2D.h>
#define PIXELS_METER 30.f
#define PI 3.1415926
double degreeToRadian(double degree) {
double radian = 0;
radian = degree * (PI/180);
return radian;
}
double radianToDegree(double radian) {
double degree = 0;
degree = radian * (180/PI);
return degree;
}
b2Body* makePolBody(b2World *World, b2BodyType type, sf::Shape &Shape, const sf::Vector2f &Position, const sf::Vector2f &Size)
{
// Create the body
b2BodyDef def;
def.type = type;
def.position.Set(Position.x/PIXELS_METER, Position.y/PIXELS_METER);
b2Body* Body = World->CreateBody(&def);
b2PolygonShape pol;
pol.SetAsBox(Size.x/PIXELS_METER, Size.y/PIXELS_METER);
b2FixtureDef fix;
fix.shape = &pol;
fix.density = 1.0f;
fix.friction = 0.3f;
Body->CreateFixture(&fix);
Shape = sf::Shape::Rectangle(0.f, 0.f, Size.x*2, Size.y*2, sf::Color::White);
Shape.SetCenter(Size);
Shape.SetPosition(Position);
return Body;
}
b2Body* makeCirBody(b2World *World, b2BodyType type, sf::Shape &Shape, const sf::Vector2f &Position, const float radius)
{
// Create the body
b2BodyDef def;
def.type = type;
def.position.Set(Position.x/PIXELS_METER, Position.y/PIXELS_METER);
b2Body* Body = World->CreateBody(&def);
// Creating the box2d circle shape (size in pixels)
b2CircleShape circle;
circle.m_p.Set(Position.x/PIXELS_METER, Position.y/PIXELS_METER);
circle.m_radius = radius/PIXELS_METER;
b2FixtureDef fix;
fix.shape = &circle;
fix.density = 1.0f;
fix.friction = .0f;
fix.restitution = 1.0f;
b2Vec2 force = b2Vec2(10, 10);
Body->ApplyLinearImpulse(force, def.position);
Body->CreateFixture(&fix);
Shape = sf::Shape::Circle(Position, radius, sf::Color::White);
return Body;
}
void Synchronize(b2Body *Body, sf::Shape &Shape)
{
Shape.SetRotation((float)-radianToDegree(Body->GetAngle()));
Shape.SetPosition(Body->GetPosition().x*PIXELS_METER, Body->GetPosition().y*PIXELS_METER);
}
int main() {
// Create the SFML Window
sf::RenderWindow Game(sf::VideoMode(800, 600, 32), "SFML Blank Window");
sf::Event Event;
Game.SetFramerateLimit(60);
// Create the world
b2Vec2 Gravity(.0f, .0f);
b2World World(Gravity, true);
// Create the body and the shape
sf::Shape ball;
b2Body *bBody = makeCirBody(&World, b2_dynamicBody, ball, sf::Vector2f(200,200),15);
sf::Shape tWall;
sf::Shape bWall;
sf::Shape lWall;
sf::Shape rWall;
b2Body *tGroundBody = makePolBody(&World, b2_staticBody, tWall, sf::Vector2f(600.f, 1.f), sf::Vector2f(800.f, 1.f));
b2Body *bGroundBody = makePolBody(&World, b2_staticBody, bWall, sf::Vector2f(1.f, 600.f), sf::Vector2f(800.f, 1.f));
b2Body *lGroundBody = makePolBody(&World, b2_staticBody, lWall, sf::Vector2f(1.f, 800.f), sf::Vector2f(1.f, 800.f));
b2Body *rGroundBody = makePolBody(&World, b2_staticBody, rWall, sf::Vector2f(800.f, 1.f), sf::Vector2f(1.f, 600.f));
while (Game.IsOpened())
{
while (Game.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
Game.Close();
}
World.Step(Game.GetFrameTime(), 10, 10);
Synchronize(bBody, ball);
Game.Clear();
Game.Draw(ball);
Game.Draw(tWall);
Game.Draw(bWall);
Game.Draw(lWall);
Game.Draw(rWall);
Game.Display();
}
return EXIT_SUCCESS;
}
[/code]