小编典典

box2d中不正确的碰撞

java

我正在开发一种游戏,其中用户必须击打高速球。为了击球,我使用旋转关节将一个矩形物体与演员连接在一起,并使它的马达以指定速度(马达速度)旋转。现在一切都非常完美,但是当球的速度很高时,它绕过了矩形车身。使用碰撞列表器,我发现发生了碰撞,但是碰撞后球没有被反射。因为只有在球高速运转时才会发生这种情况,所以要碰撞的物体的密度是bcoz。还是它的旋转关节马达?我在这里想念什么吗?

这是两个主体的代码

//矩形体的方法

public Body createRectangleBodyPart(float x, float y, float width,
        float height, short groupIndex) {
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(width*WORLD_TO_BOX, height*WORLD_TO_BOX);
    MassData massData = new MassData();
    massData.mass = 15;
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.KinematicBody;
    bodyDef.position.y = y*WORLD_TO_BOX;
    bodyDef.position.x = x*WORLD_TO_BOX;
     body = world.createBody(bodyDef);
    body.setMassData(massData);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 1;
    fixtureDef.friction = 100f;
    fixtureDef.restitution = 0.5f;
    fixtureDef.filter.groupIndex=groupIndex;
    body.createFixture(fixtureDef);
    shape.dispose();
    return body;
}

//球体的方法

    public Body createRoundBodyPart2(float x, float y, float radius,
        short groupIndex, float density, int mass) {

    CircleShape shape = new CircleShape();
    shape.setPosition(new Vector2(0, 0));
    shape.setRadius(radius*WORLD_TO_BOX ); // *18
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DynamicBody;
    bodyDef.position.y = y*WORLD_TO_BOX;
    bodyDef.position.x = x*WORLD_TO_BOX;
    MassData massData = new MassData();
    massData.mass = 8;
    Body body = world.createBody(bodyDef);
    body.setMassData(massData);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 0.5f;
    fixtureDef.restitution=0.007f;
    fixtureDef.filter.groupIndex = groupIndex;
    body.createFixture(fixtureDef);
    shape.dispose();
    return body;
}

阅读 211

收藏
2020-11-26

共1个答案

小编典典

尝试isBullet=true在球体上使用属性

2020-11-26