Weird Error Thrown On RenderDebugData() Method

Dec 20, 2013 at 1:12 AM
Hello. I have a body that is being created using the BodyFactory.CreatePolygon() method and I am sure that this is causing the problem. When I send 9 vertices into the method, an error is thrown in a completely different place in the Draw() method when the RenderDebugData() method is called. If I reduce the quantity of vertices to less then 9, this error is not thrown.

This is the code for the debug draw render method. This has always worked just fine until now.
var projection = Matrix.CreateOrthographicOffCenter(0f, ConvertUnits.ToSimUnits(_graphics.GraphicsDevice.Viewport.Width),
ConvertUnits.ToSimUnits(_graphics.GraphicsDevice.Viewport.Height),
                                                                0f,
                                                                0f,
                                                                1f);
_debugView.RenderDebugData(ref projection);
Is there a limit to how many vertices I can use per polygon? I also thought maybe it has to do with 2 vertices being to close to each other. Is this a possibility?

Here is the error I get. Its an error about accessing an index of an array outside the bounds of the array. I assume its the array that is created from the projection variable? Totally stumped. Any help would be great.

Error
IndexOutOfRangeException was unhandled

Index was outside the bounds of the array.
Developer
Dec 20, 2013 at 11:12 AM
KinsonDigital wrote:
Is there a limit to how many vertices I can use per polygon?
Yes. It's eight. You can increase this in Settings.cs. The Debug View creates a fixed vertices array for shape rendering and uses Settings.MaxPolygonVertices from the settings for its size. So there you have your IndexOutOfRangeException.

I presume you are running your program in Release instead of Debug mode? CreatePolygon() should already check the vertices constraint and throw an error. This is disabled for performance reasons in Release mode. So for testing it is a very bad idea to not use Debug mode (or a Debug build of Farseer if you include it via dll instead of code)!

KinsonDigital wrote:
Here is the error I get. Its an error about accessing an index of an array outside the bounds of the array. I assume its the array that is created from the projection variable? Totally stumped. Any help would be great.
Please don't take this the wrong way, but assumptions like this one generally kill my desire to answer any question instantly. This has nothing to do with the problem at all. The projection variable is a matrix and never even converted into an array. It is only related to the camera. Basic knowledge of affine transformations and how projection, world, view matrices work (specifically in XNA with the spriteBatch) should be your main priority before you should even start looking into something as complex as a 2D physics engine. Farseer uses this a lot... for everything. You will have a very hard time setting up joints properly and aligning any rendering related stuff to the physics engines bodies without a basic understanding of these concepts.
Dec 22, 2013 at 11:32 PM
Thanks Elsch. I was pretty sure that the projection had nothing to do with it. Only reason I even mentioned it was cause of where the error was thrown and that was during the draw statement for the debug draw which was right after the projection variable was created. I was under the understanding that the error should of been thrown on the actual polygon creation line. I just wanted to make sure that the amount of information was sufficient for an answer. As for release or debug mode, I am definitely was running it in debug mode.

Is there any reason that I shouldn't use more then 8 vertices? Maybe for performance reasons? Also, do you have any suggested websites, tutorials, or something of the like that will give me a better clear understanding of the matrix transformations and what they are and how they work? I do have some basics but I also know that some further reading and understanding would be a great thing for me and am definitely willing to work on that area. Thanks so much for you help!!