Android v 2.2 – Set up and 12 balls.

After over 48 hours with 4hours of sleep, 18 hours of work, 2hours of catching up with university, 0.5hour of set up eclipse on pc – with android SKD and configuration, 2hours of searching and 1hour of actual codding I come up with 12 balls that bounce on my phone – at the moment only from top of display to the bottom but I think that – considering my mental condition – I have got around 12 coffees and 2 relentless.. yeah my hands are shaking – anyway. I have 12 balls.

I ll describe step by step what to do to make life as easy as possible and simple as it should be – I am really tired of searching net for simple things.

Step 1 – Prepare your environment!

Note: Use most recent versions of software available

  • Install Eclipse IDE for Java Developers (~99 MB)
  • You will need as well Java SDK and Android SDK
  • Set up ADT plugin for Eclipse – great how to on this site – quick overview
  • go to help -> Install New Software -> add -> in location, name – up to you.
  • install packages you may need – I have selected all of them to make sure I will have everything 🙂
  • restart eclipse
  • now go to window -> preferences -> tab Android -> find location of your Android SDK – root level. if list of “Androids” will appear below –  we are home if not – something went wrong. Simple
  • time to create virtual android based device. Between printer and debug mode we can see android icon
    “Android SDK and AVD Manager”  – click it – then new and fill fields..   target (I am currently developing apps for Android 2.2 – this is next thing I hate about internet in google we can find old posts that are not up to date and instead of showing you in the title what version author is using you need to spend some time to figure out that your version is not compatible with his – pardon – authors version is not UP TO freaking DATE ) – anyway – SD Card – 50 MB is enough. Create AVD
  • now.. lunch it – you can go and make a tea or coffee – it will take a while – or it is just my PC 🙂 – honestly this is slow
  • Woooopiii we have mobile device with android on our not so mobile desktop  – you are now owner

Step 2 – Write it!

Note: This is really good video about making your UI fast and efficient, enjoy!

In this example I have used jBox2d-2.0.1-full.jar.

  • lets start new android project – yeah that’s cool.
  • inside project add folder eg libs
  • import jBox2D-2.0.1-full.jar into it
  • go to your project properties -> Java Buld Path -> Libraries tab -> Add JAR’s
  • chose jBox library. OK

Now we are able to use cool stuff from jBox2D library.

Here is a code for my PhysicsWorld (I took code from one of sites during research – I haven’t past and copy, I have retyped it!)

package com.napier;

import java.util.Random;

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.content.Context;
import android.view.View;

public class PhysicsWorld extends View{

protected static final int GUIUPDATEIDENTIFIER = 0x231;

public int targetFPS = 40;
public float timeStep = 10.0f / targetFPS;
public int iterations = 5;

private Body[] bodies;
private int count = 0;

private AABB worldAABB;
public World world;
private PolygonDef groundShapeDef;

public int World_W,World_H;

private Paint paint;

private float radius=10;

public PhysicsWorld(Context context,int W,int H) {
// Step 1: Create Physics World Boundaries
worldAABB = new AABB();

Vec2 min = new Vec2(-50, -50);
Vec2 max = new Vec2(World_W + 50, World_H + 50);


// 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 :
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(new Vec2((float) 0.0, (float) -10.0));
Body groundBody = world.createBody(bodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox((float) World_W, (float) 10);
// up :
bodyDef = new BodyDef();
bodyDef.position.set(new Vec2((float) 0.0, (float) (World_H+10.0) ));
groundBody = world.createBody(bodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox((float) World_W, (float) 10);
// left :
bodyDef = new BodyDef();
bodyDef.position.set(new Vec2((float) -10, (float) 0.0 ));
groundBody = world.createBody(bodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox((float)10, (float) World_H);
// right :
bodyDef = new BodyDef();
bodyDef.position.set(new Vec2((float) World_W+10, (float) 0.0 ));
groundBody = world.createBody(bodyDef);
groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox((float)10, (float) World_H);

// step 4: initialize
bodies=new Body[50];

paint=new Paint();

public void addBall() {
// Create Dynamic Body
BodyDef bodyDef = new BodyDef();
Random rnd = new Random();
bodyDef.position.set((float) radius*2+rnd.nextInt( (int)(World_W-radius*4) ), (float)2*radius+ rnd.nextInt( (int)(World_H-radius*4) ));
bodies[count] = world.createBody(bodyDef);

// Create Shape with Properties
CircleDef circle = new CircleDef();
circle.radius = (float) radius;
circle.density = (float) 1.0;

// Assign shape to Body

// Increase Counter
count += 1;

public void update() {

world.step(   timeStep  , iterations);
protected void onDraw(Canvas canvas) {
// draw balls
for(int j = 0;j
canvas.drawCircle(bodies[j].getPosition().x,World_H- bodies[j].getPosition().y, radius, paint);


I can see couple things that could be done better – eg instead of array we could use ArrayList – I ll update post later on – I am really tired now.

The idea of this class is to create world in 2D and creates some balls in this world.. simple

now my main class

package com.napier;

import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;

public class Engine extends Activity   {
PhysicsWorld mWorld;
private Handler mHandler;

public void onCreate(Bundle savedInstanceState) {

DisplayMetrics dm = new DisplayMetrics();

mWorld = new PhysicsWorld(this,dm.widthPixels,dm.heightPixels);

// Add 12 Balls
for (int i=0; i<12; i++) {

// Start Regular Update
mHandler = new Handler();;

protected void onPause() {

private Runnable update = new Runnable() {
public void run() {
mHandler.postDelayed(update, (long) (10/mWorld.timeStep));

protected void onResume()

protected void onStop()

simple indeed.. but I need to agree – I had a loads of the problems with it.. unexpected crash – debugging is a nightmare!

Published by

Luke Iwanski

Senior Graphics Programmer @ CD Projekt RED

3 thoughts on “Android v 2.2 – Set up and 12 balls.”

    1. Hey Manish,

      I don’t have this project anymore – it have been almost two years now.
      and, I don’t really understand what do you have a problem with,

      can you write more about the problem? – maybe I will be able to help.

Leave a Reply

Your email address will not be published. Required fields are marked *