Advertisement

Accuracy linking sfml rectangles with box2d boxes?

Started by March 15, 2015 12:02 PM
0 comments, last by jHaskell 9 years, 9 months ago

Hello bros biggrin.png

I joined this forum a few weeks ago and started to learn basics about game programming.. Last night I tried to get this and do a simple experiment with SFML and Box2d. Here is some code:

Block.cpp


const int PPM = 60;


Block::Block(b2World *world, float width, float height, sf::Vector2f position, sf::Color color, float mu, bool dynamic, float e)
{
rectangle = sf::RectangleShape( sf::Vector2f(width, height) );
rectangle.setPosition(position);
rectangle.setFillColor(color);
// Define la entidad que se dibuja en la pantalla


// Definicion del cuerpo
b2BodyDef bodydef;
bodydef.position.Set(position.x / PPM , position.y/PPM );
if (dynamic)
bodydef.type = b2_dynamicBody;
// La unica parte del cuerpo es un rectangulo
b2FixtureDef fixturedef;
b2PolygonShape shape;
shape.SetAsBox(width/(2*PPM), height / (2*PPM) );
fixturedef.shape = &shape;
fixturedef.friction = mu;
fixturedef.density = 1.0f;
fixturedef.restitution = e;


// Inicializamos el cuerpo para la simulacion
body = world->CreateBody(&bodydef);
body->CreateFixture(&fixturedef);
// cout << body->GetPosition().x << endl; 
}

Block.h


class Block
{
private:
b2Body *body;
sf::RectangleShape rectangle;


public:
// Constructor
Block(b2World *world, float width, float height, sf::Vector2f position, sf::Color color, float mu, bool dynamic, float e);
// Obtiene el objeto
b2Body* GetBody();
// Obtiene la forma
sf::Shape & GetShape();
// Actualiza el estado en pantalla
void Update();


};

As you can see I tried to make an object such that When I call the constructor I create a rectangle shape and a body. There is another method to update the position of the rectangle shape with the position of the body (in box2d). In the main I have something like this


std::vector<Block> bloques;
bloques.push_back( Block(world, 900.0f, 50.0f, sf::Vector2f(0, height-50), blanco, 0.5f, false, 0) );
bloques.push_back(Block(world, 50.0f, 50.0f, sf::Vector2f(0, 0), azul, 0.5f, true, 0.5f));
bloques.push_back(Block(world, 30.0f, 25.0f, sf::Vector2f(51, 0), azul, 0.5f, true, 0.4f));
I created a ground, a 50x50 box and another 30 x 25... The boxes should fall and collide with the wall, that's all
Now... The thing I don't understand is why the second box doesn't collide with accuracy while the first one does. There is a screenshot in the files attached.
Do you have any idea of why this happens? Or where is my error? I'm pretty sure I had a problem over there...
Thank you!

Greetings

EDIT: I solved the problem, after hours of studying the case I found a solution. Give a chance to edit again a post the correct code with an explanation, so other beginners can use this code as a test!

Couple things I would recommend when working with SFML and Box2d.

First, use a View that matches the Box2D scene instead of 'PPM'. Then your SFML dimensions can all be the same as your Box2d dimensions. Cuts out all those PPM converions.

Second, Box2d shapes use origin points that are centered on the shapes themselves. SFML Sprite default origins are top left corner of the sprite. Make sure you're offsetting the Sprite's origin to align with it. The reason the first box looks like it's colliding properly with the ground box is because it has the same height, so their location offsets are the same. The smaller box has a different height, with a different location offset, and thus shows the problem. Change the size of the first box and it'll show the problem as well.

This topic is closed to new replies.

Advertisement