HAM's looking for a full-time Interactive PM/Producer. Got skillz? http://bit.ly/95Kqoo
Twitter

How to set friction on circles in Box2D

AUTHOR
Jon Voth
DATE
23 March 2010

This one stumped me for a while the other day. While building a game I was trying to use a circular Box2D physics object for the main character. I wanted to apply different friction settings to it in different situations (i.e. lower friction while walking on the ground, higher friction while falling/landing to stop quickly). After attempting to set the Shape and Body’s friction property several ways, I stumbled upon the section in the Box2D documentation that states: “Box2d does not support rolling resistance. As a consequence, circles roll forever on flat surfaces with no damping.” A ha! No wonder the friction setting had no effect; I was applying a friction parameter that was being ignored by the engine. The solution was more simple than I could have imagined. Since Box2D ignores “rolling resistance” all one has to do is prevent the circle from “rolling.” To accomplish this, there is a handy Boolean in b2BodyDef called “fixedRotation.” Setting fixedRotation to true prevents the body from rotating or responding to angular force. Here’s how that looks in pseudocode:

//Create Body Definition
_bodyDef = new b2BodyDef();

//Prevent rotation
_bodyDef.fixedRotation = true;

//Create body in new b2World
_box2DWorld = new b2World();
_box2DWorld.CreateBody(_bodyDef);

As I mentioned, this solution cost me a couple hours. Hopefully it can save you a couple in return.