博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
andriod jbox2d学习笔记二 通过移动关节移动body
阅读量:4587 次
发布时间:2019-06-09

本文共 4010 字,大约阅读时间需要 13 分钟。

/**
 * 通过移动关节移动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();
}
}
}
}

转载于:https://www.cnblogs.com/retacn-yue/archive/2012/09/13/2761258.html

你可能感兴趣的文章
MyEclipse导入jquery-1.8.0.min.js等文件报错的解决方案
查看>>
<pythonchallenge.com>----Lv1
查看>>
Android App测试要点
查看>>
【新手入门篇】新浪微博应用开发之Java入门篇
查看>>
2-10
查看>>
CentOS 7 安装 Docker
查看>>
三、MapReduce学习
查看>>
MySQL的表分区详解 - 查看分区数据量,查看全库数据量----转http://blog.csdn.net/xj626852095/article/details/51245844...
查看>>
课程作业02将所有动手动脑的问题以及课后实验问题
查看>>
oracle_(第二课)监听器配置
查看>>
使用xdebug调试程序后程序很慢的原因
查看>>
windows下配置Tomcat7.0.22
查看>>
Perl中命令行参数以及打开管道文件
查看>>
习题 11 提问
查看>>
2018-07-05-Python全栈开发day25-python中的继承
查看>>
MySQL 数据类型(转贴)
查看>>
Maven 常用命令
查看>>
Java注解知识点摘抄
查看>>
决战Leetcode: easy part(1-50)
查看>>
数组中出现次数超过一半的数字
查看>>