Ejemplo n.º 1
0
    def __init__(self, space, rect, playfield=None):
        super(PlungerAssembly, self).__init__()
        self.chute_counter = 0
        self.rect = Rect(0, 0, 1, 1)

        spring_strength = 100 * plunger_mass
        chute_opening = playfield.position + rect.center - (rect.width / 2. -
                                                            ball_radius * 4, 0)
        plunger_rect = Rect(0, 0, rect.width * .2, ball_radius / 2.)
        anchor0 = chute_opening - playfield.position - (ball_radius * 3., 0)
        anchor1 = anchor0 + (rect.width * .8, 0)
        anchor2 = -plunger_rect.width / 2., 0

        plunger_body = Body(plunger_mass, pymunk.inf)
        plunger_shape = Poly.create_box(plunger_body, plunger_rect.size)
        plunger_shape.layers = 1
        plunger_shape.friction = 0.1
        plunger_shape.elasticity = 1.0
        plunger_shape.collision_type = plunger_type
        plunger_body.position = chute_opening + (plunger_rect.width / 2., 0)

        j0 = GrooveJoint(playfield, plunger_body, anchor0, anchor1, anchor2)
        j1 = DampedSpring(playfield, plunger_body, anchor0, anchor2, 0,
                          spring_strength, 5)

        s0 = Circle(Body(), ball_radius / 2.)
        s0.layers = 1
        s0.sensor = True
        s0.collision_type = sensor0_type
        s0.body.position = chute_opening + (ball_radius * 4., 0.0)

        s1 = Circle(Body(), ball_radius * 3.)
        s1.layers = 1
        s1.sensor = True
        s1.collision_type = sensor1_type
        s1.body.position = chute_opening

        def inc_counter(space, arbiter):
            self.chute_counter += 1
            return True

        def dec_counter(space, arbiter):
            self.chute_counter -= 1

        f = space.add_collision_handler
        f(sensor1_type, plunger_type, begin=inc_counter, separate=dec_counter)

        self.playfield = playfield
        self.plunger_offset = playfield.position - plunger_body.position + (
            ball_radius * 3, 0)
        self.spring = j1
        self.spring_length = rect.width / 2.
        self.spring_strength = spring_strength
        self.plunger_body = plunger_body
        self.ball_chute = Rect(0, 0, ball_radius * 2., ball_radius * 2.)
        self.ball_chute.center = chute_opening
        self._original_image = pygame.Surface(plunger_rect.size)
        self._original_image.fill((192, 255, 255))
        self.shapes = [plunger_shape, s0, s1, j0, j1]
        self.visible = 0
Ejemplo n.º 2
0
    def __init__(self, space, rect, playfield=None):
        super(PlungerAssembly, self).__init__()
        self.chute_counter = 0
        self.rect = pygame.Rect(0, 0, 0, 0)

        spring_strength = 100 * plunger_mass
        chute_opening = playfield.position + rect.center - (rect.width / 2 - ball_radius * 4, 0)
        plunger_rect = pygame.Rect(0, 0, rect.width * .2, ball_radius / 2)
        anchor0 = chute_opening - playfield.position - (ball_radius * 3, 0)
        anchor1 = anchor0 + (rect.width * .8, 0)
        anchor2 = -plunger_rect.width / 2, 0

        plunger_body = Body(plunger_mass, pymunk.inf)
        plunger_shape = Poly.create_box(plunger_body, plunger_rect.size)
        plunger_shape.layers = 1
        plunger_shape.friction = 0
        plunger_shape.elasticity = 1.0
        plunger_shape.collision_type = plunger_type
        plunger_body.position = chute_opening + (plunger_rect.width / 2, 0)

        j0 = GrooveJoint(playfield, plunger_body, anchor0, anchor1, anchor2)
        j1 = DampedSpring(playfield, plunger_body, anchor0, anchor2, 0, spring_strength, 5)

        s0 = Circle(Body(), ball_radius / 2)
        s0.layers = 1
        s0.sensor = True
        s0.collision_type = sensor0_type
        s0.body.position = chute_opening + (ball_radius * 4, 0)

        s1 = Circle(Body(), ball_radius * 3)
        s1.layers = 1
        s1.sensor = True
        s1.collision_type = sensor1_type
        s1.body.position = chute_opening

        def inc_counter(space, arbiter):
            self.chute_counter += 1
            return True

        def dec_counter(space, arbiter):
            self.chute_counter -= 1

        f = space.add_collision_handler
        f(sensor1_type, plunger_type, begin=inc_counter, separate=dec_counter)

        self.playfield = playfield
        self.plunger_offset = playfield.position - plunger_body.position + (ball_radius * 3, 0)
        self.spring = j1
        self.spring_length = rect.width / 2
        self.spring_strength = spring_strength
        self.plunger_body = plunger_body
        self.ball_chute = pygame.Rect(0, 0, ball_radius * 2, ball_radius * 2)
        self.ball_chute.center = chute_opening
        self._original_image = pygame.Surface(plunger_rect.size)
        self._original_image.fill((192, 255, 255))
        self.shapes = [plunger_shape, s0, s1, j0, j1]
        self.visible = 0
Ejemplo n.º 3
0
    def __init__(self,
                 arm_anchor_point,
                 arm_segment_lengths,
                 target_position,
                 do_render=False,
                 sparse=True,
                 sparse_distance=SPARSE_DISTANCE):
        super().__init__(do_render, sparse, MAX_MOTOR_FORCE)

        self.sparse_distance = sparse_distance
        self.target_position = target_position
        self.space.gravity = (0.0, 0.0)

        self.end_effector_body = None
        end_anchor, end_pos = self.add_arm(arm_segment_lengths,
                                           arm_anchor_point)

        self.end_effector_body = pymunk.Body(1, 1)
        self.end_effector_body.position = end_pos[0], end_pos[1]
        end_effector_shape = Circle(self.end_effector_body, 8)
        end_effector_shape.collision_type = NO_COLLISION_TYPE
        self.space.add(self.end_effector_body, end_effector_shape)

        pin = pymunk.PinJoint(end_anchor, self.end_effector_body,
                              (-arm_segment_lengths[-1] / 2, 0), (0, 0))

        self.space.add(pin)
Ejemplo n.º 4
0
def init_space():
    sp = Space()
    sp.gravity = (0, 50)

    chain = make_pivot_chain(sp, (0, 0), (240, 30), 30)
    sp.add(constraint.PivotJoint(chain[0], sp.static_body, chain[0].position))

    # Cria quadrado
    L = 25
    player = Body(mass=1, moment=100)
    shape = Poly(player, [(-L, -L), (L, -L), (L, L), (-L, L)])
    player.position = (90, 60)
    player.velocity = (-25, 25)
    shape.elasticity = 1.0
    shape.color = pyxel.COLOR_RED
    shape.collision_type = 42

    ball = Body(mass=1, moment=200)
    ball_shape = Circle(ball, 20)
    ball.position = (player.position.x, 130)
    ball_shape.elasticity = 1.0
    shape.color = pyxel.COLOR_NAVY
    ball_shape.collision_type = 42

    joint1 = constraint.DampedSpring(player, ball, (0, 0), (20, 0), 20, 3, 0.5)
    joint2 = constraint.PivotJoint(sp.static_body, player, (65, 35))
    joint1.collide_bodies = False
    sp.add(joint1, joint2)

    body2 = Body(1, 100)
    sp.add(body2)
    sp.add(Poly(body2, [(-3, 3), (3, 3), (3, -3), (-3, -3)]))
    body2.position = 220, 50
    sp.add(constraint.DampedRotarySpring(body2, ball, 0, 2, 1))
    sp.body2 = body2

    # Cria margens
    line = Body(body_type=Body.STATIC)
    e = 0
    lines = [
        Segment(line, (-e, -e), (240 + e, -e), 2),
        Segment(line, (-e, 180 + e), (240 + e, 180 + e), 2),
        Segment(line, (-e, -e), (-e, 180 + e), 2),
        Segment(line, (240 + e, -e), (240 + e, 180 + e), 2),
    ]
    for line in lines:
        line.elasticity = 1.0
    lines = []

    # Adiciona elementos ao espaço
    sp.add(player, shape, ball, ball_shape, *lines)
    sp.player = player

    #handler = sp.add_collision_handler(42, 42)
    #handler.begin = lambda *args: False
    return sp
Ejemplo n.º 5
0
    def __init__(self,
                 width=600,
                 height=600,
                 obstacle_num=5,
                 obstacle_radius=30,
                 feed_num=0,
                 feed_radius=5):
        import pyglet
        from pymunk import Space, Segment, Body, Circle, moment_for_circle, pyglet_util
        super(VehicleSimulator, self).__init__()
        self.__left_sensor_val = 0
        self.__right_sensor_val = 0
        self.__feed_sensor_val = False
        self.__feed_touch_counter = {}
        self.__feed_bodies = []
        self.__feed_radius = feed_radius

        self.__window = pyglet.window.Window(
            self.ARENA_SIZE + self.DISPLAY_MARGIN * 2,
            self.ARENA_SIZE + self.DISPLAY_MARGIN * 2,
            vsync=False)
        self.__draw_options = pyglet_util.DrawOptions()
        self.__closed = False

        @self.__window.event
        def on_draw():
            pyglet.gl.glClearColor(255, 255, 255, 255)
            self.__window.clear()
            self.__simulation_space.debug_draw(self.__draw_options)

        @self.__window.event
        def on_close():
            pyglet.app.EventLoop().exit()
            self.__closed = True

        self.__simulation_space = Space()
        self.__simulation_space.gravity = 0, 0

        # arena
        walls = [
            Segment(
                self.__simulation_space.static_body,
                (self.DISPLAY_MARGIN, self.DISPLAY_MARGIN),
                (self.ARENA_SIZE + self.DISPLAY_MARGIN, self.DISPLAY_MARGIN),
                0),
            Segment(
                self.__simulation_space.static_body,
                (self.ARENA_SIZE + self.DISPLAY_MARGIN, self.DISPLAY_MARGIN),
                (self.ARENA_SIZE + self.DISPLAY_MARGIN,
                 self.ARENA_SIZE + self.DISPLAY_MARGIN), 0),
            Segment(
                self.__simulation_space.static_body,
                (self.ARENA_SIZE + self.DISPLAY_MARGIN,
                 self.ARENA_SIZE + self.DISPLAY_MARGIN),
                (self.DISPLAY_MARGIN, self.ARENA_SIZE + self.DISPLAY_MARGIN),
                0),
            Segment(
                self.__simulation_space.static_body,
                (self.DISPLAY_MARGIN, self.ARENA_SIZE + self.DISPLAY_MARGIN),
                (self.DISPLAY_MARGIN, self.DISPLAY_MARGIN), 0)
        ]
        for w in walls:
            w.collision_type = self.COLLISION_TYPE.OBJECT
            w.friction = 0.2
        self.__simulation_space.add(walls)

        # vehicle
        mass = 1
        self.__vehicle_body = Body(
            mass, moment_for_circle(mass, 0, self.VEHICLE_RADIUS))
        self.__vehicle_shape = Circle(self.__vehicle_body, self.VEHICLE_RADIUS)
        self.__vehicle_shape.friction = 0.2
        self.__vehicle_shape.collision_type = self.COLLISION_TYPE.VEHICLE
        self.__simulation_space.add(self.__vehicle_body, self.__vehicle_shape)

        # left sensor
        sensor_l_s = Segment(self.__vehicle_body, (0, 0),
                             (self.SENSOR_RANGE * np.cos(self.SENSOR_ANGLE),
                              self.SENSOR_RANGE * np.sin(self.SENSOR_ANGLE)),
                             0)
        sensor_l_s.sensor = True
        sensor_l_s.collision_type = self.COLLISION_TYPE.LEFT_SENSOR
        handler_l = self.__simulation_space.add_collision_handler(
            self.COLLISION_TYPE.LEFT_SENSOR, self.COLLISION_TYPE.OBJECT)
        handler_l.pre_solve = self.__left_sensr_handler
        handler_l.separate = self.__left_sensr_separate_handler
        self.__simulation_space.add(sensor_l_s)

        # right sensor
        sensor_r_s = Segment(self.__vehicle_body, (0, 0),
                             (self.SENSOR_RANGE * np.cos(-self.SENSOR_ANGLE),
                              self.SENSOR_RANGE * np.sin(-self.SENSOR_ANGLE)),
                             0)
        sensor_r_s.sensor = True
        sensor_r_s.collision_type = self.COLLISION_TYPE.RIGHT_SENSOR
        handler_r = self.__simulation_space.add_collision_handler(
            self.COLLISION_TYPE.RIGHT_SENSOR, self.COLLISION_TYPE.OBJECT)
        handler_r.pre_solve = self.__right_sensr_handler
        handler_r.separate = self.__right_sensr_separate_handler
        self.__simulation_space.add(sensor_r_s)

        # obstacles
        for a in (np.linspace(0, np.pi * 2, obstacle_num, endpoint=False) +
                  np.pi / 2):
            body = Body(body_type=Body.STATIC)
            body.position = (self.DISPLAY_MARGIN + self.ARENA_SIZE / 2 +
                             self.ARENA_SIZE * 0.3 * np.cos(a),
                             self.DISPLAY_MARGIN + self.ARENA_SIZE / 2 +
                             self.ARENA_SIZE * 0.3 * np.sin(a))
            shape = Circle(body, obstacle_radius)
            shape.friction = 0.2
            shape.collision_type = self.COLLISION_TYPE.OBJECT
            self.__simulation_space.add(shape)

        for i in range(feed_num):
            body = Body(1, 1)
            self.__feed_bodies.append(body)
            shape = Circle(body, self.__feed_radius)
            shape.sensor = True
            shape.color = self.FEED_COLOR
            shape.collision_type = self.COLLISION_TYPE.FEED
            handler = self.__simulation_space.add_collision_handler(
                self.COLLISION_TYPE.VEHICLE, self.COLLISION_TYPE.FEED)
            handler.pre_solve = self.__feed_touch_handler
            handler.separate = self.__feed_separate_handler
            self.__simulation_space.add(body, shape)
            self.__feed_touch_counter[shape] = 0

        self.reset()