def iterate_forces(self):

        objs = list(self.objects.values())

        for i in range(len(objs)):
            for v_vect, w in [(objs[i].v, objs[i].w),
                              (Vector2D(), objs[i].w),
                              (objs[i].v, 0)]:
                # print(objs)
                orig_x = objs[i].x
                orig_o = objs[i].o
                objs[i].x += v_vect
                objs[i].o += w
                collision = False
                obj1 = objs[i]
                for j in range(len(objs)):
                    if i == j:
                        continue
                    obj2 = objs[j]

                    collision |= self.compute(obj1, obj2)

                if collision:
                    objs[i].x = orig_x
                    objs[i].o = orig_o
                else:
                    break
                    
Пример #2
0
    def __init__(self, points):
        # calculate the center of mass
        if not isinstance(points[0], Vector2D):
            points = [Vector2D(*i) for i in points]
        self.x, self.area = center_of_mass(points)
        self.o = 0
        # print(self.x, 'COM')
        # translate all points to have center at 0, 0

        self._points = monotone_chain.build_convex(
            [i - self.x for i in points])
        self.radius = self.set_radius()
Пример #3
0
 def velocity(self, point):
     # if self.v.magnitude() > 20: self.v = Vector2D(0.1, 0.1)
     """
     Get the instantanous velocity of a point
     :param point: Vector2D
     :return: Vector2D
     """
     if not isinstance(point, Vector2D):
         point = Vector2D(*point)
     dist_vec = point - self.x
     tang_vec = dist_vec.unit().rotate(
         pi / 2) * dist_vec.magnitude() * self.w
     return self.v + tang_vec
Пример #4
0
    def __init__(self, name, mass, col_ticks=1, loss=0.99):

        self.name = name

        self.x = Vector2D()
        self.v = Vector2D()

        self.o = 0  # theta
        self.w = 0  # omega
        self.static = False

        self.su = None  # static friction
        self.ku = None  # kinetic friction

        self.m = mass  # mass
        self.i = None  # moment of inertia

        # determine if handler should process these movements
        self.handler_update = False
        self.col_ticks = col_ticks
        self.loss = loss

        self.forces = []
        self.torques = []
Пример #5
0
def segment_intersection(line1, line2):
    if not intersect(*line1, *line2):
        return False

    xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
    ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])

    def det(a, b):
        return a[0] * b[1] - a[1] * b[0]

    div = det(xdiff, ydiff)
    if div == 0:
        return False

    d = (det(*line1), det(*line2))
    x = det(d, xdiff) / div
    y = det(d, ydiff) / div
    return Vector2D(x, y)
Пример #6
0
def center_of_mass(points):
    """
    Computes the center of mass of a set of points by breaking it into a number of triangles
    :param points:
    :return: center of mass, area of points
    """
    # break into many triangles
    # each point is part of two triangles
    cor = [sum(points) / len(points)]
    mass_points = []
    area = 0
    for i in range(len(points) - 1):
        triangle = cor + points[i:i + 2]
        # print(triangle)
        mass_points.append(build_triangle_point_mass(triangle))
        area += shoelace_area(triangle)
        # print(triangle, area)
    mass_points.append(build_triangle_point_mass(cor +
                                                 [points[-1], points[0]]))
    area += shoelace_area(cor + [points[-1], points[0]])
    return Vector2D(*find_com(*zip(*mass_points))), area
Пример #7
0
                               (600, 12010)])
    box4 = Polygon('box4', 2, [(-1000, 12010), (1000, 12210), (-1000, 12210),
                               (1000, 12010)])
    box5 = Polygon('box5', 2, [(-500, 12710), (500, 12210), (-500, 12210),
                               (500, 12710), (0, 13210)])

    # box1 = Polygon('box1', 2, [(-5000, 10010), (5000, 10510), (-5000, 10510), (5000, 10010)])
    active.append(p2)
    active.append(box1)
    active.append(box2)
    active.append(box3)
    active.append(box4)
    active.append(box5)
    p3 = Polygon('p3', 200, [(-2000, 11510), (-1800, 11710), (-2000, 11710),
                             (-1800, 11510)])
    p3.v = Vector2D(50, 10)
    active.append(p3)

    for g in gen_gravs(active):
        w.add_heap_unit(g)

    for i in active:
        w.add_object(i)
    # print(p1.points)
    # print(p2.points)
    # print("what?")
    # print("---+++")
    w.save("new_test.pkl")
    w.objects = []
    w = w.load("new_test.pkl")
    # print("FDSFDSFDSFDSF")