开发者

jBox2D Circle ends up moving horizontally or vertically

I tried to implement a circle bouncing between 4 wall. But, it dosen't matter what is initial velocity vector of that circle, afters some time it always ends up moving horizontally or vertically. Is it normal behaviour ? If not, how can I fix it ?

Thank you for your responses.

Walls:

BodyDef bd = new BodyDef();
    Body body = w.createBody(bd);
    PolygonDef sd = new PolygonDef();
    sd开发者_运维技巧.density = 0.0f;
    sd.friction = 0.01f;
    sd.restitution = 1f;

    sd.setAsBox(250, 10f, new Vec2(250, 1), 0);
    body.createShape(sd);

    sd.setAsBox(250, 10f, new Vec2(250, 499), 0.0f);
    body.createShape(sd);

    sd.setAsBox(250, 10f, new Vec2(1, 250), (float) (Math.PI / 2));
    body.createShape(sd);

    sd.setAsBox(250, 10f, new Vec2(499, 250), (float) (Math.PI / 2));
    body.createShape(sd);

// Circle :

BodyDef bd = new BodyDef();
    bd.position.set((float) (Math.random() * 400 + 50), (float) (Math.random() * 400 + 50));
    //bd.isBullet = true;
    Body body = w.createBody(bd);
    Vec2 v = new Vec2((float) (Math.random() * 20 - 10), (float) (Math.random() * 20 - 10));
    System.out.println(body + "  " + v);
    body.setLinearVelocity(v);
    body.setAngularVelocity(0.1f);

    CircleDef sd = new CircleDef();
    sd.radius = (float) (Math.random() * 40 + 5);
    sd.density = 15.0f;
    sd.restitution = 0.8f;

    body.createShape(sd);
    body.setMassFromShapes();


Try setting the ball's fixture's friction to 0.

My story:

I had a similar setup and problem (4 walls, no gravity, ball bouncing inside - under LibGDX though, not jBox2D). After hours of searching and fiddling, I discovered that setting the ball fixture's friction to 0 made the ball bounce/reflect properly inside the walls. Non-zero and it would bounce a few times at odd reflection angles (not square / right angle, as expected), ending in a steady state bouncing horizontally or vertically.

More info on my case, in case it's relevant to anybody (Google had almost nothing on this!):

After staring at it for a long time I finally realized it wasn't actually bouncing exactly horizontally or vertically. Based on my (admittedly very poor) understanding of physics I think there is a bug somewhere in Box2D; w.l.o.g bouncing against a horizontal wall you would expect v.x *= -1f; but in the steady state it would also do v.y *= -1f. Angular velocity had no effect (I even tried setting fixed angle on the body, and separately adding an angular velocity jitter to the object every tick), and it had nothing to do with the body type (making the wall kinematic, static, and dynamic all yielded the same effect). I tried with both the velocity threshold at 0f and at the default, and with a skin radius and without. Changing Restitution would effect how long it took to end in its horizontal/vertical steady state; but it always reached it nonetheless.

Effect may have to do with (based on what op claimed was happening regarding velocity effecting angle of reflection in gravity-less space): http://www.box2d.org/forum/viewtopic.php?f=3&t=7716

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜