Silverlight 2 Farseer Physics Geom Visual Helper Method

by Cameron Albert 21. August 2008 21:43

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);
                }
            }
        }

Tags:

Game Development | Silverlight 2 Development | Silverlight Games

Comments

8/24/2008 4:13:21 AM #

pingback

Pingback from geekswithblogs.net

Silverlight Cream for August 23, 2008 -- #354

geekswithblogs.net

Powered by BlogEngine.NET 1.5.0.7
Modified Theme by Mads Kristensen

About the Author

CameronAlbert.com I am Senior Software Development Consultant specializing in Silverlight, WPF and the Microsoft .NET Framework. 

My current project Perenthia is a Silverlight multi-player game based in a fantasy world that combines text adventure games with some moderate graphics

View Cameron Albert's profile on LinkedIn
See how we're connected

Follow cameronalbert on Twitter

 

Recommended Books

Silverlight 4 Business Application Development - Beginner's Guide:

http://www.packtpub.com/microsoft-silverlight-4-business-application-development-beginners-guide/book

Microsoft Silverlight 4 Business Application Development: Beginner’s Guide