JBox2D Circle 在与另一个 Body 碰撞时开始垂直或水平移动

JBox2D Circle starts moving vertically or horizontally when it collides with another Body

我正在尝试使用 Java 中的 JBox2D 库让球在 4 面墙之间弹跳。上面的代码是我用来创建世界中的球的代码。

    // Creating the Body Definition
    BodyDef bodyDef = new BodyDef();
    // Set position to Body Definition
    bodyDef.position.set(x, y);
    // Setting body type to body definition
    bodyDef.type = bodyType;

    // Creating CircleShape object
    CircleShape circleShape = new CircleShape();
    // Setting radius to CircleShape
    circleShape.m_radius = radius;

    / /Creating Fixture Definition object
    FixtureDef fixtureDef = new FixtureDef();
    // Setting circleShape as shape of fixture definition
    fixtureDef.shape = circleShape;
    // This defines the heaviness of the body with respect to its area
    fixtureDef.density = density;
    // This defines how bodies slide when they come in contact with each other.
    // Friction value can be set between 0 and 1. Lower value means more slippery bodies.
    fixtureDef.friction = friction;
    // This define how bouncy is the body.
    // Restitution values can be set between 0 and 1.
    // Here higher value means more bouncy body.
    fixtureDef.restitution = restitution;

    // "Uploading" the ball into the world
    Body body = world.createBody(bodyDef);
    // Setting fixtureDef as body's fixture
    body.createFixture(fixtureDef);

这是我用来制作 wall 的代码。比如右墙。

    // Creating the Body Definition
    BodyDef bodyDef = new BodyDef();
    // Set position to Body Definition
    bodyDef.position.set(850f, 0f);
    // Setting body type as static
    bodyDef.type = BodyType.STATIC;

    // Creating CircleShape object
    PolygonShape polygonShape = new PolygonShape();
    // Set polygon shape as a box
    polygonShape.setAsBox(1f - 44,1000);

    // Creating Fixture Definition object
    FixtureDef fixtureDef = new FixtureDef();
    // Setting circleShape as shape of fixture definition
    fixtureDef.shape = polygonShape;
    fixtureDef.friction = 0f;

    // "Uploading" the ball into the world
    Body body = world.createBody(bodyDef);
    // Setting fixtureDef as body's fixture
    body.createFixture(fixtureDef);

球在与另一个物体碰撞时开始垂直或水平移动。 圆圈在与另一个物体碰撞之前一直很好。正如其他一些帖子所说,我尝试 将球的摩擦力设置为 0,但这对我不起作用 。 这些是我用于球的值:

tileFixture.density = 1f;
tileFixture.friction = 1f;
tileFixture.restitution = 10000f;

我发现了我的错误。问题在于恢复原状的价值。 T他的值很多 所以JBox2D 可以水平或垂直移动。 我将 restitution 的值更改为 1f,现在效果很好。