I implemented this simple code to test the spectator in Carla. However the spectator view is not aligned with the vehicle view. The car is looking forward while the spectator is looking backward.
import carla
import math
import time
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()
blueprint_library = world.get_blueprint_library()
vehicle_bp = blueprint_library.filter('vehicle')[0]
spawn_point = world.get_map().get_spawn_points()[0]
vehicle = world.spawn_actor(vehicle_bp, spawn_point)
car_transform = vehicle.get_transform()
spectator = world.get_spectator()
while(True):
spectator.set_transform(car_transform)
I am using carla 0.9.16
1 Answer 1
I finally figured out the issue.
In Carla, usually the cars drop from the sky and then they stabilize after hitting the ground to get their initial pose. I just needed to keep getting the car transform in the while loop to correct for the mismatch between the car pose at the start of simulation (when i call the spawn_actor function) and the initial car pose after hitting the ground. The corrected code is given below:
import carla
import math
import time
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()
blueprint_library = world.get_blueprint_library()
vehicle_bp = blueprint_library.filter('vehicle')[0]
spawn_point = world.get_map().get_spawn_points()[0]
vehicle = world.spawn_actor(vehicle_bp, spawn_point)
spectator = world.get_spectator()
while(True):
car_transform = vehicle.get_transform()
spectator.set_transform(car_transform)