Monday, 19 December 2011

JBox2D Tutorial : Creating an object body

In last post we have learned how to create the JBox2D WorldNow our next step will be to create an object body and add it to the world. 

To create bodies in JBox2D world we need to first create:

  • Body definition
  • Body shape and
  • Body fixtures.


Body Definition

Body definition is created as below.

BodyDef bd = new BodyDef();
bd.position.set(50, 50);  
bd.type = BodyType.DYNAMIC;

Main properties of the body definition are

  • Position - position of the body in JBox2d world i.e. x and y coordinates.
  • Body type – type of the 
Body can be static, dynamic or kinematic.


Static Body

  • Static bodies have zero velocity.
  • Static bodies do not move under simulations.
  • Static bodies can be moved manually by user.
  • Static bodies have infinite mass.
  • Static bodies do not collide with other static and kinematic bodies.
  • Examples - Ground, Walls.


Dynamic Body –

  • Dynamic bodies move under simulation.
  • Dynamic bodies respond to the forces and they move accordingly.
  • Dynamic bodies can be moved manually.
  • Dynamic bodies always have non-zero mass. If mass is set to zero then it acquires 1Kg mass.
  • Dynamic bodies collide with dynamic, kinematic and static body types.
  • Examples – Ball, Box,


Kinematic Body –

  • Under simulation, kinematic bodies move according to its velocity.
  • Kinematic bodies do not move according to forces.
  • Kinematic bodies can be moved manually by user.
  • Kinematic bodies have infinite mass.
  • Kinematic bodies do not collide with static or kinematic bodies.
  • Kinematic body’s movement does not get affected by gravity.
  • Kinematic body’s movement does not get affected when dynamic bodies are collided with it.
  • Example – moving platforms in games.


Body Shape

Next we need to define the body shape. JBox2D support Circle shape, Polygon Shape, Edge Shape and Chain Shape. We will discuss each shape in details in future tutorials.

Following code snippet shows how to create a circle shape.

CircleShape cs = new CircleShape();
cs.m_radius = 0.5f;  

Fixtures

Fixture defines the material properties of the body. Fixtures are also used for attaching shape to the body. These material properties describe how two bodies should react when they collide with each other.

FixtureDef fd = new FixtureDef();
fd.shape = cs;
fd.density = 0.5f;
fd.friction = 0.3f;        
fd.restitution = 0.5f;

Density – This defines the heaviness of the body with respect to its area.

Friction - This defines how bodies slide when they come in contact with each other. Friction value can be set between 0 and 1. Lower value means more slippery bodies.

Restitution – This define how bouncy is the body. Restitution values can be set between 0 and 1. Here higher value means more bouncy body.

There are some more properties of fixture, will cover them in future post.

Creating body

Now final step is to create a body and add fixture to it. The bodies are created using World class.

Body body =  world.createBody(bd);
body.createFixture(fd);

The complete code snippet would look like:
//body definition
BodyDef bd = new BodyDef();
bd.position.set(50, 50);  
bd.type = BodyType.DYNAMIC;

//define shape of the body.
CircleShape cs = new CircleShape();
cs.m_radius = 0.5f;  

//define fixture of the body.
FixtureDef fd = new FixtureDef();
fd.shape = cs;
fd.density = 0.5f;
fd.friction = 0.3f;        
fd.restitution = 0.5f;

//create the body and add fixture to it
Body body =  world.createBody(bd);
body.createFixture(fd);
In next post we will learn how to simulate the JBox2D World.

<<JBox2D World                                                Simulating the JBox2D world>>

3 comments:

  1. How to attach image to this bodies???.....

    ReplyDelete
    Replies
    1. Hi Nab, I have added sample code snippet for attaching an image to the body here

      Delete
  2. Thanks for the tutorial. Please keep publishing more tutorials for Jbox2d.

    ReplyDelete

There was an error in this gadget