Debug Draw

Wireframe boxes, spheres, axes, and rays.

▶ Run in browser

Tags: debug

DebugDraw lets any node scribble world-space wireframes for visualising collisions, raycasts, AI targets, and other invisible state. Primitives persist for a single frame.

Source

 1"""Debug Draw: Wireframe boxes, spheres, axes, and rays.
 2
 3`DebugDraw` lets any node scribble world-space wireframes for visualising
 4collisions, raycasts, AI targets, and other invisible state. Primitives
 5persist for a single frame.
 6"""
 7
 8import math
 9
10from simvx.core import (
11    Camera3D,
12    Material,
13    Mesh,
14    MeshInstance3D,
15    Node,
16    Text2D,
17)
18from simvx.graphics import App
19from simvx.graphics.debug_draw import DebugDraw
20
21
22class DebugDrawScene(Node):
23    def on_ready(self):
24        # Camera
25        cam = Camera3D(position=(0, 5, -12))
26        cam.look_at((0, 0, 0))
27        self.add_child(cam)
28
29        # A few solid cubes as reference
30        for x in [-3, 0, 3]:
31            mat = Material(colour=(0.4, 0.4, 0.5, 1.0))
32            cube = MeshInstance3D(
33                mesh=Mesh.cube(),
34                material=mat,
35                position=(x, 0, 0),
36            )
37            self.add_child(cube)
38
39        self.add_child(Text2D(text="Debug Draw: boxes, sphere, axes, ray", x=10, y=10, font_scale=1.5))
40        self._time = 0.0
41
42    def on_process(self, dt):
43        self._time += dt
44
45        # Wireframe boxes around each solid cube
46        for x in [-3, 0, 3]:
47            DebugDraw.box((x, 0, 0), (0.6, 0.6, 0.6), colour=(0, 1, 0, 1))
48
49        # Origin axes
50        DebugDraw.axes((0, -1.5, 0), size=3.0)
51
52        # Orbiting wireframe sphere
53        sx = 4 * math.cos(self._time)
54        sz = 4 * math.sin(self._time)
55        DebugDraw.sphere((sx, 1, sz), radius=0.8, colour=(1, 0.5, 0, 1), segments=12)
56
57        # Ray from above pointing down
58        DebugDraw.ray((0, 4, 0), (0, -1, 0), length=3.0, colour=(1, 0, 1, 1))
59
60        # Grid on the floor
61        for i in range(-5, 6):
62            DebugDraw.line((i, -1.5, -5), (i, -1.5, 5), colour=(0.3, 0.3, 0.3, 0.5))
63            DebugDraw.line((-5, -1.5, i), (5, -1.5, i), colour=(0.3, 0.3, 0.3, 0.5))
64
65
66if __name__ == "__main__":
67    app = App(title="Debug Draw Demo", width=1280, height=720)
68    app.run(DebugDrawScene())