Box2D(LibGDX) - 球的奇怪行为

时间:2013-07-01 09:25:34

标签: android box2d libgdx physics

我正在努力创造简单的网球比赛。每一面都有一面墙。还有一个盒子和一个球。我的环境没有引力。盒子和球没有任何速度。当盒子接触球时(我用不同速度的鼠标移动它),它(球)只是改变它的位置并且不继续移动,有时这些物体不会相互碰撞:

enter image description here

我希望盒子可以用不同的角度和力量击球。

enter image description here

我该怎么做?我应该改变球和盒子的属性?

代码段如下:

public void createBall(Vector2 position, Vector2 velocity, float angle, Object userData){
    // First we create a body definition
    BodyDef bodyDef = new BodyDef();
    // We set our body to dynamic, for something like ground which doesnt move we would set it to StaticBody
    bodyDef.type = BodyType.DynamicBody;
    // Set our body's starting position in the world
    bodyDef.position.set(position);

    // Create our body in the world using our body definition
    Body body = world.createBody(bodyDef);

    body.setUserData(userData);
    // Create a circle shape and set its radius to 6
    CircleShape circle = new CircleShape();
    circle.setRadius(10f);

    PolygonShape poly = new PolygonShape();     
    poly.setAsBox(12, 12);

    // Create a fixture definition to apply our shape to
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = circle;
    fixtureDef.density = 0.0f; 
    fixtureDef.friction = 0.2f;
    fixtureDef.restitution = 1f; // Make it bounce a little bit
    fixtureDef.isSensor=false;

    // Create our fixture and attach it to the body
    Fixture f = body.createFixture(fixtureDef);
    f.setUserData("ball");      
    circle.dispose();   
}

  private Body createBox(World world, float width, float height, float density) {
        BodyDef def = new BodyDef();
        def.type =  BodyType.KinematicBody;
        Body box = world.createBody(def);

        PolygonShape poly = new PolygonShape();
        poly.setAsBox(width/2, height/2);

        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = poly;
        fixtureDef.density = 0.0f; 
        fixtureDef.friction = 0.3f;
        fixtureDef.restitution = 0.1f; // Make it bounce a little bit
        fixtureDef.isSensor=false;

        // Create our fixture and attach it to the body
        Fixture f = box.createFixture(fixtureDef);
        f.setUserData("platform");
        poly.dispose();

        return box;
    }
        public void createWall(World world, Vector2 position, float hx, float hy){
    // Create our body definition
    BodyDef groundBodyDef =new BodyDef();  
    // Set its world position
    groundBodyDef.position.set(position);  
        groundBodyDef.type=BodyType.StaticBody;
    // Create a body from the defintion and add it to the world
    Body groundBody = world.createBody(groundBodyDef);  

    // Create a polygon shape
    PolygonShape groundBox = new PolygonShape();  
    // Set the polygon shape as a box which is twice the size of our view port and 20 high
    // (setAsBox takes half-width and half-height as arguments)
    groundBox.setAsBox(hx, hy);
    // Create a fixture from our polygon shape and add it to our ground body  
    groundBody.createFixture(groundBox, 0.0f); 
    // Clean up after ourselves
    groundBox.dispose();
}

3 个答案:

答案 0 :(得分:3)

您正在通过更改其位置来移动您的盒子。但是碰撞解决的一个重要部分是速度。并且你的盒子的速度总是为零(从box2d外观点)。因此,您会遇到奇怪的碰撞解决方案

答案 1 :(得分:1)

我认为你的屏幕宽度和高度太大......如果是这样的话,请尝试使用世界宽度和高度... 20x12单位..而不是800x480。

答案 2 :(得分:0)

Box2D不喜欢实体,其中灯具的密度为0.通常,模拟以某种方式执行,但行为不正确。例如,尝试使用值0.3。

重叠问题可以通过设置标志b2BodyDef :: bullet来解决。来自Box2D参考的描述:

  

bool b2BodyDef :: bullet

     

这是一个快速移动的身体,应该防止穿过其他移动的身体?请注意,所有物体都不会穿过运动和静态物体。此设置仅在动态实体上考虑。

     

警告:    你应该谨慎使用这个标志,因为它会增加处理时间。