Assertion Failed Error (Probably Polygon Related)

Topics: Developer Forum, User Forum
Jul 24, 2011 at 7:57 PM
Edited Jul 24, 2011 at 7:59 PM

Hello all,

Ever since I fixed my previous polygon issue, I have had a new error when using polygons in farseer. When I run my game, I get the following assertion failed error:  

 

If I press ignore, another identical error pops up. However if I keep pressing ignore, the errors eventually stop appearing and the game works perfectly. This is what I find most strange.
If I press abort, visual studio highlights the following lines of code (Note: The highlighted line of code varies between the multitude of error messages):

  • Debug.Assert(edge.LengthSquared() > Settings.Epsilon * Settings.Epsilon); (From FarseerPhysics.Collision.Shapes.PolygonShape.cs.) 
  • Debug.Assert(s > 0.0f); (Also from PolygonShape.cs.)

My polygon creation code (Converts FlatRedBall polygons to Farseer polygons):

        public static Fixture CreateFarseerEntityFromPoly(Polygon source, BodyType bodytype)
        {
            Body body = BodyFactory.CreateBody(physicsworld);
            body.BodyType = bodytype;
            body.Rotation = source.RotationZ;
            Vertices verts = new Vertices();
            foreach (FlatRedBall.Math.Geometry.Point point in source.Points)
            {
                Vector2 pointvector = new Vector2((float)point.X, (float)point.Y);
                verts.Add(pointvector);
            }
            FarseerPhysics.Common.Decomposition.BayazitDecomposer.ConvexPartition(verts);
            PolygonShape shape = new PolygonShape(verts, 1);
            Fixture fixture = body.CreateFixture(shape);
            fixture.Body.Position = new Vector2(source.X, source.Y);
            return fixture;
        }

 

Thanks in advance for your help,
-egg651 

Jul 25, 2011 at 2:14 AM
Edited Jul 25, 2011 at 2:16 AM

The problem is here

FarseerPhysics.Common.Decomposition.BayazitDecomposer.ConvexPartition(verts);
PolygonShape shape = new PolygonShape(verts, 1);

You decompose the vertices, however, you dont store them. I mean, the "verts" you use in the second line are the same than in the first one.
You should do something like this
List<Vertices> list = FarseerPhysics.Common.Decomposition.BayazitDecomposer.ConvexPartition(verts);            
PolygonShape shape = new PolygonShape(list, 1);
Jul 25, 2011 at 2:46 PM
Edited Jul 25, 2011 at 2:46 PM

Unfortunately, your solution does not work, as a PolygonShape will only take Vertices, and not a list of them. However, you did point out my idiocy (usually the root of my programming problems) and help me find a way to make it work. The first thing I noticed was tat the Bayazit decomposer was not working for me, as it was only creating one triangular polygon and not completing the shape. Thankfully, switching to the ear-clipping decomposer solved this. Secondly I realised that I should not be returning a fixture from my function , but a body, as if a polygon is decomposed then the body is going to need multiple fixtures to hold all the different polygons together. Finally, I discovered the BodyFactory.CreateCompoundPolygon command, which was pretty much exactly what I was looking for. It creates shapes and fixtures from a list of vertices and ties them all together with a body. In the end my code ended up like this:  (Note - I've added an unrelated friction setting)

 

        public static Body CreateFarseerEntityFromPoly(Polygon source, BodyType bodytype, float friction)
        {
            Vertices verts = new Vertices();
            foreach (FlatRedBall.Math.Geometry.Point point in source.Points)
            {
                Vector2 pointvector = new Vector2((float)point.X, (float)point.Y);
                verts.Add(pointvector);
            }
            List<Vertices> list = FarseerPhysics.Common.Decomposition.EarclipDecomposer.ConvexPartition(verts);
            Body body = BodyFactory.CreateCompoundPolygon(physicsworld, list, 1);
            body.BodyType = bodytype;
            body.Rotation = source.RotationZ;
            body.Position = new Vector2(source.X, source.Y);
            body.Friction = friction;
            return body;
        }

Thanks!

-egg651

Jul 25, 2011 at 3:38 PM

Yeah, I also use BodyFactory.CreateCompoundPolygon. I adapted my code to fit yours and I didn't reallise that PolygonShape does not take lists of vertices.