0

I want to create a circle from Vertices in Farseer:

Vertices circleVertices = PolygonTools.CreateCircle(ConvertUnits.ToSimUnits(96), 10);

As you see the radius is 96 px and it has got 10 edges. No problem so far!
Next I wanted to display it (with help of DebugViewXNA):

Body circleBody = BodyFactory.CreatePolygon(_world, circleVertices, 1f);
circleBody.BodyType = BodyType.Static;

Now I get an assertion error:

   at FarseerPhysics.Collision.Shapes.PolygonShape.set_Vertices(Vertices value) in d:\Dateien\Downloads\Farseer Physics Engine 3.5 HelloWorld\Farseer Physics Engine 3.5 HelloWorld\Farseer Physics Engine 3.5\Collision\Shapes\PolygonShape.cs:line 90.
   at FarseerPhysics.Collision.Shapes.PolygonShape..ctor(Vertices vertices, Single density) in d:\Dateien\Downloads\Farseer Physics Engine 3.5 HelloWorld\Farseer Physics Engine 3.5 HelloWorld\Farseer Physics Engine 3.5\Collision\Shapes\PolygonShape.cs:line 50.
   at FarseerPhysics.Factories.FixtureFactory.AttachPolygon(Vertices vertices, Single density, Body body, Object userData) in d:\Dateien\Downloads\Farseer Physics Engine 3.5 HelloWorld\Farseer Physics Engine 3.5 HelloWorld\Farseer Physics Engine 3.5\Factories\FixtureFactory.cs:line 66.
   at FarseerPhysics.Factories.BodyFactory.CreatePolygon(World world, Vertices vertices, Single density, Vector2 position, Object userData) in d:\Dateien\Downloads\Farseer Physics Engine 3.5 HelloWorld\Farseer Physics Engine 3.5 HelloWorld\Farseer Physics Engine 3.5\Factories\BodyFactory.cs:line 112.
   at FarseerPhysics.Factories.BodyFactory.CreatePolygon(World world, Vertices vertices, Single density, Object userData) in d:\Dateien\Downloads\Farseer Physics Engine 3.5 HelloWorld\Farseer Physics Engine 3.5 HelloWorld\Farseer Physics Engine 3.5\Factories\BodyFactory.cs:line 106.
   at FarseerPhysics.Samples.Game1.LoadContent() in d:\Dateien\Downloads\Farseer Physics Engine 3.5 HelloWorld\Farseer Physics Engine 3.5 HelloWorld\Farseer Physics HelloWorld 3.5\Game1.cs:line 124.
   at Microsoft.Xna.Framework.Game.Initialize()
   at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
   at Microsoft.Xna.Framework.Game.Run()

I did the same with a rectangle defined by Vertices:

Vertices groundVertices = new Vertices();
groundVertices.Add(new Vector2(0, 5));
groundVertices.Add(new Vector2(ConvertUnits.ToSimUnits(800), 5));
groundVertices.Add(new Vector2(ConvertUnits.ToSimUnits(800), ConvertUnits.ToSimUnits(480)));
groundVertices.Add(new Vector2(0, ConvertUnits.ToSimUnits(480)));

Body groundBody = BodyFactory.CreatePolygon(_world, groundVertices, 1f);
groundBody.BodyType = BodyType.Static;

There has never been a problem with that.
Why can't the circle work too?

user2737037
  • 1,119
  • 1
  • 16
  • 29

1 Answers1

0

As I found out by actually looking into the file where the assertion failed, I found out, that the maximum number of Vertices per Polygon is limited to 8 in Farseer Physics library. For this to work the circleVertices must be triangulated (or at least form smaller Polygons):

List<Vertices> circleTriangulated = Triangulate.ConvexPartition(circleVertices, TriangulationAlgorithm.Bayazit);

Then the circle can be displayed as compound body.

Body circleBody = BodyFactory.CreateCompoundPolygon(_world, circleTriangulated, 1f);
user2737037
  • 1,119
  • 1
  • 16
  • 29