Implementation:CARLA simulator Carla World Get Random Location From Navigation
| Knowledge Sources | |
|---|---|
| Domains | Navigation Mesh, Pedestrian Simulation |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for sampling random walkable locations from the pedestrian navigation mesh, provided by the CARLA simulator.
Description
The World.get_random_location_from_navigation() method queries the Recast/Detour navigation mesh loaded for the current map and returns a random point on a walkable surface. This is the primary mechanism for generating valid pedestrian spawn locations and walking destinations.
Internally, the method calls into CARLA's Navigation module (carla::nav::Navigation), which interfaces with Detour's dtNavMeshQuery to sample a random polygon on the NavMesh, then selects a random point within that polygon. The returned location is guaranteed to lie on a navigable surface (sidewalk, crosswalk, plaza, etc.).
The method returns None if the navigation mesh is not loaded or if the random sampling fails. This can happen if the map was loaded without its corresponding NavMesh binary file.
Usage
Use this method when you need to:
- Generate spawn positions for pedestrian walkers on sidewalks
- Produce random destination waypoints for pedestrian AI controllers
- Create realistic pedestrian distributions across the map without manually specifying positions
Code Reference
Source Location
- Repository: CARLA
- File:
LibCarla/source/carla/nav/Navigation.cpp - Lines: L1062-1100
- Python Binding:
PythonAPI/carla/src/World.cpp, L310
Signature
World.get_random_location_from_navigation() -> carla.Location or None
Import
import carla
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (none) | -- | -- | This method takes no parameters. It samples from the NavMesh associated with the currently loaded map. |
Outputs
| Name | Type | Description |
|---|---|---|
| return | carla.Location or None | A random 3D location (x, y, z) on the navigation mesh. Returns None if the NavMesh is not available or sampling fails. |
Usage Examples
Basic Example
import carla
import random
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()
# Sample a random walkable location for a pedestrian
spawn_location = world.get_random_location_from_navigation()
if spawn_location is not None:
print(f"Pedestrian spawn at: x={spawn_location.x:.2f}, "
f"y={spawn_location.y:.2f}, z={spawn_location.z:.2f}")
Spawning Multiple Pedestrians
import carla
import random
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()
# Get walker blueprints
walker_blueprints = world.get_blueprint_library().filter('walker.pedestrian.*')
number_of_walkers = 50
# Generate spawn points on the navigation mesh
spawn_points = []
for _ in range(number_of_walkers):
spawn_loc = world.get_random_location_from_navigation()
if spawn_loc is not None:
spawn_points.append(carla.Transform(spawn_loc))
# Batch-spawn pedestrians at the sampled locations
batch = []
for spawn_point in spawn_points:
walker_bp = random.choice(walker_blueprints)
if walker_bp.has_attribute('is_invincible'):
walker_bp.set_attribute('is_invincible', 'false')
batch.append(carla.command.SpawnActor(walker_bp, spawn_point))
results = client.apply_batch_sync(batch, do_tick=True)
walker_ids = []
for result in results:
if not result.error:
walker_ids.append(result.actor_id)
print(f"Spawned {len(walker_ids)} pedestrians on sidewalks")
Generating Walking Destinations
import carla
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()
# Assume walkers and their AI controllers are already spawned
# walker_controllers is a list of WalkerAIController actors
for controller in walker_controllers:
controller.start()
# Use get_random_location_from_navigation as the walking destination
destination = world.get_random_location_from_navigation()
if destination is not None:
controller.go_to_location(destination)
controller.set_max_speed(1.4) # Average walking speed in m/s
Robust Sampling with Retry
import carla
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()
def get_valid_spawn_locations(world, count, max_attempts_per_location=10):
"""Sample 'count' valid locations from the NavMesh with retry logic."""
locations = []
for _ in range(count):
for attempt in range(max_attempts_per_location):
loc = world.get_random_location_from_navigation()
if loc is not None:
locations.append(loc)
break
else:
print("Warning: Could not sample a valid NavMesh location after "
f"{max_attempts_per_location} attempts")
return locations
# Sample 100 valid spawn locations
spawn_locations = get_valid_spawn_locations(world, count=100)
print(f"Sampled {len(spawn_locations)} valid pedestrian locations")