Exemplo n.º 1
0
 def stopObjectEarly(obj, colTime, time, neighborDict, collisionList):
   
   newPos = obj.getPosInTime(colTime)
   
   #Set the new object position as the position right before the collision
   #set its movement to 0
   obj.pos = newPos;
   obj.requestedPos = newPos;    
   obj.movedDir = (0,0)    
   obj.vel = Vector(0.0,0.0)
   
   #remove all references to obj from collisionList
   
   collisionList = [x for x in collisionList if x[1] is not obj and x[3] is not obj]
   
   #Now, recalculate collision for all of its check if any of its neighbors
       
   s1 = obj.shape.translate(obj.getPos())
   s1vel = obj.vel
   
   for n in neighborDict[obj]:
     
       s2 = n.shape.translate(n.getPos())
       s2vel = n.vel
       
       colInfo = getCollisionInfo(s1, s1vel, s2, s2vel)
       
       if colInfo is not None:
         
         
         #only consider events <= time
         cTime, objIStops, objJStops = colInfo         
         
         #ctime must be > original colTime
         if cTime <= time and cTime >= colTime:
           #print colInfo
           
           #if objI not in d:
           #  d[objI] = set([()])
           
           collisionList.append((cTime, obj, objIStops,n,objJStops))
 
   #sort collision list again 
   collisionList.sort(key=lambda x:x[0])
Exemplo n.º 2
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