简体   繁体   English

box2d中不正确的碰撞

[英]not proper collision in box2d

I am developing a game in which the user have to hit a high speed ball. 我正在开发一种游戏,其中用户必须击打高速球。 To hit the ball I have joined a rectangular body with the actor using revolute joint and enabled its motor, to rotate it with a specified speed(motor speed). 为了击球,我使用旋转关节将一个矩形物体与演员连接在一起,并使它的马达以指定的速度(马达速度)旋转。 Now everything is perfect but sometime when ball`s velocity is high , it is bypassing the rectagular body. 现在一切都非常完美,但是当球的速度很高时,它绕过了矩形身体。 Using collision listner i found that collision is happening but ball is not getting reflected after collision. 使用碰撞列表器,我发现发生了碰撞,但是碰撞后球没有被反射。 As this is happening only when ball is on a high speed, is it bcoz of density of bodies that are colliding . 因为只有在球高速运转时才会发生这种情况,所以碰撞的物体密度是bcoz。 Or its the motor of revolute joint that is responsible for it ?? 还是它的旋转关节马达? Am i missing something here?? 我在这里想念什么吗?

Here is the code for both bodies 这是两个主体的代码

// method for rectangular body //矩形体的方法

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

// method for ball 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;
}

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

Have you tried playing around with these properties: density, friction and restitution. 您是否尝试过以下这些特性:密度,摩擦力和恢复性。

The ball could be moving so fast that on impact with the rectangular body, the force of the ball is to high for the rec body. 球的移动速度可能如此之快,以至于与矩形物体发生碰撞时,球的力对于直肠物体而言会很大。 Which means that the ball cannot be stopped by the rec body, that why it is passing through it. 这意味着球不能被rec主体挡住,这就是为什么它会通过。

Just a guess. 只是一个猜测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM