情过南山 2015-06-18 05:32 采纳率: 0%
浏览 693

jbox2d游戏引擎问题,求高手解答!

我用jbox2d写了一个例子为什么跑不起来啊,我用的是jbox2d的最新版本,求高手赐教,谢谢!请看代码。

package com.example.androidxuexi;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

import com.example.android.R;

public class MyView_11 extends SurfaceView implements Callback, Runnable {
private SurfaceHolder sur;
private Canvas canvas;
private Paint paint;

private Thread t;
private boolean threadFlag;

private Bitmap ball;
private float ball_x, ball_y;
private float screen_width, screen_height;
private float ground_x, ground_y;
private Bitmap ground;

private World world;
private Vec2 gravity;
private final float RATE = 30;
private float timeStep = 1 / 60;
private int velocityIterations = 10;
private int positionIterations = 8;

public MyView_11(Context context) {
    super(context);
    sur = this.getHolder();
    sur.addCallback(this);

    paint = new Paint();
    paint.setAntiAlias(true);

    gravity = new Vec2(0, 10);
    world = new World(gravity, true);
}

public Body createCircle(float x, float y, float r) {
    CircleShape circle = new CircleShape();
    circle.m_radius = r / RATE;

    FixtureDef fDef = new FixtureDef();
    fDef.density = 2.1f;
    fDef.friction = 0.5f;
    fDef.restitution = 0.3f;
    fDef.shape = circle;

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(x / RATE, y / RATE);

    Body body = world.createBody(bodyDef);
    body.m_userData = ball;
    body.createFixture(fDef);
    return body;
}

public void logic() {
    world.step(timeStep, velocityIterations, positionIterations);
    Body body = world.getBodyList();
    Vec2 position = body.getPosition();
    ball_x = position.x * RATE;
    ball_y = position.y * RATE;
}

public void initBitmap() {
    ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
    ground = BitmapFactory
            .decodeResource(getResources(), R.drawable.ground);
    ground_x = 0;
    ground_y = screen_height - ground.getHeight();
    ball_x = screen_width / 2 - ball.getWidth() / 2;
    ball_y = 50 + screen_height / 20;
}

public void myDraw() {
    try {
        canvas = sur.lockCanvas();
        if (canvas != null) {
            canvas.drawColor(Color.BLACK);
            canvas.drawBitmap(ball, ball_x, ball_y, paint);
            canvas.drawBitmap(ground, ground_x, ground_y, paint);

        }

    } catch (Exception e) {
    } finally {
        if (canvas != null) {
            sur.unlockCanvasAndPost(canvas);
        }
    }
}

@Override
public void run() {
    if (threadFlag) {
        long startTime = System.currentTimeMillis();
        logic();
        myDraw();

        long endTime = System.currentTimeMillis();

        if ((endTime - startTime) < 50) {
            try {
                Thread.sleep(50 - (endTime - startTime));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    screen_width = this.getWidth();
    screen_height = this.getHeight();
    initBitmap();
    createCircle(ball_x, ball_y, ball.getHeight() / 2);

    threadFlag = true;
    t = new Thread(this);
    t.start();

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    release();
    threadFlag = false;
}

private void release() {
    if (!ball.isRecycled()) {
        ball.recycle();
    }
}

}

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?