SFML Box2D - How to attach sprites to Box2D bodies

Started by
1 comment, last by Alian Vesuf 10 years, 2 months ago

I'm new to this world and i have some skills in SFML. Are there any good tutorials with Box2D and SFML. Since Box2D is just a physics engine i want to know how can i tie a sprite/image to Box2D body and make it dynamic for collision etc...
A simple step by step guide would be helpful. Im not trying to copy & paste things, im trying to learn the concepts here.
So i want to know how can i attach a sprite to act as a Box2D body with physics collision and all that stuff.

Im reading the manual but it actually explains only the engine itself, not specifically talking about how can we integrate sprites and images and have visible objects moving and acting on screen.

Advertisement

First of all, there is no perfect way for this.

A usual way, however, is to create a class for all objects. This object class will hold a sprite and the box2d body. You probably want to set the "userdata" pointer of the body to this object class. If you collide with another object, you can get a pointer to the object class by reading this pointer. Make sure that if the object is destroyed, both the SFML objects and box2d bodies are deleted.

Specific details as how to sync the body position with the sprite position is very implementation-specific. I'm sure you can figure out a way, I don't think there is a perfect one.

Yes i figured out actually. The logic behind this scene is this. You cannot actually attach a sprite to a box2d body. But what you can do is at every frame,

update the position of the sprite with the position of the actual body. And whatever movements the box2d body will do, so the sprite will do.
When the body stops moving so the sprite will stop moving.

So for example you have a dynamic body that falls to the ground. You specify the size of the body to match the size of your sprite, and in the Update functions
you set the position of the sprite by getting the position of the box2d body. A pseudocode for this :

sprite.setPosition(body->GetPosition().x * scaleFactor, body->getPosition().y * scaleFactor);

The sprite will move according to the box2d body movement. And if you match the size of the body with the size of the sprite
you can have objects collide at the exact spots, and thus you would have a visual representation of the box2d physics bodies.

Since box2d has different type of bodies i made structures for each one of them so i can create vectors
when making tiles, and during map reading process i push back new object of the structures according to the tile position and size.


struct BoxStatic
{
b2BodyDef BodyDef;
b2Body *Body;
b2PolygonShape Shape;
b2FixtureDef FixtureDef;
BoxStatic(float x, float y, int w, int h);
};


struct BoxDynamic
{
b2BodyDef BodyDef;
b2Body *Body;
b2PolygonShape Shape;
b2FixtureDef FixtureDef;
BoxDynamic(float x, float y, int w, int h);
};

In the map parser function


for(int i = 0; i < mapHeight; i++){
    for(j = 0; i < mapWidth; j++){
    m_collisionCoords.push_back(new BoxStatic(j * tileWidth, i * tileHeight, tileWidth, tileHeight));
    }
}

in the map show function


for(int i = 0; i < mapHeight; i++){
   for(j = 0; i < mapWidth; j++){
      sprite->setPosition(m_collisionCoords[j]->Body->GetPosition().x * scaleFactor, m_collisionCoords[i]-Body->GetPosition().y * sca      leFactor);
  }
}

So the logic is to attach sprite position to the box2d body position. And when making tiles you read all coordinates from the
file, push back new struct object for each tile you read from file with size and position to match the tile.
Hope this helps somebody to grasp the logic behind making sfml sprites behave based on box2d physics bodies.

This topic is closed to new replies.

Advertisement