JBox2D For Android - hello box2d

鉴于最近工作涉及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.     }  


你可能感兴趣的:(android,工作)