Full Code below, fully working SFML 2.0 test bed with Box2D. Requires Eduard Engine Debugger also (get it from the link above)
NOTE: Code is not in objects for now - instead uses a hack in 'GetUserData()' to switch drawing between a box or a circle.
Help from veeableful & FRex.
Right click to spawn a wheel, left click to spawn a box. Press Space Bar to create an explosion where you mouse is
- hold it down to create a blender!! haha
textures used are both 32x32 pixels in dimention.#include <Box2D/Box2D.h>
#include <SFML/Graphics.hpp>
#include "EEDebugDraw3.h"
#include <cstdio>
#include <iostream>
static const float SCALE = 30.f;
void CreateGround(b2World& World, float X, float Y, float angle);
void CreateBox(b2World& World, int MouseX, int MouseY);
void CreateCircle(b2World& World, int MouseX, int MouseY);
int main()
{
/** Prepare the window */
sf::RenderWindow Window(sf::VideoMode(1300, 720, 32), "SFML Physics Testing - MPEngine");
Window.setFramerateLimit(60);
EEDebugDraw3 debugdraw;
debugdraw.LinkTarget(Window);
/** Prepare the world */
b2Vec2 Gravity(0.f, 8.0f);
b2World World(Gravity, true);
//Roof.
CreateGround(World, 10, 10, 0);
CreateGround(World, 500, 10, 0);
CreateGround(World, 1000, 10, 0);
CreateGround(World, 1300, 10, 0);
//Ground
CreateGround(World, 10, 710, 0);
CreateGround(World, 500, 710, 0);
CreateGround(World, 1000, 710, 0);
CreateGround(World, 1300, 710, 0);
//Right Wall
CreateGround(World, 10, 720, 1.57079633);
CreateGround(World, 10, 400, 1.57079633);
CreateGround(World, 10, 200, 1.57079633);
//Left Wall
CreateGround(World, 1290, 720, 1.57079633);
CreateGround(World, 1290, 400, 1.57079633);
CreateGround(World, 1290, 200, 1.57079633);
//Platforms - { now it gets interesting. }
CreateGround(World, 400, 300, 0);
CreateGround(World, 900, 600, 1.0);
CreateGround(World, 10, 10, 0);
World.SetDebugDraw(&debugdraw);
/** Prepare textures */
sf::Texture GroundTexture;
sf::Texture BoxTexture;
sf::Texture Wheel;
Wheel.loadFromFile("wheel.png");
GroundTexture.loadFromFile("wall.png");
BoxTexture.loadFromFile("box.png");
bool ApplyForce = false;
while (Window.isOpen())
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
int MouseX = sf::Mouse::getPosition(Window).x;
int MouseY = sf::Mouse::getPosition(Window).y;
CreateBox(World, MouseX, MouseY);
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Right))
{
int MouseX = sf::Mouse::getPosition(Window).x;
int MouseY = sf::Mouse::getPosition(Window).y;
CreateCircle(World, MouseX, MouseY);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
ApplyForce = true;
}
else
{
ApplyForce = false;
}
World.Step(1/60.f, 8, 3);
Window.clear(sf::Color::Yellow);
int BodyCount = 0;
for (b2Body* BodyIterator = World.GetBodyList(); BodyIterator != 0; BodyIterator = BodyIterator->GetNext())
{
if(ApplyForce)
BodyIterator->ApplyForce(b2Vec2(0, 5.0f), b2Vec2(sf::Mouse::getPosition(Window).x, sf::Mouse::getPosition(Window).y));
if (BodyIterator->GetType() == b2_dynamicBody && BodyIterator->GetUserData() != "Circle" )
{
sf::Sprite Sprite;
Sprite.setTexture(BoxTexture);
Sprite.setOrigin(16.f, 16.f);
Sprite.setPosition(SCALE * BodyIterator->GetPosition().x, SCALE * BodyIterator->GetPosition().y);
Sprite.setRotation(BodyIterator->GetAngle() * 180/b2_pi);
Window.draw(Sprite);
++BodyCount;
}
else if(BodyIterator->GetUserData() == "Circle" )
{
sf::Sprite WheelSprite;
WheelSprite.setTexture(Wheel);
WheelSprite.setOrigin( WheelSprite.getGlobalBounds().width / 2,WheelSprite.getGlobalBounds().height / 2 );
WheelSprite.setPosition(BodyIterator->GetPosition().x * SCALE, BodyIterator->GetPosition().y * SCALE);
WheelSprite.setRotation(BodyIterator->GetAngle() * 180/b2_pi);
Window.draw(WheelSprite);
++BodyCount;
}
else
{
sf::Sprite GroundSprite;
GroundSprite.setTexture(GroundTexture);
GroundSprite.setOrigin( GroundSprite.getLocalBounds().width / 2,GroundSprite.getLocalBounds().height / 2 );
GroundSprite.setPosition(BodyIterator->GetPosition().x * SCALE, BodyIterator->GetPosition().y * SCALE);
GroundSprite.setRotation(BodyIterator->GetAngle() * 180/b2_pi);
Window.draw(GroundSprite);
}
}
//World.DrawDebugData(); //Uncomment for debug drawing
Window.display();
}
return 0;
}
void CreateCircle(b2World& World, int MouseX, int MouseY)
{
b2BodyDef BodyDef;
BodyDef.type = b2_dynamicBody;
BodyDef.position = b2Vec2(MouseX/SCALE, MouseY/SCALE);
BodyDef.userData = "Circle"; //Naughty ;)
b2Body* Body = World.CreateBody(&BodyDef);
b2CircleShape circle;
circle.m_radius = 15.0f/SCALE;
b2FixtureDef FixtureDef;
FixtureDef.density = 2.0f;
FixtureDef.friction = 2.0f;
FixtureDef.restitution = 0.5f;
FixtureDef.shape = &circle;
Body->CreateFixture(&FixtureDef);
}
void CreateBox(b2World& World, int MouseX, int MouseY)
{
b2BodyDef BodyDef;
BodyDef.position = b2Vec2(MouseX/SCALE, MouseY/SCALE);
BodyDef.type = b2_dynamicBody;
b2Body* Body = World.CreateBody(&BodyDef);
b2PolygonShape Shape;
Shape.SetAsBox((32.f/2)/SCALE, (32.f/2)/SCALE);
b2FixtureDef FixtureDef;
FixtureDef.density = 5.f;
FixtureDef.friction = 5.0f;
FixtureDef.restitution = 0.0f;
FixtureDef.shape = &Shape;
Body->CreateFixture(&FixtureDef);
}
void CreateGround(b2World& World, float X, float Y, float angle)
{
b2BodyDef BodyDef;
BodyDef.position = b2Vec2(X/SCALE, Y/SCALE);
BodyDef.type = b2_staticBody;
BodyDef.angle = angle;
b2Body* Body = World.CreateBody(&BodyDef);
b2PolygonShape Shape;
Shape.SetAsBox((500.0f/2)/SCALE, (16.0f/2)/SCALE);
b2FixtureDef FixtureDef;
FixtureDef.density = 0.f;
FixtureDef.shape = &Shape;
Body->CreateFixture(&FixtureDef);
}