Example #1
0
  def generateCollisionList(self, time):
    """Generate a dictionary of collisions between object
    that are allowed to generate collision events
    TODO: FINISH DOC HERE
    
    only considers collisions that happen <= time"""
    
    #d = {}
    l = []
    
    #TODO: improve, this is O(n^2) for testing
    
    for i in range(len(self.objectList)):
      
      objI = self.objectList[i]
      
      
      #create bounding box of trajectory and test for collision with appropriate tiles
      p1 = objI.pos
      p2 = objI.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)
      
      tl =  [int(floor(bbox.x1/32)), int(floor(bbox.y1/32))] #top left tile indices
      br = [int(ceil(bbox.x2/32)), int(ceil(bbox.y2/32))] #bottom right tile indices
      #print tl[0], br[0]
      for x in range(tl[0], br[0]+1):
        for y in range(tl[1], br[1]+1):
          objJ = self.tileGrid[x][y]
          #the following is copied from below
          if not CollisionFilter.canCollide(objI, objJ):
            continue
          #Note, the shapes are at the origin, so we have to translate them
          # to the objects current position
          s1 = objI.shape.translate(objI.getPos())
          s1vel = objI.vel
          
          s2 = objJ.shape.translate(objJ.getPos())
          s2vel = objJ.vel
  
          #TODO: this will always return none!!!
          colInfo = getCollisionInfo(s1,s1vel,s2,s2vel)
                  
          if colInfo is not None:          
            
            #only consider events <= time
            colTime, objIStops, objJStops = colInfo         
            
            if colTime <= time:
              #print colInfo
              
              #if objI not in d:
              #  d[objI] = set([()])
              
              l.append((colTime, objI, objIStops,objJ,objJStops))
        ####end of copied code
      
      #check collision with other objects      
      for j in range(i+1, len(self.objectList)):
        
        #objI = self.objectList[i]
        objJ = self.objectList[j]
        
        
        #If the objects can't collide, don't even bother 
        # comparing them
        if not CollisionFilter.canCollide(objI, objJ):
          continue
        
        #Note, the shapes are at the origin, so we have to translate them
        # to the objects current position
        s1 = objI.shape.translate(objI.getPos())
        s1vel = objI.vel
        
        s2 = objJ.shape.translate(objJ.getPos())
        s2vel = objJ.vel

        #TODO: this will always return none!!!
        colInfo = getCollisionInfo(s1,s1vel,s2,s2vel)
                
        if colInfo is not None:          
          
          #only consider events <= time
          colTime, objIStops, objJStops = colInfo         
          
          if colTime <= time:
            #print colInfo
            
            #if objI not in d:
            #  d[objI] = set([()])
            
            l.append((colTime, objI, objIStops,objJ,objJStops))

    return l   
Example #2
0
 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