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
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)
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
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_contact(bodies[-2]) bodies[-1].add_force(Gravity(g=100)) # make projectile c = Circle([50, 500], 20, restitution=restitution) bodies.append(c) c.add_force(ExternalForce(hor_impulse, multiplier=2000)) clock = Circle([975, 575], 20, vel=[1, 0, 0]) bodies.append(clock) recorder = None # recorder = Recorder(DT, screen) world = World(bodies, joints, dt=DT, post_stab=True) run_world(world, run_time=TIME * 2, screen=screen, recorder=recorder)
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
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)
def testCircle(self): c1 = Circle([0, 0], 1, vel=[0, 0]) c2 = Circle([0, 0, 0], 1) c3 = Circle(torch.tensor([0, 0], dtype=DTYPE), torch.tensor(1, dtype=DTYPE), vel=torch.tensor([0, 0], dtype=DTYPE)) c4 = Circle(torch.tensor([0, 0, 0], dtype=DTYPE), torch.tensor(1, dtype=DTYPE), vel=torch.tensor([1, 1, 1], dtype=DTYPE)) c5 = Circle([0, 0, 0], 1, [0, 0, 0], mass=torch.tensor(1, dtype=DTYPE)) c1.add_no_contact(c2) c2.add_force(Gravity()) c2.apply_forces(1) c3.set_p(c3.p.new_tensor([1, 1, 1])) c4.move(0.1)
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)
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
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)
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)