PolygonTools.CreatePolygon accuracy issue

Topics: Developer Forum, User Forum
Jan 13, 2011 at 2:18 PM

Hey,

Im having a problem with the CreatePolygon(uint[] data, ...) functions.

I pass in a uint[] array for a solid block along with the correct width and height (And have verified the data is indeed what I excpect it to be) but the result is always wrong. If passing in a solid 32 x 32 block the result is always a 31 x 31 square (And strangely made up 10-11 vertices instead of the required 4). For more complex data the result is even worse with hull tolerances over 0.9, with 90 degree angles getting rounded in.

I know theres the CreateRectangle option which would deal with the former but the array may not always contain a solid block of data so thats not an option.

Examples:

http://img508.imageshack.us/img508/319/farseerissue.jpg

Is this a known issue with Farseer or is there a gotcha than Im not aware of?

Thanks.

Coordinator
Jan 13, 2011 at 4:47 PM

It looks like you are using jpg compression and have some compression artifacts in your image. You should consider using a different format.

Developer
Jan 13, 2011 at 4:53 PM

Also there is a CreatePolygon option, which you can use for arbitrarily shaped objects. I think the Texture2Polygon tools might receive an update some time in the future. But if you want full control over how your shapes are created, you should create them manualy.

Jan 14, 2011 at 9:43 AM
Edited Jan 17, 2011 at 8:07 AM
Genbox wrote:

It looks like you are using jpg compression and have some compression artifacts in your image. You should consider using a different format.

 Unfortunately this is not the issue. The level was just zoomed in to more obviously show the gaps which is why it looks so bad. I found no difference between a jpg and bmp format. I have also verified the data (ie. The contents of the uint[]) going in is correct. More information below.

Elsch wrote:

Also there is a CreatePolygon option, which you can use for arbitrarily shaped objects. I think the Texture2Polygon tools might receive an update some time in the future. But if you want full control over how your shapes are created, you should create them manualy.

By CreatePolygon do you mean PolygonTools.CreatePolygon? Because that is what I am using and that is where the problem lies.

I'll provide more information on what I am doing.

I load in a texture image and analyze it (Not using farseer) to produce a list of 32x32 uint arrays containing blocks that make up the level along with their relative positions.  Each of these blocks is then passed in to the CreatePolygon function in turn.

Its at this point the problem occurs. Lets take the simple example of the rectangle: At this point I have a uint[] array which contains 1024 elements, all of which contain the value 0xFF000000, and a size of {32, 32}. So I call

List<Vertices> result = PolygonTools.CreatePolygon(shape.Contents, (int)shape.Size.X, (int)shape.Size.Y);

For the case where the contents specifies a pure 32x32 block of 0xFF00000000 I would expect a result of {0,0}, {32,0}, {32,32}, {0,32}.

Instead the result is:

+		[0]	{X:0 Y:0}		Microsoft.Xna.Framework.Vector2
+		[1]	{X:28 Y:0}	Microsoft.Xna.Framework.Vector2
+		[2]	{X:30 Y:0}	Microsoft.Xna.Framework.Vector2
+		[3]	{X:31 Y:0}	Microsoft.Xna.Framework.Vector2
+		[4]	{X:31 Y:28}	Microsoft.Xna.Framework.Vector2
+		[5]	{X:31 Y:30}	Microsoft.Xna.Framework.Vector2
+		[6]	{X:31 Y:31}	Microsoft.Xna.Framework.Vector2
+		[7]	{X:3 Y:31}	Microsoft.Xna.Framework.Vector2
+		[8]	{X:1 Y:31}	Microsoft.Xna.Framework.Vector2
+		[9]	{X:0 Y:31}	Microsoft.Xna.Framework.Vector2
+		[10]	{X:0 Y:3}		Microsoft.Xna.Framework.Vector2

 I hopes this clears up the issue I am having a bit.

Thanks

Developer
Jan 14, 2011 at 10:38 AM

CreatePolygon(uint[]) was written to trace textures and create geometry automatically from a supplied image. What I suggested instead was, that there are maybe smarter ways to create your level geometry. E.g. by directly specifying the vertices which form the outline of your level.

There is probably lots of room for improvement in the tracing process and I think someone is currently working on it. Converting a bitmap to a vector based image format (and that is what the function does essentially) will never produce perfect and clean results. So either you have to live with the inaccuracies (at least for now) or generate your geometry elsewhere.

Personally (and genbox might beg to differ here :O) I always regarded the vertices from texture approach to be a tool for getting quick prototyping results, as even most commercial solutions often generate a lot more vertices than you would optimally need.

Feb 19, 2011 at 1:35 PM
MyNameIsBob wrote:

I load in a texture image and analyze it (Not using farseer) to produce a list of 32x32 uint arrays containing blocks that make up the level along with their relative positions.  Each of these blocks is then passed in to the CreatePolygon function in turn.

Its at this point the problem occurs. Lets take the simple example of the rectangle: At this point I have a uint[] array which contains 1024 elements, all of which contain the value 0xFF000000, and a size of {32, 32}. So I call

List<Vertices> result = PolygonTools.CreatePolygon(shape.Contents, (int)shape.Size.X, (int)shape.Size.Y);

For the case where the contents specifies a pure 32x32 block of 0xFF00000000 I would expect a result of {0,0}, {32,0}, {32,32}, {0,32}.

Instead the result is:

+		[0]	{X:0 Y:0}	Microsoft.Xna.Framework.Vector2
+		[1]	{X:28 Y:0}	Microsoft.Xna.Framework.Vector2
+		[2]	{X:30 Y:0}	Microsoft.Xna.Framework.Vector2
+		[3]	{X:31 Y:0}	Microsoft.Xna.Framework.Vector2
+		[4]	{X:31 Y:28}	Microsoft.Xna.Framework.Vector2
+		[5]	{X:31 Y:30}	Microsoft.Xna.Framework.Vector2
+		[6]	{X:31 Y:31}	Microsoft.Xna.Framework.Vector2
+		[7]	{X:3 Y:31}	Microsoft.Xna.Framework.Vector2
+		[8]	{X:1 Y:31}	Microsoft.Xna.Framework.Vector2
+		[9]	{X:0 Y:31}	Microsoft.Xna.Framework.Vector2
+		[10]	{X:0 Y:3}	Microsoft.Xna.Framework.Vector2

Yes, I can confirm this.  I provided a 100x100 solid color png image and I expected the following:

{X:0 Y:0}
{X:100 Y:0}
{X:100 Y:100}
{X:0 Y:100}

But I got this instead: 

{X:0 Y:0}
{X:75 Y:0}
{X:88 Y:0}
{X:93 Y:0}
{X:96 Y:0}
{X:98 Y:0}
{X:99 Y:50}
{X:99 Y:75}
{X:99 Y:85}
{X:99 Y:90}
{X:99 Y:93}
{X:99 Y:95}
{X:99 Y:97}
{X:99 Y:99}
{X:24 Y:99}
{X:11 Y:99}
{X:6 Y:99}
{X:3 Y:99}
{X:1 Y:99}
{X:0 Y:49}

Coordinator
Feb 19, 2011 at 9:37 PM

Is this the released 3.2 version or the one from the source control?

Elsch is indeed right in that texture to vertices tools should only be used in the design process and for quick prototypes. You will always get sub-optimal results using it in-game.