This project has moved and is read-only. For the latest updates, please go here.

FPE2 to 3 - Landscape

Topics: Developer Forum, User Forum
Aug 31, 2010 at 1:16 PM


since two days I am trieng to upgrade my game to Farseer Physics 3.0.
In the old version of Farseer I always had some problems with the player car. It got stuck in the track and some performance issues on the Xbox360.

In 2.3 I created the track in the following way:

bodyTrack = BodyFactory.Instance.CreatePolygonBody(verts2, 1f);
bodyTrack.IsStatic = true;        
geomTrack = GeomFactory.Instance.CreatePolygonGeom(GameHelpfulGlobals.Instance.physicsSimulator, bodyTrack, verts2, 0.35f);geomTrack.CollisionGroup = (int)CollisionCategory.Cat1;

I found out that I should use EdgeShapes in 3.0, so I updated it:

 for (int i = 0; i < verts2.Count-1; i++)
                Vector2 vec1 = new Vector2(verts2[i].X, verts2[i].Y);
                Vector2 vec2  = new Vector2(verts2[i+1].X, verts2[i+1].Y);

                FarseerPhysics.Collision.Shapes.PolygonShape shape = new FarseerPhysics.Collision.Shapes.PolygonShape();
                shape.SetAsEdge(vec1, vec2);
                ground.CreateFixture(shape, 1);            
               ground.IsStatic = true;
               ground.BodyType = BodyType.Static;
               ground.IgnoreGravity = true;

Is this really the best way in 3.0? Sometimes I am getting an error...

How can I add the ground Body to the CollisionCategory.Cat1? Because my player checks different collisions with the CollisionCategories!

And by the way: what happend with the ConvertUnits.ToSimUnits, ConvertUnits.ToDisplayUnits? Should I convert my textures to the SimUnits or can I use the DisplayUnits in the Farseer Physics Engine 3.0?

Thank you!

Aug 31, 2010 at 1:31 PM

This is the error message I am getting sometimes... but I am not sure were the error happens exactly.. TrackItem.cs(300), TrackItem(284), or Player.cs(721)? Thx

Sep 1, 2010 at 3:03 PM

Check that your vertices are being created counterclockwise. Maybe there could be the error.

Sep 1, 2010 at 3:59 PM

Thank you, I will check this!
And how about the ConvertUnits.ToSimUnits? Because I have to add huge values to get my player to drive:

bodyCar.Body.ApplyForce(new Vector2(5000000, 0f)); 

And the player is falling down very slow too, but it's mass is 100 and the world's grafity is new Vector2(0,150f);

Is this because I am using the Display Units? The image of the player is 300x200 pixels and i add them 1:1 to farseer..

Sep 2, 2010 at 10:43 AM

About the units, thinks that in farseer the units are Meters, Kilograms and Seconds. So the numbers should be lower when converting your game to FP3.0. As I see from the impulse you need to apply to the body, the body seems to be quite heavy, try to make it lighter, if in your game, the body is the rollercoaster, think about 100 Kg (I guess that's the weight a rollercoaster has :P ). Also instead of apply force, if your goal is to move an object try applying impulse, you should need lower values.

Sep 4, 2010 at 5:59 PM
Thank you for the help!!! In the following days I could fix some problems:

1st Problem - Mass and the huge values - solved

After reading following at the Box2D Manual:

Being a 2D physics engine, it is tempting to use pixels as your units. Unfortunately this will lead to a poor simulation and possibly weird behavior. An object of length 200 pixels would be seen by Box2D as the size of a 45 story building. Imagine trying to simulate the movement of a high-rise building with an engine that is tuned to simulate ragdolls and barrels. It isn't pretty.

So I started again with my old FP2.3 project and ported it with the ConvertUnits-Class to FP3.0. This helped a lot and I can use the old lower values now :)


2nd Problem - The error that happened sometimes... - solved too

I always got this strange error( Due the bad performance on creating geometry objects in FB2.3 on the xbox360, I made a 2nd thread to load track parts while playing. But in FP3.0 you are not allowed to create bodies during callbacks. 

  while (GameHelpfulGlobals.Instance.physicsSimulator.IsLocked)

After adding this line before creating the body the error was gone :)


3rd Problem - Roller Coaster Car gets stuck in the car - NEW

I got a new problem with my roller coaster car, sometimes it gets stuck in the track.. To show you the problem I have uploaded a small video to youtube:

Creating the car:

 bodyCar = FixtureFactory.CreateRectangle(GameHelpfulGlobals.Instance.physicsSimulator,ConvertUnits.ToSimUnits(112f),ConvertUnits.ToSimUnits(34f),0.3f);

bodyCar.Body.Mass = 1000;
bodyCar.Body.Inertia = 0.0f; 
bodyCar.Body.AngularDamping = 10f;
bodyCar.Body.BodyType = BodyType.Dynamic;
bodyCar.Friction = 0.1f;

bodyCar.CollisionGroup = (int)CollisionCategory.Cat2;
bodyCar.CollisionCategories = CollisionCategory.Cat2;
bodyCar.CollidesWith = CollisionCategory.Cat1 & CollisionCategory.Cat4 & CollisionCategory.Cat5
                                 & CollisionCategory.Cat6;
bodyCar.Body.Active  = true;

For moving the object, I'm using the LinearVelocity value:

if ( input.CurrentKeyboardStates[0].IsKeyDown(Microsoft.Xna.Framework.Input.Keys.W))
	if (bodyCar.Body.LinearVelocity.X <= 5f)
		bodyCar.Body.LinearVelocity = new Vector2(bodyCar.Body.LinearVelocity.X + 0.3f, bodyCar.Body.LinearVelocity.Y);
		bodyCar.Body.ApplyForce(new Vector2(0f, -100f));

But the car also gets stuck if I am not pressing the W Key...
This makes the game unplayable at the moment :( Do you have any ideas what could solve the problem?

Thank you!!

Sep 5, 2010 at 1:12 AM

I've had the same issue with things not sliding smoothly along edge shapes.  I can't prove it, but I think there might be an issue with edgeshapes.  They were suppose to be designed specifically for smooth sliding behavior.  Rectangles, however do not slide nicely along them.


There is a way you can minimize the problem. For me it was "good enough", but not perfect.

Change your car from a rectangle to a rounded rectangle ("CreateRoundedRectangle") and play around with the rounding and edges parameters.  You want to avoid sharp edges on things that are sliding.


Hope this helps.

Sep 6, 2010 at 3:59 PM
Thank you - this helped a lot!!

I have uploaded a new video with the rounded rectanlge:

It is really not perfect but it is okay!  and a huge difference between the both videos :)

But is it the right way I am using it?!

List<Fixture> bodycars = FixtureFactory.CreateRoundedRectangle(GameHelpfulGlobals.Instance.physicsSimulator, ConvertUnits.ToSimUnits(112f), ConvertUnits.ToSimUnits(34f), ConvertUnits.ToSimUnits(100f/2f), ConvertUnits.ToSimUnits(30f/2f),10, 1f); 
bodyCar = bodycars[0]; 

BodyCar was my old Fixture, and then I am adding everything to the bodyCar:

bodyCar.Body.Position = ConvertUnits.ToSimUnits(startPosition);
bodyCar.Body.Mass = 100;
bodyCar.CollisionGroup = (int)CollisionCategory.Cat2;
bodyCar.CollisionCategories = CollisionCategory.Cat2;
and so on...

Or do I have do add the position and everything to all Fixtures in the List of the created rounded rectangle?(In Debugmode the List has 8 Fixtures in it) Thank you!!