def generateNeighborDict(self, time): """Generate dict of neighbors. A neighbor is any object within the bounding box of an objects trajectory during time. Objects must be able to collide to be considered neighbors""" #first, calculate bounding box for each object boundDict = {} for i in range(len(self.objectList)): p1 = self.objectList[i].pos p2 = self.objectList[i].getPosInTime(time) shape = self.objectList[i].shape #Get bounding boxes at the 2 positions box1 = shape.getBoundingBox().translate(p1) box2 = shape.getBoundingBox().translate(p2) #generate the union between them and store it bbox = Rect.union(box1, box2) boundDict[i] = bbox d = {} #TODO: improve, this is O(n^2) for testing #TODO: is this only necessary for objects that can stop each other? for i in range(len(self.objectList)): iObj = self.objectList[i] iRect = boundDict[i] #Check nearby tiles tl = [int(floor(iRect.x1/32)), int(floor(iRect.y1/32))] #top left tile indices br = [int(ceil(iRect.x2/32)), int(ceil(iRect.y2/32))] #bottom right tile indices for x in range(tl[0], br[0]+1): for y in range(tl[1], br[1]+1): jObj = self.tileGrid[x][y] #Following Code copied from below #this is only applicable for objects which can create # collision events if not CollisionFilter.canCollide(iObj,jObj): continue #iRect = boundDict[i] jRect = jObj.shape.getBoundingBox().translate(jObj.pos) if Rect.touches(iRect, jRect): if iObj in d: d[iObj].add(jObj) else: d[iObj] = set([jObj]) if jObj in d: d[jObj].add(iObj) else: d[jObj] = set([iObj]) #End of copied code #Check other objects for j in range(i+1, len(self.objectList)): #iObj = self.objectList[i] jObj = self.objectList[j] #this is only applicable for objects which can create # collision events if not CollisionFilter.canCollide(iObj,jObj): continue #iRect = boundDict[i] jRect = boundDict[j] if Rect.touches(iRect, jRect): if iObj in d: d[iObj].add(jObj) else: d[iObj] = set([jObj]) if jObj in d: d[jObj].add(iObj) else: d[jObj] = set([iObj]) return d