   Python + Pyglet Collision Issue
  Ok been working at this and I can't seem to get it right.  To me my collision code looks fine but it seems to screw up everything so it must not be fine.  Open for some suggestions.  I could be calculating the intersection wrong or something.  Here is my code.Collision check code:    def update_cannon_balls(self):   for ball in self.cannon_balls:  if ball.alive: ball.update_loc(ball.position[0] + ball.velocity[0],   ball.position[1] + ball.velocity[1]) if (ball.position[0] > self.window.width orball.position[1] > self.window.height):ball.alive = Falseball.update_loc(self.cannon.position[0], self.cannon.position[1])continue   ball_box = bbox.BoundingBox(int(ball.position[0]),    int(ball.position[1]),    ball.sprite.width,    ball.sprite.height) for enemy in self.enemies:enemy_box = bbox.BoundingBox(int(enemy.position[0]),    int(enemy.position[1]),    enemy.sprite.width,    enemy.sprite.height)if ball_box.intersects(enemy_box):    ball.alive = False    enemy.alive = False    breakBoundingBox code:class BoundingBox(object):    def __init__(self, x, y, width, height):   self.left = x   self.right = x + width   self.top = y + height   self.bottom = y  def intersects(self, bounding_box):   return ((self.left < bounding_box.right) or (self.right > bounding_box.left) or (self.bottom < bounding_box.top) or (self.top > bounding_box.bottom))
  Your BoundingBox intersects() code is faulty. Consider what happens if you have an enemy to the left of the cannonball. When you do ball_box.intersects(enemy_box), the code will say "Ah! My right is greater than his left; we must be intersecting!" But of course, that's not sufficient. Consider the different ways that bounding boxes can intersect, and how their vertices relate to each other in each situation. You should be able to come up with an accurate intersection test.On an unrelated note, it's traditional in programming for Y values to increase as you move down the screen. You appear to be doing the reverse. There's nothing that says that you can't do things that way, but it's nonstandard.   ;  pyglets coord system is basically the upper right quadrant of the Cartesian plane compared to the typical lower right.^ y+##############> x+(0,0)   ;  Ok I made some changes to the code but it seems to not work just right.  At the moment only the occasional ball is actually colliding.  I can't get them to collide consistently maybe a math error on my part.  Not sure why this is being such a pain lol.Rect:class Rect(object):    def __init__(self, x1, y1, x2, y2):   self.left = x1   self.right = x1 + x2   self.bottom = y1   self.top = y1 + y2  def intersects(self, rect):   return (self.bottom <= rect.top and self.top >= rect.bottom and self.right >= rect.left and self.left <= rect.right)update_cannon_balls:def update_cannon_balls(self):   for ball in self.cannon_balls:  if ball.alive: ball.update_loc(ball.x + ball.velocity[0],   ball.y + ball.velocity[1]) if (ball.x > self.window.width orball.y > self.window.height):ball.alive = Falseball.update_loc(self.cannon.x, self.cannon.y)continue ball_rect = Rect.Rect((ball.x - (ball.sprite.width/2)),(ball.y - (ball.sprite.height/2)),ball.sprite.width,ball.sprite.height) result = self.check_for_collision(ball_rect, self.enemies) if not result == None:ball.alive = Falseresult.alive = Falseball.update_loc(self.cannon.x, self.cannon.y)check_for_collision:def check_for_collision(self, test_rect, graphics_object_list):   for go in graphics_object_list:  rect = Rect.Rect((go.x - (go.sprite.width/2)),    (go.y - (go.sprite.height/2)),    go.sprite.width,    go.sprite.height)    if test_rect.intersects(rect): return go  else: returnEDIT:  Fixed I accidentally had a else return in the check_for_collision method.[Edited by - blewisjr on June 29, 2009 5:23:44 PM];  Your new intersection test will catch collisions only when the cannonball is entirely contained within the rect for the enemy object. That's one valid collision case, but it's far from the only one. I recommend reading this article, which appears to be a fairly good introduction to bounding box checks.   