Android上的jbox2d - 圈子没有应用重力和其他工作人员

时间:2011-09-12 12:11:40

标签: android graphics box2d game-development jbox2d

我正在做一个简单的应用程序,当用户点击屏幕然后将该圆放在我定义的PhysicalWorld中时,应该在Canvas上放置圆圈。

我在“onDraw”方法中绘制圆圈,但是当它被创建时,它的位置不会改变(看起来像重力和工作人员没有应用)并且圆是静态的(保持在它创建的位置)。 / p>

你可以查看这段代码并告诉我我是否做错了什么:

[更新]

public class PhysicsWorld extends SurfaceView implements SurfaceHolder.Callback {


    private AABB worldAABB;
    private World world;

    private PolygonDef groundShapeDef; 
    public int W_width, W_height;
    private static final String TAG = PhysicsWorld.class.getSimpleName();
    protected static final int GUIUPDATEIDENTIFIER = 0x231;
    public int targetFPS = 40; 
    public float timeStep = (10.0f/targetFPS);
    public int iterations = 5   ; 
    private int count=0;
    private Body[] theBodies ;
    private Paint paint;
    private float radius = 20;

    private MyThread mMyThread;

    public PhysicsWorld(Context context) {
        super(context);
    }

    public PhysicsWorld(Context context, AttributeSet set) {
        super(context, set);

        getHolder().addCallback(this);
        W_width = 500;
        W_height = 700;
        worldAABB = new AABB();
        Vec2 min = new Vec2(-50, -50);
        Vec2 max = new Vec2(W_width+50, W_height+50);
        worldAABB.lowerBound.set(min);
        worldAABB.upperBound.set(max); 
        Vec2 gravity = new Vec2((float) 10.0, (float) 9.8);
        boolean doSleep = false;
        world = new World(worldAABB, gravity, doSleep); 

        BodyDef groundBodyDef = new BodyDef();
        groundBodyDef.position.set(new Vec2((float) 0.0, (float) -10.0));
        Body groundBody = world.createBody(groundBodyDef);
        groundShapeDef = new PolygonDef();
        groundShapeDef.setAsBox(W_width, 10);
        groundBody.createShape(groundShapeDef);

        // up :
        groundBodyDef = new BodyDef();
        groundBodyDef.position.set(new Vec2((float) 0.0, (float) (W_height + 10.0)));
        groundBody = world.createBody(groundBodyDef);
        groundShapeDef = new PolygonDef();
        groundShapeDef.setAsBox(W_width, 10);
        groundBody.createShape(groundShapeDef);

        // left :
        groundBodyDef = new BodyDef();
        groundBodyDef.position.set(new Vec2(-10, (float) 0.0));
        groundBody = world.createBody(groundBodyDef);
        groundShapeDef = new PolygonDef();
        groundShapeDef.setAsBox(10, W_height);
        groundBody.createShape(groundShapeDef);

        // right :
        groundBodyDef = new BodyDef();
        groundBodyDef.position.set(new Vec2((float) W_width + 10, (float) 0.0));
        groundBody = world.createBody(groundBodyDef);
        groundShapeDef = new PolygonDef();
        groundShapeDef.setAsBox(10, W_height);
        groundBody.createShape(groundShapeDef);

        theBodies = new Body[50];
        paint = new Paint();
        paint.setStyle(Style.FILL);
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);

//      setWillNotDraw(false);
    }

    public void addBall(int x, int y) {
        BodyDef bodyDef = new BodyDef();
        bodyDef.position.set(x, y);
        Log.d(TAG,"Ball Created At: " + Integer.toString(x) + "," + Integer.toString(y));
        theBodies[count] = world.createBody(bodyDef);

        CircleDef circle = new CircleDef();
        circle.radius = radius;
        circle.density = (float) 1.0; 
        circle.restitution = 0.5f;
        theBodies[count].createShape(circle);
        theBodies[count].setMassFromShapes();
        count+=1;
    }

    public void update() {
        world.step(timeStep, iterations);
        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        paint = new Paint();
        paint.setStyle(Style.FILL);
        paint.setColor(Color.RED);
        for (int j = 0; j < count; j++) {
            canvas.drawCircle(theBodies[j].getPosition().x, W_height - theBodies[j].getPosition().y, radius, paint);
            Log.v(TAG + " x: ", String.valueOf(theBodies[j].getPosition().x));
            Log.v(TAG + " y:", String.valueOf(W_height - theBodies[j].getPosition().y));
        }
    }       

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            addBall((int)event.getX(),(int)event.getY());
        }

        return true;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format,
            int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mMyThread = new MyThread(holder, this);
        mMyThread.setFlag(true);
        mMyThread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }

}

1 个答案:

答案 0 :(得分:0)

我对box2D的体验是在C ++和iPhone中,但似乎你忘了让Ball变得动态。

我有点猜测解决方案,因为我不知道JBox2D的确切语法(如果有人知道实际代码,请编辑)

 bodyDef.type = BodyDef.dynamicBody;

我已经查看了Google代码的文档,我认为您应该使用world.createDynamicBody(bodyDef);