This project has moved. For the latest updates, please go here.

Rounded Rectangle

Topics: User Forum
Feb 20, 2011 at 3:27 PM

I'm using a rounded rectangle for my player car. But I don't get it how it is really working..

I have looked up the RoundedRectangle.cs file in the testbed project! So that's the way you create the rounded rectangle:

List<Fixture> rounded = FixtureFactory.CreateRoundedRectangle(World, 10, 10, 2.5F, 2.5F, _segments, 10, position); 
rounded[0].Body.BodyType = BodyType.Dynamic; // you are only setting the DynamicType to Index 0, is that right?

Okay, and that is my code:

private List<Fixture> bodyCar;

bodyCar = FixtureFactory.CreateRoundedRectangle(HelpfulGlobals.Instance.physicsSimulator, ConvertUnits.ToSimUnits(playerItem.Texture.Width), ConvertUnits.ToSimUnits(playerItem.Texture.Height - 10), ConvertUnits.ToSimUnits(playerItem.Texture.Width / 3f), ConvertUnits.ToSimUnits(playerItem.Texture.Height / 3f), 10, 1f);       

bodyCar[0].Body.Position = ConvertUnits.ToSimUnits(new Vector2(1200, 100));
bodyCar[0].Body.Mass = 1000;
bodyCar[0].CollisionFilter.CollisionGroup = (int)Category.Cat2;
and so on...

Do i have to set all Fixtures to the right values, because the player falls thorugh objects? For example:

 for (int i = 0; i < bodyCar.Count; i++) {

bodyCar[i].Body.Position = ConvertUnits.ToSimUnits(new Vector2

(1200, 100));

bodyCar[i].Body.Mass = 1000;

and so on...

First I used a rectangle and everything was working..

Thank you!!

Feb 23, 2011 at 9:04 AM

Have a look at the Box2DManual ( for details how fixtures and bodies and shapes relate to each other. An rounded rectangle has several fixtures which share a common body in your case. So any fixture's Body reference refers to the same body i.e. it makes no difference if you use bodyCar[0].Body, bodyCar[1].Body, ...

Collision filtering is per fixture however. So you have to set it in a for loop like you suggested. Position and Mass only have to be set once.

In our next release we switch to body factories as it makes more sense, cause they are normally the root object of any physics object. Body has been enhanced with helper methods which set certain properties on all of a body's fixtures to the same value. So you might wanna have a look at that already (it is in the current source).

Feb 26, 2011 at 6:35 PM
Edited Feb 26, 2011 at 6:35 PM

Thank you for your great answer!

It's working fine now!