Android下2d物理引擎Box2d用法簡單實(shí)例
本文實(shí)例講述了Android下2d物理引擎Box2d用法。分享給大家供大家參考。具體如下:
程序運(yùn)行的時候需要加載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;//屏幕到現(xiàn)實(shí)世界的比例 10px:1m;
private AABB worldAABB;
//創(chuàng)建 一個管理碰撞的世界
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();
//上下界,以屏幕的左上方為 原點(diǎn),如果創(chuàng)建的剛體到達(dá)屏幕的邊緣的話,會停止模擬
worldAABB.lowerBound.set(-100.0f,-100.0f);
worldAABB.upperBound.set(100.0f, 100.0f);
//注意這里使用的是現(xiàn)實(shí)世界的單位
Vec2 gravity = new Vec2(0.0f,10.0f);
boolean doSleep = true;
world = new World(worldAABB, gravity, doSleep);//創(chuàng)建世界
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程序設(shè)計(jì)有所幫助。
相關(guān)文章
解析Android開發(fā)中多點(diǎn)觸摸的實(shí)現(xiàn)方法
多點(diǎn)觸摸(MultiTouch),指的是允許計(jì)算機(jī)用戶同時通過多個手指來控制圖形界面的一種技術(shù)。與多點(diǎn)觸摸技術(shù)相對應(yīng)的就是單點(diǎn)觸摸,單點(diǎn)觸摸的設(shè)備已經(jīng)有很多年了,小尺寸的有觸摸式的手機(jī),大尺寸的最常見的就是銀行里的ATM機(jī)和排隊(duì)查詢機(jī)等等2013-05-05
Android Build Variants 為項(xiàng)目設(shè)置變種版本的方法
下面小編就為大家分享一篇Android Build Variants 為項(xiàng)目設(shè)置變種版本的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
詳解Android中Intent對象與Intent Filter過濾匹配過程
這篇文章主要介紹了Android中Intent對象與Intent Filter過濾匹配過程,感興趣的小伙伴們可以參考一下2015-12-12
TextView實(shí)現(xiàn)跑馬燈效果 就這么簡單!
TextView實(shí)現(xiàn)跑馬燈效果,就這么簡單輕松實(shí)現(xiàn),這篇文章介紹了TextView制作跑馬燈效果的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Android啟動屏實(shí)現(xiàn)左右滑動切換查看功能
這篇文章主要介紹了Android啟動屏實(shí)現(xiàn)左右滑動切換查看功能的相關(guān)資料,針對新功能屬性介紹和啟動屏進(jìn)行詳細(xì)講解,感興趣的小伙伴們可以參考一下2016-01-01
Android Content Provider詳解及示例代碼
本文主要講解Android Content Provider,這里提供相關(guān)文檔資料,并附有實(shí)現(xiàn)代碼和實(shí)現(xiàn)效果圖,有需要的小伙伴可以參考下2016-08-08
Android ViewPager制作新手導(dǎo)航頁(動態(tài)加載)
這篇文章主要為大家詳細(xì)介紹了Android ViewPager制作新手導(dǎo)航頁,了解什么是動態(tài)加載指示器,感興趣的小伙伴們可以參考一下2016-05-05

