Texture to Image Drawing Problem

Aug 6, 2011 at 5:12 AM

Hey,

So I have a game object and I create a body by loading in a texture from file and converting it:

 

 

Texture2D polygonTexture = Texture2D.FromStream(Game.graphicsDevice, File.OpenRead(filepath));
                uint[] data = new uint[polygonTexture.Width * polygonTexture.Height];
                polygonTexture.GetData(data);

                //convert it to a polygon
                Vertices textureVertices = PolygonTools.CreatePolygon(data, polygonTexture.Width, false);
                Vector2 centroid = -textureVertices.GetCentroid();
                textureVertices.Translate(ref centroid);
                Vector2 origin = -centroid;
                textureVertices = SimplifyTools.ReduceByDistance(textureVertices, 4f);
                List<Vertices> list = BayazitDecomposer.ConvexPartition(textureVertices);
                Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1));
                foreach (Vertices vertices in list)
                {
                    vertices.Scale(ref vertScale);
                }

                //creat the body from the verticies
                Body compound = BodyFactory.CreateCompoundPolygon(Game.world, list, 1f, BodyType.Dynamic);
                compound.BodyType = BodyType.Dynamic;

                //set the object's verticies to the polygon and the texture to its image
                obj.body = compound;

 

 

And then I draw it like this in my draw event:

 

GraphicsDevice.Clear(bgColor);
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera2D.get_transformation(graphicsDevice));


obj.body.Position = new Vector2(0, 0);
spriteBatch.Draw(polygonTexture,
ConvertUnits.ToDisplayUnits(obj.body.Position),
                            null,
                            obj.tint,
                            obj.body.Rotation,
                            Vector2.Zero, 1f,
                            SpriteEffects.None,
                            obj.layer);

                spriteBatch.End();

The image doesn't show up at all. What am I doing wrong?

 

Aug 7, 2011 at 1:47 AM

I've found more info about the problem:

ConvertUnits.ToDisplayUnits(obj.body.Position) <== this is wrong
obj.body.Position <== this works.