前言
相比Ios UiKit原生支持物理引擎,Android確實麻煩的不要不要。
為什么用 libgdx
- Android上最方便的方案是jbox2D,缺點是在java層實現(xiàn),物理多了之后性能很卡。筆者近期沒有測試,11年左右在里程碑1上使用的時候那是巨卡無比。
- libgdx的物理引擎其實是封裝的native版本box2D,在滿足性能需求的同時,避免了開發(fā)JNI的煩惱,對于java程序員來說目前是最便捷的方案。
使用libgdx-box2d
STEP1: build.gradle中添加依賴
dependencies {
configurations { natives }
implementation "com.badlogicgames.gdx:gdx-box2d:$box2dVersion"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$box2dVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$box2dVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-box2d-platform:$box2dVersion:natives-arm64-v8a"
}
task copyAndroidNatives() {
file("libs/armeabi/").mkdirs();
file("libs/armeabi-v7a/").mkdirs();
file("libs/arm64-v8a/").mkdirs();
configurations.natives.files.each { jar ->
def outputDir = null
if(jar.name.endsWith("natives-arm64-v8a.jar")) outputDir = file("libs/arm64-v8a")
if(jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a")
if(jar.name.endsWith("natives-armeabi.jar")) outputDir = file("libs/armeabi")
if(outputDir != null) {
copy {
from zipTree(jar)
into outputDir
include "*.so"
}
}
}
}
STEP2: 渲染層實現(xiàn)
- libgdx本身是個游戲引擎,提供基于openGLES的渲染引擎,但是對于大多數(shù)APPer來說使用成本略高。這里我們通過實現(xiàn)一個自定義view,并通過Canvas#draw()的方式進行渲染。
- 2.1 初始化box2D
private void setupBox2D() {
Box2D.init();
//第一個參數(shù)為x、y兩個方向的重力值,(0,0)點在左上y軸重力用10
mWorld = new World(new Vector2(0, 10), true);
}
- 2.2 在組件四周添加靜態(tài)剛體
private void createTopAndBottomBounds() {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
PolygonShape box = new PolygonShape();
float boxWidth = pixelsToMeters(mWidth);
float boxHeight = pixelsToMeters(mRatio);
box.setAsBox(boxWidth, boxHeight);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = box;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.3f;
fixtureDef.restitution = 0.5f;
bodyDef.position.set(0, -boxHeight * .5f);
Body topBody = mWorld.createBody(bodyDef);
topBody.createFixture(fixtureDef);
bodyDef.position.set(0, pixelsToMeters(mHeight) + boxHeight * .5f);
Body bottomBody = mWorld.createBody(bodyDef);
bottomBody.createFixture(fixtureDef);
}
private void createLeftAndRightBounds() {
float boxWidth = pixelsToMeters(mRatio);
float boxHeight = pixelsToMeters(mHeight);
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.StaticBody;
PolygonShape box = new PolygonShape();
box.setAsBox(boxWidth, boxHeight);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = box;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.3f;
fixtureDef.restitution = 0.5f;
bodyDef.position.set(-boxWidth * .5f, boxHeight);
Body leftBody = mWorld.createBody(bodyDef);
leftBody.createFixture(fixtureDef);
bodyDef.position.set(pixelsToMeters(mWidth) + boxWidth * .5f, 0);
Body rightBody = mWorld.createBody(bodyDef);
rightBody.createFixture(fixtureDef);
}
- 2.3 添加一個動態(tài)剛體,從(0,0)點下落
public void addGiftBody() {
// First we create a body definition
BodyDef bodyDef = new BodyDef();
// We set our body to dynamic, for something like ground which doesn't move we would set it to StaticBody
bodyDef.type = BodyDef.BodyType.DynamicBody;
// Set our body's starting position in the world
bodyDef.position.set(pixelsToMeters(3), pixelsToMeters(3));
bodyDef.linearVelocity.set((float) Math.random(), (float) Math.random() * 100);
// Create our body in the world using our body definition
Body body = mWorld.createBody(bodyDef);
//TODO 隨機
Bitmap bitmap = mBitmaps[0];
GiftInfo giftInfo = new GiftInfo(bitmap);
body.setUserData(giftInfo);
body.setFixedRotation(false);
PolygonShape box = new PolygonShape();
float boxWidth = pixelsToMeters(pixelsToMeters(bitmap.getWidth()));
float boxHeight = pixelsToMeters(pixelsToMeters(bitmap.getHeight()));
box.setAsBox(boxWidth, boxHeight);
// Create a fixture definition to apply our shape to
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = box;
fixtureDef.density = 1.5f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = .6f; // Make it bounce a little bit
// Create our fixture and attach it to the body
Fixture fixture = body.createFixture(fixtureDef);
mBodyList.add(body);
box.dispose();
}
2.4 渲染、更新物體坐標、以此循環(huán)
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawGift(canvas);
long time = System.currentTimeMillis() - mLastRenderTime;
mLastRenderTime = time;
doPhysicsStep(time);
postInvalidate();
}
private void drawGift(Canvas canvas) {
int size = mBodyList.size();
for (int i = 0; i < size; i++) {
Body body = mBodyList.get(i);
GiftInfo giftInfo = (GiftInfo) body.getUserData();
Bitmap bitmap = giftInfo.getBitmap();
canvas.drawBitmap(bitmap, metersToPixels(mBodyList.get(i).getPosition().x) - bitmap.getWidth() * .5f,
metersToPixels(mBodyList.get(i).getPosition().y) - bitmap.getHeight() * .5f, mPaint);
}
}
private void doPhysicsStep(float deltaTime) {
// fixed time step
// max frame time to avoid spiral of death (on slow devices)
float frameTime = Math.min(deltaTime, 0.25f);
mAccumulator += frameTime;
while (mAccumulator >= Constants.TIME_STEP) {
mWorld.step(Constants.TIME_STEP, Constants.VELOCITY_ITERATIONS, Constants.POSITION_ITERATIONS);
mAccumulator -= Constants.TIME_STEP;
}
}
結(jié)尾
- 希望能夠通過這個簡單例子幫助大家使用libgdx-box2d,先水一波。