Advertisement
BrukHabtu

SATCollision

May 19th, 2011
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1.     def SATCollision(self,boundingbox):    
  2.         #generate an axis that is perpendicular to each edge of each object
  3.         #since rectangles are used, 4 axis axes are needed and not 8
  4.        
  5.      
  6.        
  7.        
  8.         A_TR = self.Rect.topright #Top right corner
  9.         A_TL = self.Rect.topleft #Top left corner
  10.         A_BR = self.Rect.bottomright
  11.        
  12.         A_BL = self.Rect.bottomleft #not used in axis
  13.        
  14.         B_TL = boundingbox.Rect.topleft
  15.         B_TR = boundingbox.Rect.topright
  16.         B_BL = boundingbox.Rect.bottomleft
  17.        
  18.         B_BR = boundingbox.Rect.bottomright #not used in axis
  19.        
  20.        
  21.         Axis1 = [  -(A_TR[1] - A_TL[1]),
  22.                  A_TR[0] - A_TL[0]
  23.                  ]
  24.         Axis2 = [ -(A_TR[1] - A_BR[1]),
  25.                  A_TR[0] - A_BR[0]
  26.                  ]
  27.        
  28.         Axis3 = [ -(B_TL[1] - B_BL[1]),
  29.                  B_TL[0] - B_BL[0]
  30.                  ]
  31.         Axis4 = [- (B_TL[1] - B_TR[1]),
  32.                  B_TL[0] - B_TR[0]
  33.                  ]
  34.        
  35.         axes = (Axis1, Axis2, Axis3, Axis4)
  36.         for axis in axes:
  37.             #get the min and max values, ignore
  38.             # negatives and get their absolute value
  39.             a_min  = 0
  40.             a_max = 0
  41.             b_min = 0
  42.             b_max = 0
  43.            
  44.             A_Projections = [ getProjection(A_TL, axis),
  45.                         getProjection(A_TR, axis),
  46.                         getProjection(A_BL, axis),
  47.                         getProjection(A_BR, axis),
  48.                          ]
  49.            
  50.             B_Projections = [ getProjection(B_TL, axis),
  51.                          getProjection(B_TR, axis),
  52.                          getProjection(B_BL, axis),
  53.                          getProjection(B_BR, axis),
  54.                          ]
  55.            
  56.             for i in xrange(4):
  57.                 for j in xrange(i+1,4):
  58.                     line = math.sqrt( (A_Projections[j][0] - A_Projections[i][0]) ** 2 + (A_Projections[j][1] - A_Projections[i][1]) **2)
  59.                     if line > a_max: a_max = line
  60.                     if line > a_max: a_max = line
  61.                     elif line < a_min: a_min = line
  62.          
  63.             for i in xrange(4):
  64.                 for j in xrange(i+1,4):
  65.                     line = math.sqrt((B_Projections[j][0] - B_Projections[i][0]) ** 2 + (B_Projections[j][1] - B_Projections[i][1]) **2)
  66.                     if line > b_max: b_max = line
  67.                     elif line < b_min: b_min = line
  68.                    
  69.            
  70.             # print B_Scalars
  71.             if not ( b_min <= a_max and b_max >= a_min ):
  72.                 #no overlap so no collision
  73.                 return 0
  74.        
  75.         Debugger.debugPrint('SAT Detected collided')
  76.            
  77.         return 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement