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

Drawing a body made of polygons

Topics: Developer Forum, User Forum
Apr 2, 2012 at 10:55 AM


I'm just a beginner in using Farseer, and I wonder what's the best function to draw a body on the screen which is made of polygons?

I used this piece of code to create polygons from a texture and create a body.

//load texture that will represent the tray
            trayTexture = Content.Load("tray");
            //Create an array to hold the data from the texture
            uint[] data = new uint[trayTexture.Width * trayTexture.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, trayTexture.Width, false);

            //Since it is a concave polygon, we need to partition it into several smaller convex polygons
            _list = BayazitDecomposer.ConvexPartition(verts);
            Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1));
            foreach (Vertices verti in _list)
                verti.Scale(ref vertScale);
        //Creating the body    
	tray = BodyFactory.CreateCompoundPolygon(MyWorld, _list, 10);

So now I have the body and the list of vertices. What function should I use to draw it?

Apr 2, 2012 at 2:47 PM

In order to draw anything from Farseer on the screen, you have to include the Debug view project in your solution. Check out this thread to use the Debug View.

Apr 2, 2012 at 4:18 PM

Thanks, that was very useful but I don't quite understand this piece:



  // calculate the projection and view adjustments for the debug view
            Matrix projection = Matrix.CreateOrthographicOffCenter(0f, _graphics.GraphicsDevice.Viewport.Width / MeterInPixels,
                                                             _graphics.GraphicsDevice.Viewport.Height / MeterInPixels, 0f, 0f,
            Matrix view = Matrix.CreateTranslation(new Vector3((_cameraPosition / MeterInPixels) - (_screenCenter/ MeterInPixels), 0f)) * Matrix.CreateTranslation(new Vector3((_screenCenter / MeterInPixels), 0f));



I understand what is MeterInPixels, for that I can use ConvertUnits. But I'm not using any camera tools, so what values should I give it instead?

Apr 7, 2012 at 4:02 PM

^^ Anyone?

Apr 9, 2012 at 2:41 AM

Sorry for the late response.

The projection defines the screen size of what will be shown to the player. The view is the particular area in world space that will be shown to the player.

                Matrix projection = Matrix.CreateOrthographicOffCenter(0f, ScreenManager.GraphicsDevice.Viewport.Width.ConvertUnits(),
                         ScreenManager.GraphicsDevice.Viewport.Height.ConvertUnits(), 0f, 0f,
                Matrix view2 = Matrix.CreateTranslation(new Vector3((Vector2.Zero - (player.Position), 0f))
                    * Matrix.CreateTranslation(new Vector3((screenCenter.ConvertUnits()), 0f));

This is what you could use to put the player in the center of the screen at all times.