use carla::{
client::Client,
geom::{BoundingBox, Location, Rotation, Vector3D},
rpc::Color,
};
use std::{thread, time::Duration};
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Connecting to CARLA simulator...");
let client = Client::connect("localhost", 2000, None)?;
println!("Connected!");
println!("\nGetting current world...");
let mut world = client.world()?;
println!("World ready!");
let debug = world.debug()?;
let origin = Location::new(0.0, 0.0, 0.5);
println!("\nDrawing debug visualizations...");
println!("1. Drawing colored points...");
let colors = [
("Red", Color::RED),
("Green", Color::GREEN),
("Blue", Color::BLUE),
("Yellow", Color::YELLOW),
("Cyan", Color::CYAN),
("Magenta", Color::MAGENTA),
("White", Color::WHITE),
];
for (i, (name, color)) in colors.iter().enumerate() {
let x = (i as f32) * 2.0;
let location = Location::new(origin.x + x, origin.y, origin.z);
debug.draw_point(location, 0.5, *color, 60.0, false)?;
let label_pos = Location::new(origin.x + x, origin.y, origin.z + 1.0);
debug.draw_string(label_pos, name, true, Color::WHITE, 60.0, false)?;
}
println!("2. Drawing lines...");
let line_start = Location::new(origin.x, origin.y + 5.0, origin.z);
let line_points = [
Location::new(0.0, 0.0, 0.0),
Location::new(5.0, 0.0, 0.0),
Location::new(5.0, 5.0, 0.0),
Location::new(0.0, 5.0, 0.0),
Location::new(0.0, 0.0, 0.0),
Location::new(0.0, 0.0, 3.0),
Location::new(5.0, 0.0, 3.0),
Location::new(5.0, 0.0, 0.0),
Location::new(5.0, 0.0, 3.0),
Location::new(5.0, 5.0, 3.0),
Location::new(5.0, 5.0, 0.0),
Location::new(5.0, 5.0, 3.0),
Location::new(0.0, 5.0, 3.0),
Location::new(0.0, 5.0, 0.0),
Location::new(0.0, 5.0, 3.0),
Location::new(0.0, 0.0, 3.0),
];
for i in 0..line_points.len() - 1 {
let start = Location::new(
line_start.x + line_points[i].x,
line_start.y + line_points[i].y,
line_start.z + line_points[i].z,
);
let end = Location::new(
line_start.x + line_points[i + 1].x,
line_start.y + line_points[i + 1].y,
line_start.z + line_points[i + 1].z,
);
debug.draw_line(start, end, 0.1, Color::GREEN, 60.0, false)?;
}
debug.draw_string(
Location::new(line_start.x + 2.5, line_start.y + 2.5, line_start.z + 4.0),
"Line Box",
true,
Color::GREEN,
60.0,
false,
)?;
println!("3. Drawing arrows...");
let arrow_x = origin.x;
let arrow_y = origin.y + 12.0;
let arrow_z = origin.z;
let arrow_length = 5.0;
debug.draw_arrow(
Location::new(arrow_x, arrow_y, arrow_z),
Location::new(arrow_x + arrow_length, arrow_y, arrow_z),
0.1,
0.5,
Color::RED,
60.0,
false,
)?;
debug.draw_string(
Location::new(arrow_x + arrow_length, arrow_y, arrow_z + 0.5),
"X",
true,
Color::RED,
60.0,
false,
)?;
debug.draw_arrow(
Location::new(arrow_x, arrow_y, arrow_z),
Location::new(arrow_x, arrow_y + arrow_length, arrow_z),
0.1,
0.5,
Color::GREEN,
60.0,
false,
)?;
debug.draw_string(
Location::new(arrow_x, arrow_y + arrow_length, arrow_z + 0.5),
"Y",
true,
Color::GREEN,
60.0,
false,
)?;
debug.draw_arrow(
Location::new(arrow_x, arrow_y, arrow_z),
Location::new(arrow_x, arrow_y, arrow_z + arrow_length),
0.1,
0.5,
Color::BLUE,
60.0,
false,
)?;
debug.draw_string(
Location::new(arrow_x, arrow_y, arrow_z + arrow_length),
"Z",
true,
Color::BLUE,
60.0,
false,
)?;
println!("4. Drawing bounding boxes...");
let box_base = Location::new(origin.x + 20.0, origin.y, origin.z);
for i in 0..4 {
let yaw = i as f32 * 30.0;
let y_offset = (i as f32) * 5.0;
let bbox = BoundingBox::new(
Location::new(box_base.x, box_base.y + y_offset, box_base.z + 1.5),
Vector3D {
x: 2.0,
y: 1.0,
z: 1.5,
},
);
let rotation = Rotation {
pitch: 0.0,
yaw,
roll: 0.0,
};
let color = match i {
0 => Color::RED,
1 => Color::GREEN,
2 => Color::BLUE,
_ => Color::YELLOW,
};
debug.draw_box(&bbox, rotation, 0.1, color, 60.0, false)?;
debug.draw_string(
Location::new(box_base.x, box_base.y + y_offset, box_base.z + 3.5),
&format!("Yaw: {}°", yaw),
true,
Color::WHITE,
60.0,
false,
)?;
}
println!("5. Drawing text labels...");
debug.draw_string(
Location::new(origin.x + 10.0, origin.y + 20.0, origin.z + 5.0),
"CARLA Debug Visualization",
true,
Color::CYAN,
60.0,
false,
)?;
debug.draw_string(
Location::new(origin.x + 10.0, origin.y + 20.0, origin.z + 3.5),
"Points, Lines, Arrows, Boxes, and Text",
false,
Color::WHITE,
60.0,
false,
)?;
println!("\n✓ Debug visualizations drawn!");
println!(" All visualizations will persist for 60 seconds.");
println!(" Switch to spectator view in CARLA to see them.");
println!(" Location: Around origin (0, 0, 0)");
println!("\nWaiting 5 seconds for visualizations to be visible...");
thread::sleep(Duration::from_secs(5));
println!("✓ Example complete!");
println!(" Note: Visualizations will remain visible for 60 seconds total.");
Ok(())
}