Debug Draw

Wireframe boxes, spheres, axes, and rays.

▶ Run in browser

Tags: debug

DebugDraw (from simvx.graphics.debug_draw) draws world-space wireframes for state a game cannot otherwise see: collision bounds, raycasts, AI targets. It is immediate mode. Every call queues lines for the current frame only, and the renderer clears the queue once it has drawn them, so anything you want to keep seeing is drawn again each frame from on_update:

def on_update(self, dt):
    DebugDraw.box(self.world_position, (0.6, 0.6, 0.6), colour=(0, 1, 0, 1))

The five calls, all in world coordinates; the first four take an RGBA colour, and axes colours its own lines:

  • line(start, end): one segment. The floor grid here is drawn with it.

  • box(center, half_extents): an axis-aligned wireframe box, drawn here in green slightly larger than each solid cube.

  • sphere(center, radius, segments=16): three great circles. The orange one here orbits the cubes, moved by drawing it somewhere new each frame.

  • ray(origin, direction, length=10.0): a line along a direction; the magenta one points down from above the middle cube.

  • axes(position, size=1.0): the X, Y and Z axes in red, green and blue.

Source

 1"""Debug Draw: Wireframe boxes, spheres, axes, and rays.
 2
 3`DebugDraw` (from `simvx.graphics.debug_draw`) draws world-space wireframes for
 4state a game cannot otherwise see: collision bounds, raycasts, AI targets. It is
 5immediate mode. Every call queues lines for the current frame only, and the
 6renderer clears the queue once it has drawn them, so anything you want to keep
 7seeing is drawn again each frame from `on_update`:
 8
 9```python
10def on_update(self, dt):
11    DebugDraw.box(self.world_position, (0.6, 0.6, 0.6), colour=(0, 1, 0, 1))
12```
13
14The five calls, all in world coordinates; the first four take an RGBA
15`colour`, and `axes` colours its own lines:
16
17- `line(start, end)`: one segment. The floor grid here is drawn with it.
18- `box(center, half_extents)`: an axis-aligned wireframe box, drawn here in
19  green slightly larger than each solid cube.
20- `sphere(center, radius, segments=16)`: three great circles. The orange one
21  here orbits the cubes, moved by drawing it somewhere new each frame.
22- `ray(origin, direction, length=10.0)`: a line along a direction; the magenta
23  one points down from above the middle cube.
24- `axes(position, size=1.0)`: the X, Y and Z axes in red, green and blue.
25"""
26
27import math
28
29from simvx.core import Anchor, Camera3D, CanvasLayer, Label, Material, Mesh, MeshInstance3D, Node
30from simvx.graphics import App
31from simvx.graphics.debug_draw import DebugDraw
32
33CUBE_X = (-3, 0, 3)
34
35
36class DebugDrawScene(Node):
37    def on_ready(self):
38        self.add_child(Camera3D(position=(0, 5, -12), look_at=(0, 0, 0)))
39        grey = Material(colour=(0.4, 0.4, 0.5, 1.0))
40        for x in CUBE_X:
41            self.add_child(MeshInstance3D(mesh=Mesh.box(), material=grey, position=(x, 0, 0)))
42
43        hud = self.add_child(CanvasLayer())
44        hud.add_child(Label("Debug Draw: boxes, sphere, axes, ray", anchor=Anchor.TOP_LEFT, inset=10, font_size=24))
45
46    def on_update(self, dt):
47        for x in CUBE_X:
48            DebugDraw.box((x, 0, 0), (0.6, 0.6, 0.6), colour=(0, 1, 0, 1))
49
50        DebugDraw.axes((0, -1.5, 0), size=3.0)
51
52        t = self.tree.now
53        DebugDraw.sphere((4 * math.cos(t), 1, 4 * math.sin(t)), radius=0.8, colour=(1, 0.5, 0, 1), segments=12)
54
55        DebugDraw.ray((0, 4, 0), (0, -1, 0), length=3.0, colour=(1, 0, 1, 1))
56
57        for i in range(-5, 6):
58            DebugDraw.line((i, -1.5, -5), (i, -1.5, 5), colour=(0.3, 0.3, 0.3, 0.5))
59            DebugDraw.line((-5, -1.5, i), (5, -1.5, i), colour=(0.3, 0.3, 0.3, 0.5))
60
61
62if __name__ == "__main__":
63    App(title="Debug Draw Demo", width=1280, height=720).run(DebugDrawScene())