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 Transform Constructor

From Leeroopedia
Knowledge Sources
Domains Simulation, Perception
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for defining 3D sensor mounting positions and orientations relative to a vehicle provided by the CARLA simulator.

Description

carla.Transform combines a carla.Location (x, y, z in meters) and a carla.Rotation (pitch, yaw, roll in degrees) to define a 6-DOF pose in 3D space. When used with World.spawn_actor() and the attach_to parameter, the transform specifies the sensor's mounting position and orientation relative to the parent vehicle's local coordinate frame. The Location constructor takes positional arguments (x, y, z) and the Rotation constructor takes (pitch, yaw, roll), all as floating-point values.

Usage

A Transform is created for each sensor being spawned and passed as the second argument to World.spawn_actor(). Each sensor in a data collection rig requires its own transform defining where it sits on the vehicle and which direction it faces.

Code Reference

Source Location

  • Repository: CARLA
  • File: LibCarla/source/carla/geom/Transform.h, PythonAPI/carla/src/Geometry.cpp
  • Lines: Transform.h (full file), Geometry.cpp (full file)

Signature

carla.Location(x: float = 0.0, y: float = 0.0, z: float = 0.0)
carla.Rotation(pitch: float = 0.0, yaw: float = 0.0, roll: float = 0.0)
carla.Transform(location: carla.Location, rotation: carla.Rotation)

Import

import carla

I/O Contract

Inputs

Name Type Required Description
x float No Forward offset in meters from the vehicle reference frame origin. Positive = forward. Default 0.0.
y float No Lateral offset in meters. Positive = right. Default 0.0.
z float No Vertical offset in meters. Positive = up. Default 0.0.
pitch float No Rotation around the y-axis in degrees. Positive = nose up. Default 0.0.
yaw float No Rotation around the z-axis in degrees. Positive = clockwise from above. Default 0.0.
roll float No Rotation around the x-axis in degrees. Positive = right side down. Default 0.0.

Outputs

Name Type Description
location carla.Location 3D position vector with x, y, z attributes.
rotation carla.Rotation 3D orientation with pitch, yaw, roll attributes.
transform carla.Transform Combined 6-DOF pose object. Provides get_forward_vector(), get_right_vector(), get_up_vector(), and get_matrix() for accessing the underlying transformation.

Usage Examples

Basic Example

import carla

# Define a forward-facing camera on the vehicle roof
camera_transform = carla.Transform(
    carla.Location(x=1.5, y=0.0, z=2.4),   # 1.5m forward, 2.4m above origin
    carla.Rotation(pitch=0.0, yaw=0.0, roll=0.0)  # looking straight ahead
)

# Define a roof-mounted LiDAR
lidar_transform = carla.Transform(
    carla.Location(x=0.0, y=0.0, z=2.5),   # centered, 2.5m above origin
    carla.Rotation(pitch=0.0, yaw=0.0, roll=0.0)
)

Full Sensor Suite Placement

import carla

# --- Forward-facing RGB camera (dashboard mount) ---
front_cam_tf = carla.Transform(
    carla.Location(x=1.5, y=0.0, z=2.4),
    carla.Rotation(pitch=-5.0, yaw=0.0, roll=0.0)  # slight downward tilt
)

# --- Left-facing camera (side mirror position) ---
left_cam_tf = carla.Transform(
    carla.Location(x=0.5, y=-0.9, z=1.8),
    carla.Rotation(pitch=0.0, yaw=-90.0, roll=0.0)  # facing left
)

# --- Right-facing camera (side mirror position) ---
right_cam_tf = carla.Transform(
    carla.Location(x=0.5, y=0.9, z=1.8),
    carla.Rotation(pitch=0.0, yaw=90.0, roll=0.0)   # facing right
)

# --- Rear-facing camera ---
rear_cam_tf = carla.Transform(
    carla.Location(x=-2.0, y=0.0, z=2.0),
    carla.Rotation(pitch=0.0, yaw=180.0, roll=0.0)  # facing backward
)

# --- Roof-mounted LiDAR (centered, elevated) ---
lidar_tf = carla.Transform(
    carla.Location(x=0.0, y=0.0, z=2.5),
    carla.Rotation(pitch=0.0, yaw=0.0, roll=0.0)
)

# --- Front radar (behind bumper) ---
radar_tf = carla.Transform(
    carla.Location(x=2.5, y=0.0, z=0.7),
    carla.Rotation(pitch=5.0, yaw=0.0, roll=0.0)    # slight upward tilt
)

# --- GNSS at center of gravity ---
gnss_tf = carla.Transform(
    carla.Location(x=0.0, y=0.0, z=0.0),
    carla.Rotation(pitch=0.0, yaw=0.0, roll=0.0)
)

# --- IMU at center of gravity ---
imu_tf = carla.Transform(
    carla.Location(x=0.0, y=0.0, z=0.0),
    carla.Rotation(pitch=0.0, yaw=0.0, roll=0.0)
)

# Spawn sensors attached to the ego vehicle
ego_vehicle = world.get_actors().filter('vehicle.*')[0]

front_cam = world.spawn_actor(rgb_bp, front_cam_tf, attach_to=ego_vehicle)
left_cam = world.spawn_actor(rgb_bp, left_cam_tf, attach_to=ego_vehicle)
right_cam = world.spawn_actor(rgb_bp, right_cam_tf, attach_to=ego_vehicle)
rear_cam = world.spawn_actor(rgb_bp, rear_cam_tf, attach_to=ego_vehicle)
lidar = world.spawn_actor(lidar_bp, lidar_tf, attach_to=ego_vehicle)
radar = world.spawn_actor(radar_bp, radar_tf, attach_to=ego_vehicle)
gnss = world.spawn_actor(gnss_bp, gnss_tf, attach_to=ego_vehicle)
imu = world.spawn_actor(imu_bp, imu_tf, attach_to=ego_vehicle)

Related Pages

Implements Principle

Page Connections

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