Ejemplo n.º 1
0
def setup_world():
    world_bounds = b2AABB()
    world_bounds.lowerBound = (-200, -1000)
    world_bounds.upperBound = (200, 200)
    world = b2World(
        world_bounds,
        b2Vec2(0, -30),  # Gravity vector
        True  # Use "sleep" optimisation
    )

    wallsdef = b2BodyDef()
    walls = world.CreateBody(wallsdef)
    walls.userData = 'Blocks'

    WALLS = [
        (W, FLOOR * 0.5, (W / 2, FLOOR * 0.5), 0),  # floor
        (W / 2, 1, (W / 2, H + 1), 0),  # ceiling
        (1, 600, (-1, -500), 0),  # left wall
        (1, 600, (W + 1, -500), 0),  # right wall
    ]

    for wall in WALLS:
        shape = b2PolygonDef()
        shape.SetAsBox(*wall)
        walls.CreateShape(shape)

    return world
Ejemplo n.º 2
0
def on_mouse_press(x, y, button, modifiers):
    global mouse_joint
    if mouse_joint:
        return

    p = screen_to_world((x, y))

    # Create a mouse joint on the selected body (assuming it's dynamic)

    # Make a small box.
    aabb = b2AABB()
    aabb.lowerBound = p - (0.001, 0.001)
    aabb.upperBound = p + (0.001, 0.001)

    # Query the world for overlapping shapes.
    body = None
    k_maxCount = 10  # maximum amount of shapes to return

    (count, shapes) = world.Query(aabb, k_maxCount)
    for shape in shapes:
        shapeBody = shape.GetBody()
        if not shapeBody.IsStatic() and shapeBody.GetMass() > 0.0:
            if shape.TestPoint(shapeBody.GetXForm(), p):  # is it inside?
                body = shapeBody
                break

    if body:
        # A body was selected, create the mouse joint
        md = b2MouseJointDef()
        md.body1 = world.GetGroundBody()
        md.body2 = body
        md.target = p
        md.maxForce = 100000.0
        mouse_joint = world.CreateJoint(md).getAsType()
        body.WakeUp()
Ejemplo n.º 3
0
    def MouseDown(self, p):
        """
        Indicates that there was a left click at point p (world coordinates)
        """
        if self.mouseJoint is not None:
            return

        # Create a mouse joint on the selected body (assuming it's dynamic)
        # Make a small box.
        aabb = b2AABB(lowerBound=p - (0.001, 0.001),
                      upperBound=p + (0.001, 0.001))

        # Query the world for overlapping shapes.
        query = fwQueryCallback(p)
        self.world.QueryAABB(query, aabb)

        if query.fixture:
            body = query.fixture.body
            # A body was selected, create the mouse joint
            self.mouseJoint = self.world.CreateMouseJoint(
                bodyA=self.groundbody,
                bodyB=body,
                target=p,
                maxForce=1000.0 * body.mass)
            body.awake = True
Ejemplo n.º 4
0
    def MouseDown(self, p):
        """
        Indicates that there was a left click at point p (world coordinates)
        """
        if self.mouseJoint is not None:
            return

        # Create a mouse joint on the selected body (assuming it's dynamic)
        # Make a small box.
        aabb = b2AABB(lowerBound=p - (0.001, 0.001),
                      upperBound=p + (0.001, 0.001))

        # Query the world for overlapping shapes.
        query = fwQueryCallback(p)
        self.world.QueryAABB(query, aabb)

        if query.fixture:
            body = query.fixture.body
            # A body was selected, create the mouse joint
            self.mouseJoint = self.world.CreateMouseJoint(
                bodyA=self.groundbody,
                bodyB=body,
                target=p,
                maxForce=1000.0 * body.mass)
            body.awake = True
Ejemplo n.º 5
0
 def __init__(self, width, height):
     self.render_list = []
     aabb = b2AABB()
     aabb.lowerBound = (-width, -height)
     aabb.upperBound = (width, height)
     gravity = (0, -10)
     do_sleep = True
     self.world = b2World(aabb, gravity, do_sleep)
Ejemplo n.º 6
0
 def __init__(self, root_layer, camera_config, gravity=(0, 0, 0)):
     self._camera = Camera(
         target=camera_config['target'],
         width=camera_config['width'],
         rect=camera_config['rect'],
         limits=camera_config['limits'],
     )
     self._root_layer = root_layer
     self._chunks = []
     worldAABB = b2AABB()
     worldAABB.lowerBound = (-1000, -1000)
     worldAABB.upperBound = (1000, 1000)
     gravity_x, gravity_y = gravity
     gravity = b2Vec2(gravity_x, gravity_y)
     self._box2d_world = b2World(worldAABB, gravity, True)
     self._contact_listener = ContactListener()
     self._box2d_world.SetContactListener(self._contact_listener)
     self._remove_list = []
Ejemplo n.º 7
0
    def rotateRight(self, p):
        """
        Indicates that there was a left click at point p (world coordinates)
        """
        # if self.mouseJoint is not None:
        #     return

        # Create a mouse joint on the selected body (assuming it's dynamic)
        # Make a small box.
        aabb = b2AABB(lowerBound=p - (0.001, 0.001),
                      upperBound=p + (0.001, 0.001))

        # Query the world for overlapping shapes.
        query = fwQueryCallback(p)
        self.world.QueryAABB(query, aabb)

        if query.fixture:
            body = query.fixture.body
            body.ApplyAngularImpulse(-15**3, True)
Ejemplo n.º 8
0
    def ShowProperties(self, p):
        aabb = b2AABB(lowerBound=p - (0.001, 0.001),
                      upperBound=p + (0.001, 0.001))

        # Query the world for overlapping shapes.
        query = fwQueryCallback(p)
        self.world.QueryAABB(query, aabb)

        if query.fixture:
            self.window.reset_properties_list()

            fixture = query.fixture
            body = fixture.body
            self._ShowProperties(body)

            shape = fixture.shape
            self._ShowProperties(shape)

            self.selected_shapebody = (shape, body)
Ejemplo n.º 9
0
    def ShowProperties(self, p):
        aabb = b2AABB(lowerBound=p - (0.001, 0.001),
                      upperBound=p + (0.001, 0.001))

        # Query the world for overlapping shapes.
        query = fwQueryCallback(p)
        self.world.QueryAABB(query, aabb)

        if query.fixture:
            self.window.reset_properties_list()

            fixture = query.fixture
            body = fixture.body
            self._ShowProperties(body)

            shape = fixture.shape
            self._ShowProperties(shape)

            self.selected_shapebody = (shape, body)
Ejemplo n.º 10
0
def setup_world():
    global buoyancy
    world_bounds = b2AABB()
    world_bounds.lowerBound = (-200, -100)
    world_bounds.upperBound = (1000, 1000)
    world = b2World(
        world_bounds,
        b2Vec2(0, -30),  # Gravity vector
        True  # Use "sleep" optimisation
    )

    wallsdef = b2BodyDef()
    walls = world.CreateBody(wallsdef)
    walls.userData = 'Blocks'

    WALLS = [
        #(W, FLOOR * 0.5, (W / 2, FLOOR * 0.5), 0),  # floor
        #(W / 2, 1, (W / 2, H + 1), 0),  # ceiling
        (1, 600, (-1, -500), 0),  # left wall
        #(1, 600, (W + 1, -500), 0),  # right wall
    ]

    for wall in WALLS:
        shape = b2PolygonDef()
        shape.SetAsBox(*wall)
        walls.CreateShape(shape)

    for shape in read_shapes_from_svg('shapes/ground.svg'):
        walls.CreateShape(shape)

    buoyancydef = b2BuoyancyControllerDef()
    buoyancydef.normal = b2Vec2(0, 1)
    buoyancydef.offset = WATER_LEVEL
    buoyancydef.density = 2.5
    buoyancydef.angularDrag = 0.5
    buoyancydef.linearDrag = 3
    buoyancy = world.CreateController(buoyancydef)

    return world
Ejemplo n.º 11
0
    def onMouseDown(self, event):
        p = self.convertScreenToWorld(event.pos)

        if self.mouseJoint is not None:
            return

        # Create a mouse joint on the selected body (assuming it's dynamic)
        # Make a small box.
        aabb = b2AABB(lowerBound=b2Vec2(p) - (0.001, 0.001),
                      upperBound=b2Vec2(p) + (0.001, 0.001))

        # Query the world for overlapping shapes.
        query = fwQueryCallback(p)
        self.m_b2dWorld.QueryAABB(query, aabb)

        if query.fixture:
            body = query.fixture.body
            # A body was selected, create the mouse joint
            self.mouseJoint = self.m_b2dWorld.CreateMouseJoint(
                bodyA=self.m_groundBody,
                bodyB=body,
                target=p,
                maxForce=1000.0 * body.mass)
            body.awake = True
Ejemplo n.º 12
0
 elif event.type == pygame.MOUSEBUTTONUP:
     
     #########################
     #                       #
     #   HANDLE MOUSE INPUT  #
     #                       #
     #########################
     
     if event.button == 1 and not(quakeActive):              # CREATE BODY ON LEFT CLICK
         pos = pixelsToWorld(pygame.mouse.get_pos())
         createBlock(pos)
         ###
     elif event.button == 2 and not(quakeActive):            # DELETE BODY ON MIDDLE MOUSE CLICK
         pos = screenToWorld(pygame.mouse.get_pos())
         cb = QueryClickCallback(pos)
         aabb = b2AABB(lowerBound=pos - (0.001 , 0.001),
                       upperBound=pos + (0.001 , 0.001))
         world.QueryAABB(cb , aabb)
         if cb.fixture != None:
             world.DestroyBody(cb.fixture.body)
         ###
     elif event.button == 3 and not(quakeActive):            # CREATE JOINT ON RIGHT CLICK
         pos = screenToWorld( pygame.mouse.get_pos())
         cb = QueryClickCallback(pos)
         aabb = b2AABB(lowerBound=pos - (0.001, 0.001),
                       upperBound=pos + (0.001, 0.001))
         print( aabb )
         world.QueryAABB( cb, aabb )
         print( "fixture = ", cb.fixture )
         if cb.fixture != None:
             if firstBody == None:
                 firstBody = cb.fixture.body