import sys
try:
sys.path.append('D:\work_software\Carla\Carla_0.9.10\CARLA_0.9.10\WindowsNoEditor\PythonAPI\carla\dist\carla-0.9.10-py3.7-win-amd64.egg')
except IndexError:
pass
import carla
client = carla.Client("localhost", 2000)
client.set_timeout(5.0)
world = client.load_world("Town01")
vehicle_actor_blueprint = world.get_blueprint_library().filter("vehicle.*.*")[0]
vehicle_actor_transform = carla.Transform(carla.Location(x = 0, y = 0, z = 10), carla.Rotation(yaw = 0))
vehicle_actor = world.spawn_actor(vehicle_actor_blueprint, vehicle_actor_transform)
# 注意1:对象【vehicle_actor】的类型为【类 carla.Actor】
# 注意2:【carla.Actor】中的 actor 包括【行人】【车俩】【传感器】【交通标志】
#(2)Handling
# ------------------------------------------------------------------------------------------------------------------------
vehicle_attribute_dict = vehicle_actor.attributes # 属性【.attributes】 -- 属于【类 carla.Actor】
# -- 作用:返回一个包含该actor【所有属性】的【字典】
print("\n", vehicle_attribute_dict) # {'role_name': 'autopilot', 'sticky_control': 'true', 'color': '73,155,204', 'object_type': '', 'number_of_wheels': '4'}
print(type(vehicle_attribute_dict)) # <class 'dict'>
# 1.函数【carla.Actor.get_acceleration()】 作用:返回客户端在最后一个tick收到的角色的【3D加速度向量】 -- 返回值类型为【carla.Vector3D】
print("\n", vehicle_actor.get_acceleration()) # Vector3D(x=0.000000, y=0.000000, z=0.000000)
# 2.函数【carla.Actor.get_velocity()】 作用:返回客户端在最后一个tick期间接收到的角色的【速度向量】 -- 返回值类型为【carla.Vector3D】
print("\n", vehicle_actor.get_velocity()) # Vector3D(x=0.000000, y=0.000000, z=0.000000)
# 3.函数【carla.Actor.get_location()】 作用:返回客户端在上一次tick期间接收到的【参与者的位置】 -- 返回值类型为【carla.Location】
# 函数【carla.Actor.set_location()】 作用:将角色传送到给定位置,参数类型为【carla.Location】
vehicle_location = vehicle_actor.get_location()
vehicle_location.z += 10.0
vehicle_actor.set_location(vehicle_location) # 注意:虽然车辆设置的是z的坐标变换,但是车辆在x,y坐标上的位置也发生了变化【解释:如下图1】
# 4.函数【carla.Actor.set_simulate_physics()】 作用:启用或禁用此actor上的物理模拟
vehicle_actor.set_simulate_physics(enabled = False) # 结果:因为禁用了【物理模拟】,所以运行完程序以后车辆直接升天了,而不遵守这个地图的物理法则约束了
# ------------------------------------------------------------------------------------------------------------------------
#(3)Destruction
# ------------------------------------------------------------------------------------------------------------------------
# 注:当Python脚本完成时,actor不会被销毁,他们必须明确地摧毁自己
# 函数【carla.Actor.destroy()】 作用:告诉carla模拟器销毁这个actor,如果成功则返回True,如果已经被破坏则没有任何效果
destroyed_successfully = vehicle_actor.destroy()
print(destroyed_successfully) # 注意:返回值为【False】,但是地图上已经找不到车辆了【解释:如下图2】
# ------------------------------------------------------------------------------------------------------------------------
图1:
图2: