예제 #1
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
예제 #2
0
파일: core.py 프로젝트: hugonxc/Gattatico
def generate_pickles(space):
    for i in range(0, 10):
        pickle = Body(mass=1, moment=1)
        pickle_shape = Pickle(pickle, 6)
        pickle_shape.elasticity = 1

        x = random.randrange(260, 800)
        y = random.randrange(0, 180)
        pickle.position = (x, y)
        pickle.velocity_func = zero_gravity

        v = random.randrange(-200, -70)
        pickle.velocity = (v, 0)

        # Set collison type for pickle
        pickle_shape.collision_type = 2

        # Setup the collision callback function Cat and Pickle
        h = space.add_collision_handler(1, 2)
        h.begin = dead

        # Setup the collision callback function Pickle and Pickle
        p = space.add_collision_handler(2, 2)
        p.begin = same_elem_collision

        # Setup the collision callback function Pickle and Pickle under boost
        pb = space.add_collision_handler(2, 6)
        pb.begin = same_elem_collision

        space.add(pickle, pickle_shape)
예제 #3
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
예제 #4
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
예제 #5
0
    def __init__(self, center, road, side):

        # setup shape
        mass = 90
        radius = 2.5
        inertia = moment_for_circle(mass, 0, radius * 2, (0, 0))
        body = Body(mass, inertia)
        body.position = center
        self.shape = Circle(body, radius * 2, (0, 0))
        self.shape.color = (0, 255, 255)
        self.shape.collision_type = CollisionType.Pedestrian
        self.shape.elasticity = 0.05

        # Walk parameter
        self.lenRange = road.length
        self.widthRange = (road.nLanes + 1) * road.width * 2
        self.side = side

        # Bool flags
        self.dead = False

        # Move parameters
        self.moving = 0
        self.direction = road.direction
        self.normal = road.normal
        self.speed = random.randint(3, 6)

        # Flags for crossing
        self.crossing = False
        self.beginCrossing = False
예제 #6
0
    def __init__(self, world, position, width=1.6, length=4.0, tire_width=.25, tire_length=.8, skidmarks=None, body_density=1.0):
        mass = width * length * body_density
        inertia = moment_for_box(mass, width, length)
        self.world = world
        body = Body(mass, inertia, )
        body.position = position
        shape = Poly.create_box(body, size=(width, length))
        super(Car, self).__init__(world, body, shape)

        slot_density = .01
        slot_radius = .1
        slot_mass = slot_density * (slot_radius ** 2) * pi
        slot_inertia = moment_for_circle(slot_mass, 0.0, slot_radius)
        #self.slot = Body(slot_mass, slot_inertia)


        flpos = position[0] - width / 2.0 - tire_width * 2, position[1] + length / 2.0
        self.front_left = Tire(self, flpos, tire_width, tire_length, skidmarks=skidmarks, powered=False, density=body_density)


        frpos = position[0] + width / 2.0 + tire_width * 2, position[1] + length / 2.0
        self.front_right = Tire(self, frpos, tire_width, tire_length, skidmarks=skidmarks, powered=False, density=body_density)
        
        rlpos = position[0] - width / 2.0 - tire_width * 2, position[1] - length / 2.0
        self.rear_left = Tire(self, rlpos, tire_width * 1.5, tire_length, steerable=False, skidmarks=skidmarks, density=body_density)

        rrpos = position[0] + width / 2.0 + tire_width * 2, position[1] - length / 2.0
        self.rear_right = Tire(self, rrpos, tire_width * 1.5, tire_length, steerable=False, skidmarks=skidmarks, density=body_density)
        self.tires = [self.front_left, self.front_right, self.rear_left, self.rear_right]
예제 #7
0
파일: core.py 프로젝트: hugonxc/Gattatico
def generate_stars(space):
    for i in range(0, 5):
        star = Body(mass=0.00000001, moment=1)
        star_shape = Star(star, 5)

        x = random.randrange(400, 1600)
        y = random.randrange(0, 180)
        star.position = (x, y)
        star.velocity_func = zero_gravity

        v = random.randrange(-90, -50)
        star.velocity = (v, 0)
        star_shape.collision_type = 3

        # Setup the collision callback function Cat and Star
        g = space.add_collision_handler(1, 3)
        g.begin = boost

        # Setup the collision callback function Star and Star
        s = space.add_collision_handler(3, 3)
        s.begin = same_elem_collision

        # Setup the collision callback function Star and Pickle
        s = space.add_collision_handler(2, 3)
        s.begin = same_elem_collision

        # Setup the collision callback function Star and Pickle under boost
        pb = space.add_collision_handler(3, 6)
        pb.begin = same_elem_collision

        space.add(star, star_shape)
예제 #8
0
 def add_rect(self, x, y, w, h, dynamic=True):
     body = Body(body_type=(Body.STATIC, Body.DYNAMIC)[int(dynamic)])
     body.position = x, y
     poly = Poly.create_box(body, size=(w, h))
     poly.density = Environment.DEFAULT_DENSITY
     self.space.add(body, poly)
     self.bodies.append(body)
     return body
예제 #9
0
 def add_polygon(self, x, y, *vertices, dynamic=True):
     body = Body(body_type=(Body.STATIC, Body.DYNAMIC)[int(dynamic)])
     body.position = x, y
     poly = Poly(body, vertices)
     poly.density = Environment.DEFAULT_DENSITY
     self.space.add(body, poly)
     self.bodies.append(body)
     return body
예제 #10
0
 def add_circle(self, x, y, r, dynamic=True):
     body = Body(body_type=(Body.STATIC, Body.DYNAMIC)[int(dynamic)])
     body.position = x, y
     circle = Circle(body, r)
     circle.density = Environment.DEFAULT_DENSITY
     self.space.add(body, circle)
     self.bodies.append(body)
     return body
예제 #11
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
예제 #12
0
 def __init__(self, x, y, side, dir):
     body = Body(0, 0, Body.STATIC)
     body.position = x, y
     self.side = side
     self.dir = dir
     self.shape = Circle(body, self.radius * 2, (0, 0))
     self.shape.color = (0, 0, 255, 0)
     self.shape.elasticity = 0.95
     self.shape.collision_type = CollisionType.Goalpost
예제 #13
0
    def _create_poly(self):
        """
        Create the polygon used for ray-casting. (Ultrasonic sensor)
        :return: a Pymunk Poly object.
        """

        body = Body(body_type=Body.STATIC)
        body.position = Vec2d(self.center_x, self.center_y)

        return Poly.create_box(body, (self.width, self.height))
예제 #14
0
    def _create_poly(self) -> Circle:
        """
        Create the polygon used for ray-casting. (Ultrasonic sensor)
        :return: a Pymunk Circle object.
        """

        body = Body(body_type=Body.STATIC)
        body.position = Vec2d(self.center_x, self.center_y)

        return Circle(body, self.radius)
예제 #15
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
예제 #16
0
파일: core.py 프로젝트: hugonxc/Gattatico
def generate_cat(space):
    cat = Body(mass=1, moment=1)
    cat_shape = Cat(cat, 15)
    cat_shape.elasticity = 1
    cat.position = (50, 120)
    cat_shape.collision_type = 1

    # Setup the collision callback function Cat and Pickle under boost
    g = space.add_collision_handler(1, 6)
    g.begin = same_elem_collision

    space.add(cat, cat_shape)
예제 #17
0
파일: sprite.py 프로젝트: zai1208/stuntcat
 def __init__(self, rect):
     super(Ball, self).__init__()
     radius = rect.width / 2
     body = Body()
     body.position = rect.center
     self.shape = Circle(body, radius)
     self.shape.mass = ball_mass
     self.shape.elasticity = .25
     self.shape.friction = 1
     self.rect = Rect(0, 0, rect.width, rect.width)
     self.original_image = resources.gfx("yarnball.png", convert_alpha=True)
     self.pymunk_shapes = (body, self.shape)
예제 #18
0
    def __init__(self, center, width, height):

        # Create body
        body = Body(0, 0, Body.STATIC)
        body.position = center
        points = [Vec2d(width,height),Vec2d(-width,height),-Vec2d(width,height),Vec2d(width,-height)]
        self.width = width
        self.height = height
        self.points = [p+center for p in points]
        self.shape = Poly(body,points)
        self.shape.color = (200, 200, 200)
        self.shape.elasticity = 0.05
        self.shape.collision_type = CollisionType.Obstacle
예제 #19
0
 def __init__(self, space, rect):
     super(Ball, self).__init__()
     radius = rect.width / 2
     body = Body(ball_mass, moment_for_circle(ball_mass, 0, radius))
     body.position = rect.center
     self.shape = Circle(body, radius)
     self.shape.elasticity = .5
     self.shape.friction = 0
     self.shape.layers = 1
     self.shape.collision_type = ball_type
     self.rect = pygame.Rect(0, 0, rect.width, rect.width)
     image = smoothscale(prepare.GFX.get('ball-bearing'), self.rect.size)
     self._original_image = image.convert_alpha()
예제 #20
0
 def __init__(self, space, rect):
     super(Ball, self).__init__()
     radius = rect.width / 2
     body = Body(ball_mass, moment_for_circle(ball_mass, 0, radius))
     body.position = rect.center
     self.shape = Circle(body, radius)
     self.shape.elasticity = .5
     self.shape.friction = 0
     self.shape.layers = 1
     self.shape.collision_type = ball_type
     self.rect = pygame.Rect(0, 0, rect.width, rect.width)
     image = smoothscale(prepare.GFX.get('ball-bearing'), self.rect.size)
     self._original_image = image.convert_alpha()
예제 #21
0
 def __init__(self, space, rect):
     super(Handle, self).__init__()
     color = (192, 192, 220)
     radius = rect.width / 2
     body = Body()
     body.position = rect.center
     shape = Circle(body, radius)
     rect2 = pygame.Rect(0, 0, rect.width, rect.width)
     image = pygame.Surface(rect2.size, pygame.SRCALPHA)
     pygame.draw.circle(image, color, rect2.center, int(radius//4))
     pygame.draw.line(image, (0, 0, 255), rect2.center, rect2.midtop, 8)
     self.shapes = [shape]
     self.rect = rect
     self._original_image = image
예제 #22
0
 def __init__(self, world, x=0, y=0):
     self.movable = Movable()
     self.spriteobject = SpriteObject(
         world.get_texture("player"), x, y, batch="player"
     )
     self.directionalsprite = DirectionalSprite(world, "player")
     phys_body = Body(5, inf)
     phys_body.position = x, y
     shape = Circle(phys_body, 8, (8, 8))
     self.physicsbody = PhysicsBody(shape)
     world.phys_space.add(self.physicsbody.body, self.physicsbody.shape)
     self.groundingobject = GroundingObject()
     self.jumpobject = JumpObject()
     self.inputobject = InputObject("kb")
예제 #23
0
 def __init__(self, space, rect):
     super(Handle, self).__init__()
     color = (192, 192, 220)
     radius = rect.width / 2
     body = Body()
     body.position = rect.center
     shape = Circle(body, radius)
     rect2 = pygame.Rect(0, 0, rect.width, rect.width)
     image = pygame.Surface(rect2.size, pygame.SRCALPHA)
     pygame.draw.circle(image, color, rect2.center, int(radius // 4))
     pygame.draw.line(image, (0, 0, 255), rect2.center, rect2.midtop, 8)
     self.shapes = [shape]
     self.rect = rect
     self._original_image = image
예제 #24
0
def create_circle(r=1, x=0, y=0, m=1, bt=Body.DYNAMIC):
    '''
    given radius (r), x-position (x), y-position (y), mass (m), body_type (bt)
    return the (body, shape) tuple for a circle
    '''
    given_bt = bt
    bt = Body.DYNAMIC if given_bt == 'dynamic' else Body.DYNAMIC
    bt = Body.STATIC if given_bt == 'static' else Body.DYNAMIC
    bt = Body.KINEMATIC if given_bt == 'kinematic' else Body.DYNAMIC
    moment = moment_for_circle(mass=m, inner_radius=0, outer_radius=r)
    body = Body(mass=m, moment=moment, body_type=bt)
    shape = Circle(body=body, radius=r)
    body.position = (x, y)
    return body, shape
예제 #25
0
 def __init__(self, world, x=0, y=0):
     self.movable = Movable()
     self.spriteobject = SpriteObject(world.get_texture("player"),
                                      x,
                                      y,
                                      batch="player")
     self.directionalsprite = DirectionalSprite(world, "player")
     phys_body = Body(5, inf)
     phys_body.position = x, y
     shape = Circle(phys_body, 8, (8, 8))
     self.physicsbody = PhysicsBody(shape)
     world.phys_space.add(self.physicsbody.body, self.physicsbody.shape)
     self.groundingobject = GroundingObject()
     self.jumpobject = JumpObject()
     self.inputobject = InputObject("kb")
예제 #26
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
예제 #27
0
 def __init__(self, space, rect, playfield=None):
     super(Spinner, self).__init__()
     r, cy = rect.width / 2, rect.height / 2
     assert (r == cy)
     body = Body(.1, moment_for_circle(.1, 0, r))
     body.position = rect.center
     top = Circle(body, r)
     top.layers = 2
     rect2 = pygame.Rect((-r, -cy), rect.size)
     cross0 = Segment(body, rect2.midleft, rect2.midright, 1)
     cross1 = Segment(body, rect2.midtop, rect2.midbottom, 1)
     j0 = PivotJoint(playfield, body, body.position)
     j1 = SimpleMotor(playfield, body, 0)
     j1.max_force = 200
     self.shapes = [top, cross0, cross1, j0, j1]
     self.rect = pygame.Rect(rect)
     self._original_image = prepare.GFX['pachinko-spinner']
예제 #28
0
 def __init__(self, space, rect, playfield=None):
     super(Spinner, self).__init__()
     r, cy = rect.width / 2, rect.height / 2
     assert (r == cy)
     body = Body(.1, moment_for_circle(.1, 0, r))
     body.position = rect.center
     top = Circle(body, r)
     top.layers = 2
     rect2 = pygame.Rect((-r, -cy), rect.size)
     cross0 = Segment(body, rect2.midleft, rect2.midright, 1)
     cross1 = Segment(body, rect2.midtop, rect2.midbottom, 1)
     j0 = PivotJoint(playfield, body, body.position)
     j1 = SimpleMotor(playfield, body, 0)
     j1.max_force = 200
     self.shapes = [top, cross0, cross1, j0, j1]
     self.rect = pygame.Rect(rect)
     self._original_image = prepare.GFX['pachinko-spinner']
예제 #29
0
def create_rect(w=1, h=1, scalar=1, m=1, x=0, y=0, bt=Body.DYNAMIC):
    '''
    given the width (w), height (h), mass (m), x-position (x), y-position (y),
        scalar <to augment default square>, and the body_type (bt).
    returns a `rigid body` which is a shapeless object that
    has physical properties (mass, position, rotation, velocity, etc)
    ALSO returns a Poly which is the Shape that really gets drawn
    '''
    poly_size = (w * scalar, h * scalar)
    poly = Poly.create_box(body=None, size=poly_size)
    # moment depends on mass and size.
    # bigger poly >> bigger moment.
    # more massive >> bigger moment
    moment_of_inertia = moment_for_poly(m, poly.get_vertices())
    body = Body(mass=m, moment=moment_of_inertia, body_type=bt)
    body.position = (x, y)
    poly.body = body
    return body, poly
예제 #30
0
def create_segment(
        p1=(0, 0), p2=(0, 1), thicc=1, x=0, y=0, m=1, scalar=1,
        bt=Body.DYNAMIC):
    '''
    given point_1 (p1), point_2 (p2), thickness (thicc),
          x-position (x), y-position (y), mass (m),
          scalar <to augment the length>, body_type (bt)
    return (body, shape) tuple for a line segment
    '''
    given_bt = bt
    bt = Body.DYNAMIC if given_bt == 'dynamic' else Body.DYNAMIC
    bt = Body.STATIC if given_bt == 'static' else Body.DYNAMIC
    bt = Body.KINEMATIC if given_bt == 'kinematic' else Body.DYNAMIC
    p2 = (p2[0] * scalar, p2[1] * scalar)
    moment = moment_for_segment(mass=m, a=p1, b=p2, radius=thicc)
    body = Body(mass=m, moment=moment, body_type=bt)
    shape = Segment(body=body, a=p1, b=p2, radius=thicc)
    body.position = (x, y)
    return body, shape
예제 #31
0
    def __init__(self,x,y,radius):

        # setup shape
        mass = 10
        inertia = moment_for_circle(mass, 0, radius*2, (0, 0))
        body = Body(mass, inertia)
        body.position = x, y
        body.velocity_func = friction_ball
        self.shape = Circle(body, radius*2, (0, 0))
        self.shape.color = (255, 0, 0, 0)
        self.shape.elasticity = 0.98
        self.shape.friction = 3.0
        self.shape.collision_type = CollisionType.Ball

        # Initial and previous positions
        self.initPos = x,y
        self.prevPos = Vec2d(x,y)

        # List of robots who last touched the ball
        self.lastKicked = []
예제 #32
0
    def __init__(self,center,angle,type,team,goal):

        # Params based on vehicle type
        self.width = self.widths[type]
        self.height = self.lengths[type]
        mass = self.masses[type]
        self.points = [Vec2d(self.height,self. width), Vec2d(-self.height, self.width),
                       -Vec2d(self.height, self.width), Vec2d(self.height, -self.width)]
        inertia = moment_for_poly(mass,self.points)

        # Basic params
        self.type = type
        self.team = team
        self.goal = goal

        # Flags
        self.finished = False
        self.crashed = False

        # Direction
        self.direction = Vec2d(1,0)
        self.direction.rotate(angle)

        # Position
        self.position = LanePosition.OffRoad

        # For reward
        self.prevPos = center

        # Create body
        body = Body(mass, inertia, Body.DYNAMIC)
        body.position = center
        body.angle = angle
        body.velocity_func = friction_car
        self.shape = Poly(body, self.points)
        self.shape.color = (0, 255, 0)
        self.shape.elasticity = 0.05
        self.shape.collision_type = CollisionType.Car
예제 #33
0
    def __init__(self, car, position, width=10.0, length=20.0, steerable=True, skidmarks=None, powered=True, density=1.0):
        world = car.world
        self.steerable = steerable
        self.powered = powered
        self.skidding = False

        mass = width * length * density
        inertia = moment_for_box(mass, width, length)
        self.world = world
        body = Body(mass, inertia, )
        body.position = position
        shape= Poly.create_box(body, size=(width, length))
        super(Tire, self).__init__(world, body, shape)
        
        ## self.bodyDef = box2d.b2BodyDef()
        ## self.bodyDef.position = position

        
        ## body = world.CreateBody(self.bodyDef)
        ## super(Tire, self).__init__(world, body)

        ## self.shapeDef = box2d.b2PolygonDef()
        ## self.shapeDef.density = 1
        ## self.shapeDef.SetAsBox(width, length)
        ## self.shap = self.body.CreateShape(self.shapeDef)
        ## self.body.SetMassFromShapes()

        # our joint
        joint = PivotJoint(self.body, car.body, self.position)
        joint.error_bias = pow(1.0 - 0.1, 60.0) * 10
        self.world.add(joint)


        #joint = PinJoint(self.body, car.body)
        #self.world.add(joint)

        self.rot_joint = RotaryLimitJoint(self.body, car.body, 0, 0)
        self.world.add(self.rot_joint)
예제 #34
0
def create_triangle(p1=(0, 0),
                    p2=(1, 0),
                    p3=(.5, .866),
                    x=0,
                    y=0,
                    m=1,
                    scalar=1,
                    bt=Body.DYNAMIC):
    '''
    given points (p1..p3), mass (m), x-position (x), y-position (y),
          scalar <to augment default equilateral triangle>, body_type (bt),
    The default values for p1,p2,p3 make an approx. equilateral triangle.
    return (body, shape) tuple for a triangle
    '''
    vertices = (p1, p2, p3)  # equilateral
    vertices = tuple((v[0] * scalar, v[1] * scalar) for v in vertices)
    shape = Poly(body=None, vertices=vertices)  # will set body later
    vertices = shape.get_vertices()  # because Vec2d of vertices is needed
    moment = moment_for_poly(mass=m, vertices=vertices)
    body = Body(mass=m, moment=moment)
    body.position = (x, y)
    shape.body = body  # set body here because init None above
    return body, shape
예제 #35
0
def create_pentagon(p1=(0, 0),
                    p2=(2, 0),
                    p3=(3, 2),
                    p4=(1, 4),
                    p5=(-1, 2),
                    x=0,
                    y=0,
                    m=1,
                    scalar=1,
                    bt=Body.DYNAMIC):
    '''
    given points (p1..p5), mass (m), x-position (x), y-position (y),
          scalar <to augment default points>, body_type (bt),
    return (body, shape) tuple for a pentagon
    '''
    vertices = (p1, p2, p3, p4, p5)
    vertices = tuple((v[0] * scalar, v[1] * scalar) for v in vertices)
    shape = Poly(body=None, vertices=vertices)  # will set body later
    vertices = shape.get_vertices()  # because Vec2d of vertices is needed
    moment = moment_for_poly(mass=m, vertices=vertices)
    body = Body(mass=m, moment=moment)
    body.position = (x, y)
    shape.body = body  # set body here because init None above
    return body, shape
예제 #36
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
예제 #37
0
def make_pivot_chain(space, a, b, n, mass=1):
    a, b = map(Vec2d, (a, b))
    delta = (b - a) / n
    L = delta.length
    pos = a

    objs = []
    prev_body = None
    for _ in range(n):
        final = pos + delta
        body = Body(mass=mass / n, moment=(mass / n) * L**2 / 2)
        shape = Segment(body, -delta / 2, delta / 2, 2)
        body.position = pos + delta / 2
        objs.append(body)

        if prev_body is not None:
            joint = constraint.PivotJoint(body, prev_body, pos)
            joint.collide_bodies = False
            space.add(joint)
        space.add(body, shape)
        pos = final
        prev_body = body

    return objs
예제 #38
0
	def load(self):
		self.world_size = Size(3000, 3000)

		self.camera = Camera(self.size, self.world_size, 1000, 10)

		self._tool = None
		self.tool = None

		self.batch = graphics.Batch()
		self.background = CameraGroup(graphics.OrderedGroup(0), self.camera)
		self.foreground = CameraGroup(graphics.OrderedGroup(1), self.camera)
		self.playerg = CameraGroup(graphics.OrderedGroup(2), self.camera)
		self.world_ui = CameraGroup(graphics.OrderedGroup(3), self.camera)
		self.ui = graphics.OrderedGroup(2)

		self.space = Space()
		self.space.gravity = (0.0, 0.0)
		buffer = 100
		borders = Body()
		borders.position = (0, 0)
		left = Segment(borders, (-buffer, -buffer), (-buffer, self.world_size.height+buffer), buffer)
		bottom = Segment(borders, (-buffer, -buffer), (self.world_size.width+buffer, -buffer), buffer)
		right = Segment(borders, (self.world_size.width+buffer, self.world_size.height+buffer),
			(self.world_size.width+buffer, -buffer), buffer)
		top = Segment(borders, (self.world_size.width+buffer, self.world_size.height+buffer),
			(-buffer, self.world_size.height+buffer), buffer)
		self.space.add_static(left, bottom, right, top)

		self.stars = Stars(self.world_size, self.batch, self.background)

		self.asteroids = Asteroid.populate(50, 100, self.world_size, self.batch, self.foreground, self.space)

		if not self.asteroids:
			print("None of a particular resource on this asteroid belt, that'd be unfair. Trying again.")
			self.end(Main())
			return

		self.home_world = choice([asteroid for asteroid in self.asteroids if asteroid.position.y > self.world_size.height/4*3])
		self.home_world.type = "home"
		self.home_world.populated = True

		x, y = self.home_world.position
		self.camera.move(Vector(x-self.size.width/2, y-self.size.height/2))

		# Let's make stuff a bit more interesting.
		for asteroid in self.asteroids:
			if not asteroid.type == "home":
				asteroid.body.apply_impulse((triangular(-20000, 20000, 0), triangular(-20000, 20000, 0)))

		x, y = self.home_world.position
		self.player = Person(x+150, y+150, self.batch, self.playerg, self.space)
		self.mouse = x+150, y+150

		centre = Vector(self.size.width/2, self.size.height/2)
		image = centre_image(resource.image("logo.png"))
		self.logo = sprite.Sprite(image, centre.x, centre.y, batch=self.batch, group=self.ui)
		self.logo.opacity = 255
		self.fade = True
		self.faded = False

		planet = centre_image(resource.image("planet.png"))
		x = self.world_size.width/2
		y = planet.height/2
		self.planet_sprite = sprite.Sprite(planet, x, y, batch=self.batch, group=self.world_ui)
		self.win_box = BB(x-200, y-200, x+200, y+200)

		#self.tools = sorted([tool(self.space) for tool in Tool.__subclasses__()], key=attrgetter("order"), reverse=True)
		#self.buttons = {tool: Button(30, 30+number*50, tool.image, tool.description, self.use_tool(tool), self.ui, self.batch) for number, tool in enumerate(self.tools)}

		self.constraints = set()