Exemple #1
0
def init_space():
    sp = Space()

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

    # Cria margens
    line = Body(body_type=Body.STATIC)
    lines = [
        Segment(line, (-30, -30), (270, -30), 2),
        Segment(line, (-30, 210), (270, 210), 2),
        Segment(line, (-30, -30), (-30, 210), 2),
        Segment(line, (270, -30), (270, 210), 2),
    ]
    for line in lines:
        line.elasticity = 1.0

    # Adiciona elementos ao espaço
    sp.add(player, shape, *lines)
    sp.player = player
    return sp
Exemple #2
0
def get_pymunk_space(gravity=(0, -9.807)):
    '''returns a `space` where the physics happens'''
    space = Space()
    # gravity is represented by a tuple
    # 0 acceleration in x-axis and -9.807 in y-axis
    space.gravity = gravity
    return space
Exemple #3
0
 def __init__(self):
     self.items = []
     self.words = []
     self.springs = []
     init_pymunk()
     self.space = Space()
     self.space.resize_static_hash()
     self.space.resize_active_hash()
Exemple #4
0
 def __init__(self):
     self.items = []
     init_pymunk()
     self.space = Space()
     self.space.gravity = (0, -0.5)
     self.space.resize_static_hash()
     self.space.resize_active_hash()
     self.leaves = []
     self.end_game = False
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
    def __init__(self, config):
        self.sprite_list = _arcade.SpriteList()
        self.obstacles = []
        self.static_obstacles = []
        self.falling_obstacles = []
        self.color_obstacles = []

        self.robots = []
        self.space = Space()
        self.space.damping = 0.1

        self.board_width = config['board_width']
        self.board_height = config['board_height']
        board_color = tuple(config['board_color'])

        board = Board(self.board_width / 2, self.board_height / 2, self.board_width, self.board_height, board_color)
        self.static_obstacles.append(board)

        for robot_conf in config['robots']:
            self.robots.append(RobotState(robot_conf))

        edge = Edge(self.board_width, self.board_height)
        self.static_obstacles.append(edge)
        self.falling_obstacles.append(edge)

        for obstacle in config['obstacles']:
            if obstacle['type'] == 'lake':
                lake = Lake.from_config(obstacle)
                self.static_obstacles.append(lake)
                if lake.hole is not None:
                    self.falling_obstacles.append(lake.hole)
                self.color_obstacles.append(lake)
            elif obstacle['type'] == 'rock':
                rock = Rock.from_config(obstacle)
                self.obstacles.append(rock)
            elif obstacle['type'] == 'border':
                border = Border.from_config(self.board_width, self.board_height, obstacle)
                self.static_obstacles.append(border)
                self.color_obstacles.append(border)
            elif obstacle['type'] == 'bottle':
                bottle = Bottle.from_config(obstacle)
                self.obstacles.append(bottle)
            elif obstacle['type'] == 'tile':
                tile = Tile.from_config(obstacle)
                self.static_obstacles.append(tile)
                self.color_obstacles.append(tile)
            else:
                print("unknown obstacle type")

        self.falling_obstacles.append(board)
        self.color_obstacles.append(board)

        self.selected_object = None
Exemple #7
0
    def __init__(self, game):
        super().__init__(game)
        self.player = None
        self.active = True

        self.geometry = list()
        self.space = Space()
        self.space.gravity = (0, 1000)
        self.sprites = LayeredUpdates()
        self.event_handler = event_handling.EventQueueHandler()
        self.background = resources.gfx("background.png", convert=True)
        self.load()
        pygame.mixer.music.load(resources.music_path("zirkus.ogg"))
        pygame.mixer.music.play(-1)
Exemple #8
0
def init_space():
    sp = Space()

    player = Body(mass=1, moment=1)
    shape = Circle(player, 10)
    player.position = (20, 90)
    player.velocity = (5, 0)
    shape.color = pyxel.COLOR_YELLOW

    line = Body(body_type=Body.STATIC)
    line_shape = Segment(line, (0, 1), (240, 1), 2)
    line_shape.color = pyxel.COLOR_RED

    sp.add(player, shape, line, line_shape)
    sp.player = player
    return sp
 def test_distance_no_obstacle(self):
     config = {
         'name': 'ultrasonic-sensor-front',
         'type': 'ultrasonic_sensor',
         'x_offset': 0,
         'y_offset': -91.5,
         'brick': 0,
         'port': 'ev3-ports:in3'
     }
     robot = MagicMock()
     us = UltrasonicSensor(config, robot)
     us.setup_pymunk_shape(1, None)
     us.sprite = MagicMock()
     us.sprite.angle = 0
     space = Space()
     val = us.distance(space)
     self.assertEqual(val, 2550)
Exemple #10
0
def init_space():
    sp = Space()
    sp.gravity = (0, 50)
    sp.damping = 1.0

    floor = Body(body_type=Body.STATIC)
    stick = Body(mass=100, moment=100 * 50**2)
    L = 20
    shapes = [
        Poly(stick, [(-L, -L), (L, -L), (L, L), (0, L + L / 2), (-L, L)],
             radius=3),
        Segment(floor, (1, 179), (239, 179), 1),
        Segment(floor, (1, 1), (239, 1), 1),
        Segment(floor, (1, 1), (1, 179), 1),
        Segment(floor, (239, 1), (239, 179), 1),
    ]
    stick.position = (120, L)

    bodies = []
    for _ in range(L):
        r = random.uniform(2, 6)
        mass = pi * r**2
        body = Body(mass=mass, moment=mass * r**2 / 2)
        circle = Circle(body, r)

        x = random.uniform(r, 240 - r)
        y = random.uniform(r, 180 - r)
        body.position = (x, y)

        vx = random.uniform(-L, L)
        vy = random.uniform(-L, L)
        body.velocity = (vx, vy)

        bodies.append(body)
        shapes.append(circle)
        circle.color = random.randint(1, 15)

    for shape in shapes:
        shape.elasticity = 1.0

    sp.add(floor, stick, *bodies, *shapes)
    return sp
    def __init__(self, do_render, sparse, max_motor_force):
        self.do_render = do_render
        self.sparse = sparse
        self.max_motor_force = max_motor_force

        if self.do_render:
            pygame.init()
            self.screen = pygame.display.set_mode((ENV_SIZE, ENV_SIZE))
            self.draw_options = pygame_util.DrawOptions(self.screen)
            self.clock = pygame.time.Clock()
        self.motors = []
        self.segment_bodies = []

        self.space = Space()
        self.space.iterations = 20

        no_collision = self.space.add_collision_handler(NO_COLLISION_TYPE, NO_COLLISION_TYPE)
        no_collision.begin = lambda a, b, c: False
        ghost_collision = self.space.add_wildcard_collision_handler(GHOST_TYPE)
        ghost_collision.begin = lambda a, b, c: False
Exemple #12
0
    def __init__(self):
        super().__init__()
        self.space = Space(threaded=True)
        self.space.collision_slop = 0.7
        self.iter = 0
        self.time = 0.0
        self.on_reset = []
        self.P, self.I, self.D = 4.25347222, 0.0001041666, -4.67881944
        self.I = 0.0

        y = 0
        L = 500
        self.ground = self.line(-L,
                                y,
                                L,
                                y,
                                static=True,
                                friction=0.75,
                                radius=5)
        handler = self.space.add_wildcard_collision_handler(1)
        handler.begin = self.on_collision
Exemple #13
0
def init_game():
    sp = Space()
    # sp.gravity = (0,90)
    # sp.damping = 0.8

    # Add Cat to Space
    generate_cat(sp)

    # Add pickle to Space
    generate_pickles(sp)

    # Add star to Space
    generate_stars(sp)

    # Add floor to Space
    floor = Body(body_type=Body.STATIC)
    floor_shape = Floor(floor, (0, SCREEN_H - 1), (SCREEN_W, SCREEN_H - 1), 1)
    floor_shape.elasticity = 0.7
    sp.add(floor, floor_shape)

    return sp
Exemple #14
0
def init_space():
    sp = Space()

    h = 20 * sqrt(2)
    player = Body(mass=1, moment=400)
    shape = Poly(player, [(-20, -h / 3), (20, -h / 3), (0, 2 / 3 * h)])
    player.position = (90, 90)
    shape.elasticity = 1.0
    shape.color = pyxel.COLOR_YELLOW

    line = Body(body_type=Body.STATIC)
    lines = [
        Segment(line, (0, 1), (240, 1), 2),
        Segment(line, (0, 179), (240, 179), 2),
        Segment(line, (1, 0), (1, 180), 2),
        Segment(line, (239, 0), (239, 180), 2),
    ]
    for line in lines:
        line.elasticity = 1.0
        line.color = pyxel.COLOR_PEACH

    sp.add(player, shape, *lines)
    sp.player = player
    return sp
    def setup(self):
        """
        Set up all the necessary shapes and sprites which are used in the simulation.
        These elements are added to lists to make buffered rendering possible to improve performance.
        """

        self.robot_elements = arcade.SpriteList()
        self.obstacle_elements = arcade.ShapeElementList()

        if self.large_sim_type:
            self.robot = RobotLarge(self.cfg, self.robot_pos[0],
                                    self.robot_pos[1], self.robot_pos[2])
        else:
            self.robot = RobotSmall(self.cfg, self.robot_pos[0],
                                    self.robot_pos[1], self.robot_pos[2])

        for s in self.robot.get_sprites():
            self.robot_elements.append(s)

        for s in self.robot.get_sensors():
            self.robot_state.load_sensor(s)

        self.blue_lake = BlueLake(self.cfg)
        self.green_lake = GreenLake(self.cfg)
        self.red_lake = RedLake(self.cfg)

        self.obstacle_elements.append(self.blue_lake.shape)
        self.obstacle_elements.append(self.green_lake.shape)
        self.obstacle_elements.append(self.red_lake.shape)

        self.border = Border(
            self.cfg,
            eval(self.cfg['obstacle_settings']['border_settings']
                 ['border_color']))
        self.edge = Edge(self.cfg)

        for s in self.border.shapes:
            self.obstacle_elements.append(s)

        self.space = Space()

        if self.large_sim_type:
            self.rock1 = Rock(apply_scaling(825), apply_scaling(1050),
                              apply_scaling(150), apply_scaling(60),
                              arcade.color.DARK_GRAY, 10)
            self.rock2 = Rock(apply_scaling(975), apply_scaling(375),
                              apply_scaling(300), apply_scaling(90),
                              arcade.color.DARK_GRAY, 130)

            self.ground = arcade.create_rectangle(apply_scaling(1460),
                                                  apply_scaling(950),
                                                  apply_scaling(300),
                                                  apply_scaling(10),
                                                  arcade.color.BLACK)

            self.obstacle_elements.append(self.rock1.shape)
            self.obstacle_elements.append(self.rock2.shape)
            self.obstacle_elements.append(self.ground)

            touch_obstacles = [self.rock1, self.rock2]
            falling_obstacles = [
                self.blue_lake.hole, self.green_lake.hole, self.red_lake.hole,
                self.edge
            ]

            self.space.add(self.rock1.poly)
            self.space.add(self.rock2.poly)

        else:
            self.bottle1 = Bottle(apply_scaling(1000), apply_scaling(300),
                                  apply_scaling(40),
                                  arcade.color.DARK_OLIVE_GREEN)
            self.obstacle_elements.append(self.bottle1.shape)

            touch_obstacles = [self.bottle1]
            falling_obstacles = [self.edge]

            self.space.add(self.bottle1.poly)

        color_obstacles = [
            self.blue_lake, self.green_lake, self.red_lake, self.border
        ]

        self.robot.set_color_obstacles(color_obstacles)
        self.robot.set_touch_obstacles(touch_obstacles)
        self.robot.set_falling_obstacles(falling_obstacles)
Exemple #16
0
 def __init__(self, space=None, camera=None, steps=1):
     self.space = space or Space()
     self.camera = camera or Camera()
     self.camera_ctrl = None
Exemple #17
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()