Converting List<Vertices> to Vector2[]

Topics: Developer Forum
Jan 24, 2011 at 3:31 PM
Edited Jan 24, 2011 at 3:31 PM

Once I've got a List of Farseer Vertices from a Texture2D, is there a way I can convert it to a Vector2[] array?


 List<Vertices> _list = EarclipDecomposer.ConvexPartition(verts);



I'm looking to create a shape based on the series of points it produces, after the fact.

Ta muchly :)


Jan 24, 2011 at 4:14 PM

What you got is basically a list of lists.

You can convert it to a array of arrays or a list of arrays by using the ToArray() method on the list.


Jan 24, 2011 at 4:22 PM
Edited Jan 24, 2011 at 4:50 PM

Hm, I'm getting 'cannot convert from Vertices to Vector2' errors whenever I try to extract the Vector2s...

Will keep fiddling. :)


Any shunts in the right direction would be much appreciated though ;)

Jan 24, 2011 at 5:06 PM
Edited Jan 24, 2011 at 5:07 PM

Could you post the portion of the code, where you do your conversion?

Most likely you got List<Vertices> wrong. "Vertices" itself is a list. It contains a polygon (consisting of several Vertices/Vector2s hence the plural). You have to use Vertices.toArray() to get a Vector2 array (Vertices inherits from List<Vector2>).

List<Vertices> is actually a list of several polygons and not a list of Vector2s.

You want to do something like this:

for(int i = 0; i < _list.Count; ++i)
    Vector2[] polygon = _list[i].ToArray();
    // process polygon here


Jan 25, 2011 at 9:09 AM

This is the crux of the code. I'm using Krypton for light effects but I'm having some issues getting the two to behave - the 'tiles' in the image below are created from a Texture2D, using an Earclip Decompressor to work out a poly and convert it to fixtures. Works really well, and I was hoping it'd be relatively simple to grab the list of Vertices it produces during that process and use them to create a 'ShadowHull' for each tile, which is what Krypton uses to block out light sources.

It's sort of working, but I'm getting some weird behaviour, as seen in the image.

My light source is hidden by certain parts of the poly, and not others.

Also, no debug ShadowHull debug is being drawn, even though the light's reacting to one. Doing some simple tests, I think that's because Debug only draws ShadowHulls when the vertices it gets fed are clockwise. I'm not sure, but I think the verts I'm getting from the Farseer Earclip aren't. I don't think they're counter-clockwise either, because otherwise a simple reverse() of the list should draw a ShadowHull, right?

So I'm left thinking that my Verts are ordered left-to-right.. or something. I'm not sure.

Any advice? Here's some code for what's happening:


public List<Vertices> getVertsFromTexture(String sTexture, Vector2 vSize)
            //load texture that represents the body
            Texture2D polygonTexture = getTextureFromSpriteSheet(spriteSheet, sTexture);

            //Create an array to hold the data from the texture
            uint[] data = new uint[polygonTexture.Width * polygonTexture.Height];

            //Transfer the texture data to the array

            //Find the vertices that makes up the outline of the shape in the texture
            Vertices verts = PolygonTools.CreatePolygon(data, polygonTexture.Width, polygonTexture.Height, false);
            //Since it is a concave polygon, we need to partition it into several smaller convex polygons
            List<Vertices> _list =  EarclipDecomposer.ConvexPartition(verts); 
            return _list;


I'm then creating my ShadowHulls based on that _list, as discussed:

	    for (int i = 0; i < _list.Count; i++)
                Vector2[] polygon = _list[i].ToArray();

                if (polygon != null && polygon.Length >= 3)
                    var _hull = ShadowHull.CreateConvex(ref polygon);
                    _hull.Position = vPosition;
                    _hull.Angle = fAngle;


I'm hoping I'm just doing something really stupid here... any idea what that might be?


(some of this a crosspost from Krypton ;)


Jan 25, 2011 at 3:29 PM

A series of polygon vertices is always either in clockwise or counterclockwise order. At least for non-selfintersecting and convex polygons. Farseer works only with convex polygons in counterclockwise winding order. So Vertices.reverse() would indeed give you the same polygon in clockwise winding order. So you should check the winding order before adding the polygons to krypton. Only other thing that might be off is vPosition and fAngle... where do you get those from?

Jan 25, 2011 at 4:06 PM

fAngle and vPosition are the object's simple stored position and angle; I've checked them, and they're right and working fine.

Hmm. I'm still convinced it's a clockwise/ counterclockwise issue, but nothing I do seems to make any difference... is there a handy sorting algorithm tucked away in Farseer I can use to run that list through and make sure it's ship shape?

Jan 25, 2011 at 5:12 PM

Well you get convex polygons... there is not much sorting to be done. They can just have one winding order. Somehow ordering the vectors differently (other than reversing the order) will get you a new polygon with a different outline... most likely selfintersecting and concave.

If you mail me your project, i could have a look at it. Never used krypton before though, but it seems to be rather straigth forward.

Jan 25, 2011 at 7:24 PM

Turns out the code was fine; once I'd reversed it with Array.Reverse();

Problem was simply the type of shader that was being used. Working beautifully now.


Thanks for the help though, much appreciated. :)