First Solid Body

Learn how to create your first solid body

Source Code

Create your First Body

The code below shows you how to create a physical world with a box inside. You can follow the steps below in order to reproduce this code. You don’t have to understand everything at this moment, this is just a little demo, you will learn more about body creation later.

////////////////////////////////////////////////////////////
// Nero Game Engine - Box2D Tutorials
////////////////////////////////////////////////////////////
///////////////////////////HEADERS//////////////////////////
//Box2D
#include <Box2D/Box2D.h>
////////////////////////////////////////////////////////////
int main()
{
    //let's create a box
        //Step 1 : create the physical world
    //gravity
    b2Vec2 gravity(0.f, -9.8f);
    //create the physical world
    b2World physicWorld(gravity);
        //Step 2 : create an empty body
    b2BodyDef bodyDef;
    bodyDef.position      = b2Vec2(0.f, 0.f);
    bodyDef.angle         = 0.f;
    bodyDef.allowSleep    = true;
    bodyDef.fixedRotation = false;
    bodyDef.gravityScale  = 1.f;
    bodyDef.type          = b2_dynamicBody;
    b2Body* boxBody       = physicWorld.CreateBody(&bodyDef);
        //Step 3 : create a box shape
    b2PolygonShape boxShape;
    boxShape.SetAsBox(1, 1); //values are in meter not pixel
        //Step 4 : create a fixture and provide the shape to the body
    b2FixtureDef fixtureDef;
    fixtureDef.shape        = &boxShape;
    fixtureDef.isSensor     = false;
    fixtureDef.density      = 1.f;
    fixtureDef.friction     = 0.1f;
    fixtureDef.restitution  = 0.1f;
    boxBody->CreateFixture(&fixtureDef);
     //Step 5 : clean everything
    boxBody = nullptr;
    //print the world in console
    physicWorld.Dump();
    return 0;
}

Step 1 : Create the Physical World

The first step is to create an empty world, exactly how we did in the previous tutorial : Physics World. The method dump() will display what’s inside our world. You will see that after we create our first body the dump will be different.

//Box2D
#include <Box2D/Box2D.h>
////////////////////////////////////////////////////////////
int main()
{
    //gravity
    b2Vec2 gravity(0.f, -9.8f);
    //create the physical world
    b2World physicWorld(gravity);
    //We create our first body here
    //print the world in console
    physicWorld.Dump();
    return 0;
}

Step 2 : Create an Empty body

The next step is to create an Empty Body. In Box2D, an empty body is a body that does not have any shape, it’s not a box, a ball or anything. It’s just like a point located somewhere inside the physical world. While an empty body does not have any shape it holds some important properties like : body type, position, rotation, etc. You will learn more about these properties later.

//Step 2 : create an empty body
b2BodyDef bodyDef;
bodyDef.position      = b2Vec2(0.f, 0.f);
bodyDef.angle         = 0.f;
bodyDef.allowSleep    = true;
bodyDef.fixedRotation = false;
bodyDef.gravityScale  = 1.f;
bodyDef.type          = b2_dynamicBody;
b2Body* boxBody       = physicWorld.CreateBody(&bodyDef);

Step 3 : Create a Shape

Now that we have an empty body, we need to create a Shape for it. The code below shows how to create a Square shape measuring one (1) meter on each side. Unlike SFML, Box2D does not use pixels, its use dimensions like : Meter, Kilogram, Second and Radian

//Step 3 : create a box shape
b2PolygonShape boxShape;
boxShape.SetAsBox(1, 1); //values are in meter not pixel

Step 4 : Provide the Shape to the Body

We have our empty Body and a Square shape. The way we provide the Shape to the Body is by using another object called Fixture. The fixture holds the shape and others properties concerning the shape like its density, friction or restitution.

Important Note : A Body can have several shapes and fixtures, in this tutorial we are only using one.

Once the fixture is created, we call the method Dump() and see what’s displayed in the console, all information concerning our new box body should be visible now.

//Step 4 : create a fixture and provide the shape to the body
b2FixtureDef fixtureDef;
fixtureDef.shape        = &boxShape;
fixtureDef.isSensor     = false;
fixtureDef.density      = 1.f;
fixtureDef.friction     = 0.1f;
fixtureDef.restitution  = 0.1f;
boxBody->CreateFixture(&fixtureDef)

Step 5 : Clean everyting

The final step is to clean our code by nullifying all pointers. Box2D manages its own memory for all object created, so we are only concerned by the pointers we’ve created ourself.

//Step 5 : clean everything
boxBody = nullptr;