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(Gravity(g=100)) r2 = Rect([160, 100], [60, 60], restitution=restitution, fric_coeff=fric_coeff) bodies.append(r2) joints += [FixedJoint(r, r2)] r2.add_no_contact(r) r2.add_force(Gravity(g=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)
def testRect(self): r1 = Rect([0, 0], [1, 1], vel=[0, 0]) r2 = Rect([0, 0, 0], [1, 1]) r3 = Rect(torch.tensor([0, 0], dtype=DTYPE), [1, 1], vel=torch.tensor([0, 0], dtype=DTYPE)) r4 = Rect(torch.tensor([0, 0, 0], dtype=DTYPE), [1, 1], vel=torch.tensor([1, 1, 1], dtype=DTYPE)) r5 = Rect([0, 0, 0], [1, 1], [0, 0, 0], mass=torch.tensor(1, dtype=DTYPE)) r1.add_no_contact(r2) r2.add_force(Gravity()) r2.apply_forces(1) r3.set_p(r3.p.new_tensor([1, 1, 1])) r4.move(0.1)
def make_world(paddle_params, blocks_params, ball_params, ball_vel): bodies = [] constraints = [] # Add ball if it exists ball_pos, ball_rad = ball_params ball_body = Circle(ball_pos, ball_rad, vel=ball_vel, mass=BALL_MASS, restitution=STD_RESTITUTION, fric_coeff=FRIC_COEFF, col=BALL_COLOR, thickness=0) bodies.append(ball_body) # Add paddle paddle_pos, paddle_dims = paddle_params paddle_body = Rect(paddle_pos, paddle_dims, restitution=STD_RESTITUTION, fric_coeff=FRIC_COEFF, col=PADDLE_COLOR, thickness=0) bodies.append(paddle_body) constraints += [ # YConstraint(paddle_body), # RotConstraint(paddle_body) ] paddle_idx = len(bodies) - 1 # Add walls left_wall = Rect([WALL_WIDTH / 2.0, STOPPER_LINE / 2.0], [WALL_WIDTH, STOPPER_LINE], restitution=STD_RESTITUTION, fric_coeff=FRIC_COEFF, col=WALL_COLOR, thickness=0) constraints.append(TotalConstraint(left_wall)) left_wall.add_no_contact(paddle_body) bodies.append(left_wall) right_wall = Rect([ SCREEN_WIDTH - WALL_WIDTH / 2.0, (STOPPER_LINE + STOPPER_HEIGHT) / 2.0 ], [WALL_WIDTH, STOPPER_LINE + STOPPER_HEIGHT], restitution=STD_RESTITUTION, fric_coeff=FRIC_COEFF, col=WALL_COLOR, thickness=0) constraints.append(TotalConstraint(right_wall)) paddle_body.add_no_contact(right_wall) bodies.append(right_wall) roof = Rect([SCREEN_WIDTH / 2.0, ROOF_LINE / 2.0], [SCREEN_WIDTH - 2 * WALL_WIDTH, ROOF_LINE], restitution=STD_RESTITUTION, fric_coeff=FRIC_COEFF, col=WALL_COLOR, thickness=0) roof.add_no_contact(paddle_body) roof.add_no_contact(left_wall) roof.add_no_contact(right_wall) constraints.append(TotalConstraint(roof)) bodies.append(roof) # Add stoppers left_stopper = Rect( [STOPPER_WIDTH / 2.0, STOPPER_LINE + STOPPER_HEIGHT / 2.0], [STOPPER_WIDTH, STOPPER_HEIGHT], restitution=STOPPER_RESTITUTION, fric_coeff=FRIC_COEFF, col=LEFT_STOPPER_COLOR, thickness=0) constraints.append(TotalConstraint(left_stopper)) bodies.append(left_stopper) left_stopper.add_no_contact(left_wall) left_stopper.add_no_contact(ball_body) right_stopper = Rect([ SCREEN_WIDTH + STOPPER_WIDTH / 2.0, STOPPER_LINE + STOPPER_HEIGHT / 2.0 ], [STOPPER_WIDTH, STOPPER_HEIGHT], restitution=STOPPER_RESTITUTION, fric_coeff=FRIC_COEFF, col=WALL_COLOR, thickness=0) constraints.append(TotalConstraint(right_stopper)) bodies.append(right_stopper) right_stopper.add_no_contact(right_wall) right_stopper.add_no_contact(ball_body) # Add blocks blocks_bodies = [] for block_params in blocks_params: block_pos, block_dims = block_params import math # XXX level = int((block_pos[1] - block_dims[1] / 2 - BLOCKS_TOP_LEFT[0]) / BLOCK_HEIGHT) # TODO Define restitution by level? (different levels have different bounces) block_restitution = 1 block_body = Rect(block_pos, block_dims, restitution=block_restitution, fric_coeff=FRIC_COEFF, col=BLOCK_COLORS[level], thickness=0) bodies.append(block_body) blocks_bodies.append(block_body) constraints.append(TotalConstraint(block_body)) block_body.add_no_contact(left_wall) block_body.add_no_contact(right_wall) for other_block in blocks_bodies[:-1]: block_body.add_no_contact(other_block) return World(bodies, constraints, dt=DT), ball_body, paddle_body, blocks_bodies, paddle_idx