Advertisement
vikhik

loscheck v2

May 26th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1.     def loscheck(self, target_pos):
  2.         canSee = True
  3.         for obj in self.world.objs:
  4.             #is the obj.pos + obj.radius in the line between self.pos and target_pos
  5.                 #return True
  6.                
  7.             #find closest point on line (self.pos to target_pos) to obj.pos
  8.            
  9.             A = self.pos
  10.             B = target_pos
  11.             P = obj.pos
  12.            
  13.             AB = B - A
  14.            
  15.             if AB.lengthSq() == 0: #I'm me, ofc I can see myself.
  16.                 return False
  17.            
  18.             AP = P - A
  19.            
  20.             AB_sq = float(AB.lengthSq())
  21.            
  22.             AP_dot_AB = float(AP.dot(AB))
  23.            
  24.             if (AP_dot_AB > 0.0) and (AP_dot_AB < 1.0):
  25.                 #d = dist from A to closest point to P along line AB
  26.                 d = AP_dot_AB / AB_sq
  27.                
  28.                 #D = nearest point on line AB to P
  29.                 D = A + AB*d
  30.    
  31.                 DP = P - D
  32.    
  33.                 #is this point inside of the obj radius
  34.                 if (DP.length() < obj.radius):
  35.                     if canSee:
  36.                         canSee = False
  37.            
  38.         return canSee
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement