def __init__(self, edge_id, p0, p1, name, lanes): self.id = edge_id self.p0 = p0 self.p1 = p1 self.name = name self.lanes = lanes self.distance = math_utils.distance(p0, p1) self.dx = self.p1.x - self.p0.x self.dy = self.p1.y - self.p0.y self.length = math_utils.pythag(self.dx, self.dy) self.unit_x = self.dx / self.length self.unit_y = self.dy / self.length self.lines = [] self.width = 2 self.way_gap = 0 # size of gap between each road direction self.lane_gap = 4 # size of gap between lines self.click_threshold = self.width * self.lanes for i in range(lanes): u0 = self.get_lane_adjusted_point(self.p0.x, self.p0.y, i) u1 = self.get_lane_adjusted_point(self.p1.x, self.p1.y, i) self.lines.append(self.create_line(u0, u1, self.width)) self.text = self.create_text()
def distance_between_vertices(self, vertex_id_1, vertex_id_2): v1 = self.vertices[vertex_id_1] v2 = self.vertices[vertex_id_2] dx = v2.x - v1.x dy = v2.y - v1.y dist = math_utils.pythag(dx, dy) return dist
def move_towards_dest(self, dt): movement = (dt * self.speed) nx, ny = self.gps.get_lane_adj_coords(self.next_dest_id, self.current_edge, self.lane_index) dx = nx - self.x dy = ny - self.y dist = math_utils.pythag(dx, dy) if dist <= movement: new_source = self.next_dest_id self.next_dest_id = self.get_next_dest() self.current_edge = self.gps.get_edge(new_source, self.next_dest_id) self.speed_limit = self.get_speed_limit() return mv_x = (dx / dist) * movement mv_y = (dy / dist) * movement self.x += mv_x self.y += mv_y
def check_collision(self, other): """Check whether this car is about to smash into another car""" # Only check for forward collisions by checking movement vector adjustment_factor = 20.0 nx, ny = self.gps.get_lane_adj_coords(self.next_dest_id, self.current_edge, self.lane_index) dx = nx - self.x dy = ny - self.y dist = math_utils.pythag(dx, dy) or 1.0 mv_x = (dx / dist) * adjustment_factor mv_y = (dy / dist) * adjustment_factor # width vs height is dependent on the car's orientation, # simplify by always checking longest one x_overlap = (abs(self.x + mv_x - other.x) * 2) < (self.height + other.height) y_overlap = (abs(self.y + mv_y - other.y) * 2) < (self.height + other.height) collision_detected = x_overlap and y_overlap return collision_detected
def can_vertices_be_reduced(self, vertex_id_1, vertex_id_2, vertex_id_3): """Check whether 3 vertices can be reduced to 2 vertices""" v2 = self.vertices[vertex_id_2] if v2.is_intersection: return False v1 = self.vertices[vertex_id_1] dx1 = v2.x - v1.x dy1 = v2.y - v1.y dist = math_utils.pythag(dx1, dy1) if dist < 10.0: return True v3 = self.vertices[vertex_id_3] dx2 = v3.x - v2.x dy2 = v3.y - v2.y angle1 = math_utils.angle(dx1, dy1) angle2 = math_utils.angle(dx2, dy2) if abs(angle2 - angle1) < 0.10: # ~5.7 degrees return True return False