Thanks for answering, but I think I do it that way and I have problems, well I have to change something. I have to put the center of the SFML shape to the center (to rotate correctly) and the positioning of the shape changes to the center like you said, in the box2D style. Maybe I'm not correctly.
Well I am playing with a Demo to understand the physics so here is the code (is a demo and isn't well designed, sorry):
Piece.h
#ifndef PIECE_H
#define PIECE_H
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Box2D/Box2D.h"
#include "DemoWorld.h"
class Piece
{
public:
//Piece();
Piece(int pX, int pY, int sX, int sY,float rot, float fric, float rest, float dens, sf::Color c, bool dynamic);
virtual ~Piece();
void setPostion(int x, int y);
void updatePos();
sf::Shape getShape();
b2Body *getBody();
void poorDebugging();
private:
b2Body *body;
sf::Shape shape;
float sizeX, sizeY, posX, posY, rotAngle;
};
#endif // PIECE_H
Piece.cpp
#include "Piece.h"
//DemoWorld::SCALE
/*Piece::Piece()
{}*/
Piece::~Piece()
{
//dtor
}
Piece::Piece(int pX, int pY, int sX, int sY,float rot, float fric, float rest, float dens, sf::Color c, bool dynamic)
{
float32 angle, positionX, positionY, sizePieceX, sizePieceY;
//positionX = (pX/DemoWorld::SCALE);
//positionY = (pY/DemoWorld::SCALE);
////////////////problem??/////////////////////////////////////
positionX = ((pX+sX/2)/DemoWorld::SCALE);
positionY = ((pY+sY/2)/DemoWorld::SCALE);
/////////////////////////////////////////////////////////////
angle = rot * b2_pi / 180.0;
sizePieceX = (sX/2)/DemoWorld::SCALE;
sizePieceY = (sY/2)/DemoWorld::SCALE;
///physics
//create body definition (definition of the body, position and those things)
b2BodyDef pieceBodyDef = b2BodyDef();
if (dynamic)
pieceBodyDef.type = b2_dynamicBody; //make the forces affect to the object?
pieceBodyDef.angle = angle;
pieceBodyDef.position.Set(positionX,positionY);
//create the body;
b2Body *tempBody = DemoWorld::WORLD->CreateBody(&pieceBodyDef);
//create the shape (create the Shape with a size) for the fixture
b2PolygonShape pieceShape = b2PolygonShape();
pieceShape.SetAsBox(sizePieceX, sizePieceY);
//create fixture definition (denition of the material more or less)
b2FixtureDef pieceFixtureDef = b2FixtureDef();
pieceFixtureDef.friction = fric;
pieceFixtureDef.restitution = rest;
pieceFixtureDef.density = dens;
pieceFixtureDef.shape = &pieceShape;
//create fixture
tempBody->CreateFixture(&pieceFixtureDef);
///graphics
sf::Shape s = sf::Shape::Rectangle(0, 0, sX, sY, c);
//s.SetPosition(pX,pY);
////////////////problem??/////////////////////////////////////
s.SetPosition(pX+sX/2,pY+sY/2);
/////////////////////////////////////////////////////////////
s.SetRotation(rot);
s.SetCenter(sX/2,sY/2);
///update object atributes
body = tempBody;
shape = s;
posX = pX;
posY = pY;
sizeX = sX;
sizeY = sY;
rotAngle = angle;
}
void Piece::setPostion(int x, int y)
{
}
void Piece::updatePos()
{
/*float32 x = (body->GetPosition().x*DemoWorld::SCALE);
float32 y = (body->GetPosition().y*DemoWorld::SCALE);*/
////////////////problem??/////////////////////////////////////
float32 x = (body->GetPosition().x*DemoWorld::SCALE)+(sizeX/2);
float32 y = (body->GetPosition().y*DemoWorld::SCALE)+(sizeY/2);
//////////////////////////////////////////////////////////////
float32 r = -(body->GetAngle()*(180/b2_pi));
shape.SetPosition(x,y);
shape.SetRotation(r);
posX = x;
posY = y;
rotAngle = r;
}
sf::Shape Piece::getShape()
{
return this->shape;
}
b2Body *Piece::getBody()
{
return this->body;
}
void Piece::poorDebugging()
{
std::cout << "Box2D: [" << this->getBody()->GetPosition().x << "," << this->getBody()->GetPosition().y << "]\n";
std::cout << "SFML : [" << this->getShape().GetPosition().x << "," << this->getShape().GetPosition().y << "]\n";
std::cout << "---------------------------------\n";
}
DemoWorld.cpp
#include <SFML/Window.hpp>
#include "DemoWorld.h"
#include "Piece.h"
b2World* DemoWorld::WORLD; //the static has to be like a global variable
sf::RenderWindow* DemoWorld::WINDOW;
DemoWorld::DemoWorld()
{
///world
b2Vec2 gravity = b2Vec2(0.0f, 10.0f);
//Define sleeping objects
bool ignoreSleeping = true;
//create the world
b2World *w = new b2World(gravity, ignoreSleeping);
WORLD = w;
///window
sf::RenderWindow *winApp = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "Box2D & SFML demo V2");
winApp->SetFramerateLimit(60);
WINDOW = winApp;
}
DemoWorld::~DemoWorld()
{
//dtor
}
void DemoWorld::run()
{
Piece floor = Piece(0, 580, 800, 20, 0.0, 0.8, 0.3, 0.7, sf::Color::White, false);
Piece box = Piece(340, 0, 50, 100, 30, 0.8, 0.7, 0.7, sf::Color::Red, true);
Piece box2 = Piece(250, 100, 300, 100, 0.0, 0.8, 0.9, 0.7, sf::Color::Blue, true);
Piece box3 = Piece(0, 0, 100, 100, 0.0, 0.8, 0.7, 0.7, sf::Color::Green, true);
std::cout << "bodies: " << WORLD->GetBodyCount() << "\n";
while ( WINDOW->IsOpened())
{
WORLD->Step(1.0f/60.0f,10,10);
// Process events
sf::Event Event;
while (WINDOW->GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
WINDOW->Close();
}
//Physics
WORLD->ClearForces();
//the floor isn't updated
box.updatePos();
box2.updatePos();
box3.updatePos();
box.poorDebugging();
// Display window contents on screen
WINDOW->Clear();
WINDOW->Draw(floor.getShape());
WINDOW->Draw(box.getShape());
WINDOW->Draw(box2.getShape());
WINDOW->Draw(box3.getShape());
WINDOW->Display();
}
}
That's the code as I said before is a demo.
EDIT: I had a problem in one variable(I have been changing the positioning form so many times that I wrote one variable for updating Y with an X position xD) but still doesn't work