andriod jbox2d学习笔记二 通过移动关节移动body

/**
 * 通过移动关节移动body
 * 
 * @time 下午12:50:47
 * @author retacn yue
 * @Email zhenhuayue@sina.com
 */
@SuppressWarnings("unused")
public class PrismaticJointSurfaceView extends SurfaceView implements Callback, Runnable {
private Thread thread;
private int screenW, screenH;
private Canvas canvas;
private SurfaceHolder sfh;
private Paint paint;
private boolean flag;


// 创建物理世界
private AABB aabb;
private World world;
private float timeStep = 1f / 60f;
private Vec2 gravity;
private static final float RATE = 30;
private int iterations = 10;


// 移动关节
Body body1;
// 设置对象坐标
private float body1X = 10, body1Y = 40, body1W = 40, body1H = 40;


/**
* 构造函数

* @param context
*/
public PrismaticJointSurfaceView(Context context) {
super(context);


this.getWidth();
this.getHeight();


sfh = this.getHolder();
sfh.addCallback(this);


paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Style.STROKE);


this.setFocusable(true);
this.setKeepScreenOn(true);


// 创建物理世界
aabb = new AABB();// 创建物理世界的范围
aabb.lowerBound.set(-100, -100);// 设置左上角坐标
aabb.upperBound.set(100, 100);// 设置右下角坐标
gravity = new Vec2(0f, 10f);// 重力向量对象
world = new World(aabb, gravity, true);// 创建物理世界


// 添加一个矩形body
body1 = createPolygon(body1X, body1Y, body1W, body1H, false);
// 设置移动关节
createPrismaticJoint();


}


@Override
public void surfaceCreated(SurfaceHolder holder) {
flag = true;
thread = new Thread(this);
thread.start();
}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {


}


@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
}


/**
* 创建对象
*/
private Body createPolygon(float x, float y, float w, float h, boolean isStatic) {
// 创建一个皮肤
PolygonDef polygonDef = new PolygonDef();
if (isStatic) {
polygonDef.density = 0;// 设置矩形对象为静态
} else {
polygonDef.density = 1;// 设置矩形对象为静态
}
// 摩擦力
polygonDef.friction = 0.8f;
// 恢复力
polygonDef.restitution = 0.3f;
// 快捷成盒子
polygonDef.setAsBox(w / 2 / RATE, h / 2 / RATE);
// 创建一个刚体
BodyDef def = new BodyDef();
def.position.set((x + w / 2) / RATE, (y + h / 2) / RATE);// 设置刚体坐标


Body body = world.createBody(def);// 创建物体
body.m_userData = new MyRect(x, y, w, h);
body.createShape(polygonDef);// 设置皮肤
// 整个物体计算打包
body.setMassFromShapes();
return body;


}


/**
* 创建移动关节
*/
private PrismaticJoint createPrismaticJoint() {
PrismaticJointDef def = new PrismaticJointDef();
// 预设马达的最大力
def.maxMotorForce = 10;
// 马达的最终力
def.motorSpeed = 10;
// 启动马达
def.enableMotor = true;
// 位移的最小偏移值
def.lowerTranslation = -80f / RATE;
// 位移动最大偏移值
def.upperTranslation = 80f / RATE;
// 启动限制
def.enableLimit = true;
// 初始化关节数据
def.initialize(world.getGroundBody(), body1, body1.getWorldCenter(), new Vec2(1, 0));


PrismaticJoint prismaticJoint = (PrismaticJoint) world.createJoint(def);// 创建物理世界的移动关节
return prismaticJoint;


}


/**
* 绘制方法
*/
private void draw() {
try {
canvas = sfh.lockCanvas();
if (null != canvas) {
canvas.drawColor(Color.WHITE);
Body body = world.getBodyList();
for (int i = 1; i < world.getBodyCount(); i++) {
// 绘制矩形
((MyRect) body.m_userData).draw(canvas, paint);
body = body.m_next;
}
// 画直线
canvas.drawLine(body1X + body1W / 2, body1Y + body1Y / 2, body1.getWorldCenter().x * RATE, body1.getWorldCenter().y * RATE, paint);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != canvas) {
sfh.unlockCanvasAndPost(canvas);
}
}


}


/**
* 游戏逻辑
*/
private void logic() {
// 运行物理世界
world.step(timeStep, iterations);


Body body = world.getBodyList();
for (int i = 1; i < world.getBodyCount(); i++) {
// 更新矩形坐标和角度
MyRect myRect = (MyRect) body.m_userData;
myRect.setX(body.getPosition().x * RATE - myRect.getW() / 2);
myRect.setY(body.getPosition().y * RATE - myRect.getH() / 2);
myRect.setAngle((float) (body.getAngle() * 180 / Math.PI));
body = body.m_next;
}


}


@SuppressWarnings("static-access")
@Override
public void run() {
while (flag) {
draw();
logic();
try {
thread.sleep((long) timeStep * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}


}


}
posted @ 2012-09-13 12:47  retacn_yue  阅读(234)  评论(0编辑  收藏  举报