Skip to main content
added 93 characters in body
Source Link
nmin
  • 101
  • 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.

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.

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.

Source Link
nmin
  • 101
  • 1

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.