reset password
Author Message
ggonza83
Posts: 14
Posted 17:00 Apr 26, 2015 |

Good afternoon.

I understand that there is a function that will allow a specific sprite to detect a collision with another sprite in a group pygame.sprite.spritecollide()

My question is if there is a way to detect what section of the self.rect() is being collided with another to produce the correct bounce?

kknaur
Posts: 540
Posted 17:17 Apr 26, 2015 |

There is no built in way using Pygame.  It merely checks if there is a collision, but does not tell you from which side the collision happened.  If you want to check for this you would have to figure it out mathematically and use a custom collision function as a callback function in the spritecollide() function. (I will be discussing what I mean by using a callback function in this week's lecture.) 

You really do not have to worry about this for this lab.  As long as you keep track of the direction of the sprite, making it "bounce" is just a matter of making it go in the opposite direction.

Let's say you have S (a single sprite) and G (a group of sprites).  Use collisions = spritecollide(S, G, False) to get a list of collisions between S and any sprite in G.  If there is a collision, then make S go in the opposite direction.  I am aware this may not be as accurate and there may be issues with more than one sprite colliding at the same time, but it's good enough for me for this lab. 

If anyone has any other ideas, feel free to share!