XNA DebugOverlay Utility
The DebugOverlay utility is a class that I’ve been using in my XNA projects for a while. It allows me to draw visualization objects from anywhere in my application – even in separate threads.
Some areas where I have used this utility are:
- The AI system can display a units’ velocity and position through Lines, Arrows, and Spheres. Use ScreenText to show the current state or goal hierarchy of a given unit.
- The Physics system can use bounding boxes and spheres to show collision volumes. Points can be used to show raycast intersections or points of impact.
- The Graphics system can use Spheres, Lines, and Arrows to show the position and direction of lights.
- The Editor (or other tools) can use Lines, Arrows, and Bounding Boxes for axis representation during translate/rotate/scale.
- Vertex Display – For a software-based skinning implementation, I used Points to visualize the locations of my vertices, and Spheres for the locations of the bones. It’s possible to use Spheres for everything, but thousands of Spheres can get a little expensive.
This utility is only meant for Debug purposes, and therefore uses the [Conditional("DEBUG")] tag on each of it’s exposed methods.
To use the DebugOverlay, create the object during Initialization:
1 | new DebugOverlay(GraphicsDevice, Content); |
Call the visualization functions from anywhere in your code:
1 2 3 4 5 6 | void ScreenText(string text, Vector2 pos, Color c) void Line(Vector3 start, Vector3 end, Color color) void Arrow(Vector3 start, Vector3 end, float arrowSize, Color color) void BoundingBox(BoundingBox boundingBox, Color color) void Point(Vector3 pos, Color color) void Sphere(Vector3 pos, float radius, Color color) |
In your Draw routine, call the DebugOverlay.Draw method, passing in the projection and view matrices:
1 | DebugOverlay.Singleton.Draw(mCamera.ProjectionMatrix, mCamera.ViewMatrix); |
I made a small sample application to show off the functionality. Below is a screenshot and the complete source code.
Download: source + demo