JBox2D For Android - hello box2d

来源:互联网 发布:炒股软件安全吗 编辑:程序博客网 时间:2024/06/05 15:44

鉴于最近工作涉及JBox2D,打算围绕testbed的sample来做些细致的了解。也希望自己学习的同时跟大家探讨。

虽然box2d的手册已经详细的描述了hello box2d的代码,本篇也主要是简单用android实现,没有绘制部分,但是很清楚的描述了box2d运行的基本情况。

</pre><pre name="code" class="java"

  1. /********************************/  
  2. /*Box2D v2.2.0 User Manual 文中代码的android实现版本*/  
  3. /*本例调用JBox2d4Android_2.1.2.jar*/  
  4. /*下载<span style="background-color: rgb(255, 255, 255); ">JBox2d4Android_2.1.2.jar </span> http://download.csdn.net/detail/z1074971432/3831279*/  
  5. /********************************/  
  6. import org.jbox2d.collision.shapes.PolygonShape;  
  7. import org.jbox2d.common.Vec2;  
  8. import org.jbox2d.dynamics.Body;  
  9. import org.jbox2d.dynamics.BodyDef;  
  10. import org.jbox2d.dynamics.BodyType;  
  11. import org.jbox2d.dynamics.FixtureDef;  
  12. import org.jbox2d.dynamics.World;  
  13.   
  14. import android.app.Activity;  
  15. import android.content.Context;  
  16. import android.os.Bundle;  
  17. import android.view.View;  
  18.   
  19. public class Box2d_lesson1_hellobox2dActivity extends Activity {  
  20.     /** Called when the activity is first created. */  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(new helloBox2dView(this));  
  25.     }  
  26.   
  27.     class helloBox2dView extends View {  
  28.   
  29.         public helloBox2dView(Context context) {  
  30.             super(context);  
  31.             World world;  
  32.             /** Creating a World */  
  33.             {  
  34.                 Vec2 gravity = new Vec2(0, -10f);  
  35.                 boolean dosleep = true;  
  36.                 world = new World(gravity, dosleep);  
  37.             }  
  38.             /** Creating a Ground Box */  
  39.             {  
  40.             // Bodies are built using the following steps:  
  41.                 // Step 1. Define a body with position, damping, etc.  
  42.                 BodyDef groundBodyDef = new BodyDef();  
  43.                 groundBodyDef.position.set(0.0f, 1.0f);  
  44.                 // Step 2. Use the world object to create the body.  
  45.                 Body groundBody = world.createBody(groundBodyDef);  
  46.                 // Step 3. Define fixtures with a shape, friction, density, etc.  
  47.                 PolygonShape groundBox = new PolygonShape();  
  48.                 groundBox.setAsBox(50.0f, 1.0f);  
  49.                 // Step 4. Create fixtures on the body.  
  50.                 groundBody.createFixture(groundBox, 0.0f);  
  51.             }  
  52.             /** Creating a Dynamic Body */  
  53.             Body body;  
  54.             {  
  55.                 BodyDef bodyDef = new BodyDef();  
  56.                 bodyDef.type = BodyType.DYNAMIC;  
  57.                 bodyDef.position.set(0.0f, 4.0f);  
  58.                 body = world.createBody(bodyDef);  
  59.                   
  60.                   
  61.                 PolygonShape dynamicBox = new PolygonShape();  
  62.                 dynamicBox.setAsBox(1.0f, 1.0f);  
  63.                   
  64.                 FixtureDef fixtureDef = new FixtureDef();  
  65.                 fixtureDef.shape = dynamicBox;  
  66.                 fixtureDef.density = 1.0f;  
  67.                 fixtureDef.friction = 0.3f;  
  68.                   
  69.                 body.createFixture(fixtureDef);  
  70.                   
  71.             }  
  72.               
  73.             /** Simulating the World (of Box2D) */  
  74.             {  
  75.                 float timeStep = 1.0f / 60.0f;  
  76.                 int velocityIterations = 6;  
  77.                 int positionIterations = 2;  
  78.                   
  79.                 for (int i = 0; i < 60; ++i)  
  80.                 {  
  81.                     world.step(timeStep, velocityIterations, positionIterations);  
  82.                     Vec2 position = body.getPosition();  
  83.                     float angle = body.getAngle();  
  84.                     System.out.printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);  
  85.                 }  
  86.                   
  87.                   
  88.             }  
  89.               
  90.         }  
  91.   
  92.     }  


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝新店每天只有几个访客怎么办 注册淘宝企业店铺需要审核怎么办 淘宝店铺被投诉知识产权怎么办 一般违规扣48分怎么办 金税盘处于报税期不能开票怎么办 小规模税率开错了怎么办 我是代购卖家被买家投诉偷税怎么办 天猫盒子内存不够怎么办 天猫品牌申请不通过怎么办 天猫商家发货发个空包裹怎么办 无限流量怎么办没有4g 海外直邮身份证过期了怎么办 买车的人不过户怎么办 天猫精灵球泡离线怎么办 花呗被骗了2万怎么办 天猫公司变更地址发票怎么办 支付宝自助解限怎么办 支付宝16岁限额怎么办 支付宝提不了现怎么办 支付宝余额受限需要身份证怎么办 微信被骗了6000怎么办 被代运营骗了该怎么办 淘宝店铺过节放假无人打理怎么办 淘宝店太久没打理出现未开店怎么办 淘宝店关了售后怎么办 发货运单号发错了怎么办 天猫积分为零怎么办 山东聊城小型车脱审一年怎么办? 廉租房如果夫妻离婚怎么办 淘宝客服不给退货怎么办 天猫客服打字慢怎么办 京东买的kindle坏了怎么办 欧巴怎么办韩语怎么写 聚划算淘宝口令打不开怎么办 道聚城白银礼包下架怎么办 聚星输了很多钱怎么办 弹力运动裤被烟烧了个洞怎么办 生完宝宝胯宽怎么办 黑色纯棉裤子洗的发白怎么办 金盾保险柜密码忘了怎么办 装修好的房子漏水怎么办