Nolesh
Nolesh

Reputation: 7018

Box2D(LibGDX) - strange behaviour of ball

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

Upvotes: 2

Views: 2562

Answers (3)

Andrew
Andrew

Reputation: 24846

You are moving your box by changing its position. But an important part of collision resolution is velocity. And your box's velocity is always zero (from box2d look point). Thus you experience strange collision resolution

Upvotes: 3

Paras Mittal
Paras Mittal

Reputation: 1149

I think your screen width and height are too large... if that's the case try using world width and height ... 20x12 units.. not 800x480 something .

Upvotes: 1

Pavel
Pavel

Reputation: 2554

Box2D doesn't like bodies, which fixtures has density 0. Usual, simulation performs somehow, but behaviour is not correct. Try value 0.3 for example.

Overlap problem may be solved by setting flag b2BodyDef::bullet. There description from Box2D reference:

bool b2BodyDef::bullet

Is this a fast moving body that should be prevented from tunneling through other moving bodies? Note that all bodies are prevented from tunneling through kinematic and static bodies. This setting is only considered on dynamic bodies.

Warning: You should use this flag sparingly since it increases processing time.

Upvotes: 0

Related Questions