1

I am trying to create simple tennis game. Each side has a wall. There are also a box and a ball. My environment doesn't have gravity. The box and the ball don't have any velocity. When the box contacts the ball(I move this by mouse with different speed), it(the ball) just changes its position and doesn't continue moving and sometimes these objects don't collide with each other:

enter image description here

I want the box can strike the ball using different angle and strength.

enter image description here

How can I do this? What should I change in the properties of the ball and the box?

The code snippet is below:

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();
}
4

3 回答 3

3

你通过改变它的位置来移动你的盒子。但碰撞解决的一个重要部分是速度。并且您的盒子的速度始终为零(从 box2d 的角度来看)。因此,您会遇到奇怪的碰撞解决方案

于 2013-07-01T14:26:28.100 回答
1

我认为您的屏幕宽度和高度太大...如果是这种情况,请尝试使用世界宽度和高度... 20x12 单位.. 而不是 800x480 的东西。

于 2013-07-01T13:56:58.567 回答
0

Box2D 不喜欢密度为 0 的物体。通常,模拟以某种方式执行,但行为不正确。例如尝试值 0.3。

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

布尔 b2BodyDef::bullet

这是一个快速移动的物体,应该防止它穿过其他移动的物体吗?请注意,所有实体都无法通过运动和静态实体进行隧道传输。此设置仅在动态实体上考虑。

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

于 2013-07-01T10:33:33.637 回答