Class DebugDrawExtensions
- Assembly
- Thunder.UnitsNET.Vectors.Geometry.MonoGame.dll
Core DebugDraw infrastructure for rendering geometry shapes as wireframes using MonoGame's Microsoft.Xna.Framework.Graphics.SpriteBatch and a 1×1 pixel Microsoft.Xna.Framework.Graphics.Texture2D technique.
public static class DebugDrawExtensions
- Inheritance
-
DebugDrawExtensions
- Inherited Members
Remarks
Typical usage: call CreatePixelTexture(GraphicsDevice) once at startup to obtain a 1×1 pixel
Microsoft.Xna.Framework.Graphics.Texture2D, then call a shape's DebugDraw extension each frame inside
a SpriteBatch.Begin/SpriteBatch.End block, passing the pixel texture and a
Transform2 that encodes the camera.
// doctest-compile-only
// Once at startup:
var pixel = DebugDrawExtensions.CreatePixelTexture(graphicsDevice);
// Each frame, inside a SpriteBatch.Begin/End block:
myCircle.DebugDraw(spriteBatch, pixel, worldToScreen, Color.Red);
myRect.DebugDraw(spriteBatch, pixel, worldToScreen, Color.Green);
The worldToScreen Transform2 encodes the camera:
- PositionScreen-space origin expressed as metre-lengths (e.g. screen centre (400, 300) =
LengthPoint2.FromMeters(400, 300)). - ScalePixels per physical metre (e.g.
Ratio.FromDecimalFractions(64)= 1m = 64px). - RotationCamera rotation (usually
Direction2.Eastfor no rotation).
Methods
CreatePixelTexture(GraphicsDevice)
Creates a 1×1 white pixel Microsoft.Xna.Framework.Graphics.Texture2D suitable for use with
DrawLine(SpriteBatch, Texture2D, Vector2, Vector2, Color, float) and all DebugDraw extension methods.
public static Texture2D CreatePixelTexture(GraphicsDevice graphicsDevice)
Parameters
graphicsDeviceGraphicsDeviceThe active Microsoft.Xna.Framework.Graphics.GraphicsDevice.
Returns
- Texture2D
A 1×1 Microsoft.Xna.Framework.Graphics.Texture2D filled with Microsoft.Xna.Framework.Color.White.
Examples
// doctest-compile-only
Texture2D pixel = DebugDrawExtensions.CreatePixelTexture(graphicsDevice);
DrawLine(SpriteBatch, Texture2D, Vector2, Vector2, Color, float)
Draws a line between two screen-space points using a 1×1 pixel Microsoft.Xna.Framework.Graphics.Texture2D stretched and rotated to match the line direction and length.
public static void DrawLine(this SpriteBatch sb, Texture2D pixel, Vector2 from, Vector2 to, Color color, float thickness = 1)
Parameters
sbSpriteBatchThe Microsoft.Xna.Framework.Graphics.SpriteBatch to draw with. Must be in an active
Begin/Endblock.pixelTexture2DA 1×1 white pixel texture (see CreatePixelTexture(GraphicsDevice)).
fromVector2Start point in screen pixels.
toVector2End point in screen pixels.
colorColorLine colour.
thicknessfloatLine thickness in pixels. Defaults to 1.
Examples
// doctest-compile-only
spriteBatch.DrawLine(pixel, new Vector2(0, 0), new Vector2(100, 100), Color.Red, 2f);
WorldToScreen(LengthPoint2, Transform2)
Converts a world-space LengthPoint2 to a screen-space Microsoft.Xna.Framework.Vector2 using the given world-to-screen Transform2.
public static Vector2 WorldToScreen(LengthPoint2 worldPoint, Transform2 worldToScreen)
Parameters
worldPointLengthPoint2The point in world space.
worldToScreenTransform2The camera transform. Position = screen origin (in metre-pixels), Scale = pixels per metre.
Returns
- Vector2
Screen-space position in pixels.