# Collision in PyGame for spinning rectangular object.touching circles

I'm creating a variation of Pong. One of the differences is that I use a rectangular structure as the object which is being bounced around, and I use circles as paddles. So far, all the collision handling I've worked with was using simple math (I wasn't using the collision "feature" in PyGame). The game is contained within a 2-dimensional continuous space.

The idea is that the rectangular structure will spin at different speed depending on how far from the center you touch it with the circle. Also, any extremity of the rectangular structure should be able to touch any extremity of the circle. So I need to keep track of where it has been touched on both the circle and the rectangle to figure out the direction it will be bounced to.

I intend to have basically 8 possible directions (Up, down, left, right and the half points between each one of those). I can work out the calculation of how the objected will be dislocated once I get the direction it will be dislocated to based on where it has been touch. I also need to keep track of where it has been touched to decide if the rectangular structure will spin clockwise or counter-clockwise after it collided.

Before I started coding, I read the resources available at the PyGame website on the collision class they have (And its respective functions). I tried to work out the logic of what I was trying to achieve based on those resources and how the game will function. The only thing I could figure out that I could do was to make each one of these objects as a group of rectangular objects, and depending on which rectangle was touched the other would behave accordingly and give the illusion it is a single object. However, not only I don't know if this will work, but I also don't know if it is gonna look convincing based on how PyGame redraws the objects.

Is there a way I can use PyGame to handle these collision detections by still having a single object? Can I figure out the point of collision on both objects using functions within PyGame precisely enough to achieve what I'm looking for?

P.s: I hope the question was specific and clear enough. I apologize if there were any grammar mistakes, English is not my native language.

• Are you interested in non-pygame solutions? You could implement a function for this single purpose without requiring a full collision system. For example, gamedev.stackexchange.com/questions/33546/… – Seth Battin Apr 20 '13 at 19:54
• I don't really understand the question. Are you asking for how to model the physics of a pong game or how to detect the collisions? The physics for the original pong aren't at all realistic; do you want to have similar ones or actually accurate ones? – Anko Dec 11 '14 at 2:07
• I suggest to use a physics library like Pymunk. – skrx May 8 '18 at 5:48

I think pygame doesn't have complex functions to get collisions. But now to your question:

You can just calculate the angle between your ball and the paddle and use this for the direction of bouncing.

Maybe I do something really silly but this function will work for calculating the angle:

#returns angle and distance between two points
def angle_distance(x1,y1,x2,y2):
distance = ((x1 - x2)**2 + (y1 - y2)**2)**0.5*pos_neg(y1 - y2)
try:
angle = math.acos((x1 - x2)/distance)/math.pi*180 + 180*(y1 - y2 >= 0)
except ZeroDivisionError:
angle = 0
return angle,abs(distance)

#returns -1 or 1 if value is negative or positive
def pos_neg(value):
return ((value >= 0)*2-1)


I hope this will help. If you want some more kollision stuff, also for rotated rectangles, just ask.