Skip to content

Instantly share code, notes, and snippets.

@sunny1304
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunny1304/9190792 to your computer and use it in GitHub Desktop.
Save sunny1304/9190792 to your computer and use it in GitHub Desktop.
example of simulating moving balls with Box2D and SFML
#include <SFML/Graphics.hpp>
#include "Box2D/Box2D.h"
static const float SCALE = 30.f;
void create_balls_and_move_them()
{
// define the world;
b2Vec2 gravity(b2Vec2(0.f,9.8f)); // check if it dont work
b2World world(gravity);
//create the ground
// create the ground before creating balls
create_ground(world);
// create the window
sf::RenderWindow window(sf::VideoMode(800,600,32),"Moving Boxes");
window.setFramerateLimit(60);
sf::Event event;
while(window.isOpen())
{
while(window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
// create the balls
if (sf::Mouse::isButtonPressed(sf::Mouse::Right))
{
float32 x = (float32)sf::Mouse::getPosition(window).x;
float32 y = (float32)sf::Mouse::getPosition(window).y;
create_ball(world,x,y);
}
//load ball texture
sf::Texture ball_texture;
ball_texture.loadFromFile("ball-hd.png");
sf::Sprite ball_sprite;
ball_sprite.setOrigin(16.f,16.f);
ball_sprite.setTexture(ball_texture);
//load ground texture
sf::Texture ground_texture;
ground_texture.loadFromFile("ground.png");
sf::Sprite ground_sprite;
ground_sprite.setTexture(ground_texture);
ground_sprite.setPosition(1.f,510.f);
//simulate the world
world.Step(1/60.f,8,3);
//clear window
window.clear(sf::Color::White);
//iterator over the bodies of the world
for (b2Body *body_iterator = world.GetBodyList(); body_iterator !=0; body_iterator= body_iterator-> GetNext())
{
//world.Step(1/60.f,8,3);
if (body_iterator-> GetType() == b2_dynamicBody)
{
ball_sprite.setPosition(body_iterator-> GetPosition().x*SCALE, body_iterator-> GetPosition().y*SCALE);
window.draw(ball_sprite);
}
else
{
window.draw(ground_sprite);
}
}
window.display();
}
}
void create_ball(b2World& world, float32 x, float32 y)
{
//define the body
b2BodyDef ball_bodydef;
ball_bodydef.position.Set(x/SCALE, y/SCALE);
ball_bodydef.type = b2_dynamicBody;
//create the body
b2Body *ball_body = world.CreateBody(&ball_bodydef);
//define the shape
b2CircleShape ball_shape;
ball_shape.m_radius= 50.f/SCALE;
//define the fixture
b2FixtureDef ball_fixturedef;
ball_fixturedef.shape = &ball_shape;
ball_fixturedef.density = 1.5f;
ball_fixturedef.friction = 0.3f;
ball_fixturedef.restitution = 0.5f;
//attach the fixture with body
ball_body -> CreateFixture(&ball_fixturedef);
}
void create_ground(b2World& world)
{
b2BodyDef ground_bodydef;
ground_bodydef.type = b2_staticBody;
ground_bodydef.position.Set(1.f/SCALE,533.f/SCALE);
b2Body *ground_body= world.CreateBody(&ground_bodydef);
b2PolygonShape ground_shape;
ground_shape.SetAsBox(800.f/SCALE, 50.f/SCALE);
b2FixtureDef ground_fixturedef;
ground_fixturedef.density = 1.f;
ground_fixturedef.friction = 0.f;
ground_fixturedef.shape = &ground_shape;
ground_body -> CreateFixture(&ground_fixturedef);
}
int main()
{
create_balls_and_move_them();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment