Here is a brief tutorial on how to use the popular 2D physics engine Box2D in an Android Activity.
This is only part 1, including the import of the libraries, creation of the world, adding a few dynamic objects and updating the world. Not included is the drawing part of things, which is subject to a later post, or a post by someone else

- Download jbox2d-2.0.1-library-only.jar and save it anywhere as jbox2d.jar
- In Eclipse:
- Create a folder /lib/
- Right Click on /lib, select Import
- Select General / File System
- Choose the directory of jbox2d.jar
- Import the file jbox2d.jar
- Right click on the project and select "Properties"
- Click on "Java Build Path" and select the "Libraries" tab
- Click on "Add JARs..." and pick the file jbox2d.jar
- Initialize a new world
- Add a ground box
- Create some dynamic bodies (balls)
- Simulate the world
Using java Syntax Highlighting
- import org.jbox2d.collision.AABB;
- import org.jbox2d.collision.CircleDef;
- import org.jbox2d.collision.PolygonDef;
- import org.jbox2d.common.Vec2;
- import org.jbox2d.dynamics.Body;
- import org.jbox2d.dynamics.BodyDef;
- import org.jbox2d.dynamics.World;
- import android.util.Log;
- public class PhysicsWorld {
- public int targetFPS = 40;
- public int timeStep = (1000 / targetFPS);
- public int iterations = 5;
- private Body[] bodies = new Body[100];
- private int count = 0;
- private AABB worldAABB;
- private World world;
- private BodyDef groundBodyDef;
- private PolygonDef groundShapeDef;
- public void create() {
- // Step 1: Create Physics World Boundaries
- worldAABB = new AABB();
- worldAABB.lowerBound.set(new Vec2((float) -100.0, (float) -100.0));
- worldAABB.upperBound.set(new Vec2((float) 100.0, (float) 100.0));
- // Step 2: Create Physics World with Gravity
- Vec2 gravity = new Vec2((float) 0.0, (float) -10.0);
- boolean doSleep = true;
- world = new World(worldAABB, gravity, doSleep);
- // Step 3: Create Ground Box
- groundBodyDef = new BodyDef();
- groundBodyDef.position.set(new Vec2((float) 0.0, (float) -10.0));
- Body groundBody = world.createBody(groundBodyDef);
- groundShapeDef = new PolygonDef();
- groundShapeDef.setAsBox((float) 50.0, (float) 10.0);
- groundBody.createShape(groundShapeDef);
- }
- public void addBall() {
- // Create Dynamic Body
- BodyDef bodyDef = new BodyDef();
- bodyDef.position.set((float) 6.0+count, (float) 24.0);
- bodies[count] = world.createBody(bodyDef);
- // Create Shape with Properties
- CircleDef circle = new CircleDef();
- circle.radius = (float) 1.8;
- circle.density = (float) 1.0;
- // Assign shape to Body
- bodies[count].createShape(circle);
- bodies[count].setMassFromShapes();
- // Increase Counter
- count += 1;
- }
- public void update() {
- // Update Physics World
- world.step(timeStep, iterations);
- // Print info of latest body
- if (count > 0) {
- Vec2 position = bodies[count].getPosition();
- float angle = bodies[count].getAngle();
- Log.v("Physics Test", "Pos: (" + position.x + ", " + position.y + "), Angle: " + angle);
- }
- }
- }
Parsed in 0.015 seconds, using GeSHi 1.0.8.4
To use this in an Activity, you may write a code like this:
Using java Syntax Highlighting
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- public class Physics extends Activity {
- PhysicsWorld mWorld;
- private Handler mHandler;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- mWorld = new PhysicsWorld();
- mWorld.create();
- // Add 50 Balls
- for (int i=0; i<50; i++) {
- mWorld.addBall();
- }
- // Start Regular Update
- mHandler = new Handler();
- mHandler.post(update));
- }
- @Override
- protected void onPause() {
- super.onPause();
- mHandler.removeCallbacks(update);
- }
- private Runnable update = new Runnable() {
- public void run() {
- mWorld.update();
- mHandler.postDelayed(update, (long) (mWorld.timeStep*1000));
- }
- };
- }
Parsed in 0.011 seconds, using GeSHi 1.0.8.4
This is a short version of the full post at 4feets.com. Further suggested reading:
- Box2D User Manual
- Box2D Class Reference
- JBox2D Wiki (explains the differences from Box2D and JBox2D)
- Physics Engine Comparison, 2
- Speed Tests
- Chris
[edit]add: bodies=new Body[100];[/edit]