Android 上的 jbox2d - 圆圈未应用重力和其他人员

发布于 2024-12-04 06:48:55 字数 4797 浏览 2 评论 0原文

我正在做一个简单的应用程序,当用户点击屏幕时,应该在画布上放置圆圈,然后将该圆圈放入我定义的物理世界中。

我在“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

    }

}

I am doing simple application that should put circle on Canvas when the user taps the screen and then put that circle in the PhysicalWorld I have defined.

I draw the circle in "onDraw" method, but when it is created its position does not change (seems like the gravity and staff are not applied) and the circle is static (stays on the position where it's created).

can you check this code and tell me if I am doing anything wrong:

[update]

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

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青丝拂面 2024-12-11 06:48:55

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

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

 bodyDef.type = BodyDef.dynamicBody;

我已经查看了 google code 上的文档,我认为你应该使用 world .createDynamicBody(bodyDef);

My experience with box2D is in C++ and iPhone, but it seems your forgetting to make the Ball dynamic.

I've kinda guessed at the solution as I don't know the exact syntax for JBox2D (Please edit if anyone knows actual code)

 bodyDef.type = BodyDef.dynamicBody;

I've had a look at the documentation on google code and I think you should use world.createDynamicBody(bodyDef);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文