Circular Ping Pong Game

Viewed 174

I'm trying to develop a simple circular pong game in pygame. The game looks something like this.

Ping pong game

At the moment I struggle with the direction of the ball after it hits the palette (that is theoretically just an arc).

So far I've done this but things don't seem that natural.

def getNewTraiectory(staAng, stpAng, ball, angle):    
    if(ball.speedX > 2.2 or ball.speedY > 2.2):
        ball.speedX /= 2
        ball.speedY /= 2
        
    if (abs(angle-staAng) < abs(stpAng - angle )):
        ball.speedX -=(stpAng - angle)/40
        ball.speedY +=(stpAng - angle)/40

    elif (abs(angle-staAng) > abs(stpAng - angle )):
        ball.speedX += (angle - staAng)/40
        ball.speedY -= (angle - staAng)/40

    speed = random.uniform(0.7,1.96)
    ball.speedX *= (-1)*(speed)
    ball.speedY *= (-1)*(speed)

Where staAng is the starting angle of the pallete, stpAngle is the stopping angle of the pallete, ball is an object of the class Ball (that has a speed on axis X, a speed on axis Y and a position on both axes).

Theoretically, I just check the place where the ball hits on the sprite (close to the starting angle or closer to the stopping angle and based on that I try to give a somewhat opposite direction).

Can you help me to find a better method to give the direction of the ball bouncing the palletes?

1 Answers

The normal vector of a circle is the vector from the point on the circle to the center of the circle. Use pygame.math.Vector2.reflect() to reflect the ball from the circle.
In the following direction, center_point, hit_point and new_dircection are pygame.math.Vector2 objects. center_point is the center of the circle, hit_point is the point where the ball collides with the circle and direction is the direction of movement of the ball:

new_dircection = direction.reflect(hit_point - center_point)
Related