This project has moved and is read-only. For the latest updates, please go here.

Texture to Polygon Issues

Topics: Developer Forum
Nov 17, 2012 at 12:04 AM

I am currently having issues with texture to polygon conversion. I am using the function below to create the body. At the end where I am logging it says it created 43 fixtures from my image but when I launch the game the body isn't there and the character falls through the section where it is meant to be. Does anyone know why this would happen?
        public static void ConvertImageToCollision(String texture, ref Body physicsBody)
            //load texture that will represent the physics body
            Texture2D polygonTexture = GameManager.renderManager.screenManager.ContentManager.Load<Texture2D>(texture);   

            //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, true);

            //For now we need to scale the vertices (result is in pixels, we use meters)
            Vector2 scale = new Vector2(100.0f, 100.0f);
            verts.Scale(ref scale);

            //Create a single body with multiple fixtures
            FixtureFactory.AttachCompoundPolygon(BayazitDecomposer.ConvexPartition(verts), 1, physicsBody)

            LoggingManager.Log(Severity.DEBUG, "PhysicsUtils", "Created " + physicsBody.FixtureList.Count() + " fixtures");

            physicsBody.Enabled = true;

Nov 19, 2012 at 8:36 AM

OK I finally got this working ok. Although now I am having an issue where when i use multiple shape detection they all stack on top of each other. Does anytone know how to resolve this?

Nov 20, 2012 at 8:20 PM

Take a look at AdvancedDemo5 included in the XNA Samples. (You get it under downloads).

I load in the alphabet from a texture and create decomposed bodies from all the letters. You will have to move each polygon by the centroid and then offset the polygons by the distance you want.

Nov 21, 2012 at 8:05 AM
Thanks GenBox I have it working now :) I also have another issue. When converting one of my levels ( it seems to be taking hours to process. Now this isn't too much an issue as I am doing it offline then caching the results so I can use textures above the limit but I have had this runnng for 4 1/2 hours now and it is still detecting vertices. Is this level too complex for the converter or should I just leave it running and it will finally finish?
Nov 22, 2012 at 1:34 AM

I think you might have hit an infinite loop in that map. It should be able to detect the vertices within a second.

Nov 22, 2012 at 8:57 AM

Ok did some debugging last night and yeah it was stuck in a loop. I seems to get stuck on 

if (SearchNextHullEntrance(detectedPolygons, polygonEntrance.Value, out polygonEntrance))
          searchOn = true;

with the polygonEntrance constantly being the same. Do you know why this could happen? 

Nov 25, 2012 at 12:34 AM

I don't know what is going on. The algorithm was contributed by a user. I will contact him with the image you supplied and see if he is able to figure out what is going on.

Nov 26, 2012 at 7:13 AM
Edited Nov 26, 2012 at 8:17 AM

Have no fear the user is here.

First of all: Wow. I never tested the algorithm with a texture this big. It's 5161x6987. When Farseer 2 was the latest version, everyone would have told you to split up your texture, because the collision detection algorithm would have problems and your game would render 15 frames per hour at max or objects would fall through walls. AFAIR. It's different now with the triangulation algorithm in the engine, etc.

If you need a fix right away, try less detail with higher HullTolerance. It will generate less detailed results but will also (hopefully) move away from the location where the algorithm falls into the infinite loop. Set it to a value over 1.5f (max is 4f). You can also try to split your texture into smaller ones.

If it doesn't help, then please post the exact pixel position in the texture. Maybe I'll see the error immediately or can at least tell you how to edit your texture.

I currently have very little spare time: In ten days I'll become 30 years old and I've set this as my Now-Or-Never-Deadline. I have bet on it :). ... my small game is at about 85%.

That means: You'll have to wait about two weeks. Then I can take a look at the algorithm to optimize the performance or fix bugs. I'm sorry. It's really bugging me, but I have to focus or else I'll fail with my other plans -.- ...

Nov 26, 2012 at 10:01 AM

Hi SickBattery

Cheers for the response. Yeah I ended up splitting it up and it worked fine :). Yeah I am doing all this processing offline just so there isn't massive amount of bodies being created and we have the best polygon we can get for it. I was going to have a look at it but I am in the same boat as you lots going on and not had a lot of time of my hands so I just told my team to split it. Cheers for committing the tool though been really handy :)

Nov 28, 2012 at 2:35 AM

Sickbattery is the original author of the algorithm. Thanks a lot for helping out!

To Sickbattery: Get that game done and come do magic with me :) also post a link to your game so I can see what crazy stuff you have been up to, and also where I can buy it!

To Phoenixknight88: Sorry for hijacking your thread. Glad it worked out for you.