Cameron Albert

Ramblings of software development, games and technology.

Silverlight 2 Farseer Physics Geom Visual Helper Method

I ported some code from the Farseer Physics Engine XNA demos that displayed  a debug view of the geom vertices used with the Physics Simulator. This is useful if you want to see where your geoms are being rendered to the screen to make sure you have everything in the right place. I know I need this a lot, helps me to understand where I am placing body geoms.

Anyway, here is the method, the _debug variable is just a Canvas I added at the top of the UI. Keep in mind that this method will slow your game WAY down so be sure and use it to test where geoms are positioned and if everything is moving properly. I use a static variable for the PhysicsSimulator, hence the cleverly named Physics.Simulator.

        private void DrawVertices()
        {
            _debug.Children.Clear();
            int verticeCount = 0;
            for (int i = 0; i < Physics.Simulator.GeomList.Count; i++)
            {
                verticeCount = Physics.Simulator.GeomList[i].LocalVertices.Count;
                for (int j = 0; j < verticeCount; j++)
                {
                    Line line = new Line();
                    line.Fill = new SolidColorBrush(Colors.Transparent);
                    line.Stroke = new SolidColorBrush(Colors.Magenta);
                    line.StrokeThickness = 1;
                    if (j < verticeCount - 1)
                    {
                        line.X1 = Physics.Simulator.GeomList[i].WorldVertices[j].X;
                        line.Y1 = Physics.Simulator.GeomList[i].WorldVertices[j].Y;
                        line.X2 = Physics.Simulator.GeomList[i].WorldVertices[j + 1].X;
                        line.Y2 = Physics.Simulator.GeomList[i].WorldVertices[j + 1].Y;
                    }
                    else
                    {
                        line.X1 = Physics.Simulator.GeomList[i].WorldVertices[j].X;
                        line.Y1 = Physics.Simulator.GeomList[i].WorldVertices[j].Y;
                        line.X2 = Physics.Simulator.GeomList[i].WorldVertices[0].X;
                        line.Y2 = Physics.Simulator.GeomList[i].WorldVertices[0].Y;
                    }
                    _debug.Children.Add(line);
                }
            }
        }
Advertisements

Written by Cameron

August 21, 2008 at 9:43 pm

%d bloggers like this: