Ejemplo n.º 1
0
    def testChain(self):
        bodies = []
        joints = []

        # make chain of rectangles
        r = Rect([300, 50], [20, 60])
        bodies.append(r)
        joints.append(XConstraint(r))
        joints.append(YConstraint(r))
        for i in range(1, 10):
            r = Rect([300, 50 + 50 * i], [20, 60])
            bodies.append(r)
            joints.append(Joint(bodies[-1], bodies[-2], [300, 25 + 50 * i]))
            bodies[-1].add_no_collision(bodies[-2])
        bodies[-1].add_force(ExternalForce(down_force, multiplier=100))

        # make projectile
        c = Circle([50, 500], 20, restitution=1)
        bodies.append(c)
        c.add_force(ExternalForce(hor_impulse, multiplier=1000))

        recorder = None
        # recorder = Recorder(DT, self.screen)
        world = World(bodies, joints, dt=DT)
        run_world(world, run_time=TIME, screen=self.screen, recorder=recorder)
Ejemplo n.º 2
0
        def make_world(forces, mass):
            bodies = []
            joints = []

            # make chain of rectangles
            r = Rect([300, 50], [20, 60])
            bodies.append(r)
            joints.append(Joint(r, None, [300, 30]))
            for i in range(1, 10):
                if i < 9:
                    r = Rect([300, 50 + 50 * i], [20, 60])
                else:
                    r = Rect([300, 50 + 50 * i], [20, 60], mass=mass)
                bodies.append(r)
                joints.append(Joint(bodies[-1], bodies[-2],
                                    [300, 25 + 50 * i]))
                bodies[-1].add_no_collision(bodies[-2])
            bodies[-1].add_force(ExternalForce(down_force, multiplier=100))

            # make projectile
            m = 13
            c1 = Circle([50, 500], 20)
            bodies.append(c1)
            for f in forces:
                c1.add_force(ExternalForce(f, multiplier=100 * m))

            world = World(bodies, joints, dt=DT)
            return world, r
Ejemplo n.º 3
0
def fixed_joint_demo(screen):
    bodies = []
    joints = []
    restitution = 0.5
    fric_coeff = 0.15

    r = Rect([120, 100], [60, 60],
             restitution=restitution, fric_coeff=fric_coeff)
    bodies.append(r)
    r.add_force(ExternalForce(gravity, multiplier=100))
    r2 = Rect([160, 100], [60, 60],
              restitution=restitution, fric_coeff=fric_coeff)
    bodies.append(r2)
    joints += [FixedJoint(r, r2)]
    r2.add_no_collision(r)
    r2.add_force(ExternalForce(gravity, multiplier=100))

    inclination = math.pi / 32
    r = Rect([inclination, 500, 500], [900, 10],
             restitution=restitution, fric_coeff=fric_coeff)
    bodies.append(r)
    joints.append(TotalConstraint(r))

    recorder = None
    # recorder = Recorder(DT, screen)
    world = World(bodies, joints, dt=DT)
    run_world(world, run_time=TIME, screen=screen, recorder=recorder)
Ejemplo n.º 4
0
def chain_demo(screen):
    bodies = []
    joints = []
    restitution = 0.9

    # make chain of rectangles
    r = Rect([300, 50], [20, 60], restitution=restitution)
    bodies.append(r)
    joints.append(XConstraint(r))
    joints.append(YConstraint(r))
    for i in range(1, 10):
        r = Rect([300, 50 + 50 * i], [20, 60], restitution=restitution)
        bodies.append(r)
        joints.append(Joint(bodies[-1], bodies[-2], [300, 25 + 50 * i]))
        bodies[-1].add_no_collision(bodies[-2])
    bodies[-1].add_force(ExternalForce(gravity, multiplier=100))

    # make projectile
    c = Circle([50, 500], 20, restitution=restitution)
    bodies.append(c)
    c.add_force(ExternalForce(hor_impulse, multiplier=1000))

    clock = Circle([975, 575], 20, vel=[1, 0, 0])
    bodies.append(clock)

    recorder = None
    # recorder = Recorder(DT, screen)
    world = World(bodies, joints, dt=DT)
    run_world(world, run_time=TIME * 2, screen=screen, recorder=recorder)
Ejemplo n.º 5
0
    def testFric(self):
        bodies = []
        joints = []

        def timed_force(t):
            if 1 < t < 2:
                return ExternalForce.RIGHT
            else:
                return ExternalForce.ZEROS

        r = Rect([400, 400], [900, 10])
        bodies.append(r)
        r.add_force(ExternalForce(timed_force, multiplier=100))
        r.add_force(ExternalForce(gravity, multiplier=100))

        c = Circle([200, 364], 30)
        bodies.append(c)
        c.add_force(ExternalForce(gravity, multiplier=100))

        c = Circle([50, 436], 30)
        bodies.append(c)
        joints.append(XConstraint(c))
        joints.append(YConstraint(c))
        c = Circle([800, 436], 30)
        bodies.append(c)
        joints.append(XConstraint(c))
        joints.append(YConstraint(c))

        recorder = None
        # recorder = Recorder(DT, self.screen)
        world = World(bodies, joints, dt=DT)
        run_world(world, run_time=10, screen=self.screen, recorder=recorder)
Ejemplo n.º 6
0
def make_world(forces, controlT, controlU):

    bodies = []
    joints = []

    # make chain of rectangles

    r = Rect([0, 120, 240], [60, 60], mass=1)

    r2 = Rect([0, 200, 240], [60, 60], mass=1)
    #r.set_p(r.p.new_tensor([1, 1, 1]))
    bodies.append(r)
    bodies.append(r2)
    #joints.append(Joint(r, None, [300, 30]))
    r.add_force(Gravity(g=10))
    r2.add_force(Gravity(g=10))

    controlForceL = lambda t: controlForce(t, controlT, controlU)
    cf = ExternalForce(controlForceL, multiplier=1)

    controlForceL2 = lambda t: controlForce2(t, controlT, controlU)
    cf2 = ExternalForce(controlForceL2, multiplier=1)

    r.add_force(cf)
    r2.add_force(cf2)

    floor = Rect([0, 300], [1000, 30], mass=100)
    floorStep = Rect([300, 240], [100, 89], mass=100)
    floorStep2 = Rect([0, 240], [100, 89], mass=100)

    joints.append(TotalConstraint(floor))
    bodies.append(floor)
    joints.append(TotalConstraint(floorStep))
    bodies.append(floorStep)
    joints.append(TotalConstraint(floorStep2))
    bodies.append(floorStep2)

    #joints.append(Joint(bodies[-1], bodies[-2], [300, 25 + 50 * i]))
    #bodies[-1].add_no_contact(bodies[-2])

    # make projectile
    #m = 3
    #c_pos = torch.tensor([50, bodies[-1].pos[1]])  # same Y as last chain link
    #c = Circle(c_pos, 20, restitution=1.)
    #bodies.append(c)
    #for f in forces:
    #    c.add_force(ExternalForce(f, multiplier=500 * m))

    world = World(bodies,
                  joints,
                  dt=DT,
                  post_stab=True,
                  strict_no_penetration=True)
    return world, r
Ejemplo n.º 7
0
    def testFric(self):

        restitution = 0.75
        fric_coeff = 1

        bodies = []
        joints = []

        def timed_force(t):
            if 1 < t < 2:
                return ExternalForce.RIGHT
            else:
                return ExternalForce.ZEROS

        r = Rect([400, 400], [900, 10],
                 restitution=restitution,
                 fric_coeff=fric_coeff)
        bodies.append(r)
        r.add_force(ExternalForce(timed_force, multiplier=100))
        r.add_force(ExternalForce(down_force, multiplier=100))

        c = Circle([200, 364],
                   30,
                   restitution=restitution,
                   fric_coeff=fric_coeff)
        bodies.append(c)
        c.add_force(ExternalForce(down_force, multiplier=100))

        c = Circle([50, 436],
                   30,
                   restitution=restitution,
                   fric_coeff=fric_coeff)
        bodies.append(c)
        joints.append(XConstraint(c))
        joints.append(YConstraint(c))
        c = Circle([800, 436],
                   30,
                   restitution=restitution,
                   fric_coeff=fric_coeff)
        bodies.append(c)
        joints.append(XConstraint(c))
        joints.append(YConstraint(c))

        clock = Circle([975, 575], 20, vel=[1, 0, 0])
        bodies.append(clock)

        recorder = None
        # recorder = Recorder(DT, screen)
        world = World(bodies, joints, dt=DT)
        run_world(world, run_time=10, screen=self.screen, recorder=recorder)
Ejemplo n.º 8
0
def make_world(forces, mass, num_links=10):
    bodies = []
    joints = []

    # make chain of rectangles
    link_mass = mass / num_links
    r = Rect([300, 50], [20, 60], mass=link_mass)
    bodies.append(r)
    joints.append(Joint(r, None, [300, 30]))
    for i in range(1, num_links):
        if i < num_links - 1:
            r = Rect([300, 50 + 50 * i], [20, 60], mass=link_mass)
        else:
            r = Rect([300, 50 + 50 * i], [20, 60], mass=link_mass)
        r.add_force(Gravity(g=100))
        bodies.append(r)
        joints.append(Joint(bodies[-1], bodies[-2], [300, 25 + 50 * i]))
        bodies[-1].add_no_contact(bodies[-2])

    # make projectile
    m = 3
    c_pos = torch.tensor([50, bodies[-1].pos[1]])  # same Y as last chain link
    c = Circle(c_pos, 20, restitution=1.)
    bodies.append(c)
    for f in forces:
        c.add_force(ExternalForce(f, multiplier=500 * m))

    world = World(bodies, joints, dt=DT, post_stab=True)
    return world, r
Ejemplo n.º 9
0
def make_world(forces, mass, num_links=10):
    bodies = []
    joints = []

    # make chain of rectangles
    r = Rect([300, 50], [20, 60])
    bodies.append(r)
    joints.append(Joint(r, None, [300, 30]))
    for i in range(1, num_links):
        if i < num_links - 1:
            r = Rect([300, 50 + 50 * i], [20, 60])
        else:
            r = Rect([300, 50 + 50 * i], [20, 60], mass=mass)
        bodies.append(r)
        joints.append(Joint(bodies[-1], bodies[-2], [300, 25 + 50 * i]))
        bodies[-1].add_no_collision(bodies[-2])
    bodies[-1].add_force(Gravity(g=100))

    # make projectile
    m = 13
    c1 = Circle([50, bodies[-1].pos.data[1]], 20)  # same Y as last chain link
    bodies.append(c1)
    for f in forces:
        c1.add_force(ExternalForce(f, multiplier=100 * m))

    world = World(bodies, joints, dt=DT)
    return world, r
Ejemplo n.º 10
0
    def testSlide(self):
        bodies = []
        joints = []
        restitution = 0.5
        fric_coeff = 0.2

        clock = Circle([975, 575], 20, vel=[1, 0, 0])
        bodies.append(clock)

        p1 = Hull([500, 300], [[450, 5], [-450, 5], [-450, -5], [450, -5]],
                  restitution=restitution, fric_coeff=fric_coeff)
        p1.rotate_verts(get_tensor(math.pi / 32))
        bodies.append(p1)
        joints.append(TotalConstraint(p1))

        # Rectangle
        # p2 = Hull([100, 100], [[30, 30], [-30, 30], [-30, -30], [30, -30]],
        #           restitution=restitution, fric_coeff=fric_coeff)
        # Pentagon
        p2 = Hull([100, 100], [[50, 0], [30, 50], [-30, 30], [-50, -30], [0, -50]],
                  restitution=restitution, fric_coeff=fric_coeff)
        # Hexagon
        p2 = Hull([100, 100], [[50, 0], [30, 50], [-30, 30], [-50, -30], [0, -50], [30, -30]],
                  restitution=restitution, fric_coeff=fric_coeff)
        bodies.append(p2)
        p2.add_force(ExternalForce(down_force, multiplier=100))
        # p2.add_force(ExternalForce(hor_impulse, multiplier=-100))

        recorder = None
        # recorder = Recorder(DT, self.screen)
        world = World(bodies, joints, dt=DT)
        run_world(world, run_time=TIME, screen=self.screen, recorder=recorder)
Ejemplo n.º 11
0
def debug_demo(screen):
    bodies = []
    joints = []
    # Ball hitting object constrained by 1 joint
    for i in range(1, 3):
        c = Circle([150, 150 + 80 * (i - 1)], 20)
        if i == 1:
            c.add_force(ExternalForce(vert_impulse, multiplier=500))
        bodies.append(c)
    joints.append(Joint(bodies[-1], None, [140, 220]))

    # Ball bouncing on body fixed in place
    for i in range(1, 3):
        c = Circle([300 + 1 * (i - 1), 150 + 80 * (i - 1)], 20)
        if i == 1:
            c.add_force(Gravity(g=100))
        # else:
        #     c.add_force(ExternalForce(neg_gravity, multiplier=100))
        bodies.append(c)
    joints.append(TotalConstraint(bodies[-1]))

    # 2 free ball collision angled
    for i in range(1, 3):
        c = Circle([225 - 10 * (i - 1), 300 + 80 * (i - 1)], 20)
        if i == 1:
            c.add_force(Gravity(g=100))
        bodies.append(c)

    # 2 free ball collision straight
    for i in range(1, 3):
        c = Circle([375, 300 + 80 * (i - 1)], 20)
        if i == 1:
            c.add_force(ExternalForce(vert_impulse, multiplier=500))
        bodies.append(c)

    r = Rect([300, 500], [40, 40], vel=[-1, 0, 0])
    r.add_force(Gravity(g=-100))
    bodies.append(r)

    clock = Circle([975, 575], 20, vel=[1, 0, 0])
    bodies.append(clock)

    world = World(bodies, joints, dt=DT, post_stab=True)
    run_world(world, run_time=10, screen=screen)
Ejemplo n.º 12
0
        def make_world(learned_force):
            bodies = []
            joints = []

            target = Circle([500, 300], 30)
            bodies.append(target)

            c1 = Circle([250, 210], 30)
            bodies.append(c1)
            c1.add_force(ExternalForce(learned_force))
            c1.add_no_collision(target)

            c2 = Circle([400, 250], 30)
            bodies.append(c2)
            c2.add_no_collision(target)

            world = World(bodies, joints, dt=DT)
            return world, c2, target
Ejemplo n.º 13
0
    def testDemo(self):
        bodies = []
        joints = []
        # Ball hitting object constrained by 1 joint
        for i in range(1, 3):
            c = Circle([150, 150 + 80 * (i - 1)], 20)
            if i == 1:
                c.add_force(ExternalForce(vert_impulse, multiplier=500))
            bodies.append(c)
        joints.append(Joint(bodies[-1], None, [140, 220]))

        # Ball bouncing on body fixed in place
        for i in range(1, 3):
            c = Circle([300 + 1 * (i - 1), 150 + 80 * (i - 1)], 20)
            if i == 1:
                c.add_force(ExternalForce(down_force, multiplier=100))
            bodies.append(c)
        joints.append(TotalConstraint(bodies[-1]))

        # 2 free ball collision angled
        for i in range(1, 3):
            c = Circle([225 - 10 * (i - 1), 300 + 80 * (i - 1)], 20)
            if i == 1:
                c.add_force(ExternalForce(down_force, multiplier=100))
            bodies.append(c)

        # 2 free ball collision straight
        for i in range(1, 3):
            c = Circle([375, 300 + 80 * (i - 1)], 20)
            if i == 1:
                c.add_force(ExternalForce(vert_impulse, multiplier=500))
            bodies.append(c)

        r = Rect([300, 500], [40, 40])
        r.add_force(ExternalForce(down_force, multiplier=-100))
        r.v[0] = -1
        bodies.append(r)

        r = Rect([300, 50], [40, 40])
        r.add_force(ExternalForce(down_force, multiplier=100))
        r.v[0] = -1
        for b in bodies:
            b.add_no_collision(r)
        # bodies.append(r)

        world = World(bodies, joints, dt=DT)
        run_world(world, run_time=10, screen=self.screen)
Ejemplo n.º 14
0
    def testSlide(self):
        bodies = []
        joints = []

        r = Rect([500, 300], [900, 10])
        r.v[0] = math.pi / 32
        r.move(1)
        r.v[0] = 0.
        bodies.append(r)
        joints.append(TotalConstraint(r))

        r = Rect([100, 100], [60, 60])
        r.v[0] = -math.pi / 8 * 0
        r.move(1)
        r.v[0] = 0.
        bodies.append(r)
        r.add_force(ExternalForce(down_force, multiplier=100))
        # r.add_force(ExternalForce(hor_impulse, multiplier=-100))

        # c = Circle([100, 150], 30)
        # bodies.append(c)
        # c.add_force(ExternalForce(gravity, multiplier=100))

        # c = Circle([50, 550], 30)
        # c.add_force(ExternalForce(rot_impulse, multiplier=1000))
        # bodies.append(c)

        # XXX
        # c = Circle([875, 100], 30)
        # bodies.append(c)
        # c.add_force(ExternalForce(gravity, multiplier=100))

        recorder = None
        # recorder = Recorder(DT, self.screen)
        world = World(bodies, joints, dt=DT)
        run_world(world, run_time=TIME, screen=self.screen, recorder=recorder)
Ejemplo n.º 15
0
def slide_demo(screen):
    bodies = []
    joints = []
    restitution = Params.DEFAULT_RESTITUTION
    fric_coeff = 0.15

    inclination = math.pi / 32
    r = Rect([inclination, 500, 300], [900, 10],
             restitution=restitution,
             fric_coeff=fric_coeff)
    bodies.append(r)
    joints.append(TotalConstraint(r))

    r = Rect([100, 100], [60, 60],
             restitution=restitution,
             fric_coeff=fric_coeff)
    # r = Hull([100, 100], [[30, 30], [-30, 30], [-30, -30], [30, -30]])
    bodies.append(r)
    r.add_force(ExternalForce(gravity, multiplier=100))

    recorder = None
    # recorder = Recorder(DT, screen)
    world = World(bodies, joints, dt=DT)
    run_world(world, run_time=TIME, screen=screen, recorder=recorder)
Ejemplo n.º 16
0
    def testRender(self):
        p1 = Hull([700, 200], [[math.sqrt(2) * 10 + 10, 0 + 10], [0 + 10, math.sqrt(2) * 10 + 10],
                               [math.sqrt(2) * -10 + 10, 0 + 10], [0 + 10, math.sqrt(2) * -10 + 10]])
        p2 = Hull([300, 200], [[60, 0], [60, 60], [0, 60]])
        p2.add_force(ExternalForce(hor_impulse, multiplier=1000))
        p2.add_force(ExternalForce(rot_impulse, multiplier=1000))
        p3 = Hull([700, 400], [[60, 0], [60, 60], [0, 0]])
        p4 = Hull([300, 420], [[60, 0], [0, 60], [0, 0]])
        p4.add_force(ExternalForce(hor_impulse, multiplier=1000))
        p4.add_force(ExternalForce(rot_impulse, multiplier=1000))
        p5 = Hull([500, 300], [[50, 0], [30, 60], [-30, 30], [-60, -30], [0, -60]])
        p5.add_force(ExternalForce(hor_impulse, multiplier=1000))
        p5.add_force(ExternalForce(rot_impulse, multiplier=1000))

        # print(p5.verts)
        # print(get_support(p5.verts, wrap_variable([-1, -1])))

        # world = World([p5])
        world = World([p1, p2, p3, p4, p5])
        run_world(world, screen=self.screen, run_time=180)