Ejemplo n.º 1
0
 def bodiesAtPoint(self,pt,include_static=False,include_sensor=False):
     # modified from the elements source
     # thanks guys!
     
     sx,sy = toWorld(pt)
     f = 0.01
 
     AABB=box2d.b2AABB()
     AABB.lowerBound.Set(sx-f, sy-f);
     AABB.upperBound.Set(sx+f, sy+f);
 
     amount, shapes = self.world.Query(AABB, 2)
 
     if amount == 0:
         return False
     else:
         bodylist = []
         for s in shapes:                
             if s.IsSensor() and not include_sensor: continue
             body = s.GetBody()
             if not include_static:
                 if body.IsStatic() or body.GetMass() == 0.0:
                     continue
                     
             if s.TestPoint(body.GetXForm(), box2d.b2Vec2(sx, sy)):
                 bodylist.append(body)
 
         return bodylist        
Ejemplo n.º 2
0
    def get_bodies_at_pos(self, search_point, include_static=False, area=0.01):
        """ Check if given point (screen coordinates) is inside any body.
            If yes, return all found bodies, if not found return False
        """
        sx, sy = self.to_world(search_point)
        sx /= self.ppm # le sigh, screen2world returns pixels, so convert them to meters here.
        sy /= self.ppm

        f = area/self.camera.scale_factor

        AABB=box2d.b2AABB()
        AABB.lowerBound.Set(sx-f, sy-f);
        AABB.upperBound.Set(sx+f, sy+f);

        amount, shapes = self.world.Query(AABB, 2)

        if amount == 0:
            return False
        else:
            bodylist = []
            for s in shapes:
                body = s.GetBody()
                if not include_static:
                    if body.IsStatic() or body.GetMass() == 0.0:
                        continue
                        
                if s.TestPoint(body.GetXForm(), box2d.b2Vec2(sx, sy)):
                    bodylist.append(body)

            return bodylist
Ejemplo n.º 3
0
    def get_bodies_at_pos(self, search_point, include_static=False, area=0.01):
        """ Check if given point (screen coordinates) is inside any body.
            If yes, return all found bodies, if not found return False
        """
        sx, sy = self.to_world(search_point)
        sx /= self.ppm
        sy /= self.ppm

        f = area / self.camera.scale_factor

        AABB = box2d.b2AABB()
        AABB.lowerBound = (sx - f, sy - f)
        AABB.upperBound = (sx + f, sy + f)

        query_cb = Query_CB()

        self.world.QueryAABB(query_cb, AABB)

        bodylist = []
        for f in query_cb.fixtures:
            body = f.body
            if body is None:
                continue
            if not include_static:
                if body.type == box2d.b2_staticBody or body.mass == 0.0:
                    continue

            if f.TestPoint((sx, sy)):
                bodylist.append(body)

        return bodylist
Ejemplo n.º 4
0
    def get_bodies_at_pos(self, search_point, include_static=False, area=0.01):
        """ Check if given point (screen coordinates) is inside any body.
            If yes, return all found bodies, if not found return False
        """
        sx, sy = self.to_world(search_point)
        sx /= self.ppm  # le sigh, screen2world returns pixels, so convert them to meters here.
        sy /= self.ppm

        f = area / self.camera.scale_factor

        AABB = box2d.b2AABB()
        AABB.lowerBound.Set(sx - f, sy - f)
        AABB.upperBound.Set(sx + f, sy + f)

        amount, shapes = self.world.Query(AABB, 2)

        if amount == 0:
            return False
        else:
            bodylist = []
            for s in shapes:
                body = s.GetBody()
                if not include_static:
                    if body.IsStatic() or body.GetMass() == 0.0:
                        continue

                if s.TestPoint(body.GetXForm(), box2d.b2Vec2(sx, sy)):
                    bodylist.append(body)

            return bodylist
Ejemplo n.º 5
0
    def get_bodies_at_pos(self, search_point, include_static=False, area=0.01):
        """ Check if given point (screen coordinates) is inside any body.
            If yes, return all found bodies, if not found return False
        """
        sx, sy = self.to_world(search_point)
        sx /= self.ppm
        sy /= self.ppm

        f = area / self.camera.scale_factor

        AABB = box2d.b2AABB()
        AABB.lowerBound = (sx - f, sy - f)
        AABB.upperBound = (sx + f, sy + f)

        query_cb = Query_CB()

        self.world.QueryAABB(query_cb, AABB)

        bodylist = []
        for f in query_cb.fixtures:
            body = f.body
            if body is None:
                continue
            if not include_static:
                if body.type == box2d.b2_staticBody or body.mass == 0.0:
                    continue

            if f.TestPoint((sx, sy)):
                bodylist.append(body)

        return bodylist
Ejemplo n.º 6
0
    def initializeWorld(self):
        global contactList
        contactList = []
        worldAABB = box2d.b2AABB()
        worldAABB.lowerBound.Set(- 100, - 100)
        worldAABB.upperBound.Set(100, 100)
        gravity = box2d.b2Vec2(0, - 10)
        doSleep = True
        self.world = box2d.b2World(worldAABB, gravity, doSleep)
        self.levelSwitchCountingDown = False
        self.levelSwitchCounter = 0

        # make sun
        BodySprite(self.world.GetGroundBody(), 'Rollcats_Sun.png', 20, 20, -10, -10, self.worldToScreen, self.screenToWorld)

        self.contactListener = myContactListener()
        self.world.SetContactListener(self.contactListener)
Ejemplo n.º 7
0
 def __init__(self):
     global SCREENHEIGHT
     SCREENHEIGHT = Globals.game.screen.get_size()[1]
     
     # set up box2D    
     worldAABB=box2d.b2AABB()
     worldAABB.lowerBound.Set(-100, -100)
     worldAABB.upperBound.Set(100, 100)
     gravity = box2d.b2Vec2(0, -10)
     self.world = box2d.b2World(worldAABB, gravity, True)
     
     self.timeStep = 1.0/60
     self.run = True
     
     self.contactListener = X2OContactListener()
     self.world.SetContactListener(self.contactListener)
     
     self.makeBoundingBox()
Ejemplo n.º 8
0
    def __init__(self,
                 screen_size,
                 gravity=(0.0, -9.0),
                 ppm=100.0,
                 renderer='pygame'):
        """ Init the world with boundaries and gravity, and init colors.
        
            Parameters:
              screen_size .. (w, h) -- screen size in pixels [int]
              gravity ...... (x, y) in m/s^2  [float] default: (0.0, -9.0)
              ppm .......... pixels per meter [float] default: 100.0
              renderer ..... which drawing method to use (str) default: 'pygame'

            Return: class Elements()
        """
        self.set_screenSize(screen_size)
        self.set_drawingMethod(renderer)

        # Create Subclasses
        self.add = add_objects.Add(self)
        self.callbacks = callbacks.CallbackHandler(self)
        self.camera = camera.Camera(self)

        # Set Boundaries
        self.worldAABB = box2d.b2AABB()
        self.worldAABB.lowerBound.Set(-100.0, -100.0)
        self.worldAABB.upperBound.Set(100.0, 100.0)

        # Gravity + Bodies will sleep on outside
        gx, gy = gravity
        self.gravity = box2d.b2Vec2(gx, gy)
        self.doSleep = True

        # Create the World
        self.world = box2d.b2World(self.worldAABB, self.gravity, self.doSleep)

        # Init Colors
        self.init_colors()

        # Set Pixels per Meter
        self.ppm = ppm
Ejemplo n.º 9
0
    def __init__(self, screen_size, gravity=(0.0,-9.0), ppm=100.0, renderer='pygame'):
        """ Init the world with boundaries and gravity, and init colors.
        
            Parameters:
              screen_size .. (w, h) -- screen size in pixels [int]
              gravity ...... (x, y) in m/s^2  [float] default: (0.0, -9.0)
              ppm .......... pixels per meter [float] default: 100.0
              renderer ..... which drawing method to use (str) default: 'pygame'

            Return: class Elements()
        """
        self.set_screenSize(screen_size)
        self.set_drawingMethod(renderer)
        
        # Create Subclasses
        self.add = add_objects.Add(self)
        self.callbacks = callbacks.CallbackHandler(self)
        self.camera = camera.Camera(self)
        
        # Set Boundaries
        self.worldAABB=box2d.b2AABB()
        self.worldAABB.lowerBound.Set(-100.0, -100.0)
        self.worldAABB.upperBound.Set(100.0, 100.0)
        
        # Gravity + Bodies will sleep on outside
        gx, gy = gravity
        self.gravity = box2d.b2Vec2(gx, gy);
        self.doSleep = True
    
        # Create the World
        self.world = box2d.b2World(self.worldAABB, self.gravity, self.doSleep)

        # Init Colors        
        self.init_colors()
        
        # Set Pixels per Meter
        self.ppm = ppm