android jbox2d学习笔记一 滑轮关节

/**
* 滑轮关节
*
* @time 下午12:49:27
* @author retacn yue
* @Email [email protected]
*/
@SuppressWarnings("unused")
public class PulleyJointSurfaceView extends SurfaceView implements Callback, Runnable {
private static final float RATE = 30;
private Thread thread;
private SurfaceHolder sfh;
private int screenW, screenH;
private Paint paint;
private boolean flag;
private Canvas canvas;


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


//
private Body body1, body2;
private float body1X = 40, body1Y = 200, body1W = 80, body1H = 80,//
body2X = 120, body2Y = 200, body2W = 80, body2H = 80;// 设置矩形坐标及宽高


private float ga1X = 100, ga1Y = 45, ga2X = 200, ga2Y = 45;// 两个动滑轮锚点


public PulleyJointSurfaceView(Context context) {
super(context);


screenW = this.getWidth();
screenH = this.getHeight();


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


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


this.setKeepScreenOn(true);
this.setFocusable(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);


// 创建两个矩形
body1 = createPolygon(body1X, body1Y, body1W, body1H, false);
body2 = createPolygon(body2X, body2Y, body2W, body2H, false);
// 创建滑轮关节
createPulleyJoint();
}


@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 PulleyJoint createPulleyJoint() {
PulleyJointDef def = new PulleyJointDef();// 创建滑轮关节数据实例
// 初始化滑轮关节
Vec2 ga1 = new Vec2(ga1X, ga1Y);
Vec2 ga2 = new Vec2(ga2X, ga2Y);
def.initialize(body1, body2, //
ga1, ga2,// 两个滑轮的锚点
body1.getWorldCenter(), body2.getWorldCenter(), // 两个矩形的锚点
1f);// 比例系数


PulleyJoint joint = (PulleyJoint) world.createJoint(def);// 创建滑轮节点
return joint;


}


/**
* 创建矩形对象
*/
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);// 创建物理世界的对象
def.angle = (float) (15 * Math.PI / 180);
body.m_userData = new MyRect(x, y, w, h);
body.createShape(polygonDef);// 设置皮肤
body.setMassFromShapes();// 整个对象计算打包
return body;
}


/**
* 绘制方法
*/
private void draw() {
try {
canvas = sfh.lockCanvas();
if (null != canvas) {
canvas.drawColor(Color.WHITE);


Body body = world.getBodyList();
for (int i = 1; i < world.getContactCount(); i++) {
// 绘制矩形
((MyRect) body.m_userData).draw(canvas, paint);
body = body.m_next;
}
// 绘制两个定滑轮,三条连线


canvas.drawCircle(ga1X, ga1Y, 10, paint);
canvas.drawCircle(ga2X, ga2Y, 10, paint);


canvas.drawLine(ga1X, ga1Y, ga2X, ga2Y, paint);// 两滑轮之间
canvas.drawLine(ga1X, ga1Y, body1.getPosition().x * RATE, body1.getPosition().y * RATE, paint);//
canvas.drawLine(ga2X, ga2Y, body2.getPosition().x * RATE, body2.getPosition().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();
paint.setStyle(Style.STROKE);
for (int i = 1; i < world.getContactCount(); 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();
}
}


}


}

你可能感兴趣的:(android)