Recall that shapes don't know about bodies and may be used independently of the physics simulation. Therefore Box2D provides the b2Fixture class to attach shapes to bodies.

Fixtures hold the following:

Creating a Fixture

var myFixture = new b2FixtureDef;
Using the above code we can create a new fixture to asscociate with a body.



myFixture.shape = new b2CircleShape(2);
Here '2' is the Radius of the circle.


myFixture.shape = new b2PolygonShape;
myFixture.shape.SetAsBox(4,5); //Half Width, Half Height
This will create a box of size 8x10.


myFixture.shape = new b2PolygonShape;
	new b2Vec2(1 , 1),
			new b2Vec2(3, 1),
			new b2Vec2(5 , 2),
			new b2Vec2(6, 4),
			new b2Vec2(6, 5),
			new b2Vec2(5, 6),
			new b2Vec2(3, 6),
			new b2Vec2(0, 5),
			new b2Vec2(-1, 3),
			new b2Vec2(0, 2)
The above will create a polygon with 6 vertices. You can definitely increase or decrease the number of vertices. Minimum being 2.

There are a few things to be careful of when creating polygon fixtures this way. The vertices must also be specified in clockwise order, and they must describe a convex polygon. A convex polygon is one which, if you were to walk around the edges of it, you always turn the same way at each corner (either always left, or always right). Although a polygon can be created even when it does not always turn in the same direction, they may sometimes behave ABNORMALLY.

When I tried to create the same polygon in Counter Clockwise direction it passed through all the other bodies.


myFixture.shape = new b2PolygonShape;
myFixture.shape.SetAsEdge(new b2Vec2(2,2), new b2Vec2(8,3));
This will create an edge from point (2,2) to (8,3). It is very useful in creating boundaries.

Density, Friction & Restitution

We can set density, friction and Restitution of the body by using the following code.
myFixture.density = 10;
myFixture.friction = .5;
myFixture.restitution = .6; 


Restitution measures how 'bouncy' a fixture is. Like friction, it is given a value between 0 and 1, where zero means that the fixture will not bounce at all, and one means that all the energy of the bounce will be conserved. When two fixtures collide with each other, the resulting restitution tends toward the higher of their restitution values. (Source:


The density of a fixture multiplied by it's area becomes the mass of that fixture. So if two fixtures have same density, the one with bigger area will have higher mass. (Source:


Friction settings can be given a value between 0 and 1, zero being completely frictionless. When two fixtures collide with each other, the resulting friction tends toward the lower of their friction values. (Source:

Attaching Fixture to a Body

var myBody = world.CreateBody(bodyDef);
This will attach fixture "myFixture" to body "myBody".

Changing Fixture Settings during Run Time (for full list click here. Then select b2Fixture & b2FixtureDef from the menu.)

myFixture.SetDensity( ... ); // Any value
myFixture.SetRestitution( ... ); // From 0 to 1
myFixture.SetFriction( ... ); // From 0 to 1

Multiple Fixtures

We can attach multiple fixtures on one body. All the fixtures get added up to form a single body. It is not necessary that these fixtures overlaps. There can be spaces between them.

Consider the following code:
var bd = new b2BodyDef(); // Body Definition -- All fixtures will be attached to this body
bd.type = b2Body.b2_dynamicBody;
var fx = new b2FixtureDef(); // First Fixture
fx.density = 14;
fx.friction = .4;
fx.restitution = .7;
fx.shape = new b2PolygonShape();
fx.shape.SetAsOrientedBox(4,2,new b2Vec2(0,0),0); // Box of 8x4 at position (5,5) (Center position (0,0)) at an orientation of 0 radian
var fx2 = new b2FixtureDef(); // Second Fixture
fx2.shape = new b2CircleShape(.5);
fx2.shape.SetLocalPosition(new b2Vec2(2 ,2)); // Circle of Radius of 0.5 at (2,2) with respect to (5,5) i.e. (7,7) in world axis
var fx3 = new b2FixtureDef(); // Third Fixture
fx3.shape = new b2CircleShape(.5);
fx3.shape.SetLocalPosition(new b2Vec2(-2 ,2)); // Circle of Radius of 0.5 at (-2,2) with respect to (5,5) i.e. (3,7) in world axis
var fx4 = new b2FixtureDef(); // Fourth Fixture
fx4.shape = new b2PolygonShape();
fx4.shape.SetAsOrientedBox(3, 1, new b2Vec2(0, -2), 0); // ((x/2),(y/2), position with reference to body center, orientation clockwise in radians). Box of 6x2 at (0, -2) with respect to (5,5) i.e. (5,3) in world axis. And its orientation is 0. We can give any orientation in radians.
var car = world.CreateBody(bd); // Creating a body car
car.CreateFixture(fx); // Attaching first fixture 'fx'
car.CreateFixture(fx2); // Attaching second fixture 'fx2'
car.CreateFixture(fx3); // Attaching third fixture 'fx3'
car.CreateFixture(fx4); // Attaching fourth fixture 'fx4'
You must have noticed we have used "SetAsOrientedBox" in place of "SetAsBox" to give the box its size and a local position with respect to the body (5,5). We have also used "SetLocalPosition" for positioning the circles.

Iterating over all the Fixtures in the body 'b'

Use the following code to iterate over all the Fixtures in the body 'b'.
var b = new b2BodyDef();
var f = new b2FixtureDef();
for(b = world.GetBodyList(); b; b = b.GetNext())	{
	// Some Code
	for(f = b.GetFixtureList(); f; f = f.GetNext())	{
		// Do something with fixture 'f'

Destroying Fixture

If we want to remove any Fixture from a body, then we are going to use the following code.

By administrator at 12:47:19 PM 2 Comment(s)


Nice!!! Hope you keep going with this!!!

By L2L2L on 12 Oct, 2014 at 03:52:59 AM

Thank you!

By administrator on 12 Oct, 2014 at 05:35:55 AM

Add a Comment

Please enter the email address.Invalid format. (Won't be Displayed)
Notify me of followup comments
Enter the displayed Code: captcha