package com.mj;
import org.jbox2d.collision.AABB;
import org.jbox2d.collision.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.World;
import org.jbox2d.dynamics.joints.MouseJoint;
import org.jbox2d.dynamics.joints.MouseJointDef;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
public class MySurfaceView extends SurfaceView implements Callback, Runnable {
private Thread th;
private SurfaceHolder sfh;
private Canvas canvas;
private Paint paint;
private boolean flag;
// ----添加一个物理世界---->>
final float RATE = 30;
World world;
AABB aabb;
Vec2 gravity;
float timeStep = 1f / 60f;
int iterations = 10;
// ------>>旋转关节
Body body1;
float body1x = 52, body1y = 10, body1w = 10, body1h = 70;
float touchX=body1x+body1w/2, touchY=body1y+body1h/2;
MouseJoint mouseJoint;
boolean bodyFlag;
Body test = null;
public MySurfaceView(Context context) {
super(context);
this.setKeepScreenOn(true);
sfh = this.getHolder();
sfh.addCallback(this);
paint = new Paint();
paint.setStyle(Style.STROKE);
paint.setAntiAlias(true);
this.setFocusable(true);
this.setFocusableInTouchMode(true);
// --添加一个物理世界--->>
aabb = new AABB();
gravity = new Vec2(0, 10);
aabb.lowerBound.set(-100, -100);
aabb.upperBound.set(100, 100);
world = new World(aabb, gravity, true);
// ----在物理世界中添加一个矩形Body
body1 = createPolygon(body1x + body1w / 2 - body1w / 2, body1y + body1h / 2 - body1h / 2, body1w, body1h, false);
// 设置鼠标关节
mouseJoint = createMouseJoint();
mouseJoint.m_gamma = 100;// 弹性度
}
public void surfaceCreated(SurfaceHolder holder) {
//添加边界
createPolygon(5, 5, this.getWidth() - 10, 2, true);
createPolygon(5, this.getHeight() - 10, this.getWidth() - 10, 2, true);
createPolygon(5, 5, 2, this.getHeight() - 10, true);
createPolygon(this.getWidth() - 10, 5, 2, this.getHeight() - 10, true);
flag = true;
th = new Thread(this);
th.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
touchX = event.getX();
touchY = event.getY();
//唤醒body1
body1.wakeUp();
mouseJoint.m_target.set(event.getX() / RATE, event.getY() / RATE);
return true;
}
// 鼠标关节
public MouseJoint createMouseJoint() {
MouseJointDef mjd = new MouseJointDef();
//设置鼠标关节的第一个Body实例(默认使用接地Body)
mjd.body1 = world.getGroundBody();
//设置鼠标关节的第二个Body实例(被拖拽的Body)
mjd.body2 = body1;
//设置目标点的X坐标
mjd.target.x = body1.getPosition().x;
//设置目标点的Y坐标
mjd.target.y = body1.getPosition().y;
// body1.allowSleeping(false);//设置body1永不休眠
// 设置鼠标关节的目标位置
mjd.maxForce = 100;// 拉力
//由World来创建鼠标关节
MouseJoint mj = (MouseJoint) world.createJoint(mjd);
return mj;
}
public void Logic() {
world.step(timeStep, iterations);
Body body = world.getBodyList();
for (int i = 1; i < world.getBodyCount(); i++) {
MyRect mc = (MyRect) body.m_userData;
mc.setX(body.getPosition().x * RATE - mc.w / 2);
mc.setY(body.getPosition().y * RATE - mc.h / 2);
mc.setAngle((float) (body.getAngle() * 180 / Math.PI));
body = body.m_next;
}
}
public Body createPolygon(float x, float y, float width, float height, boolean isStatic) {
PolygonDef pd = new PolygonDef();
if (isStatic) {
pd.density = 0;
} else {
pd.density = 1;
}
pd.friction = 0.8f;
pd.restitution = 0.3f;
pd.setAsBox(width / 2 / RATE, height / 2 / RATE);
BodyDef bd = new BodyDef();
bd.position.set((x + width / 2) / RATE, (y + height / 2) / RATE);
Body body = world.createBody(bd);
body.m_userData = new MyRect(x, y, width, height);
body.createShape(pd);
body.setMassFromShapes();
return body;
}
public void myDraw() {
try {
canvas = sfh.lockCanvas();
if (canvas != null) {
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(body1.getPosition().x * RATE, body1.getPosition().y * RATE, touchX, touchY, paint);
}
} catch (Exception e) {
Log.e("Himi", "myDraw is Error!");
} finally {
if (canvas != null)
sfh.unlockCanvasAndPost(canvas);
}
}
public void run() {
while (flag) {
myDraw();
Logic();
try {
Thread.sleep((long) timeStep * 1000);
} catch (Exception ex) {
Log.e("Himi", "Thread is Error!");
}
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android应用源码之(鼠标关节拖拽Body.zip项目安卓应用源码下载
共27个文件
class:9个
java:4个
png:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 116 浏览量
2022-03-07
18:48:36
上传
评论
收藏 355KB ZIP 举报
温馨提示
Android应用源码之(鼠标关节拖拽Body.zip项目安卓应用源码下载Android应用源码之(鼠标关节拖拽Body.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
资源推荐
资源详情
资源评论
收起资源包目录
Android应用源码之(鼠标关节拖拽Body.zip (27个子文件)
Android应用源码之(鼠标关节拖拽Body
Android应用源码之(鼠标关节拖拽Body
7-13-6(鼠标关节-拖拽Body)
CreateMouseJoint
.project 852B
default.properties 362B
bin
com
mj
R$string.class 391B
R$layout.class 358B
MainActivity.class 756B
R$drawable.class 364B
R.class 447B
MySurfaceView.class 7KB
MyRect.class 1KB
R$attr.class 304B
R$id.class 344B
CreateMouseJoint.apk 92KB
resources.ap_ 10KB
classes.dex 174KB
proguard.cfg 1KB
AndroidManifest.xml 630B
src
com
mj
MySurfaceView.java 5KB
MyRect.java 781B
MainActivity.java 499B
res
values
strings.xml 179B
layout
main.xml 408B
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 3KB
.classpath 389B
lib
jbox2d.jar 155KB
gen
com
mj
R.java 719B
assets
共 27 条
- 1
资源评论
yxkfw
- 粉丝: 82
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功