情过南山 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条回答

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器