How can I implement DebugDraw

Jan 6, 2011 at 7:07 PM
Edited Jan 7, 2011 at 5:11 PM

I am having some problems with body placement. It seems like the collision rect is slightly off from the position of my sprite. I grabbed the DebugDraw class from the testbed, and I am trying to use it to help me sort things out.


I have assigned a Font, GraphicsDevice and SpriteBatch to it, newed up an instance on my World.DebugDraw, and call World.DrawDebugData(); on every draw call, but nothing is drawn to the screen. I poked around the testbed some more and see that you are calling DrawCircle, DrawPolygon, etc manually. Is this the key, and if so what do I need to know to pass in to the paramters?

Jan 7, 2011 at 2:37 PM

I don't know what your problem is, but here is my own code for it.


if (World.DebugPhysics)

                        DebugDraw._batch = SpriteBatch;
                        DebugDraw._device = GraphicsDevice;
                        _DebugDraw.Flags = Box2D.XNA.DebugDrawFlags.AABB |
                        Box2D.XNA.DebugDrawFlags.CenterOfMass |
                        Box2D.XNA.DebugDrawFlags.Joint |
                        Box2D.XNA.DebugDrawFlags.Pair |

                        Jemgine.Engine.PhysicsModule Physics = World.GetModule<Jemgine.Engine.PhysicsModule>();
                        Physics.PhysicsSimulator.DebugDraw = _DebugDraw;

                        BasicEffect.Projection = World.CameraModule.Camera.Projection;
                        BasicEffect.View = World.CameraModule.Camera.View;
                        BasicEffect.VertexColorEnabled = true;
                        BasicEffect.TextureEnabled = false;
                        BasicEffect.CurrentTechnique = BasicEffect.Techniques[0];



Jan 7, 2011 at 5:07 PM
Edited Jan 7, 2011 at 5:13 PM

here is what I have

void Initialize()
    DebugDraw._batch = SpriteBatch;
    DebugDraw._device = GraphicsDevice;
    DebugDraw._font = DisplayFont;

    this.debugDraw = new DebugDraw();
    debugDraw.Flags = DebugDrawFlags.AABB |
                      DebugDrawFlags.CenterOfMass |
                      DebugDrawFlags.Joint |
                      DebugDrawFlags.Pair |

    World.Physics.DebugDraw = debugDraw;

void Draw(GameTime gameTime)
   //draw stuff

When the game runs an InvalidOperationException is thrown at the second if-block

The current vertex declaration does not include all the elements required by the current vertex shader.


public void FinishDrawShapes()
    _device.BlendState = BlendState.AlphaBlend;

    if (_fillCount > 0)
        _device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, _vertsFill, 0, _fillCount);

    if (_lineCount > 0)
        _device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, _vertsLines, 0, _lineCount); //exception thrown here
    _lineCount = _fillCount = 0;


Is it because I don't have the effect? What is that doing in this context?



Jan 8, 2011 at 11:42 AM

That's absolutely why. The effect requires certain vertex elements. You're trying to draw using whatever effect was already active, and that effect apparently requires more than just position and color. I suspect you're trying to draw with SpriteBatch's shader, which expects texture coordinates.

I don't even know why the 'debug drawer' wants a sprite batch, actually, or if it's necessary to give it one, since I don't think it actually uses it.

Jan 8, 2011 at 11:50 AM

A little bit of digging reveals that the debugdraw implementation I have is just ripped out of the test bed, and the SpriteBatch is there to support string drawing, which isn't part of the DebugDraw interface itself, and the physics simulator will never use. So we can safely ignore it. It should be fairly trivial for you create a basic effect, set it up, and use it for this.

Jan 8, 2011 at 3:51 PM

Thanks, now that I understand it better I should be able to make it work.