How to check collision between objects of same class | Sololearn: Learn to code for FREE!


How to check collision between objects of same class

I have class Block which creates rectangle and makes it fall. But when i try to check for collision between multiple blocks, they either collide with itself or freeze after first collision. Full code which can be run online:

8/8/2022 1:51:56 PM


5 Answers

New Answer


Do you mean how to resolve collision between boxes?


Fake girl , i already made it work ( kind of ) but yeah, that's what i mean. Is there any way to check for collision between objects of same class using method


Artur First you need to check if they collide if yes you have to move them to not be inside each other, to do so, you check for each side, you always subtract them, then you check which side has min absolute value and there you move them from each other


Every instance of Block will have a unique memory address, to prevent a block from colliding with itself, then you have to do create a method inside the class collision(self, blocks) { for b in blocks: if b is not self: check_collision(b, self) }


Mirielle, i tried this and it`s getting stuck on "if b is not self" def fall(self, blocks): for block in blocks: if block is not self: if self.rect.colliderect(block): self.rect.move_ip(0,1) done = False while not done: for event in pygame.event.get(): if event.type == QUIT: done = True if event.type == fall_delay_event: for block in blocks: block.fall(blocks)