JBox2D For Android - hello box2d
来源:互联网 发布:炒股软件安全吗 编辑:程序博客网 时间:2024/06/05 15:44
鉴于最近工作涉及JBox2D,打算围绕testbed的sample来做些细致的了解。也希望自己学习的同时跟大家探讨。
虽然box2d的手册已经详细的描述了hello box2d的代码,本篇也主要是简单用android实现,没有绘制部分,但是很清楚的描述了box2d运行的基本情况。
</pre><pre name="code" class="java">
- /********************************/
- /*Box2D v2.2.0 User Manual 文中代码的android实现版本*/
- /*本例调用JBox2d4Android_2.1.2.jar*/
- /*下载<span style="background-color: rgb(255, 255, 255); ">JBox2d4Android_2.1.2.jar </span> http://download.csdn.net/detail/z1074971432/3831279*/
- /********************************/
- import org.jbox2d.collision.shapes.PolygonShape;
- import org.jbox2d.common.Vec2;
- import org.jbox2d.dynamics.Body;
- import org.jbox2d.dynamics.BodyDef;
- import org.jbox2d.dynamics.BodyType;
- import org.jbox2d.dynamics.FixtureDef;
- import org.jbox2d.dynamics.World;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.view.View;
- public class Box2d_lesson1_hellobox2dActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(new helloBox2dView(this));
- }
- class helloBox2dView extends View {
- public helloBox2dView(Context context) {
- super(context);
- World world;
- /** Creating a World */
- {
- Vec2 gravity = new Vec2(0, -10f);
- boolean dosleep = true;
- world = new World(gravity, dosleep);
- }
- /** 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(0.0f, 1.0f);
- // Step 2. Use the world object to create the body.
- Body groundBody = world.createBody(groundBodyDef);
- // Step 3. Define fixtures with a shape, friction, density, etc.
- PolygonShape groundBox = new PolygonShape();
- groundBox.setAsBox(50.0f, 1.0f);
- // Step 4. Create fixtures on the body.
- groundBody.createFixture(groundBox, 0.0f);
- }
- /** Creating a Dynamic Body */
- Body body;
- {
- BodyDef bodyDef = new BodyDef();
- bodyDef.type = BodyType.DYNAMIC;
- bodyDef.position.set(0.0f, 4.0f);
- body = world.createBody(bodyDef);
- PolygonShape dynamicBox = new PolygonShape();
- dynamicBox.setAsBox(1.0f, 1.0f);
- FixtureDef fixtureDef = new FixtureDef();
- fixtureDef.shape = dynamicBox;
- fixtureDef.density = 1.0f;
- fixtureDef.friction = 0.3f;
- body.createFixture(fixtureDef);
- }
- /** Simulating the World (of Box2D) */
- {
- float timeStep = 1.0f / 60.0f;
- int velocityIterations = 6;
- int positionIterations = 2;
- for (int i = 0; i < 60; ++i)
- {
- world.step(timeStep, velocityIterations, positionIterations);
- Vec2 position = body.getPosition();
- float angle = body.getAngle();
- System.out.printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
- }
- }
- }
- }
- JBox2D For Android - hello box2d
- JBox2D For Android - hello box2d
- JBox2D 引擎 for Android 详解
- JBox2D For Android - 用canvas绘制shape
- 《Box2D for Flash Games》翻译 (一)hello box2d world
- Android JBox2d,Box2d,opengl es+算法 粒子系统水流的模拟体会
- 配置JBox2D for android遇到的问题以及解决方法
- Android 游戏引擎libgdx之Box2D Hello Box2D
- android加入jbox2d
- android JBox2D 框架
- BOX2D 第二章 Hello Box2D
- jbox2d
- android的jbox2d的使用!
- android 游戏学习(jbox2d)
- Android--碰撞效果--JBox2d实现
- 第二章 Hello Box2D
- Box2D例子——Hello Box2D
- Cordova for android hello app
- XAF之Security System
- 双绞线制作实验报告
- 爱4妮 struts2.1 + hibernate3.3 +spring2.5 + 仿Google数字分页
- 学习笔记——XML Schema复杂类型
- 关于QHttp
- JBox2D For Android - hello box2d
- Fahrenheit-Celsius table
- 利用多叉树将数据库中的层次数据转换成树形结构的JSON字符串
- [gpu pro3]弱文合集
- poj 2420 A Star not a Tree?(贪心+求多边形费马点)
- cyclicBarrier同步工具类
- 仿射几何学
- 希尔排序算法实现(C++)
- 彩色运动轨迹特效