Address: http://blog.csdn.net/you_and_me12/article/details/7961573
2012-09-10
From today on, I learned how to import box2d. in Java, It is jbox2d (download!
I wrote helloworld at the end of this Article for your reference only.
Note: I) box2d is composed of the world, objects, etc. box2d can automatically calculate the position of an object, and then we need to draw it out in Android;
Ii) The units in the box2d world are standard units, and the second, meter, and kg etc. Scale proportionally. Otherwise, the speed will be very slow (Conversion in Code );
Iii) the displacement of each frame in box2d is 2 m/frame, which cannot exceed this speed. Therefore, you need to zoom in to enlarge the speed;
Iv) Note that the zero point of the coordinate system is in the upper left corner and the angle is in radians;
Coordinate System
Code:
public class Hello_Box2d extends Activity {private World mWorld;private Handler mHandler;private View mView;private int screenH, screenW;private final float WORLD2PHONE_RATE = 5;private Runnable mRunnable = new Runnable() {@Overridepublic void run() {mWorld.step(0.1f, 3, 3);//mView.invalidate();mHandler.postDelayed(this, 100);}};/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.requestWindowFeature(Window.FEATURE_NO_TITLE); //remove titlegetWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);// full screen//get the window width and heightDisplayMetrics metric = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(metric);screenH = metric.heightPixels;screenW = metric.widthPixels;//create viewmView = new helloBox2dView(this);mView.setBackgroundColor(Color.WHITE);setContentView(mView);}@Overrideprotected void onDestroy() {super.onDestroy();mHandler.removeCallbacks(mRunnable);}class helloBox2dView extends View {private Canvas mCanvas;private Body mBody;public helloBox2dView(Context context) {super(context);/** Creating a World */{Vec2 gravity = new Vec2(0f, 9.8f);// vector, the gravity direction, left and right, up and downmWorld = new World(gravity, false);}/** Creating a Ground Box */{// Bodies are built using the following steps:// Step 1. Define a body with position, damping, etc.BodyDef groundBodyDef = new BodyDef();groundBodyDef.position.set(0f, (screenH-screenW*3/4)/WORLD2PHONE_RATE);groundBodyDef.angle = (float) Math.asin(0.6);// Step 2. Use the world object to create the body.Body groundBody = mWorld.createBody(groundBodyDef);// Step 3. Define fixtures with a shape, friction, density, etc.PolygonShape groundBox = new PolygonShape();groundBox.setAsBox(screenW*5/4/WORLD2PHONE_RATE, 1f/WORLD2PHONE_RATE);// Step 4. Create fixtures on the body.groundBody.createFixture(groundBox, 0.0f);//borderBodyDef groundBodyDef2 = new BodyDef();groundBodyDef2.position.set(screenW/WORLD2PHONE_RATE, 0f);groundBodyDef2.angle = (float) (0.5f*Math.PI);Body groundBody2 = mWorld.createBody(groundBodyDef2);PolygonShape groundBox2 = new PolygonShape();groundBox2.setAsBox(screenH/WORLD2PHONE_RATE, 1f/WORLD2PHONE_RATE);groundBody2.createFixture(groundBox2, 0.0f);}/** Creating a Dynamic Body */{BodyDef bodyDef = new BodyDef();bodyDef.type = BodyType.DYNAMIC;bodyDef.position.set(100f/WORLD2PHONE_RATE, 0f);mBody = mWorld.createBody(bodyDef);CircleShape dynamicBox = new CircleShape();dynamicBox.m_radius = 10/WORLD2PHONE_RATE;FixtureDef fixtureDef = new FixtureDef();fixtureDef.shape = dynamicBox;fixtureDef.density = 500f;fixtureDef.friction = 0.5f;fixtureDef.restitution = 0.8f;mBody.createFixture(fixtureDef);}mHandler = new Handler();mHandler.post(mRunnable);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);mCanvas = canvas;//draw grounddrawGround();//draw boxVec2 position = mBody.getPosition();float angle = mBody.getAngle();drawBox(position.x*WORLD2PHONE_RATE, position.y*WORLD2PHONE_RATE, angle);}public void drawBox(float x, float y, float angle){Paint paint = new Paint();paint.setAntiAlias(true);paint.setColor(Color.RED);mCanvas.drawCircle(x, y, 10, paint);mCanvas.drawLine(x, y, (float)(x+10*Math.cos(angle)), (float)(y+10*Math.sin(angle)), new Paint());}public void drawGround(){Paint paint = new Paint();paint.setColor(Color.BLUE);paint.setStrokeWidth(10);mCanvas.drawLine(0, screenH-screenW*3/4, screenW, screenH, paint);mCanvas.drawLine(screenW, 0, screenW, screenH, paint);}}}