开发者

jbox2d on Android - circle not applied gravity and other staff

开发者 https://www.devze.com 2023-04-04 19:02 出处:网络
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 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

    }

}


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);

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号