1
votes

In my LibGDX project I am using Box2D for physics. When I try to make two bodies with no friction (0.0f), it still looks like they have friction when they move against each other. One body is static, rotated 45 degrees and the other one is dynamic. What am I missing?

The world is set with a gravity of 9.8 m/s and the pixels per meter (PPM) is 32. What I want is the two bodies to be totally frictionless. In my example code the second body falls onto the first. What I would expect happen when the bodies have no friction is that the second object slides off. What happens though is that the second object instead stops and "drags" along the first object.

Here's the code:

    Body b1, b2;
    BodyDef def1 = new BodyDef();
    PolygonShape shape1 = new PolygonShape();
    FixtureDef fDef1 = new FixtureDef();
    def1.type = BodyType.StaticBody;
    b1 = world.createBody(def1);
    shape1.setAsBox(64 / 2 / PPM, 32 / 2 / PPM);
    b1.setTransform(20 / PPM, 0, (float)Math.toRadians(60.0));
    fDef1.shape = shape1;
    fDef1.friction = 0.0f;
    fDef1.density = 1.0f;
    b1.createFixture(fDef1);
    shape1.dispose();

    BodyDef def2 = new BodyDef();
    PolygonShape shape2 = new PolygonShape();
    FixtureDef fDef2 = new FixtureDef();
    def2.type = BodyType.DynamicBody;
    b2 = world.createBody(def2);
    shape1.setAsBox(32 / 2 / PPM, 32 / 2 / PPM);
    b2.setTransform(0, 100 / PPM, (float)Math.toRadians(60.0));
    fDef2.shape = shape2;
    fDef2.friction = 0.0f;
    fDef2.density = 1.0f;
    b2.createFixture(fDef2);
    shape2.dispose();
1

1 Answers

0
votes

They are frictionless already - the problem is that you are creating body inside another and Box2D is trying to shove away them. This situation is not even mentioned in official Box2D manual what makes me think that also the result can be unexpected.

If your intention was to create "one body over second" remember that this is Box2d - 2D ;)

To check the bodies working move them with following positions (for example):

    b1.setTransform(new Vector2(20 / PPM, 0), (float)Math.toRadians(45.0));
    b2.setTransform(new Vector2(0, 100 / PPM), (float)Math.toRadians(45.0));

Also notice that if one of the bodies has friction set to 0 the second's one friction does not matter. You can check this by using:

    ...
    b1.createFixture(fDef);

    shape.setAsBox(32/2, 32/2);
    fDef.shape = shape;
    fDef.friction = 1.0f; // <-- ADD THIS
    b2.createFixture(fDef);

Also there are two errors in your code:

  1. BodyDef = new BodyDef(); instead of BodyDef def = new BodyDef(); which is result of typo I guess
  2. There is no BodyType.RigidBody in Box2D! You should use BodyType.DynamicBody there