歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android上使用Box2d

Android上使用Box2d

日期:2017/3/1 10:20:39   编辑:Linux編程

Box2d是一個非常優秀的開源2D物理引擎,可以模擬出非常真實的物理效果,今天我們介紹如何在Andoird下使用Box2d:

1.

Box2d原來使用c++寫的,不過已經有了Java版本的:JBox2D,可以在http://sourceforge.net/projects/jbox2d/下載,一共有兩個:

JBox2d-2.0.1.zip和JBox2D-2.0.1-b250-Full.jar。我的版本是2.0.1

2.

新建一個Android Project,如下所示:

2.

修改Jbox2dtestActivity.java文件如下:

  1. package com.test.jbox2d;
  2. import org.jbox2d.collision.AABB;
  3. import org.jbox2d.collision.CircleDef;
  4. import org.jbox2d.collision.PolygonDef;
  5. import org.jbox2d.common.Vec2;
  6. import org.jbox2d.dynamics.Body;
  7. import org.jbox2d.dynamics.BodyDef;
  8. import org.jbox2d.dynamics.World;
  9. import android.app.Activity;
  10. import android.content.Context;
  11. import android.graphics.Canvas;
  12. import android.graphics.Color;
  13. import android.graphics.Paint;
  14. import android.os.Bundle;
  15. import android.os.Handler;
  16. import android.view.View;
  17. import android.view.Window;
  18. import android.view.WindowManager;
  19. public class Jbox2dtestActivity extends Activity
  20. {
  21. private final static int RATE = 10; //屏幕到現實世界的比例 10px:1m;
  22. private AABB worldAABB; //創建 一個管理碰撞的世界
  23. private World world;
  24. private float timeStep = 1/60; //模擬的的頻率
  25. private int iterations = 10; //迭代越大,模擬約精確,但性能越低
  26. private Body body,body2,body3;
  27. private MyView myView;
  28. private Handler mHandler;
  29. public void onCreate(Bundle savedInstanceState)
  30. {
  31. super.onCreate(savedInstanceState);
  32. requestWindowFeature(Window.FEATURE_NO_TITLE);
  33. getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
  34. WindowManager.LayoutParams. FLAG_FULLSCREEN);
  35. worldAABB = new AABB();
  36. //上下界,以屏幕的左上方為 原點,如果創建的剛體到達屏幕的邊緣的話,會停止模擬
  37. worldAABB.lowerBound.set(-100.0f,-100.0f);
  38. worldAABB.upperBound.set(100.0f, 100.0f);//注意這裡使用的是現實世界的單位
  39. Vec2 gravity = new Vec2(0.0f,10.0f);
  40. boolean doSleep = true;
  41. world = new World(worldAABB, gravity, doSleep);//創建世界
  42. createBox(160, 470, 160, 10, true);
  43. createBox1(160, 150, 160, 10, false);
  44. createCircle(160, 100, 10);
  45. createCircle1(150, 60, 10);
  46. timeStep = 1.0f/60.0f;
  47. iterations = 10;
  48. myView = new MyView(this);
  49. setContentView(myView);
  50. mHandler = new Handler();
  51. mHandler.post(update);
  52. }
  53. private Runnable update = new Runnable()
  54. {
  55. public void run()
  56. {
  57. world.step(timeStep, iterations);//開始模擬
  58. Vec2 position = body.getPosition();
  59. Vec2 position1 = body2.getPosition();
  60. Vec2 position2 = body3.getPosition();
  61. myView.x=position.x*RATE;
  62. myView.y=position.y*RATE;
  63. myView.x1=position1.x*RATE;
  64. myView.y1=position1.y*RATE;
  65. myView.x2=position2.x*RATE;
  66. myView.y2=position2.y*RATE;
  67. myView.update();
  68. mHandler.postDelayed(update, (long)timeStep*1000);
  69. }
  70. };
  71. public void createBox(float x, float y, float half_width, float half_height, boolean isStatic)
  72. {
  73. PolygonDef shape = new PolygonDef();
  74. if(isStatic)
  75. {
  76. shape.density = 0;
  77. }
  78. else
  79. {
  80. shape.density = 2.0f;
  81. }
  82. shape.friction = 0.8f;
  83. shape.restitution = 0.3f;
  84. shape.setAsBox(half_width/RATE, half_height/RATE);
  85. BodyDef bodyDef = new BodyDef();
  86. bodyDef.position.set(x/RATE, y/RATE);
  87. Body body1= world.createBody(bodyDef);
  88. body1.createShape(shape);
  89. body1.setMassFromShapes();
  90. }
  91. public void createCircle(float x, float y, float radius)
  92. {
  93. CircleDef shape = new CircleDef();
  94. shape.density = 7;
  95. shape.friction = 0.2f;
  96. shape.radius = radius/RATE;
  97. BodyDef bodyDef = new BodyDef();
  98. bodyDef.position.set(x/RATE, y/RATE);
  99. body2 = world.createBody(bodyDef);
  100. body2.createShape(shape);
  101. body2.setMassFromShapes();
  102. }
  103. public void createCircle1(float x, float y, float radius)
  104. {
  105. CircleDef shape = new CircleDef();
  106. shape.density = 7;
  107. shape.friction = 0.2f;
  108. shape.radius = radius/RATE;
  109. BodyDef bodyDef = new BodyDef();
  110. bodyDef.position.set(x/RATE, y/RATE);
  111. body3 = world.createBody(bodyDef);
  112. body3.createShape(shape);
  113. body3.setMassFromShapes();
  114. }
  115. public void createBox1(float x, float y, float half_width, float half_height, boolean isStatic)
  116. {
  117. PolygonDef shape = new PolygonDef();
  118. if(isStatic)
  119. {
  120. shape.density = 0;
  121. }
  122. else
  123. {
  124. shape.density = 2.0f;
  125. }
  126. shape.friction = 0.3f;
  127. shape.setAsBox(half_width/RATE, half_height/RATE);
  128. BodyDef bodyDef = new BodyDef();
  129. bodyDef.position.set(x/RATE, y/RATE);
  130. body= world.createBody(bodyDef);
  131. body.createShape(shape);
  132. body.setMassFromShapes();
  133. }
  134. class MyView extends View
  135. {
  136. Canvas canvas;
  137. public float x=160,y=150;
  138. public float x1=160,y1=100;
  139. public float x2=150,y2=60;
  140. public MyView(Context context)
  141. {
  142. super(context);
  143. }
  144. public void drawBox(float x, float y)
  145. {
  146. Paint mPaint = new Paint();
  147. mPaint.setAntiAlias(true);
  148. mPaint.setColor(Color.RED);
  149. canvas.drawRect(x-160, y-10, x+160, y+10, mPaint);
  150. }
  151. public void drawGround()
  152. {
  153. Paint mPaint = new Paint();
  154. mPaint.setAntiAlias(true);
  155. mPaint.setColor(Color.BLUE);
  156. canvas.drawRect(0, 460, 320, 480, mPaint);
  157. }
  158. public void drawCircle(float x1, float y1)
  159. {
  160. Paint mPaint = new Paint();
  161. mPaint.setAntiAlias(true);
  162. mPaint.setColor(Color.GREEN);
  163. canvas.drawCircle(x1, y1, 10, mPaint);
  164. }
  165. public void update()
  166. {
  167. postInvalidate();
  168. }
  169. protected void onDraw(Canvas canvas)
  170. {
  171. super.onDraw(canvas);
  172. this.canvas = canvas;
  173. drawGround();
  174. drawBox(x, y);
  175. drawCircle(x1, y1);
  176. drawCircle(x2, y2);
  177. }
  178. }
  179. }
Copyright © Linux教程網 All Rights Reserved