def update_car(self): """Updates the car each frame by updating the scene graph created by build_car.""" verts = translate_vertices(rotate_polygon(CAR_BODY, self.car_orn, [0, 0]), self.car_pos) self.update_object_coords(self.car[0], verts) # We have to rotate the local transformation of the front-AABB verts = translate_vertices(rotate_polygon(CAR_FRONT, self.car_orn, [0, 0]), self.car_pos) rot = m2d.rot_vec(FRONT_TRANS, self.car_orn) verts = list(map(lambda x: m2d.add(x, rot), verts)) self.update_object_coords(self.car[1], verts) ray_points = [] for i in range(2, 2 + self.car_rays): vec = m2d.rot_vec(RAY_LINE, self.car_orn + i*self.ray_dtheta) self.update_object_coords(self.car[i], [self.car_pos, m2d.add(self.car_pos, vec)]) ray_points.append(m2d.add(self.car_pos, vec)) i1 = len(ray_points) - 1 for i0 in range(self.car_rays): i = i0 + 2 + self.car_rays self.update_object_coords(self.car[i], [ray_points[i1], ray_points[i0]]) i1 = i0 self.visit_tiles(ray_points) for i in range(len(self.car)): self.canvas.tag_raise(self.car[i])
def build_car(self, position, rays): """Builds the Tkinter objects for displaying the car. This must be called prior to draw_car.""" car = list() car.append(self.canvas.create_polygon(AABB_to_vertices(CAR_BODY), fill="blue")) car.append(self.canvas.create_polygon(AABB_to_vertices(CAR_FRONT), fill="red")) verts = translate_vertices(rotate_polygon(CAR_BODY, self.car_orn, [0, 0]), position) self.update_object_coords(car[0], verts) # We have to rotate the local transformation of the front-AABB verts = translate_vertices(rotate_polygon(CAR_FRONT, self.car_orn, [0, 0]), position) rot = m2d.rot_vec(FRONT_TRANS, self.car_orn) verts = list(map(lambda x: m2d.add(x, rot), verts)) self.update_object_coords(car[1], verts) ray_points = [] for i in range(rays): vec = m2d.rot_vec(RAY_LINE, i*self.ray_dtheta) verts = flatten([self.car_pos, m2d.add(self.car_pos, vec)]) ray_points.append(m2d.add(self.car_pos, vec)) car.append(self.canvas.create_line(*verts, fill="blue")) i1 = len(ray_points) - 1 for i0 in range(len(ray_points)): car.append(self.canvas.create_line(*[ray_points[i1], ray_points[i0]], fill="blue")) i1 = i0 return car
def draw_car(self): verts = self.translate_vertices(self.rotate_polygon(CAR_BODY, self.car_orn, [0, 0]), self.car_pos) self.update_object_coords(self.car[0], verts) # We have to rotate the local transformation of the front-AABB verts = self.translate_vertices(self.rotate_polygon(CAR_FRONT, self.car_orn, [0, 0]), self.car_pos) rot = m2d.rot_vec(FRONT_TRANS, self.car_orn) verts = list(map(lambda x: m2d.add(x, rot), verts)) self.update_object_coords(self.car[1], verts) ray_points = [] for i in range(2, 2 + self.car_rays): vec = m2d.rot_vec(RAY_LINE, self.car_orn + i*self.ray_dtheta) self.update_object_coords(self.car[i], [self.car_pos, m2d.add(self.car_pos, vec)]) ray_points.append(m2d.add(self.car_pos, vec)) i1 = len(ray_points) - 1 for i0 in range(self.car_rays): i = i0 + 2 + self.car_rays self.update_object_coords(self.car[i], [ray_points[i1], ray_points[i0]]) i1 = i0 # Test each rectangle that has not been seen against the ray triangles i1 = len(ray_points) - 1 for i0 in range(self.car_rays): tri = [self.car_pos, ray_points[i1], ray_points[i0]] if not m2d.is_ccw(tri): tri.reverse() for j in range(len(self.floor_polys)): if self.floors_seen[j]: continue elif m2d.test_intersection(tri, self.floor_polys[j]): self.canvas.itemconfig(self.floors_id[j], fill='lightblue') self.floors_seen[j] = True i1 = i0
def build_car(self, position, rays): """Builds the mesh for the car and the view triangles for intersecting mesh geometry""" car = list() # Two rectangles and rays - 1 triangles # car[0] car.append(self.canvas.create_polygon(self.AABB_to_vertices(CAR_BODY), fill="blue")) # car[1] car.append(self.canvas.create_polygon(self.AABB_to_vertices(CAR_FRONT), fill="red")) verts = self.translate_vertices(self.rotate_polygon(CAR_BODY, self.car_orn, [0, 0]), position) self.update_object_coords(car[0], verts) # We have to rotate the local transformation of the front-AABB verts = self.translate_vertices(self.rotate_polygon(CAR_FRONT, self.car_orn, [0, 0]), position) rot = m2d.rot_vec(FRONT_TRANS, self.car_orn) verts = list(map(lambda x: m2d.add(x, rot), verts)) self.update_object_coords(car[1], verts) ray_points = [] for i in range(rays): vec = m2d.rot_vec(RAY_LINE, i*self.ray_dtheta) verts = self.flatten([self.car_pos, m2d.add(self.car_pos, vec)]) ray_points.append(m2d.add(self.car_pos, vec)) car.append(self.canvas.create_line(*verts, fill="black")) i1 = len(ray_points) - 1 for i0 in range(len(ray_points)): car.append(self.canvas.create_line(*[ray_points[i1], ray_points[i0]], fill="black")) i1 = i0 return car
def setup_window(self): from tkinter import Canvas self.master.bind('<Destroy>', self.on_destroy) if self.is_test: self.master.bind('<Left>', lambda x: self.cmd_turn_car(self.car_orn - .1)) self.master.bind('<Right>', lambda x: self.cmd_turn_car(self.car_orn + .1)) self.master.bind('<Up>', lambda x: self.cmd_move_car(m2d.add(self.car_pos, m2d.mul(m2d.make_polar(self.car_orn), 5)))) self.master.bind('<Down>', lambda x: self.cmd_move_car(m2d.add(self.car_pos, m2d.mul(m2d.make_polar(self.car_orn), -5)))) self.canvas = Canvas(self.master, width=512, height=512) self.canvas.pack(fill="both", expand=True) self.canvas.bind('<Configure>', self.on_resize) self.width = self.canvas.winfo_width() self.height = self.canvas.winfo_height() return True
def __init__(self, master, send_q, resp_q, floor_file, walls_file, is_test=True): self.master = master master.title("PathfinderSim Display Window") master.geometry("512x600") # Parse the input files self.floors = OBJModel(floor_file) self.walls = OBJModel(walls_file) self.floors.parse() self.walls.parse() self.walls_AABB = self.walls.model_AABB() # cache this self.floors_AABB = self.floors.model_AABB() # cached self.floor_polys = [] self.floors_seen = [False] * self.floors.get_prim_count() self.floors_id = [] # Intercept the destroy event so we can shutdown gracefully master.bind("<Destroy>", self.on_destroy) self.is_test = is_test if is_test: master.bind('<Left>', lambda x: self.cmd_turn_car(self.car_orn - .1)) master.bind('<Right>', lambda x: self.cmd_turn_car(self.car_orn + .1)) master.bind('<Up>', lambda x: self.cmd_move_car(m2d.add(self.car_pos, m2d.mul(m2d.make_polar(self.car_orn), 5)))) master.bind('<Down>', lambda x: self.cmd_move_car(m2d.add(self.car_pos, m2d.mul(m2d.make_polar(self.car_orn), -5)))) self.canvas = Canvas(master, width=512, height=512) self.canvas.pack(fill="both", expand=True) self.canvas.bind('<Configure>', self.on_resize) self.width = self.canvas.winfo_width() self.height = self.canvas.winfo_height() self.car_pos = [100, 100] self.car_orn = 0 self.car_rays = 12 self.ray_dtheta = 2.*math.pi/self.car_rays # Async comms to class in separate thread self.command_q = send_q self.response_q = resp_q self.shutdown_flag = False self.button = Button(master, text="Quit", command=self.shutdown) self.button.pack() self.draw_map() self.car = self.build_car(self.car_pos, 12)
def build_grid(self): scale = self.obj.scale dims = self.obj.dims centre = m2d.mul(compute_centre(self.bound), dims[0]) print("Map Dims:", scale, dims, centre) prim_count = self.obj.get_prim_count() min_dims = m2d.mul(m2d.sub(self.bound[PB][:-1], self.bound[PA][:-1]), dims[0]) print(min_dims) tile_dims = [] ss_offset = [dims[0] / 2, dims[1] / 2] # Screen space offset ss_scale = [1, -1] # Flip y axis on screen # Find minimum dimensions for floor in range(prim_count): if floor % 2 == 1: continue # Skip odd numbered primitives (the other tri in the quad) prim = self.obj.get_prim(floor) if len(prim) != 3: continue A = self.obj.get_position( prim[0])[:-1] # Left Bottom corner, truncate z coord B = self.obj.get_position(prim[1])[:-1] # Right Bottom corner C = self.obj.get_position(prim[2])[:-1] # Left Top corner lb = m2d.sub(m2d.cp_mul([A[X], A[Y]], dims), centre) rt = m2d.sub(m2d.cp_mul([B[X], C[Y]], dims), centre) # Move the polygon into screen-space for direct display by the Display Window slb = m2d.add(m2d.cp_mul(lb, ss_scale), ss_offset) srt = m2d.add(m2d.cp_mul(rt, ss_scale), ss_offset) self.poly_arr.append([slb, srt]) tile_delta = m2d.sub(rt, lb) print(floor, A, B, tile_delta) if tile_delta[X] < min_dims[X]: min_dims[X] = tile_delta[X] if tile_delta[Y] < min_dims[Y]: min_dims[Y] = tile_delta[Y] tile_dims.append(tile_delta) print(min_dims) self.min_dims = min_dims # Compute the greatest common divisor for each axis x_dims = list(map(lambda x: x[0], tile_dims)) y_dims = list(map(lambda x: x[1], tile_dims)) xmin = functools.reduce(lambda x, y: gcd(int(x), int(y)), x_dims) ymin = functools.reduce(lambda x, y: gcd(int(x), int(y)), y_dims) print( "X Axis GCD:", xmin, x_dims) # Seems to be 1 in most cases... will have to be by pixel print("Y Axis GCD:", ymin, y_dims) print("Polygons:", self.poly_arr)