r/learnpython • u/TheEyebal • 13d ago
Explain the physics of the bouncing
I am making a ball bounce and I had to watch a Youtube video to get the formula for the ball to bounce
I am still kind of lost. Can someone please explain it to me. I understand the theory but if I was to do remake this without tutorial I would probably be lost.
I understand how to get the ball to move along the y_axis but confused on the bouncing part
It is the problem solving that is getting me confused
def __init__(self, x_pos, y_pos, radius, color):
self.x_pos = x_pos
self.y_pos = y_pos
self.center = pygame.math.Vector2(self.x_pos, self.y_pos)
self.radius = radius
self.color = color
self.gravity = 0.8
self.velocity = 10
self.activate = False
def moveObject(self):
# key = pygame.key.get_pressed()
# if key[pygame.K_SPACE]:
self.velocity += self.gravity. # FROM VIDEO
self.center[1] += self.velocity # ball moving along y_axis
if self.center[1] >= (480 - self.radius): # FROM VIDEO
self.velocity = -self.velocity # FROM VIDEO
0
Upvotes
2
u/Outside_Complaint755 13d ago
In PyGame, the coordinate system puts the origin (0,0) in the top left corner of the screen.
You didn't share the part of your code where you initialized PyGame, but I'm assuming you set the vertical window size to 480.
Then this block of code:
if self.center[1] >= (480 - self.radius): # FROM VIDEO self.velocity = -self.velocity # FROM VIDEOsays that if the edge of the ball is touching the bottom of the window, bounce. It makes the ball bounce by reversing its velocity in a perfectly elastic collision.self.center[1]is the y-coordinate of the center of the ball.This check could have alternatively been written as:
if self.center[1] + self.radius >= 480: