Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
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

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.