1
General / Some problem BOX2D/SFML
« on: August 02, 2010, 08:30:47 pm »
up ?
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
b2Body * CreateBody(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 = 1.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;
}
const int ScreenWidth = 800;
const int ScreenHeight = 600;
...
sf::RenderWindow App(sf::VideoMode(ScreenWidth,ScreenHeight,32), "Main");
...
sf::Shape ground;
b2Body *groundBody = CreateBody(&World, b2_staticBody, ground, sf::Vector2f(ScreenWidth, ScreenHeight), sf::Vector2f(ScreenWidth, 10.f));
...
b2Body * CreateBodySpr(b2World *World, b2BodyType type, sf::Sprite &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.SetColor(sf::Color::Red);
Shape.SetCenter(Size);
Shape.SetPosition(Position);
return Body;
}
...
sf::Image img = sf::Image();
img.LoadFromFile("data/mario.png");
sf::Sprite mario;
mario.SetImage(img);
b2Body *marioBody = CreateBodySpr(&World, b2_dynamicBody, mario, sf::Vector2f(ScreenWidth, ScreenHeight-50), sf::Vector2f(img.GetWidth(), img.GetHeight()));
...
pol.SetAsBox((Size.x)/(PIXELS_METER), (Size.y)/(PIXELS_METER));
pol.SetAsBox((Size.x/2)/(PIXELS_METER), (Size.y/2)/(PIXELS_METER));
Shape.SetCenter(Size);
Shape.SetCenter(Size.x / 2, Size.y / 2);
World.SetContinuousPhysics(true);
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <sfml/Graphics.hpp>
#include <list>
#include <Box2D.h>
using namespace std ;
#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;
}
float RandomFloat(void) {
return (rand() / (float)RAND_MAX);
}
float RandomFloat(float Min,float Max) {
return (RandomFloat() * (Max - Min)) + Min;
}
b2Body * CreateBodySpr(b2World *World, b2BodyType type, sf::Sprite &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/2)/(PIXELS_METER), (Size.y/2)/(PIXELS_METER));
b2FixtureDef fix;
fix.shape = &pol;
fix.density = 1.0f;
fix.friction = 0.3f;
Body->CreateFixture(&fix);
Shape.SetColor(sf::Color::Red);
Shape.SetCenter(Size.x / 2, Size.y / 2);
Shape.SetPosition(Position);
return Body;
}
b2Body * CreateBody(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 = 1.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 * CreateBodyR(b2World *World, b2BodyType type, sf::Shape &Shape, const sf::Vector2f &Position, 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 = 0.3f;
Body->CreateFixture(&fix);
Shape = sf::Shape::Circle(Position, Radius, sf::Color::White);
return Body;
}
void Synchronize(b2Body *Body, sf::Drawable &Shape)
{
Shape.SetRotation(-radianToDegree(Body->GetAngle()));
Shape.SetPosition(Body->GetPosition().x*PIXELS_METER, Body->GetPosition().y*PIXELS_METER);
}
class myclass
{
public :
b2Body *physic;
sf::Shape toaff;
int number;
public :
myclass(int x, b2Body *pP, sf::Shape pAff)
{
number = x;
this->physic = pP;
this->toaff = pAff;
}
void myupd()
{
Synchronize(physic, toaff);
}
void mdraw(sf::RenderWindow *App)
{
App->Draw(toaff);
}
~myclass()
{
}
};
typedef list<myclass*> LISTSTR;
//global vars
const int ScreenWidth = 800;
const int ScreenHeight = 600;
const int Speed = 300;
sf::String Text;
typedef void (*pFunc)();
int main()
{
LISTSTR::iterator i;
LISTSTR lstObj;
sf::RenderWindow App(sf::VideoMode(ScreenWidth,ScreenHeight,32), "Main");
bool doSleep = true;
b2Vec2 gravity(0, 9.8f);
int iteration = 10;
float scale = 30;
float timestep = 1 /60;
b2World World( gravity, doSleep);
//World.SetContactListener(collisionManager);
World.SetContinuousPhysics(true);
sf::Shape ground;
b2Body *groundBody = CreateBody(&World, b2_staticBody, ground, sf::Vector2f(ScreenWidth, ScreenHeight), sf::Vector2f(ScreenWidth/2, 10.f));
sf::Image img = sf::Image();
img.LoadFromFile("data/mario.png");
sf::Sprite mario;
cout << "W : " << img.GetWidth() << "\r\n";
cout << "H : " << img.GetHeight() << "\r\n";
mario.SetImage(img);
b2Body *marioBody = CreateBodySpr(&World, b2_dynamicBody, mario, sf::Vector2f(ScreenWidth, ScreenHeight-50), sf::Vector2f(img.GetWidth(), img.GetHeight()));
int count = 0;
while(App.IsOpened())
{
if (count > 1000)
{
float RandomH = RandomFloat(10, 100);
float RandomW = RandomFloat(10, 100);
float RandomY = RandomFloat(0, ScreenHeight-RandomH);
float RandomX = RandomFloat(0, ScreenWidth- RandomW);
sf::Uint8 R = sf::Randomizer::Random(0,255);
sf::Uint8 G = sf::Randomizer::Random(0, 255);
sf::Uint8 B = sf::Randomizer::Random(0, 255);
sf::Uint8 A = sf::Randomizer::Random(10, 250);
A = 255;
float densite = RandomFloat(0, 30);
sf::Shape Shape;
b2Body *Body;
if (sf::Randomizer::Random(0, 10) > 5)
Body = CreateBody(&World, b2_dynamicBody, Shape, sf::Vector2f(RandomX, RandomY), sf::Vector2f(RandomH, RandomW));
else
Body = CreateBodyR(&World, b2_dynamicBody, Shape, sf::Vector2f(RandomX, RandomY), RandomW);
Shape.SetColor(sf::Color(R,G,B,A));
myclass *tmp = new myclass(lstObj.size(), Body, Shape );
lstObj.insert(lstObj.end(), tmp);
count = 0;
}
count++;
World.Step(App.GetFrameTime(), 10, 1);
World.ClearForces();
sf::Event event;
while(App.GetEvent(event))
{
if(event.Type == event.Closed)
App.Close();
}
App.Clear(sf::Color(100,149,237));
if (App.GetInput().IsKeyDown(sf::Key::Escape))
App.Close();
if (App.GetInput().IsKeyDown(sf::Key::Right))
marioBody->SetLinearVelocity(b2Vec2(3,marioBody->GetLinearVelocity().y));
else if (App.GetInput().IsKeyDown(sf::Key::Left))
marioBody->SetLinearVelocity(b2Vec2(-3,marioBody->GetLinearVelocity().y));
else
marioBody->SetLinearVelocity(b2Vec2(0,marioBody->GetLinearVelocity().y));
//faut trouver qq chose ici pour empecher les double saut !!!
if (App.GetInput().IsKeyDown(sf::Key::Up) && marioBody->GetLinearVelocity().y < 1 && marioBody->GetLinearVelocity().y > -1)
marioBody->ApplyLinearImpulse(b2Vec2(0, -9.0f), marioBody->GetWorldCenter());
Synchronize(groundBody, ground);
App.Draw(ground);
Synchronize(marioBody, mario);
App.Draw(mario);
for (i = lstObj.begin(); i != lstObj.end(); ++i)
{
((myclass*)*i)->myupd();
((myclass*)*i)->mdraw(&App);
}
//App.Draw(sprite);
//Window draw
App.Display();
}
return 0;
}