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 -> https://dl-ssl.google.com/android/eclipse/ 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!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
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) {
super(context);
World_W=W;
World_H=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);

worldAABB.lowerBound.set(min);
worldAABB.upperBound.set(max);

// 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);
groundBody.createShape(groundShapeDef);
// 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);
groundBody.createShape(groundShapeDef);
// 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);
groundBody.createShape(groundShapeDef);
// 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);
groundBody.createShape(groundShapeDef);
//

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

//
paint=new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.RED);
}

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;
circle.restitution=0.5f;

// Assign shape to Body
bodies[count].createShape(circle);
bodies[count].setMassFromShapes();

// Increase Counter
count += 1;
}

public void update() {

world.step(   timeStep  , iterations);
postInvalidate();
}
@Override
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.napier;

import android.app.Activity;
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;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

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

// Add 12 Balls
for (int i=0; i<12; 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) (10/mWorld.timeStep));
}
};

@Override
protected void onResume()
{
super.onResume();
}

@Override
protected void onStop()
{
super.onStop();
}
}

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

if it doesn’t work contact me!

now I am going sleep,

L.

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

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>