Created
February 1, 2014 15:24
-
-
Save muhammadyaseen/8753692 to your computer and use it in GitHub Desktop.
Code for thread on SFML forums
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//main.cpp file | |
#include <iostream> | |
#include <Box2D/Box2D.h> | |
#include <SFML/Graphics.hpp> | |
#include <ctime> | |
#include <string> | |
#include <stdio.h> | |
#include <vector> | |
#include "SFMLDebugDraw.h" | |
const float PPM = 30.0f; | |
// 30 pixels is 1 meter | |
using namespace std; | |
using namespace sf; | |
int main() | |
{ | |
float timeStep = 1 / 60.0f; | |
// dimensions of the drawn SFML body | |
float width = 160.f; | |
float height = 40.f; | |
sf::RenderWindow app(sf::VideoMode(800,600),"Application"); | |
b2Vec2 gravity(0.0f, 10.f); // Y-axis / coordinate increases downward | |
b2World world(gravity); | |
world.SetAllowSleeping(true); | |
//start object setup | |
//SFML object | |
sf::RectangleShape rect( Vector2f( width, height ) ); | |
rect.setFillColor( Color( 255, 0 , 133) ); | |
//rect.setPosition( width, height); | |
rect.setOrigin( width / 2 , height / 2 ); | |
//Box2D object | |
b2BodyDef rectBodyDef; | |
rectBodyDef.type = b2_staticBody; | |
rectBodyDef.position.Set(width/PPM, height/PPM); | |
b2PolygonShape bx2dRectShape; | |
bx2dRectShape.SetAsBox( ( width / 2) / PPM, ( height / 2) / PPM , b2Vec2(0,0) , 0); | |
cout << ( width / 2) << " " << ( height / 2) << endl; | |
b2FixtureDef rectBodyFix; | |
rectBodyFix.density = 2.f; | |
rectBodyFix.shape = &bx2dRectShape; | |
b2Body* bx2dRectBody = world.CreateBody( &rectBodyDef ); | |
bx2dRectBody->CreateFixture( &rectBodyFix ); | |
//end object setup | |
rect.setPosition( bx2dRectBody->GetPosition().x * PPM , bx2dRectBody->GetPosition().y * PPM ); | |
SFMLDebugDraw debugDraw(app); | |
debugDraw.SetFlags(b2Draw::e_shapeBit); | |
world.SetDebugDraw( &debugDraw ); | |
sf::Clock fpsClock; | |
while(app.isOpen()) | |
{ | |
sf::Event event; | |
while (app.pollEvent(event)) | |
{ | |
if (event.type == sf::Event::Closed) | |
app.close(); | |
} | |
if ( fpsClock.getElapsedTime().asMilliseconds() >= 16 ) //16 ms = 1/60 or 60 FPS | |
{ | |
world.Step(timeStep,10,10); | |
} | |
app.clear(); | |
app.draw(rect); | |
world.DrawDebugData(); | |
app.display(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment