JBox2D学习 - 小球碰撞实例 .

 

 

使用引擎版本:jbox2d-library-2.1.2.jar

封装好的JBOX2D引擎,专门适合android平台使用

点击下载

注意:下载这个封装好的不需要slfj库支持,效率更高

之前在jbox2d 官网下载的2.1.2一直跑不起来,发现jar包有问题,后来在官网发帖子,jbox2d作者非常耐心的解答了我的问题,终于用上2.1.2的jbox2d了哇,

非常感谢老外的开源精神微笑,做出这么好的物理引擎,完全免费

jbox2d官网:http://code.google.com/p/jbox2d/

 

现在奉上我学习jbox2d做的demo,这个demo类似与testbed 里的 varying restitution

baseview.java:

 

[java] view plaincopyprint?

  1. package com.tszy.testjbox2d212;  
  2.   
  3. import org.jbox2d.collision.shapes.CircleShape;  
  4. import org.jbox2d.collision.shapes.PolygonShape;  
  5. import org.jbox2d.common.Vec2;  
  6. import org.jbox2d.dynamics.Body;  
  7. import org.jbox2d.dynamics.BodyDef;  
  8. import org.jbox2d.dynamics.BodyType;  
  9. import org.jbox2d.dynamics.FixtureDef;  
  10. import org.jbox2d.dynamics.World;  
  11. import org.jbox2d.dynamics.joints.MouseJoint;  
  12.   
  13. import android.content.Context;  
  14. import android.graphics.Canvas;  
  15. import android.graphics.Matrix;  
  16. import android.graphics.Paint;  
  17. import android.graphics.Paint.Style;  
  18. import android.view.View;  
  19.   
  20.   
  21. public class BaseView extends View implements Runnable{  
  22.     public static final float RATE = 30.0f;     //比例尺  
  23.     public static final int DEF_FPS = 30;   //默认刷新频率  
  24.        
  25.     private Thread thread;  
  26.       
  27.     protected World world;  
  28.     protected Paint paint;  
  29.     protected Matrix matrix;  
  30.     protected BodyDef bd;  
  31.     protected MouseJoint mouseJoint;  
  32.       
  33.     public boolean a = true;  
  34.     public Body m_groundBody;  
  35.       
  36.       
  37.     public BaseView(Context context) {  
  38.         super(context);  
  39.         // TODO Auto-generated constructor stub  
  40.         bd = new BodyDef();  
  41.           
  42.         paint = new Paint(Paint.ANTI_ALIAS_FLAG);// 无锯齿  
  43.         paint.setStyle(Style.FILL);  
  44.         matrix = new Matrix();  
  45.           
  46.           
  47.         Vec2 gravity = new Vec2(0, 10f);  
  48.         world = new World(gravity, true);  
  49.           
  50.         BodyDef bodyDef = new BodyDef();  
  51.         m_groundBody = world.createBody(bodyDef);  
  52.           
  53.         thread = new Thread(this);  
  54.         thread.start();  
  55.     }  
  56.   
  57.       
  58.     /** 
  59.      * 创建矩形 
  60.      * @param world 所在世界 
  61.      * @param x, y 中心点位置 
  62.      * @param w, h 尺寸 
  63.      * @param friction 摩擦力 
  64.      * @param restitution 恢复力 
  65.      * @param isStatic 静态否 
  66.      * @return 对象 
  67.      */  
  68.     public Body createPolygon(float x, float y, float w, float h,   
  69.             float friction, float restitution,  
  70.             boolean isStatic) {  
  71.         //创建多边形皮肤   
  72.         PolygonShape shape = new PolygonShape();  
  73.         shape.setAsBox(w/2/RATE, h/2/RATE);  
  74.           
  75.         FixtureDef fd = new FixtureDef();  
  76.         fd.shape = shape;  
  77.         fd.density =  1.0f; //设置密度  
  78.         fd.friction = friction;         //设置摩擦力  
  79.         fd.restitution = restitution;   //设置多边形的恢复力  
  80.           
  81.         //设置刚体初始坐标   
  82.         bd.type = isStatic? BodyType.STATIC : BodyType.DYNAMIC;  
  83.         bd.position.set(x/RATE, y/RATE);  
  84.           
  85.         //创建物体   
  86.         Body body = world.createBody(bd);   //物理世界创建物体  
  87.           
  88.         //此方法改变了   
  89.         //body.createShape(pDef);   //为body添加皮肤  
  90.         body.createFixture(fd);  
  91.           
  92.         return body;  
  93.     }  
  94.       
  95.     public Body createPolygon(float x, float y, float w, float h,   
  96.             boolean isStatic) {  
  97.         return createPolygon(x, y, w, h, 0.3f, 0.5f, isStatic);  
  98.     }  
  99.       
  100.     /** 
  101.      * 创建圆形 
  102.      * @param world 所在世界 
  103.      * @param x, y 圆心位置  
  104.      * @param r 半径 
  105.      * @param friction 摩擦力 
  106.      * @param restitution 恢复力 
  107.      * @param isStatic 静态否 
  108.      * @return 对象 
  109.      */  
  110.     public Body createCircle(float x, float y, float r,  
  111.             float friction, float restitution,  
  112.             boolean isStatic) {  
  113.         //创建圆形皮肤   
  114.         CircleShape shape = new CircleShape();  
  115.         shape.m_radius = r/RATE;  
  116.           
  117.         FixtureDef fd = new FixtureDef();  
  118.         fd.shape = shape;  
  119.         fd.density = 1.0f;  
  120.         fd.friction = friction;  
  121.         fd.restitution = restitution;  
  122.           
  123.         //创建刚体   
  124.         bd.type = isStatic? BodyType.STATIC : BodyType.DYNAMIC;  
  125.         bd.position.set((x+r)/RATE, (y+r)/RATE);  
  126.           
  127.         //创建一个body   
  128.         Body body = world.createBody(bd);  
  129.         body.createFixture(fd);  
  130.           
  131.         return body;  
  132.     }  
  133.       
  134.       
  135.     public Vec2 screen2world(Vec2 p) {  
  136.         p.x /= RATE;  
  137.         p.y /= RATE;  
  138.           
  139.         return p;  
  140.     }  
  141.       
  142.     public Vec2 world2screen(Vec2 p){  
  143.         p.x *= RATE;  
  144.         p.y *= RATE;  
  145.           
  146.         return p;  
  147.     }  
  148.       
  149.     public Vec2 screen2world2(Vec2 p) {  
  150.         return new Vec2(p.x/RATE, p.y/RATE);  
  151.     }  
  152.     public Vec2 world2screen2(Vec2 p){  
  153.         return new Vec2(p.x*RATE, p.y*RATE);  
  154.     }  
  155.       
  156.     private void drawFPS(Canvas canvas) {  
  157.         paint.setColor(0xffffffff);  
  158.         paint.setAntiAlias(true);  
  159.         if (USETIME > 0)  
  160.             canvas.drawText("FPS:" + (USETIME>0? 1000/USETIME : 33), 3,  
  161.                     3 + paint.getTextSize(), paint);  
  162.     }  
  163.       
  164.     @Override  
  165.     protected void onDraw(Canvas canvas) {  
  166.         // TODO Auto-generated method stub  
  167.         drawFPS(canvas);  
  168.         super.onDraw(canvas);  
  169.     }  
  170.       
  171.     private long USETIME;  
  172.     @Override  
  173.     public void run() {  
  174.         // TODO Auto-generated method stub  
  175.         long t1, t2;  
  176.           
  177.         while (a) {  
  178.             //模拟世界    
  179.             //速度模拟频率,位置模拟频率   
  180.             world.step(1.0f/30.0f, 3, 8);  
  181.               
  182.             t1 = System.currentTimeMillis();  
  183.             postInvalidate();  
  184.             t2 = System.currentTimeMillis();  
  185.             USETIME = t2 - t1;  
  186.   
  187.             //正常休眠   
  188.             if (USETIME < 33) {  
  189.                 try {  
  190.                     Thread.sleep(33 - USETIME);  
  191.                 } catch (InterruptedException e) {  
  192.                     // TODO Auto-generated catch block  
  193.                     e.printStackTrace();  
  194.                 }  
  195.             }  
  196.         }  
  197.     }  
  198. }  

 

View.java

 

package com.tszy.testjbox2d212;  

  1.   
  2. import org.jbox2d.callbacks.QueryCallback;  
  3. import org.jbox2d.collision.AABB;  
  4. import org.jbox2d.common.Vec2;  
  5. import org.jbox2d.dynamics.Body;  
  6. import org.jbox2d.dynamics.BodyType;  
  7. import org.jbox2d.dynamics.Fixture;  
  8. import org.jbox2d.dynamics.joints.MouseJoint;  
  9. import org.jbox2d.dynamics.joints.MouseJointDef;  
  10.   
  11. import android.content.Context;  
  12. import android.graphics.Canvas;  
  13. import android.graphics.Color;  
  14. import android.graphics.Paint;  
  15. import android.graphics.Rect;  
  16. import android.view.MotionEvent;  
  17.   
  18.   
  19. public class View6 extends BaseView {  
  20.     private static final float R = 15.0f;  
  21.       
  22.     public View6(Context context) {  
  23.         super(context);  
  24.         // TODO Auto-generated constructor stub  
  25.         create();  
  26.     }  
  27.   
  28.     private void create() {  
  29.         // 地板   
  30.         Body body = null;  
  31.           
  32.         body = createPolygon(10+460/2, 305, 460, 10, true);  
  33.   
  34.         // 球   
  35.         float restitution[] = new float[]{0.0f, 0.1f, 0.3f, 0.5f, 0.75f, 0.9f, 1.0f};  
  36.         for (int i = 0; i < restitution.length; i++) {  
  37.             body = createCircle(80 + i * 40, 40, R, 0.3f, restitution[i], false);  
  38.         }  
  39.     }  
  40.       
  41.     @Override  
  42.     protected void onDraw(Canvas canvas) {  
  43.         // TODO Auto-generated method stub  
  44.         canvas.drawColor(0xff000000);  
  45.           
  46.         //地板   
  47.         paint.setColor(0xff00ff00);  
  48.         paint.setAntiAlias(false); //消除画笔锯齿  
  49.         paint.setStyle(Paint.Style.STROKE);  
  50.         Rect r = new Rect(10, 300, 470, 310);  
  51.         canvas.drawRect(r, paint);  
  52.         r = null;  
  53.           
  54.         //球   
  55.         //得到body链表表头   
  56.         Body  body = world.getBodyList();  
  57.         for(int i=1; i<world.getBodyCount(); i++){  
  58.             if(body.getType() != BodyType.DYNAMIC){ //不是动态的物体  
  59.                 continue;  
  60.             }  
  61.             drawBall(canvas, body);  
  62.               
  63.             body = body.m_next;  
  64.         }  
  65.           
  66.         //if(mouseJoint != null)  
  67.             drawLine(canvas);  
  68.           
  69.         super.onDraw(canvas);  
  70.     }  
  71.       
  72.     private void drawLine(Canvas canvas){  
  73.         if(curBody==null || !isMoving) return;  
  74.   
  75.         paint.setAntiAlias(false); //消除画笔锯齿  
  76.         paint.setColor(Color.GREEN);  
  77.         paint.setStyle(Paint.Style.STROKE);  
  78.           
  79.         Vec2 d = world2screen2(curBody.getPosition());  
  80.         Vec2 d2 = world2screen2(p);  
  81.         canvas.drawLine(d.x, d.y, d2.x, d2.y, paint);  
  82.     }  
  83.       
  84.     private void drawBall(Canvas canvas, Body ball){  
  85.         //得到的body的质点坐标   
  86.         Vec2 p = world2screen2(ball.getPosition());  
  87.           
  88.         //canvas.save();   
  89.         //获取球的旋转角度   
  90.         float degrees = (float) ((ball.getAngle()*180/Math.PI) % 360);  
  91.           
  92.         matrix.reset();  
  93.         canvas.save();  //保存不需要绘制的区域   
  94.         if (degrees != 0)  
  95.             matrix.preRotate(degrees, p.x, p.y);    //设置矩阵旋转角度  
  96.         canvas.setMatrix(matrix);  
  97.           
  98.         //填充   
  99.         paint.setColor(0x80ffbbff);  
  100.         paint.setStyle(Paint.Style.FILL);  
  101.         canvas.drawCircle(p.x, p.y, R, paint);  
  102.           
  103.         //轮廓   
  104.         paint.setAntiAlias(true);  
  105.         paint.setColor(0xffffbbff);  
  106.         paint.setStyle(Paint.Style.STROKE);  
  107.         canvas.drawCircle(p.x, p.y, R, paint);  
  108.         //paint.setAntiAlias(false);  
  109.           
  110.         //角度   
  111.         paint.setColor(0xff00ff00);  
  112.         paint.setStyle(Paint.Style.FILL);  
  113.         canvas.drawLine(p.x, p.y, p.x, p.y-R, paint);  
  114.           
  115.         canvas.restore();  
  116.     }  
  117.       
  118.     private boolean isMoving;  
  119.     private Body curBody;  
  120.     private Vec2 p;  
  121.     private final AABB queryAABB = new AABB();  
  122.     private final TestQueryCallback callback = new TestQueryCallback();  
  123.        
  124.     @Override  
  125.     public boolean onTouchEvent(MotionEvent event) {  
  126.         // TODO Auto-generated method stub  
  127.         p = screen2world(new Vec2(event.getX(), event.getY()));  
  128.           
  129.         switch (event.getAction()) {  
  130.             case MotionEvent.ACTION_DOWN :{  
  131.                 isMoving = true;  
  132.                   
  133.                 if (mouseJoint != null) {  
  134.                     return true;  
  135.                 }  
  136.                   
  137.                 queryAABB.lowerBound.set(p.x - .001f, p.y - .001f);  
  138.                 queryAABB.upperBound.set(p.x + .001f, p.y + .001f);  
  139.                 callback.point.set(p);  
  140.                 callback.fixture = null;  
  141.                 world.queryAABB(callback, queryAABB);  
  142.                   
  143.                 if(callback.fixture != null){  
  144.                         curBody = callback.fixture.getBody();  
  145.                         MouseJointDef def = new MouseJointDef();  
  146.                         def.bodyA = m_groundBody;  
  147.                         def.bodyB = curBody;  
  148.                         def.target.set(p);  
  149.                         def.maxForce = 1000f * curBody.getMass();  
  150.                         mouseJoint = (MouseJoint) world.createJoint(def);  
  151.                         curBody.setAwake(true);  
  152.                 }  
  153.             }  
  154.               
  155.             case MotionEvent.ACTION_MOVE : {  
  156.                 if (mouseJoint != null) {  
  157.                     mouseJoint.setTarget(p);  
  158.                 }  
  159.                   
  160.                 return true;  
  161.             }  
  162.   
  163.             case MotionEvent.ACTION_UP : {  
  164.                 isMoving = false;  
  165.                   
  166.                 if (mouseJoint != null) {  
  167.                     world.destroyJoint(mouseJoint);  
  168.                     mouseJoint = null;  
  169.                 }  
  170.                   
  171.                 return true;  
  172.             }  
  173.         }  
  174.   
  175.         return super.onTouchEvent(event);  
  176.     }  
  177.       
  178.     class TestQueryCallback implements QueryCallback {  
  179.         public final Vec2 point;  
  180.         public Fixture fixture;  
  181.   
  182.         public TestQueryCallback() {  
  183.             point = new Vec2();  
  184.             fixture = null;  
  185.         }  
  186.   
  187.         /** 
  188.          * @see org.jbox2d.callbacks.QueryCallback#reportFixture(org.jbox2d.dynamics.Fixture) 
  189.          */  
  190.         public boolean reportFixture(Fixture argFixture) {  
  191.             Body body = argFixture.getBody();  
  192.             if (body.getType() == BodyType.DYNAMIC) {  
  193.                 boolean inside = argFixture.testPoint(point);  
  194.                 if (inside) {  
  195.                     fixture = argFixture;  
  196.   
  197.                     return false;  
  198.                 }  
  199.             }  
  200.   
  201.             return true;  
  202.         }  
  203.     }  
  204. }  

 

jbox2d库,这两个都需要
jbox2d-library-2.1.2.jar

slf4j-api-1.6.3.jar

 

http://blog.csdn.net/z1074971432/article/details/6912225

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值