Sorry for my english and beginner knowledge of python
I am trying to find a way to get, during a collision between two rectangles, (a moving bullet and a box), which side of the box hit the bullet. I am currently using the rect.midtop, midright, midleft, midbottom of the bullet to find it. My problem is the bullet moves multiple pixels at a time and when the bullet hits the box but not enough close to the center, it registers the wrong collision.
Example:
The bullet comes from the bottom of the screen and I can see it should be a collision with the bottom side of the box, but it doesn't touch the rect.midtop and touches the rect.midright instead and it registers the wrong collison event.
Here is my collision code:
if self.bullet.rect.collidelist(self.listbox):
self.bullet.pretx1 = 1
self.bullet.pretx2 = 1
self.bullet.prety = 1
self.bullet.pret = 1
for box in self.listbox:
if box.rect.collidepoint(self.bullet.rect.midtop) or box.rect.collidepoint(self.bullet.rect.midbottom) and self.a == 1:
self.a = 0
print("vertical")
print(self.bullet.angle)
self.bullet.angle = abs(360 - self.bullet.angle)
print(self.bullet.angle)
self.listbox.remove(box)
elif box.rect.collidepoint(self.bullet.rect.midleft) or box.rect.collidepoint(self.bullet.rect.midright) and self.a == 1:
self.a = 0
print("horizontal")
print(self.bullet.angle)
self.bullet.angle = 180 - self.bullet.angle
print(self.bullet.angle)
if self.bullet.angle < 0:
self.bullet.angle += 360
print(self.bullet.angle)
self.listbox.remove(box)
How would you do it? I tried to have a line rect, comparing angles, using masks but I couldn't get a solution to work.
Thanks for your time!