+ 1

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: https://replit.com/@artur10563/sololearn?v=1

8th Aug 2022, 1:51 PM
Artur
Artur - avatar
5 Answers
+ 2
Do you mean how to resolve collision between boxes?
8th Aug 2022, 4:18 PM
God of new world
God of new world - avatar
+ 1
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
8th Aug 2022, 4:21 PM
Artur
Artur - avatar
+ 1
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
8th Aug 2022, 4:29 PM
God of new world
God of new world - avatar
+ 1
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) }
8th Aug 2022, 10:53 PM
Mirielle
Mirielle - avatar
0
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)
13th Aug 2022, 1:04 PM
Artur
Artur - avatar