0
\$\begingroup\$

I hope you can understand my question! I'll try to be very clear.

Watching a pong tutorial (it was originally written in Javascript but I made the game in Python) The developer used an algorithm to determine, depending on which part of the paddle the ball hit, the ball velocity changed.

enter image description here

As you can see, if the ball hits more towards the bottom or top of the paddle, the angle in which it bounces gets larger, and if it hits closer to the middle, the angle becomes narrower.

Here is the algorithm and it works (Check code comments):

player_interesected_ball = detect_collision(ball, player) #It's a function that just detects if two rectangles collided
if player_interesected_ball :
    offset = (ball.y + ball.s - player.y) / \
             (player.height + ball.s) # ball.s is the ball size like 10px it means that is 10px wide and 10px high
    phi = 0.25 * math.pi * (2 * offset - 1)

    ball.vel_x *= -1 
    ball.vel_y = ball.speed * math.sin(phi)

This code works when the paddle is placed on the Right or Left side of the screen but I want to know what should I change in these values to make it work when the paddle is placed on the bottom of the screen.

Like this: enter image description here :) Thank you before hand!

\$\endgroup\$
0

2 Answers 2

1
\$\begingroup\$

You're essentially swapping the axes x and y, going from horizontal play to vertical play. Along those lines, try swapping the axes in your bounce code. All the x variables become y and all the y variables become x (i.e. ball.y -> ball.x). Width and height swap as well.

Specifically, that method would look like this:

player_interesected_ball = detect_collision(ball, player) #It's a function that just detects if two rectangles collided
if player_interesected_ball :
    offset = (ball.x + ball.s - player.x) / \
             (player.width + ball.s) # ball.s is the ball size like 10px it means that is 10px wide and 10px high
    phi = 0.25 * math.pi * (2 * offset - 1)

    ball.vel_x = ball.speed * math.sin(phi)
    ball.vel_y *= -1 
\$\endgroup\$
6
  • \$\begingroup\$ Worked, just change the last two lines for: ball.vel_y = ball.speed..... and ball.vel_x *= -1 \$\endgroup\$ Commented Jul 10, 2016 at 18:22
  • \$\begingroup\$ The last two lines in my example are incorrect? \$\endgroup\$
    – House
    Commented Jul 10, 2016 at 18:24
  • \$\begingroup\$ Yes, you have to swap them too, I did and it's behaving exactly as I wanted. vel_x variable must be turned into vel_y and also the last assigment of vel_y must be turned into vel_x *= -1 :P \$\endgroup\$ Commented Jul 10, 2016 at 18:26
  • 1
    \$\begingroup\$ The assignments in my example are swapped from the original. The variables are on the same lines as before. I'm pretty sure when doing this calculation vertically, you want the vel_y to be inverted upon collision. \$\endgroup\$
    – House
    Commented Jul 10, 2016 at 18:28
  • \$\begingroup\$ In my code the last two lines of that code block are: ball.vel_x *= -1 and ball.vel_y = ball.speed * math.sin(phi) now I'm confused. But it's working thanks to what you replied :P so, let's not worry about it. \$\endgroup\$ Commented Jul 10, 2016 at 18:40
0
\$\begingroup\$

You can easily do this with trigonometry.

The ball bounces off from the paddle in 90 degrees if it hits the exact center of the paddle and in 45 degrees, if it hits the edges of it.

This means, that the ball can bounce from the paddle between -45 and 45 degrees, in radians, this is -PI/8 -> PI / 8.

Now you need to map each point of the paddle between these 2 values. If you know the distance of the bounce point from the top of the paddle and the height of the paddle, then this can be done with

angle = PI / 4 * (distanceFromTop / paddleHeight) - PI / 8

This is the direction of the ball, using cosine and sine you can get the velocity vector of the ball.

v.x = cos(angle) * speed
v.y = sin(angle) * speed

where v is the velocity vector of the ball.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .