box2d(libgdx) stacked bodies not stable

柔情痞子 提交于 2020-01-06 02:58:42

问题


When I make a lot of bodies (rectangles) stacked on each other they don't stand stable. Even if restitution is set to 0. They are bouncy and fall off each other. I tried setting the density to a very low value, but it didn't change.

Is there a possibility to fix that?

    shape.setAsBox(0.1f, 0.1f, new Vector2(0, 0), 0);
    bDef = new BodyDef();
    bDef.type = BodyDef.BodyType.DynamicBody;
    bDef.position.set(0, 0);

    fDef.shape = shape;
    fDef.density = 0.001f;
    fDef.friction = 0.5f;
    fDef.restitution = 0.0f;

    for (int i = 1; i < 50; i++) {
        bDef.position.set(0, i * 0.201f);
        body1 = world.createBody(bDef);
        fixture = body1.createFixture(fDef);
    }

回答1:


So long as this is the problem that I think it is, then yes, this is fixable. Albeit with additional work. But in the short term, no.

When I run the "Vertical Stack" test of the Testbed from Box2D 2.3.2, I see a bunch of boxes drop down on top of each other in a vertical stack. At first they stack, then they wobble, then they tip over. This is how Box2D is designed to work. I didn't like that behavior so I changed it.

I suspect that this is essentially the same problem as you are seeing. The "fix" I used to modify this and gain a stable stacking behavior, involves changing the internal Box2D library code.

Algorithmically speaking, I modified the way position resolution is done from resolving impulses being applied one collision manifold point at a time, to resolving impulses being applied in order of penetration depth or simultaneously when they're almost equal. Code wise, I essentially made this algorithmic change to Erin's b2ContactSolver::SolvePositionConstraints and b2ContactSolver::SolveTOIPositionConstraints methods (in Erin's b2ContactSolver.cpp).

Code for this altered algorithm is in the dev branch of my fork of Box2D (see the SolvePositionConstraint function in the ContactSolver.cpp file). Unfortunately, I don't have a diff that would just apply to those methods as my code changes extend beyond those. Additionally, the changes that I've made modify the Box2D library public interface so even if you can get it to build for your platform, you'll still need to adapt your code to the new interface I have.



来源:https://stackoverflow.com/questions/36825979/box2dlibgdx-stacked-bodies-not-stable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!