Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:CARLA simulator Carla Agent Misc Utils

From Leeroopedia
Knowledge Sources
Domains Agent Navigation, Utility Functions
Last Updated 2026-02-15 05:00 GMT

Overview

A Python utility module providing auxiliary functions for CARLA agent navigation, including distance calculations, speed computation, and spatial reasoning.

Description

This module contains helper functions used by the CARLA agent navigation system. draw_waypoints(world, waypoints, z) visualizes waypoints as directional arrows in the simulation. get_speed(vehicle) computes vehicle speed in km/h from the velocity vector. get_trafficlight_trigger_location(traffic_light) calculates the trigger volume location of a traffic light by rotating its extent around the base transform. is_within_distance(target_transform, reference_transform, max_distance, angle_interval) checks if a target is within a given distance and optionally within an angular cone of the reference transform's forward direction. compute_magnitude_angle(target_location, current_location, orientation) returns the distance and relative angle to a target. distance_vehicle(waypoint, vehicle_transform) computes 2D distance between a waypoint and a vehicle. vector(location_1, location_2) returns a unit direction vector between two locations. compute_distance(location_1, location_2) returns the Euclidean distance between two 3D points. positive(num) clamps a number to be non-negative.

Usage

These utility functions are used internally by CARLA's agent classes (BasicAgent, BehaviorAgent, ConstantVelocityAgent) for spatial reasoning, obstacle detection, traffic light awareness, and debug visualization.

Code Reference

Source Location

  • Repository: CARLA
  • File: PythonAPI/carla/agents/tools/misc.py

Signature

def draw_waypoints(world, waypoints, z=0.5)
def get_speed(vehicle)
def get_trafficlight_trigger_location(traffic_light)
def is_within_distance(target_transform, reference_transform, max_distance, angle_interval=None)
def compute_magnitude_angle(target_location, current_location, orientation)
def distance_vehicle(waypoint, vehicle_transform)
def vector(location_1, location_2)
def compute_distance(location_1, location_2)
def positive(num)

Import

from agents.tools.misc import get_speed, is_within_distance, compute_distance

I/O Contract

Inputs

Name Type Required Description
vehicle carla.Vehicle Yes (get_speed) Vehicle to compute speed for
target_transform carla.Transform Yes (is_within_distance) Target actor transform
reference_transform carla.Transform Yes (is_within_distance) Reference (ego) transform
max_distance float Yes (is_within_distance) Maximum distance threshold in meters
angle_interval list[float, float] No Min and max angle in degrees for cone check

Outputs

Name Type Description
speed float Vehicle speed in km/h
is_within bool Whether target is within distance/angle constraints
(magnitude, angle) tuple(float, float) Distance and relative angle to target
unit_vector list[float] Normalized direction vector [x, y, z]
distance float Euclidean distance between two points

Usage Examples

from agents.tools.misc import get_speed, is_within_distance, compute_distance
import carla

# Get vehicle speed
speed = get_speed(vehicle)
print(f"Speed: {speed:.1f} km/h")

# Check if actor is within 50m and in front (0-90 degree cone)
in_range = is_within_distance(
    target.get_transform(),
    ego.get_transform(),
    max_distance=50.0,
    angle_interval=[0, 90]
)

# Compute distance between two locations
dist = compute_distance(loc1, loc2)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment