Android 2d physics engine box2d usage Simple example _android

Source: Internet
Author: User

The example in this article describes the box2d usage of the 2d physical engine under Android. Share to everyone for your reference. Specifically as follows:

When the program is running, it needs to load the jbox2d library, which can be downloaded at the following address (using the library Jbox2d-2.0.1-library-only.jar without the render part):

http://sourceforge.net/projects/jbox2d/

Package com.test;
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.app.Activity;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.view.View;
Import Android.view.Window;
Import Android.view.WindowManager;
 public class Mybox2d extends activity {private final static int RATE = 10;//screen to real world proportions 10px:1m;
 Private AABB Worldaabb;
 Create a world that manages collisions with private worlds;
 private float Timestep = 1/60;//analog frequency private int iterations = 10;//iteration, the simulation is approximately accurate, but the lower the performance of the private body body,body2,body3;
 Private MyView MyView;
  Private Handler Mhandler;
    public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); RequestwiNdowfeature (Window.feature_no_title); GetWindow (). SetFlags (Windowmanager.layoutparams. Flag_fullscreen, Windowmanager.layoutparams.
    Flag_fullscreen);
     
    Worldaabb = new AABB (); The upper and lower bounds, with the top left of the screen as the origin, if the created Rigidbody reaches the edge of the screen, it stops the analog worldAABB.lowerBound.set ( -100.0f,-100.0f); WorldAABB.upperBound.set (100.
    0f, 100.0f);
    Note that the real-world units used here are Vec2 gravity = new VEC2 (0.0f,10.0f);
    Boolean dosleep = true;  
    Worlds = New World (Worldaabb, Gravity, dosleep);//Create a worldwide Createbox (160, 470, 160, true);
    CreateBox1 (160, 160, false);
    Createcircle (160, 100, 10);
    CreateCircle1 (150, 60, 10);
    Timestep = 1.0f/60.0f;
    iterations = 10;
    MyView = new MyView (this);
    Setcontentview (MyView);
    Mhandler = new Handler ();
  Mhandler.post (update);
       Private Runnable update = new Runnable () {public void run () {World.step (timestep, iterations);//Start simulation
       VEC2 position = Body.getposition ();
VEC2 Position1 = Body2.getposition ();       VEC2 position2 = Body3.getposition ();
       Myview.x=position.x*rate;
       Myview.y=position.y*rate;
       Myview.x1=position1.x*rate;
       Myview.y1=position1.y*rate;
       Myview.x2=position2.x*rate;
        Myview.y2=position2.y*rate;
        Myview.update ();
    Mhandler.postdelayed (update, (long) timestep*1000);
  }
  }; public void Createbox (float x,float y,float half_width,float half_height,boolean isstatic) {polygondef shape = new Poly
   Gondef ();
   if (isstatic) {shape.density = 0;}
   else{shape.density = 2.0f;}
   Shape.friction = 0.8f;
   Shape.restitution = 0.3f;
   Shape.setasbox (Half_width/rate, half_height/rate);
   Bodydef bodydef = new Bodydef ();
   BodyDef.position.set (X/rate, y/rate);
   Body body1= world.createbody (bodydef);
   Body1.createshape (Shape);
  Body1.setmassfromshapes ();
   public void createcircle (float x,float y,float radius) {circledef shape = new Circledef ();
   shape.density = 7;
   Shape.friction = 0.2f; Shape.radius =Radius/rate;
   Bodydef bodydef = new Bodydef ();
   BodyDef.position.set (X/rate, y/rate);
   Body2 = World.createbody (bodydef);
   Body2.createshape (Shape);
  Body2.setmassfromshapes ();
   public void CreateCircle1 (float x,float y,float radius) {circledef shape = new Circledef ();
   shape.density = 7;
   Shape.friction = 0.2f;
   Shape.radius = radius/rate;
   Bodydef bodydef = new Bodydef ();
   BodyDef.position.set (X/rate, y/rate);
   Body3 = World.createbody (bodydef);
   Body3.createshape (Shape);
  Body3.setmassfromshapes ();  } public void CreateBox1 (float x,float y,float half_width,float half_height,boolean isstatic) {polygondef shape = new
   Polygondef ();
   if (isstatic) {shape.density = 0;}
   else{shape.density = 2.0f;}
   Shape.friction = 0.3f;
   Shape.setasbox (Half_width/rate, half_height/rate);
   Bodydef bodydef = new Bodydef ();
   BodyDef.position.set (X/rate, y/rate);
   body= world.createbody (BODYDEF);
   Body.createshape (Shape);
  Body.setmassfromshapes (); }
  Class MyView extends view{Canvas Canvas;
   public float x=160,y=150;
   public float x1=160,y1=100;
 public float x2=150,y2=60;
 Public MyView {Super (context);
  public void Drawbox (float x,float y) {Paint mpaint = new Paint ();
  Mpaint.setantialias (TRUE);
  Mpaint.setcolor (color.red);
 Canvas.drawrect (x-160, y-10, x+160, y+10, Mpaint);
  public void Drawground () {Paint mpaint = new Paint ();
  Mpaint.setantialias (TRUE);
  Mpaint.setcolor (Color.Blue);
 Canvas.drawrect (0, 460, mpaint);
  public void drawcircle (float x1,float y1) {Paint mpaint = new Paint ();
  Mpaint.setantialias (TRUE);
  Mpaint.setcolor (Color.green);
 Canvas.drawcircle (x1, y1, mpaint);
 public void Update () {postinvalidate ();
  } protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas);
  This.canvas = Canvas;
  Drawground ();
  Drawbox (x, y);
  Drawcircle (x1, y1);
 Drawcircle (x2, y2);

 }
 }
}

I hope this article will help you with your Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.