Example #1
0
    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
Example #2
0
    def visit_tiles(self, scale, car_pos, ray_points):
        # Test each ray against each polygon, if intersecting, rasterise the triangle into the buffer, setting the
        # visible flag to true (0, 0, 0), i.e. black, if the pixel has been visited

        # Return an index containing the tile ids updated
        changed = []
        car_rays = len(ray_points)

        i1 = len(ray_points) - 1
        for i0 in range(car_rays):
            tri = [car_pos, ray_points[i1], ray_points[i0]]
            if not m2d.is_ccw(tri):
                tri.reverse()

            for tile in range(self.poly_count()):
                LB, TR = self.get_poly(tile)
                poly = verts([LB, TR])
                if m2d.test_intersection(tri, poly):
                    w = int(TR[0] - LB[0])
                    h = int(LB[1] - TR[1])  # Y is flipped

                    img = self.images[tile]
                    img_w, img_h = img.size

                    pixels = list(img.getdata())
                    cx = int(LB[0] + w / 2)
                    cy = int(LB[1] - h / 2)

                    def cb(coord):
                        if 0 <= coord[0] < img_w and 0 <= coord[1] < img_h:
                            idx = coord[1] * img_w + coord[0]
                            if 0 < idx < len(pixels):
                                pixels[idx] = (0, 0, 0)

                    itri = list(
                        map(
                            lambda x: [
                                int(round((x[0] - cx + w / 2) / scale[0], 4)),
                                int(round((x[1] - cy + h / 2) / scale[1], 4))
                            ], tri))
                    tr.rasterise(itri, cb)
                    img.putdata(pixels)
                    changed.append(tile)

            i1 = i0

        return changed