Android上使用Box2d


Box2d是一个非常优秀的开源2D物理引擎,可以模拟出非常真实的物理效果,今天我们介绍如何在Andoird下使用Box2d:

1.

Box2d原来使用c++写的,不过已经有了Java版本的:JBox2D,可以在http://sourceforge.net/projects/jbox2d/下载,一共有两个:

JBox2d-2.0.1.zip和JBox2D-2.0.1-b250-Full.jar。我的版本是2.0.1

Android上使用Box2d

2.

新建一个Android Project,如下所示:

2.

修改Jbox2dtestActivity.java文件如下:

  1. package com.test.jbox2d;  
  2.   
  3. import org.jbox2d.collision.AABB;  
  4. import org.jbox2d.collision.CircleDef;  
  5. import org.jbox2d.collision.PolygonDef;  
  6. import org.jbox2d.common.Vec2;  
  7. import org.jbox2d.dynamics.Body;  
  8. import org.jbox2d.dynamics.BodyDef;  
  9. import org.jbox2d.dynamics.World;  
  10.   
  11. import android.app.Activity;  
  12. import android.content.Context;  
  13. import android.graphics.Canvas;  
  14. import android.graphics.Color;  
  15. import android.graphics.Paint;  
  16. import android.os.Bundle;  
  17. import android.os.Handler;  
  18. import android.view.View;  
  19. import android.view.Window;  
  20. import android.view.WindowManager;  
  21.   
  22. public class Jbox2dtestActivity extends Activity  
  23. {  
  24.   private final static int RATE = 10//屏幕到现实世界的比例 10px:1m;   
  25.   private AABB worldAABB;             //创建 一个管理碰撞的世界   
  26.   private World world;  
  27.   private float timeStep = 1/60;      //模拟的的频率   
  28.   private int iterations = 10;        //迭代越大,模拟约精确,但性能越低   
  29.   private Body body,body2,body3;  
  30.   private MyView myView;  
  31.   private Handler mHandler;  
  32.     
  33.   public void onCreate(Bundle savedInstanceState)  
  34.   {  
  35.     super.onCreate(savedInstanceState);  
  36.     requestWindowFeature(Window.FEATURE_NO_TITLE);  
  37.     getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,  
  38.     WindowManager.LayoutParams. FLAG_FULLSCREEN);  
  39.   
  40.     worldAABB = new AABB();  
  41.            
  42.     //上下界,以屏幕的左上方为 原点,如果创建的刚体到达屏幕的边缘的话,会停止模拟   
  43.     worldAABB.lowerBound.set(-100.0f,-100.0f);  
  44.     worldAABB.upperBound.set(100.0f, 100.0f);//注意这里使用的是现实世界的单位   
  45.   
  46.     Vec2 gravity = new Vec2(0.0f,10.0f);  
  47.     boolean doSleep = true;  
  48.   
  49.     world = new World(worldAABB, gravity, doSleep);//创建世界   
  50.   
  51.     createBox(16047016010true);       
  52.     createBox1(16015016010false);  
  53.   
  54.     createCircle(16010010);  
  55.     createCircle1(1506010);  
  56.     timeStep = 1.0f/60.0f;  
  57.     iterations = 10;  
  58.   
  59.     myView = new MyView(this);  
  60.     setContentView(myView);  
  61.     mHandler = new Handler();  
  62.     mHandler.post(update);  
  63.   }  
  64.   
  65.   private Runnable update = new Runnable()  
  66.   {  
  67.     public void run()  
  68.     {  
  69.       world.step(timeStep, iterations);//开始模拟   
  70.       Vec2 position = body.getPosition();  
  71.       Vec2 position1 = body2.getPosition();  
  72.       Vec2 position2 = body3.getPosition();  
  73.       myView.x=position.x*RATE;  
  74.       myView.y=position.y*RATE;  
  75.   
  76.       myView.x1=position1.x*RATE;   
  77.       myView.y1=position1.y*RATE;  
  78.   
  79.       myView.x2=position2.x*RATE;  
  80.       myView.y2=position2.y*RATE;  
  81.       myView.update();  
  82.       mHandler.postDelayed(update, (long)timeStep*1000);  
  83.     }  
  84.   };  
  85.       
  86.   public void createBox(float x, float y, float half_width, float half_height, boolean isStatic)  
  87.   {   
  88.     PolygonDef shape = new PolygonDef();  
  89.     if(isStatic)  
  90.     {  
  91.       shape.density = 0;  
  92.     }  
  93.     else  
  94.     {  
  95.       shape.density = 2.0f;  
  96.     }  
  97.     shape.friction = 0.8f;  
  98.     shape.restitution = 0.3f;  
  99.     shape.setAsBox(half_width/RATE, half_height/RATE);  
  100.   
  101.     BodyDef bodyDef = new BodyDef();  
  102.     bodyDef.position.set(x/RATE, y/RATE);  
  103.     Body body1= world.createBody(bodyDef);  
  104.     body1.createShape(shape);  
  105.     body1.setMassFromShapes();  
  106.   }  
  107.       
  108.   public void createCircle(float x, float y, float radius)  
  109.   {  
  110.     CircleDef shape = new CircleDef();  
  111.     shape.density = 7;  
  112.     shape.friction = 0.2f;  
  113.     shape.radius = radius/RATE;  
  114.   
  115.     BodyDef bodyDef = new BodyDef();  
  116.     bodyDef.position.set(x/RATE, y/RATE);  
  117.     body2 = world.createBody(bodyDef);  
  118.     body2.createShape(shape);  
  119.     body2.setMassFromShapes();  
  120.   }  
  121.   
  122.   public void createCircle1(float x, float y, float radius)  
  123.   {  
  124.     CircleDef shape = new CircleDef();  
  125.     shape.density = 7;  
  126.     shape.friction = 0.2f;  
  127.     shape.radius = radius/RATE;  
  128.   
  129.     BodyDef bodyDef = new BodyDef();  
  130.     bodyDef.position.set(x/RATE, y/RATE);  
  131.     body3 = world.createBody(bodyDef);  
  132.     body3.createShape(shape);  
  133.     body3.setMassFromShapes();  
  134.   }  
  135.   
  136.   public void createBox1(float x, float y, float half_width, float half_height, boolean isStatic)  
  137.   {   
  138.     PolygonDef shape = new PolygonDef();  
  139.     if(isStatic)  
  140.     {  
  141.       shape.density = 0;  
  142.     }  
  143.     else  
  144.     {  
  145.       shape.density = 2.0f;  
  146.     }  
  147.     shape.friction = 0.3f;  
  148.     shape.setAsBox(half_width/RATE, half_height/RATE);  
  149.   
  150.     BodyDef bodyDef = new BodyDef();  
  151.     bodyDef.position.set(x/RATE, y/RATE);  
  152.     body= world.createBody(bodyDef);  
  153.     body.createShape(shape);  
  154.     body.setMassFromShapes();  
  155.   }  
  156.   
  157.   class MyView extends View  
  158.   {  
  159.     Canvas canvas;  
  160.     public float x=160,y=150;  
  161.     public float x1=160,y1=100;  
  162.     public float x2=150,y2=60;  
  163.     public MyView(Context context)  
  164.     {  
  165.       super(context);  
  166.     }  
  167.   
  168.     public void drawBox(float x, float y)  
  169.     {  
  170.       Paint mPaint = new Paint();  
  171.       mPaint.setAntiAlias(true);  
  172.       mPaint.setColor(Color.RED);  
  173.       canvas.drawRect(x-160, y-10, x+160, y+10, mPaint);  
  174.     }  
  175.   
  176.     public void drawGround()  
  177.     {  
  178.       Paint mPaint = new Paint();  
  179.       mPaint.setAntiAlias(true);  
  180.       mPaint.setColor(Color.BLUE);  
  181.       canvas.drawRect(0460320480, mPaint);  
  182.     }  
  183.   
  184.     public void drawCircle(float x1, float y1)  
  185.     {  
  186.       Paint mPaint = new Paint();  
  187.       mPaint.setAntiAlias(true);  
  188.       mPaint.setColor(Color.GREEN);  
  189.       canvas.drawCircle(x1, y1, 10, mPaint);  
  190.     }  
  191.       
  192.     public void update()  
  193.     {  
  194.       postInvalidate();  
  195.     }  
  196.       
  197.     protected void onDraw(Canvas canvas)  
  198.     {  
  199.       super.onDraw(canvas);  
  200.       this.canvas = canvas;  
  201.       drawGround();  
  202.       drawBox(x, y);  
  203.       drawCircle(x1, y1);  
  204.       drawCircle(x2, y2);  
  205.     }  
  206.   }  
  207. }  
  • 1
  • 2
  • 下一页

相关内容