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

时间:2018-02-11 14:47:52

标签: java collision-detection box2d collision jbox2d

我正在尝试使用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);

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

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

1 个答案:

答案 0 :(得分:0)

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