Fixes of intersections and roads

This commit is contained in:
Manish 2019-01-22 17:23:46 +01:00
parent 780ada7fad
commit 32c82999bd
1 changed files with 196 additions and 135 deletions

View File

@ -135,7 +135,8 @@ class Vehicle(object):
self.surface = pygame.Surface((self.surface_size[0], self.surface_size[1]), pygame.SRCALPHA)
self.surface.set_colorkey(COLOR_BLACK)
pygame.draw.polygon(self.surface, color, [(0,0), (self.surface_size[0],0), (self.surface_size[0], self.surface_size[1]),(0, self.surface_size[1])] )
pygame.draw.polygon(self.surface, color, [(0, 0), (self.surface_size[0], 0),
(self.surface_size[0], self.surface_size[1]), (0, self.surface_size[1])])
render_module = module_manager.get_module(MODULE_RENDER)
center = (self.surface_size[0] / 2, self.surface_size[1] / 2)
@ -144,7 +145,9 @@ class Vehicle(object):
render_module.drawLine(self.surface, COLOR_BLUE, False, [center, arrow_tip], arrow_width)
render_module.drawLine(self.surface, COLOR_BLUE, False, [arrow_tip, (arrow_half - 1, 0)], arrow_width)
render_module.drawLine(self.surface, COLOR_BLUE, False, [arrow_tip, (arrow_half-1, self.surface_size[1])], arrow_width)
render_module.drawLine(
self.surface, COLOR_BLUE, False, [
arrow_tip, (arrow_half - 1, self.surface_size[1])], arrow_width)
def render(self, display):
actor_location = self.actor.get_location()
@ -301,6 +304,49 @@ class ModuleRender(object):
module_hud = module_manager.get_module(MODULE_HUD)
module_hud.add_info(self.name, module_info_text)
def getParallelLinesAtDistance(self, line, distance, transform_helper):
front_vector = (line[1][0] - line[0][0], line[1][1] - line[0][1])
left_vector = (-front_vector[1], front_vector[0])
length_left_vector = math.sqrt(left_vector[0] ** 2 + left_vector[1] ** 2)
unit_left_vector = (left_vector[0] / length_left_vector, left_vector[1] / length_left_vector)
distance = distance / 2.0
unit_right_vector = (-unit_left_vector[0], -unit_left_vector[1])
# Get lateral lines
lateral_left = [(line[0][0] + unit_left_vector[0] * distance, line[0][1] + unit_left_vector[1] * distance),
(line[1][0] + unit_left_vector[0] * distance, line[1][1] + unit_left_vector[1] * distance)]
# Get lateral lines
lateral_right = [(line[0][0] + unit_right_vector[0] * distance, line[0][1] + unit_right_vector[1] * distance),
(line[1][0] + unit_right_vector[0] * distance, line[1][1] + unit_right_vector[1] * distance)]
# Convert to screen space
lateral_left_screen = [transform_helper.convert_world_to_screen_point(lateral_left[0]),
transform_helper.convert_world_to_screen_point(lateral_left[1])]
lateral_right_screen = [transform_helper.convert_world_to_screen_point(lateral_right[0]),
transform_helper.convert_world_to_screen_point(lateral_right[1])]
return lateral_left_screen, lateral_right_screen
def drawPolygonFromParallelLines(self, surface, color, line, distance, transform_helper):
lateral_left_screen, lateral_right_screen = self.getParallelLinesAtDistance(line, distance, transform_helper)
pygame.draw.polygon(surface,
COLOR_DARK_GREY,
[lateral_left_screen[0],
lateral_left_screen[1],
lateral_right_screen[1],
lateral_right_screen[0]])
def drawLineAtDistance(self, surface, line, distance, color, transform_helper):
lateral_left_screen, lateral_right_screen = self.getParallelLinesAtDistance(line, distance, transform_helper)
line_width = 1
self.drawLine(surface, color, False, lateral_left_screen, line_width)
self.drawLine(surface, color, False, lateral_right_screen, line_width)
def drawLineList(self, surface, color, closed, list_lines, width):
for line in list_lines:
if not self.antialiasing:
@ -572,14 +618,15 @@ class ModuleWorld(object):
self._create_world_surfaces()
# Generate waypoints
waypoint_length=2.0
waypoint_length = 1.0
map_waypoints = self.town_map.generate_waypoints(waypoint_length)
# compute bounding boxes
self.x_min, self.y_min, self.x_max, self.y_max = self._compute_map_bounding_box(map_waypoints)
# Feed map bounding box and surface size to transform helper
self.transform_helper=TransformHelper((self.x_min * 1.05, self.y_min * 1.02), (self.x_max * 1.02, self.y_max * 1.02), self.surface_size)
self.transform_helper = TransformHelper(
(self.x_min * 1.02, self.y_min * 1.02), (self.x_max * 1.02, self.y_max * 1.02), self.surface_size)
# Retrieve data from waypoints orientation, width and length and do conversions into another list
self.normalized_point_list = []
@ -587,7 +634,8 @@ class ModuleWorld(object):
for waypoint in map_waypoints:
# Width of road
width=self.transform_helper.convert_world_to_screen_size((waypoint.lane_width, waypoint.lane_width))[0]
screen_width = self.transform_helper.convert_world_to_screen_size(
(waypoint.lane_width, waypoint.lane_width))[0]
direction = (1, 0)
yaw = math.radians(waypoint.transform.rotation.yaw)
@ -602,13 +650,14 @@ class ModuleWorld(object):
wp_0_screen = self.transform_helper.convert_world_to_screen_point(wp_0)
wp_1_screen = self.transform_helper.convert_world_to_screen_point(wp_1)
# Orientation of road
color = COLOR_BLACK
if waypoint.is_intersection:
self.intersection_waypoints.append(((wp_0_screen, wp_1_screen), COLOR_DARK_GREY, width))
self.intersection_waypoints.append(
((wp_0_screen, wp_1_screen), COLOR_DARK_GREY, screen_width, (wp_0, wp_1), waypoint.lane_width))
else:
self.normalized_point_list.append(((wp_0_screen, wp_1_screen), COLOR_DARK_GREY, width))
self.normalized_point_list.append(
((wp_0_screen, wp_1_screen), COLOR_DARK_GREY, screen_width, (wp_0, wp_1), waypoint.lane_width))
# Module render
self.render_module = module_manager.get_module(MODULE_RENDER)
@ -665,13 +714,22 @@ class ModuleWorld(object):
def render_map(self, display):
self.map_surface.fill(COLOR_GREY)
for point in self.normalized_point_list:
self.render_module.drawLineWithBorder(self.map_surface,
line = point[3]
front = (line[1][0] - line[0][0], line[0][1] - line[1][1])
length_front = math.sqrt(front[0] ** 2 + front[1] ** 2)
unit_front = (front[0] / length_front, front[1] / length_front)
self.render_module.drawPolygonFromParallelLines(self.map_surface,
point[1],
False,
point[0],
point[2],
3,
COLOR_WHITE)
line,
point[4],
self.transform_helper)
self.render_module.drawCircle(self.map_surface, point[0][0][0], point[0][0][1], point[2] / 2, point[1])
self.render_module.drawCircle(self.map_surface, point[0][1][0], point[0][1][1], point[2] / 2, point[1])
self.render_module.drawLineAtDistance(self.map_surface, line, point[4], COLOR_WHITE, self.transform_helper)
for point in self.intersection_waypoints:
self.render_module.drawLine(self.map_surface,
@ -680,6 +738,9 @@ class ModuleWorld(object):
point[0],
point[2])
self.render_module.drawCircle(self.map_surface, point[0][0][0], point[0][0][1], point[2] / 2, point[1])
self.render_module.drawCircle(self.map_surface, point[0][1][0], point[0][1][1], point[2] / 2, point[1])
def render_hero_actor(self, display, hero_actor, color, radius, translation_offset):
hero_radius = self.filter_radius / float((self.x_max - self.x_min)) * self.surface_size