Box2d in LibGdX

Box2D is a free open source 2-dimensional physics simulator engine written in C++ by Erin Catto and published under the zlib license. It has been used in Crayon Physics Deluxe, Limbo, Rolando, Fantastic Contraption, Incredibots, Angry Birds, Tiny Wings, Transformice, Happy Wheels and many online Flash games, as well as iPhone, iPad and Android games using the Cocos2d or Moscrif game engine and Corona framework.

Box2D performs constrained rigid body simulation. It can simulate bodies composed of convex polygons, circles, and edge shapes. Bodies are joined together with joints and acted upon by forces. The engine also applies gravity, friction, and restitution.In a simple Game when we use box2D we create a Box2d World in which we can have number of bodies with different properties and different type.These bodies can be joined to each other with the help of various joints that we discuss further.

NOTE:It is highly recommended that while using Box2d , the coordinate should be taken in meters instead of pixel because Box2d measure its bodies and distance covered by bodies in meters.

Let, first see how to make the Box2d World,but for this we need the gravity vector:

Vector2 gravity = new Vector2(0, -10);
and
World worldbox = new World(gravity, true)

and also in the render of our class we have to render step through all the bodies in our box2d world

@Override
	public void render(float delta) {
		// TODO Auto-generated method stub
                worldbox.step(Gdx.graphics.getDeltaTime(), 8, 2);
		update();
		draw();

	}

Now we have a World in which we can add bodies.So, Let see what a body is.

Bodies are the fundamental objects in the physics scene.A body have:

mass – how heavy it is
velocity – how fast and which direction it’s moving
rotational inertia – how much effort it takes to start or stop spinning
angular velocity – how fast and which way it’s rotating
location – where it is
angle – which way it is facing

By this we had just added the properties of the body, but we still don’t know how it look like and what is its shape. For that we add fixtures to the Body which give it a shape.A body can hold number of fixtures.

Bodies are of 3 types:

1.Dynamic: Bodies which can move.

2. Static: Bodies which can’t move and have infinite mass and density.

3. Kinematic: These are static bodies which can also move.

So lets create a Body for which we have a method in which we can pass different properties and position of the Body which as follow:

private Body createRectangleBodyPart(float x, float y, float width,
			float height, float angle, int mass, int density, int type,
			int groupIndex) {
		PolygonShape shape = new PolygonShape();
		shape.setAsBox(width, height);
		shape.setAsBox(width / 2, height / 2, new Vector2(0, 0),
				(float) Math.toRadians(angle));
		MassData massData = new MassData();
		massData.mass = mass;

		BodyDef bodyDef = new BodyDef();
		if (type == 0)
			bodyDef.type = BodyType.DynamicBody;
		if (type == 1)
			bodyDef.type = BodyType.StaticBody;
		if (type == 2)
			bodyDef.type = BodyType.KinematicBody;
		bodyDef.position.y = y;
		bodyDef.position.x = x;

		Body body = worldbox.createBody(bodyDef);
		body.setMassData(massData);

		FixtureDef fixtureDef = new FixtureDef();
		fixtureDef.shape = shape;
		fixtureDef.density = density;
		fixtureDef.filter.groupIndex = (short) groupIndex;
		body.createFixture(fixtureDef);
		shape.dispose();
		return body;
	}

In this method, first we are creating a shape i.e. PolygonShape and setting it as box and adding the mass into massData. Then we have a Body definition which will see the type of body and its position in the worldBox. After that we created a body and and add it to the worldBox with the BodyDefinition. Now, we made a FixtureDefinition as like BodyDefinition and add certain properties into it as we get into the method.And at last we returned the body.

The code for calling this method will be:

Body = createRectangleBodyPart(5, 5,
				WIDTH,
				HEIGHT 0, 1, 1, 0, -1);

Body.setUserData("body");

by which we will have a rectangular Body in WorldBox at 5,5.To see the body and force applying on it we have debugRender in Box2d.

Box2DDebugRenderer debugRender;

and initialize it in the constructor

debugRender = new Box2DDebugRenderer(true, true, true, true, true);

and render this in the render method

debugRender.render(worldbox, WorldRenderer.cam.combined);

now when we run this then our body will fall downside because we have gravity of -10 applied on the worldBox.

So, in this way we can make any number of bodies of various types.

Thanks.

 

Post By:- Jagdeep