Skip to main content

box2d_rust/
debug_draw.rs

1// Debug draw interface (b2DebugDraw) and the color palette (b2HexColor).
2//
3// SPDX-FileCopyrightText: 2023 Erin Catto
4// SPDX-License-Identifier: MIT
5
6use crate::math_functions::{Aabb, Pos, Vec2, WorldTransform};
7
8/// Color for debug drawing, packed 0xRRGGBB (b2HexColor). C treats the enum as
9/// a plain integer (shape custom colors are arbitrary u32 values), so the Rust
10/// port is a newtype over u32 with the named palette as associated constants.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub struct HexColor(pub u32);
13
14impl HexColor {
15    pub const ALICE_BLUE: HexColor = HexColor(0xF0F8FF);
16    pub const ANTIQUE_WHITE: HexColor = HexColor(0xFAEBD7);
17    pub const AQUA: HexColor = HexColor(0x00FFFF);
18    pub const AQUAMARINE: HexColor = HexColor(0x7FFFD4);
19    pub const AZURE: HexColor = HexColor(0xF0FFFF);
20    pub const BEIGE: HexColor = HexColor(0xF5F5DC);
21    pub const BISQUE: HexColor = HexColor(0xFFE4C4);
22    pub const BLACK: HexColor = HexColor(0x000000);
23    pub const BLANCHED_ALMOND: HexColor = HexColor(0xFFEBCD);
24    pub const BLUE: HexColor = HexColor(0x0000FF);
25    pub const BLUE_VIOLET: HexColor = HexColor(0x8A2BE2);
26    pub const BROWN: HexColor = HexColor(0xA52A2A);
27    pub const BURLYWOOD: HexColor = HexColor(0xDEB887);
28    pub const CADET_BLUE: HexColor = HexColor(0x5F9EA0);
29    pub const CHARTREUSE: HexColor = HexColor(0x7FFF00);
30    pub const CHOCOLATE: HexColor = HexColor(0xD2691E);
31    pub const CORAL: HexColor = HexColor(0xFF7F50);
32    pub const CORNFLOWER_BLUE: HexColor = HexColor(0x6495ED);
33    pub const CORNSILK: HexColor = HexColor(0xFFF8DC);
34    pub const CRIMSON: HexColor = HexColor(0xDC143C);
35    pub const CYAN: HexColor = HexColor(0x00FFFF);
36    pub const DARK_BLUE: HexColor = HexColor(0x00008B);
37    pub const DARK_CYAN: HexColor = HexColor(0x008B8B);
38    pub const DARK_GOLDEN_ROD: HexColor = HexColor(0xB8860B);
39    pub const DARK_GRAY: HexColor = HexColor(0xA9A9A9);
40    pub const DARK_GREEN: HexColor = HexColor(0x006400);
41    pub const DARK_KHAKI: HexColor = HexColor(0xBDB76B);
42    pub const DARK_MAGENTA: HexColor = HexColor(0x8B008B);
43    pub const DARK_OLIVE_GREEN: HexColor = HexColor(0x556B2F);
44    pub const DARK_ORANGE: HexColor = HexColor(0xFF8C00);
45    pub const DARK_ORCHID: HexColor = HexColor(0x9932CC);
46    pub const DARK_RED: HexColor = HexColor(0x8B0000);
47    pub const DARK_SALMON: HexColor = HexColor(0xE9967A);
48    pub const DARK_SEA_GREEN: HexColor = HexColor(0x8FBC8F);
49    pub const DARK_SLATE_BLUE: HexColor = HexColor(0x483D8B);
50    pub const DARK_SLATE_GRAY: HexColor = HexColor(0x2F4F4F);
51    pub const DARK_TURQUOISE: HexColor = HexColor(0x00CED1);
52    pub const DARK_VIOLET: HexColor = HexColor(0x9400D3);
53    pub const DEEP_PINK: HexColor = HexColor(0xFF1493);
54    pub const DEEP_SKY_BLUE: HexColor = HexColor(0x00BFFF);
55    pub const DIM_GRAY: HexColor = HexColor(0x696969);
56    pub const DODGER_BLUE: HexColor = HexColor(0x1E90FF);
57    pub const FIRE_BRICK: HexColor = HexColor(0xB22222);
58    pub const FLORAL_WHITE: HexColor = HexColor(0xFFFAF0);
59    pub const FOREST_GREEN: HexColor = HexColor(0x228B22);
60    pub const FUCHSIA: HexColor = HexColor(0xFF00FF);
61    pub const GAINSBORO: HexColor = HexColor(0xDCDCDC);
62    pub const GHOST_WHITE: HexColor = HexColor(0xF8F8FF);
63    pub const GOLD: HexColor = HexColor(0xFFD700);
64    pub const GOLDEN_ROD: HexColor = HexColor(0xDAA520);
65    pub const GRAY: HexColor = HexColor(0x808080);
66    pub const GREEN: HexColor = HexColor(0x008000);
67    pub const GREEN_YELLOW: HexColor = HexColor(0xADFF2F);
68    pub const HONEY_DEW: HexColor = HexColor(0xF0FFF0);
69    pub const HOT_PINK: HexColor = HexColor(0xFF69B4);
70    pub const INDIAN_RED: HexColor = HexColor(0xCD5C5C);
71    pub const INDIGO: HexColor = HexColor(0x4B0082);
72    pub const IVORY: HexColor = HexColor(0xFFFFF0);
73    pub const KHAKI: HexColor = HexColor(0xF0E68C);
74    pub const LAVENDER: HexColor = HexColor(0xE6E6FA);
75    pub const LAVENDER_BLUSH: HexColor = HexColor(0xFFF0F5);
76    pub const LAWN_GREEN: HexColor = HexColor(0x7CFC00);
77    pub const LEMON_CHIFFON: HexColor = HexColor(0xFFFACD);
78    pub const LIGHT_BLUE: HexColor = HexColor(0xADD8E6);
79    pub const LIGHT_CORAL: HexColor = HexColor(0xF08080);
80    pub const LIGHT_CYAN: HexColor = HexColor(0xE0FFFF);
81    pub const LIGHT_GOLDEN_ROD_YELLOW: HexColor = HexColor(0xFAFAD2);
82    pub const LIGHT_GRAY: HexColor = HexColor(0xD3D3D3);
83    pub const LIGHT_GREEN: HexColor = HexColor(0x90EE90);
84    pub const LIGHT_PINK: HexColor = HexColor(0xFFB6C1);
85    pub const LIGHT_SALMON: HexColor = HexColor(0xFFA07A);
86    pub const LIGHT_SEA_GREEN: HexColor = HexColor(0x20B2AA);
87    pub const LIGHT_SKY_BLUE: HexColor = HexColor(0x87CEFA);
88    pub const LIGHT_SLATE_GRAY: HexColor = HexColor(0x778899);
89    pub const LIGHT_STEEL_BLUE: HexColor = HexColor(0xB0C4DE);
90    pub const LIGHT_YELLOW: HexColor = HexColor(0xFFFFE0);
91    pub const LIME: HexColor = HexColor(0x00FF00);
92    pub const LIME_GREEN: HexColor = HexColor(0x32CD32);
93    pub const LINEN: HexColor = HexColor(0xFAF0E6);
94    pub const MAGENTA: HexColor = HexColor(0xFF00FF);
95    pub const MAROON: HexColor = HexColor(0x800000);
96    pub const MEDIUM_AQUA_MARINE: HexColor = HexColor(0x66CDAA);
97    pub const MEDIUM_BLUE: HexColor = HexColor(0x0000CD);
98    pub const MEDIUM_ORCHID: HexColor = HexColor(0xBA55D3);
99    pub const MEDIUM_PURPLE: HexColor = HexColor(0x9370DB);
100    pub const MEDIUM_SEA_GREEN: HexColor = HexColor(0x3CB371);
101    pub const MEDIUM_SLATE_BLUE: HexColor = HexColor(0x7B68EE);
102    pub const MEDIUM_SPRING_GREEN: HexColor = HexColor(0x00FA9A);
103    pub const MEDIUM_TURQUOISE: HexColor = HexColor(0x48D1CC);
104    pub const MEDIUM_VIOLET_RED: HexColor = HexColor(0xC71585);
105    pub const MIDNIGHT_BLUE: HexColor = HexColor(0x191970);
106    pub const MINT_CREAM: HexColor = HexColor(0xF5FFFA);
107    pub const MISTY_ROSE: HexColor = HexColor(0xFFE4E1);
108    pub const MOCCASIN: HexColor = HexColor(0xFFE4B5);
109    pub const NAVAJO_WHITE: HexColor = HexColor(0xFFDEAD);
110    pub const NAVY: HexColor = HexColor(0x000080);
111    pub const OLD_LACE: HexColor = HexColor(0xFDF5E6);
112    pub const OLIVE: HexColor = HexColor(0x808000);
113    pub const OLIVE_DRAB: HexColor = HexColor(0x6B8E23);
114    pub const ORANGE: HexColor = HexColor(0xFFA500);
115    pub const ORANGE_RED: HexColor = HexColor(0xFF4500);
116    pub const ORCHID: HexColor = HexColor(0xDA70D6);
117    pub const PALE_GOLDEN_ROD: HexColor = HexColor(0xEEE8AA);
118    pub const PALE_GREEN: HexColor = HexColor(0x98FB98);
119    pub const PALE_TURQUOISE: HexColor = HexColor(0xAFEEEE);
120    pub const PALE_VIOLET_RED: HexColor = HexColor(0xDB7093);
121    pub const PAPAYA_WHIP: HexColor = HexColor(0xFFEFD5);
122    pub const PEACH_PUFF: HexColor = HexColor(0xFFDAB9);
123    pub const PERU: HexColor = HexColor(0xCD853F);
124    pub const PINK: HexColor = HexColor(0xFFC0CB);
125    pub const PLUM: HexColor = HexColor(0xDDA0DD);
126    pub const POWDER_BLUE: HexColor = HexColor(0xB0E0E6);
127    pub const PURPLE: HexColor = HexColor(0x800080);
128    pub const REBECCA_PURPLE: HexColor = HexColor(0x663399);
129    pub const RED: HexColor = HexColor(0xFF0000);
130    pub const ROSY_BROWN: HexColor = HexColor(0xBC8F8F);
131    pub const ROYAL_BLUE: HexColor = HexColor(0x4169E1);
132    pub const SADDLE_BROWN: HexColor = HexColor(0x8B4513);
133    pub const SALMON: HexColor = HexColor(0xFA8072);
134    pub const SANDY_BROWN: HexColor = HexColor(0xF4A460);
135    pub const SEA_GREEN: HexColor = HexColor(0x2E8B57);
136    pub const SEA_SHELL: HexColor = HexColor(0xFFF5EE);
137    pub const SIENNA: HexColor = HexColor(0xA0522D);
138    pub const SILVER: HexColor = HexColor(0xC0C0C0);
139    pub const SKY_BLUE: HexColor = HexColor(0x87CEEB);
140    pub const SLATE_BLUE: HexColor = HexColor(0x6A5ACD);
141    pub const SLATE_GRAY: HexColor = HexColor(0x708090);
142    pub const SNOW: HexColor = HexColor(0xFFFAFA);
143    pub const SPRING_GREEN: HexColor = HexColor(0x00FF7F);
144    pub const STEEL_BLUE: HexColor = HexColor(0x4682B4);
145    pub const TAN: HexColor = HexColor(0xD2B48C);
146    pub const TEAL: HexColor = HexColor(0x008080);
147    pub const THISTLE: HexColor = HexColor(0xD8BFD8);
148    pub const TOMATO: HexColor = HexColor(0xFF6347);
149    pub const TURQUOISE: HexColor = HexColor(0x40E0D0);
150    pub const VIOLET: HexColor = HexColor(0xEE82EE);
151    pub const WHEAT: HexColor = HexColor(0xF5DEB3);
152    pub const WHITE: HexColor = HexColor(0xFFFFFF);
153    pub const WHITE_SMOKE: HexColor = HexColor(0xF5F5F5);
154    pub const YELLOW: HexColor = HexColor(0xFFFF00);
155    pub const YELLOW_GREEN: HexColor = HexColor(0x9ACD32);
156    pub const BOX2D_RED: HexColor = HexColor(0xDC3132);
157    pub const BOX2D_BLUE: HexColor = HexColor(0x30AEBF);
158    pub const BOX2D_GREEN: HexColor = HexColor(0x8CC924);
159    pub const BOX2D_YELLOW: HexColor = HexColor(0xFFEE8C);
160}
161
162/// Debug drawing callbacks and options (b2DebugDraw). C carries function
163/// pointers plus option fields in one struct initialized by
164/// b2DefaultDebugDraw; the Rust port is a trait where every method has a
165/// default body matching those defaults (draw callbacks default to no-ops so
166/// an implementation can supply only the subset it cares about, options
167/// default to the b2DefaultDebugDraw values). Callbacks receive world
168/// coordinates: in large world mode `Pos`/`WorldTransform` translations are
169/// double precision so they stay accurate far from the origin.
170pub trait DebugDraw {
171    /// Draw a closed polygon provided in CCW order (DrawPolygonFcn).
172    fn draw_polygon(&mut self, transform: WorldTransform, vertices: &[Vec2], color: HexColor) {
173        let _ = (transform, vertices, color);
174    }
175
176    /// Draw a solid closed polygon provided in CCW order (DrawSolidPolygonFcn).
177    fn draw_solid_polygon(
178        &mut self,
179        transform: WorldTransform,
180        vertices: &[Vec2],
181        radius: f32,
182        color: HexColor,
183    ) {
184        let _ = (transform, vertices, radius, color);
185    }
186
187    /// Draw a circle (DrawCircleFcn).
188    fn draw_circle(&mut self, center: Pos, radius: f32, color: HexColor) {
189        let _ = (center, radius, color);
190    }
191
192    /// Draw a solid circle (DrawSolidCircleFcn).
193    fn draw_solid_circle(
194        &mut self,
195        transform: WorldTransform,
196        center: Vec2,
197        radius: f32,
198        color: HexColor,
199    ) {
200        let _ = (transform, center, radius, color);
201    }
202
203    /// Draw a solid capsule (DrawSolidCapsuleFcn).
204    fn draw_solid_capsule(&mut self, p1: Pos, p2: Pos, radius: f32, color: HexColor) {
205        let _ = (p1, p2, radius, color);
206    }
207
208    /// Draw a line segment (DrawLineFcn).
209    fn draw_line(&mut self, p1: Pos, p2: Pos, color: HexColor) {
210        let _ = (p1, p2, color);
211    }
212
213    /// Draw a transform; choose your own length scale (DrawTransformFcn).
214    fn draw_transform(&mut self, transform: WorldTransform) {
215        let _ = transform;
216    }
217
218    /// Draw a point (DrawPointFcn).
219    fn draw_point(&mut self, p: Pos, size: f32, color: HexColor) {
220        let _ = (p, size, color);
221    }
222
223    /// Draw a string in world space (DrawStringFcn).
224    fn draw_string(&mut self, p: Pos, s: &str, color: HexColor) {
225        let _ = (p, s, color);
226    }
227
228    /// Draw a bounding box (DrawBoundsFcn). With double precision enabled, the
229    /// single precision bounding box gets increasing padding when moving far
230    /// from the origin.
231    fn draw_bounds(&mut self, aabb: Aabb, color: HexColor) {
232        let _ = (aabb, color);
233    }
234
235    /// World bounds to use for debug draw (drawingBounds).
236    fn drawing_bounds(&self) -> Aabb {
237        Aabb {
238            lower_bound: Vec2 {
239                x: -f32::MAX,
240                y: -f32::MAX,
241            },
242            upper_bound: Vec2 {
243                x: f32::MAX,
244                y: f32::MAX,
245            },
246        }
247    }
248
249    /// Scale to use when drawing forces (forceScale).
250    fn force_scale(&self) -> f32 {
251        1.0
252    }
253
254    /// Global scaling for joint drawing (jointScale).
255    fn joint_scale(&self) -> f32 {
256        1.0
257    }
258
259    /// Option to draw contact points (drawContacts).
260    fn draw_contacts(&self) -> bool {
261        false
262    }
263
264    /// Draw anchor A for contact points instead of anchor B (drawAnchorA).
265    fn draw_anchor_a(&self) -> bool {
266        false
267    }
268
269    /// Option to draw shapes (drawShapes). The only option on by default.
270    fn draw_shapes(&self) -> bool {
271        true
272    }
273
274    /// Option to draw chain shape normals (drawChainNormals).
275    fn draw_chain_normals(&self) -> bool {
276        false
277    }
278
279    /// Option to draw joints (drawJoints).
280    fn draw_joints(&self) -> bool {
281        false
282    }
283
284    /// Option to draw additional information for joints (drawJointExtras).
285    fn draw_joint_extras(&self) -> bool {
286        false
287    }
288
289    /// Option to draw the bounding boxes for shapes (drawBounds).
290    fn draw_bounds_boxes(&self) -> bool {
291        false
292    }
293
294    /// Option to draw the mass and center of mass of dynamic bodies (drawMass).
295    fn draw_mass(&self) -> bool {
296        false
297    }
298
299    /// Option to draw body names (drawBodyNames).
300    fn draw_body_names(&self) -> bool {
301        false
302    }
303
304    /// Option to visualize the graph coloring used for contacts and joints
305    /// (drawGraphColors).
306    fn draw_graph_colors(&self) -> bool {
307        false
308    }
309
310    /// Option to draw contact feature ids (drawContactFeatures).
311    fn draw_contact_features(&self) -> bool {
312        false
313    }
314
315    /// Option to draw contact normals (drawContactNormals).
316    fn draw_contact_normals(&self) -> bool {
317        false
318    }
319
320    /// Option to draw contact normal forces (drawContactForces).
321    fn draw_contact_forces(&self) -> bool {
322        false
323    }
324
325    /// Option to draw contact friction forces (drawFrictionForces).
326    fn draw_friction_forces(&self) -> bool {
327        false
328    }
329
330    /// Option to draw islands as bounding boxes (drawIslands).
331    fn draw_islands(&self) -> bool {
332        false
333    }
334}