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>>

4 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

  3. But Dr. Itua, Traditional Herbal Practitioner in Africa, has cured HIV/Cancer which is extracted from some rare herbs. It is highly potential to cure AIDS & Cancer 100% without any residue. Dr Itua herbal medicine has already passed various blogs on how he uses his powerful herbs to heal all kinds of diseases such as. Herpes, HIV,,Cushing’s disease,Heart failure,Multiple Sclerosis,Hypertension,Colo_Rectal Cancer, Diabetes, Hepatitis,Hpv,Weak ErectionLyme Disease,Blood Cancer,Alzheimer’s disease,Bechet’s disease,Crohn’s disease,Parkinson's disease,Schizophrenia,Lung Cancer,Breast Cancer,Colo-Rectal Cancer,Blood Cancer,Prostate Cancer,siva.Fatal Familial Insomnia Factor V Leiden Mutation ,Epilepsy Dupuytren's disease,Desmoplastic small-round-cell tumor Diabetes ,Coeliac disease,Creutzfeldt–Jakob disease,Cerebral Amyloid Angiopathy, Ataxia,Arthritis,Amyotrophic Lateral Scoliosis,Fibromyalgia,Fluoroqquinolone Toxicity,Brain Cancer,Breast Cancer,Lung Cancer,Kidney Cancer,Syndrome Fibrodysplasia Ossificans ProgresS sclerosis,Seizures,Alzheimer's disease,Adrenocortical carcinoma.Asthma,Allergic diseases.Hiv_ Aids,Herpe ,Copd,Glaucoma., Cataracts,Macular degeneration,Cardiovascular disease,Lung disease.Enlarged prostate,Osteoporosis.Alzheimer's disease,

    Dementia.,Wart Remover,Cold Sore, Epilepsy, also his herbal boost immune system as well. I'm telling this because he uses his herbal medicine to cure me from hepatitis B and HIV, which I have been living on for 9 months now with no side effects. Herbal Medicine is just as good when drinking it although i have to use the restroom after drinking it which I don't really care about because i just want to get the virus out of my body, I will recommend Dr Itua to anyone sick out here to contact Dr Itua with this following information.

    Email...drituaherbalcenter@gmail.com /
    Web: www.drituaherbalcenter.com
    He might be late to respond because he is always busy with patents, but he will surely get back to you with a positive response.

    ReplyDelete