python - How do I find the angle of incidence of an object in pygame? - Stack Overflow

admin2025-05-01  0

I recently started to learn pygame and python is my first ever programming language. I am trying to make classic ping pong as my first ever project. I think this doubt might sound stupid to good programmers but I would like to know how I can find the angle at which the ball hits the wall/paddle and how I could bounce it off at the realistic angle.

if self.paddle1_x + self.paddle_width >= self.ball_x >= self.paddle1_x and self.paddle1_y <= self.ball_y <= self.paddle1_y + self.paddle_height:
    self.b_dx = -self.b_dx
if self.paddle2_x <= self.ball_x + self.ball_radius <= self.paddle2_x + self.paddle_width and self.paddle2_y <= self.ball_y <= self.paddle2_y + self.paddle_height:
     self.b_dx = -self.b_dx

This is my current code, dir_x either being 1 or -1. The problem is that this makes the ball's movement repetitive and it only takes certain paths around the screen.
I was wondering whether there is an easy way to randomize maybe the angles at which the ball gets sent at the start and whether there was an easy way to make the ball bounce off the paddle depending on which way the paddle was going or how fast the paddle was going (for now the paddle has a certain speed but i also wanted to add a speed-up power up).

I recently started to learn pygame and python is my first ever programming language. I am trying to make classic ping pong as my first ever project. I think this doubt might sound stupid to good programmers but I would like to know how I can find the angle at which the ball hits the wall/paddle and how I could bounce it off at the realistic angle.

if self.paddle1_x + self.paddle_width >= self.ball_x >= self.paddle1_x and self.paddle1_y <= self.ball_y <= self.paddle1_y + self.paddle_height:
    self.b_dx = -self.b_dx
if self.paddle2_x <= self.ball_x + self.ball_radius <= self.paddle2_x + self.paddle_width and self.paddle2_y <= self.ball_y <= self.paddle2_y + self.paddle_height:
     self.b_dx = -self.b_dx

This is my current code, dir_x either being 1 or -1. The problem is that this makes the ball's movement repetitive and it only takes certain paths around the screen.
I was wondering whether there is an easy way to randomize maybe the angles at which the ball gets sent at the start and whether there was an easy way to make the ball bounce off the paddle depending on which way the paddle was going or how fast the paddle was going (for now the paddle has a certain speed but i also wanted to add a speed-up power up).

Share Improve this question asked Jan 2 at 20:20 Khubaib TabishKhubaib Tabish 32 bronze badges 2
  • 1 Do you know how you can calculate the angle of incidence on paper? If so, what specific difficulty do you have with expressing this calculation in Python? If not, you have a basic geometry/trigonometry problem, not a programming problem. – mkrieger1 Commented Jan 2 at 20:21
  • I just finished tenth grade so I have basic knowledge of trigonometry, but I dont really know how to implement it in python. If you know any, could you please suggest tutorials or classes on python or pygame? – Khubaib Tabish Commented Jan 3 at 6:39
Add a comment  | 

1 Answer 1

Reset to default 0

This is not at all the only way to do it, but an easy way to add some better angles to your game would be to calculate the up/down velocity of the ball based on which part of the paddle it hit from.

Something like, if the ball hit the paddle at the very top, it will reflect with a 60-ish degree angle upward, and 60-ish degree angle downward at the very bottom. If you hit the ball at the very center of the paddle it will go perfectly horizontal.

To do that, you could do the following:

  • Calculate which point of the paddle touched the ball:

    paddle_contact_ratio = (self.ball_y - self.paddle_y) / self.paddle_height
    

    This gives you a number between 0 and 1, 0 being top edge, and 1 being bottom edge.

  • Calculate your ball_dy scaled between -2 * ball_dx and 2 * ball_dx

    self.ball_dx = -self.ball_dx
    self.ball_dy = (-2 + 4*paddle_contact_ratio) * self.ball_dx
    
转载请注明原文地址:http://anycun.com/QandA/1746098402a91643.html