Texture to Polygon

Mar 9, 2010 at 2:50 PM

I've been trying to figure out how to create a polygon from a texture shown in the youTube video and I just can't seem to get it. Is there some tutorial/documentation on how to actually pull this off? 

If you care to see it, here's the code I've been trying to get to work.

 

           content = Content;
            sim = Simulator;
            levelArea = LevelArea;
            waveTexture = content.Load<Texture2D>("tempWave");
            waveOrigin = new Vector2(waveTexture.Width / 2, waveTexture.Height / 2);
            uint[] data = new uint[waveTexture.Width * waveTexture.Height];
            waveTexture.GetData(data);
            verts = new IList<Vertices>();
            verts = (Vertices.CreatePolygon(data, waveTexture.Width, waveTexture.Height, 4f, 1, true, true));
            vertices = new Vertices(verts);
            waveBody = BodyFactory.Instance.CreatePolygonBody(sim, vertices, 1);

 

       public Wave(ContentManager Content, PhysicsSimulator Simulator, Rectangle LevelArea)
        {
            content = Content;
            sim = Simulator;
            levelArea = LevelArea;
            waveTexture = content.Load<Texture2D>("tempWave");
            waveOrigin = new Vector2(waveTexture.Width / 2, waveTexture.Height / 2);
            uint[] data = new uint[waveTexture.Width * waveTexture.Height];
            waveTexture.GetData(data);
            verts = new List<Vertices>();
            verts = (Vertices.CreatePolygon(data, waveTexture.Width, waveTexture.Height, 4f, 1, true, true));
            vertices = new Vertices(verts);
            waveBody = BodyFactory.Instance.CreatePolygonBody(sim, vertices, 1);
        }

I'm getting an error telling me that the next to last line is trying to convert from Vertices to an int. 

 

 

 

Mar 17, 2010 at 4:30 PM

Did you ever get this working? I can't figure out how to do this either..

Mar 17, 2010 at 4:38 PM

I did get it working but I don't recall what the error was. I ended up using it a ton so I just created a CreatePolygon class. 

 

    class CreatePolygon
    {     
        public CreatePolygon()
        {
           
        }

        public ArrayList Create(Texture2D texture)
        {
            uint[] data = new uint[texture.Width * texture.Height];
            texture.GetData(data);
            Vertices verts = Vertices.CreatePolygon(data, texture.Width, texture.Height);
            Vector2 origin = verts.GetCentroid();
            Body body = BodyFactory.Instance.CreatePolygonBody(verts, 1.0f);
            Geom geom = GeomFactory.Instance.CreatePolygonGeom(body, verts, 48);
            ArrayList info = new ArrayList(2);
            info.Add(body);
            info.Add(geom);
            return info;
        }

    }


Hope that helps you.

 

Mar 17, 2010 at 4:46 PM

Thanks very much, that should help a lot