欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android下2d物理引擎Box2d用法简单实例

程序员文章站 2023-11-08 14:31:04
本文实例讲述了android下2d物理引擎box2d用法。分享给大家供大家参考。具体如下: 程序运行的时候需要加载jbox2d的库,可到以下地址下载(使用的是不带渲染部分...

本文实例讲述了android下2d物理引擎box2d用法。分享给大家供大家参考。具体如下:

程序运行的时候需要加载jbox2d的库,可到以下地址下载(使用的是不带渲染部分的库jbox2d-2.0.1-library-only.jar):

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;//屏幕到现实世界的比例 10px:1m;
 private aabb worldaabb;
 //创建 一个管理碰撞的世界
 private world world;
 private float timestep = 1/60;//模拟的的频率
 private int iterations = 10;//迭代越大,模拟约精确,但性能越低
 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();
     
    //上下界,以屏幕的左上方为 原点,如果创建的刚体到达屏幕的边缘的话,会停止模拟
    worldaabb.lowerbound.set(-100.0f,-100.0f);
    worldaabb.upperbound.set(100.0f, 100.0f);
    //注意这里使用的是现实世界的单位
    vec2 gravity = new vec2(0.0f,10.0f);
    boolean dosleep = true;
    world = new world(worldaabb, gravity, dosleep);//创建世界
    createbox(160, 470, 160, 10, true);  
    createbox1(160, 150, 160, 10, 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);//开始模拟
       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 polygondef();
   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(context context) {
  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, 320, 480, mpaint);
 }
 public void drawcircle(float x1,float y1){
  paint mpaint = new paint();
  mpaint.setantialias(true);
  mpaint.setcolor(color.green);
  canvas.drawcircle(x1, y1, 10, 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);
 }
 }
}

希望本文所述对大家的android程序设计有所帮助。