How to display the balls?

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

How to display the balls?

Postby wonshikee » Mon May 23, 2011 5:57 am

Trying to learn how to do android dev, using various examples (some from this board and some from elsewhere), I'm trying to use the JBox2D to basically spawn balls wherever I click. The original example simple generated them in onCreate(), I'm trying to extend this example to instead generate them when I click anywhere on the screen.

The balls seem to be created, however they do not display on the screen.

Box2D.java
Code: Select all
package com.kenny.box2d;

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 Box2D 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);
      // Start Regular Update
      mHandler = new Handler();
      mHandler.post(update);
   }

   @Override
   protected void onPause() {
      super.onPause();
      mHandler.removeCallbacks(update);

   }

   private Runnable update = new Runnable() {

      @Override
      public void run() {
         mWorld.update();
         mHandler.postDelayed(update, (long) (10 / mWorld.timeStep));
      }
   };
}


PhysicsWorld.java
Code: Select all
package com.kenny.box2d;

import org.jbox2d.collision.AABB;
import org.jbox2d.collision.shapes.CircleDef;
import org.jbox2d.collision.shapes.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.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class PhysicsWorld extends SurfaceView implements SurfaceHolder.Callback {
   
   private static final String TAG = PhysicsWorld.class.getSimpleName();
   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);
      
      getHolder().addCallback(this);
      
      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(World_W, 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(World_W, 10);
      groundBody.createShape(groundShapeDef);

      // left :
      bodyDef = new BodyDef();
      bodyDef.position.set(new Vec2(-10, (float) 0.0));
      groundBody = world.createBody(bodyDef);
      groundShapeDef = new PolygonDef();
      groundShapeDef.setAsBox(10, 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(10, 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(int x, int y) {
      // Create Dynamic Body
      BodyDef bodyDef = new BodyDef();
      // Random rnd = new Random();
      bodyDef.position.set(x, y);
      Log.d(TAG,"Ball Created At: " + Integer.toString(x) + "," + Integer.toString(y));
      bodies[count] = world.createBody(bodyDef);
      // Create Shape with Properties
      CircleDef circle = new CircleDef();
      circle.radius = 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 < count; j++) {
         canvas.drawCircle(bodies[j].getPosition().x, World_H - bodies[j].getPosition().y, radius, paint);
      }
   }
   
   @Override
   public boolean onTouchEvent(MotionEvent event) {
      switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
         addBall((int)event.getX(),(int)event.getY());
      }
      return true;
   }

   @Override
   public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
      // TODO Auto-generated method stub

   }

   @Override
   public void surfaceCreated(SurfaceHolder holder) {
      // TODO Auto-generated method stub
      Log.d(TAG,"Surface Created");

   }

   @Override
   public void surfaceDestroyed(SurfaceHolder holder) {
      // TODO Auto-generated method stub

   }

}


Thank you for any assistance.
wonshikee
Once Poster
Once Poster
 
Posts: 1
Joined: Mon May 23, 2011 5:52 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests