I'm having an absolute blast with libgdx, love using it. However, I am a newbie with box2d, so I was hoping someone smarter than me could help me out.
I have a simple test screen, where a bunch of static square tiles make up a floor, and a dynamic body (a circle) bounces around.
What I'm trying to do is to increase the friction of the floor so the ball doesn't roll as much. Like the floor was grass instead of wood.
I found a couple of things online, but neither seem to work. The most promising thing I tried is this:
tileBody.getFixtureList().get(0).setFriction(0.9f);
yet it seems to do nothing.
Reading the box2d docs suggests that I should set friction on the fixture def when I define the object originally:
FixtureDef fdef = new FixtureDef();
fdef.shape = wallshape;
fdef.density = 1.0f;
fdef.friction = 0.9f;
However, this doesn't seem to stop the ball rolling much either.
Is there a better way of doing this? I can reduce the restitution, but that'll just stop it bouncing as much right?
Body groundBody = world.createBody(groundBodyDef);
PolygonShape groundshape = new PolygonShape();
groundshape.setAsBox(30, 1.0f);
FixtureDef groundFixture = new FixtureDef();
groundFixture.density=0.0f;
groundFixture.shape = groundshape;
groundFixture.restitution = .5f;
groundFixture.friction=0f;
groundBody.createFixture(groundFixture);
groundshape.dispose();
This worked for me. Provided the things moving along the ground have friction and aren't made of dark matter, they should slow down.
It sounds more like you want to change the angular damping of the circle body depending on what it's currently touching. Angular damping will inhibit the rotation of the body, but it will not have any effect on linear movement. You could check whether or not this is what you want by doing body->SetAngularDamping( aHighValueLikeSay2to5 ) and seeing what effect you get.
If that looks good, then you'll need to keep track of when the circle is touching something that should alter the angular damping. You can do this by using a contact listener, which gives you BeginContact and EndContact notifications. When you get a BeginContact event, if the contact is between your circle and something that should affect the rolling, increment a counter by one. In EndContact do the same check and decrement the counter. This will give you a value you can check at any time to find out if the angular damping should be changed.
I found one answer in the cocos2d forum.
http://www.cocos2d-iphone.org/forum/topic/17762
Seems that circle shapes are NOT affected by friction in box2d.
So to properly apply friction to a circle you're best approximating a circle with a polygon.