0

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

我在“onDraw”方法中绘制了圆,但是当它创建时它的位置不会改变(似乎没有应用重力和工作人员)并且圆是静态的(停留在它创建的位置上)。

你能检查这段代码并告诉我我是否做错了什么:

[更新]

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

    }

}
4

1 回答 1

0

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

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

 bodyDef.type = BodyDef.dynamicBody;

我看过关于谷歌代码的文档,我认为你应该使用world.createDynamicBody(bodyDef);

于 2011-09-12T12:20:41.083 回答