想在android下使用Box2d,照著網上的教程寫了個demo,一個小球垂直掉落到地面,可小球掉落的速度卻是勻速的,并且掉到地面就不反彈了,實在是想破腦袋了。。。代碼如下,還請高人指點啊。。。頭炸了
public class DemoView extends View implements Runnable{
//box2d
private World myWorld;
private Body circle;
private Body mGround;
private Vec2 gravity;
private int radius = 10;
private boolean isSleep = true;
private boolean isStatic = false;
//screen
private float density;
private float s_w,s_h;
//draw
private Paint mPaint;
private Paint mPaintGround;
//thread
private ExecutorService executorService;
private boolean isThreadWork = true;
private final static int DUR_TIME = 16;
public DemoView(Context context) {
super(context);
initial();
createWorld();
}
//initial
private void initial(){
density = getResources().getDisplayMetrics().density;
s_w = getResources().getDisplayMetrics().widthPixels;
s_h = getResources().getDisplayMetrics().heightPixels;
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.BLUE);
mPaint.setAntiAlias(true);
mPaintGround = new Paint();
mPaintGround.setStyle(Paint.Style.FILL);
mPaintGround.setColor(Color.GRAY);
executorService = Executors.newCachedThreadPool();
}
//create world
private void createWorld(){
gravity = new Vec2(10f,10f);
myWorld = new World(gravity,isSleep);
myWorld.setContinuousPhysics(true);
circle = createCircle();
mGround = createGround();
}
//create Circle
private Body createCircle(){
CircleShape circleShape = new CircleShape();
circleShape.m_radius = radius;
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.2f;
fixtureDef.restitution = 0.8f;
fixtureDef.shape = circleShape;
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DYNAMIC;
bodyDef.position.set(s_w / 2, 10);
Body body = myWorld.createBody(bodyDef);
body.createFixture(fixtureDef);
return body;
}
//create ground
private Body createGround(){
PolygonShape polygonShape = new PolygonShape();
polygonShape.setAsBox(s_w/2,15);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = polygonShape;
fixtureDef.density = 0f;
fixtureDef.friction = 0.3f;
fixtureDef.restitution = 0.2f;
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.STATIC;
bodyDef.position.set(s_w/2,s_h/2);
Body body = myWorld.createBody(bodyDef);
body.createFixture(fixtureDef);
return body;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float x,y;
x = circle.getPosition().x;
y = circle.getPosition().y;
canvas.drawCircle(x, y, radius, mPaint);
x = mGround.getPosition().x;
y = mGround.getPosition().y;
canvas.drawRect(0,s_h/2,s_w,s_h/2+30,mPaintGround);
Log.v("roytest","x is " + x +",y is"+y);
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
if(hasWindowFocus){
isThreadWork = true;
executorService.execute(this);
}else {
isThreadWork = false;
}
Log.v("roytest","on focus changed " + hasWindowFocus);
}
@Override
public void run() {
while(isThreadWork){
myWorld.step(30,10,8);
float t1 = System.currentTimeMillis();
postInvalidate();
float t2 = System.currentTimeMillis();
float t = t2-t1;
if(t<DUR_TIME){
try {
Thread.sleep((long)(DUR_TIME-t));
} catch (InterruptedException e) {
Log.v("roytest","interrupt " + Log.getStackTraceString(e));
}
}
postInvalidate();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/71117.html
標籤:其它游戲引擎
上一篇:求助:vs2013+cocos2dx專案生成類視圖出現問題
下一篇:關于excel解決方案的問題
