New PolygonShape() Error with triangle vertices

Topics: User Forum
Jan 20, 2011 at 12:50 PM

Hi guys!

I'm just writing a little class that converts Vector Graphics to PolygonShape, but I think I didn't understand something.

Check out the short code:


    public abstract class VectorGraphic
        public float ScaleX = 0;
        public float ScaleY = 0;

        public VectorGraphic()
        { LoadVertices(); }

        protected Vertices _vertices = new Vertices();

        public abstract void LoadVertices();

        public Vertices GetVertices()
            Vertices scaledVertices = new Vertices();
            _vertices.ForEach(vert =>
                scaledVertices.Add(new Vector2((vert.X - 0.5f) * ScaleX, (vert.Y - 0.5f) * ScaleY));
            return scaledVertices;



    public class vgRectangle : VectorGraphic
        public override void LoadVertices()
            _vertices.Add(new Vector2(0, 0));
            _vertices.Add(new Vector2(1, 0));
            _vertices.Add(new Vector2(1, 1));
            _vertices.Add(new Vector2(0, 1));


    public class vgTriangle : VectorGraphic
        public override void LoadVertices()
            _vertices.Add(new Vector2(0.5f, 0.1f));
            _vertices.Add(new Vector2(0.2f, 0.8f));
            _vertices.Add(new Vector2(0.8f, 0.8f));


(Somewhere else I am doing this:       newentity.Fixture = new Fixture(newbody, new PolygonShape(vectorGraphic.GetVertices(), 1));)

I think it should be rather clear what I am trying to do.  Note that I am subtracting 0.5f in GetVertices. I have had a Problem with vgRectangle, because Farseer ended at PolygonShape.cs with
     // Your polygon is non-convex (it has an indentation) or has colinear edges.
so I thought it is intended to have the Collection of Vertices around the zero point.
Everything went just fine then, but now I can't bring vgTriangle to work, although everything seems to be ok.

Any hints?

best regards



Jan 21, 2011 at 4:37 PM

I just cant manage to do this D=

How do you make a triangle, I wasn't able to find one in the samples,
Can anyone help? 

Jan 21, 2011 at 4:38 PM

Well ScaleX and ScaleY are = 0 which would cause your polygons to have all vertices equal to Vector2.Zero.

Also polygons must be wound counter-clockwise. I didn't check yours, but using Vertices.ForceCounterClockwise can make sure they are wound right.

I hope this helps.

Jan 21, 2011 at 4:57 PM
Edited Jan 21, 2011 at 6:55 PM

Sorry, but even with 1 the same thing happens, this is my code: 


            VectorGraphic vg = new vgTriangle();
            vg.ScaleX = 1;
            vg.ScaleY = 1;
            Body newBody = new Body(_world);
            Fixture newfix = new Fixture(newBody, new PolygonShape(vg.GetVertices(), 1 ));

 =>    // Your polygon is non-convex (it has an indentation) or  has colinear edges.
It's really strange D=

OK,  now i have it, sry, didn't know that you have to lead the lines  around the body centre clockwise D|