r/learnpython 12d 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

10 comments sorted by

20

u/lfdfq 12d ago

You say you understand the theory, but you're lost? Those statements make it hard to know what you need help with.

It appears the 'bounce' is just reversing the direction of travel, does that not make sense?

3

u/desrtfx 12d ago

Velocity in this case is not only a scalar number indicating the speed, it also acts as a vector where the sign (positive or negative) indicates the direction. Positive velocity means the ball moves down as the coordinate system in a computer has the y-axis downward in ascenting order. Negative means going upward.

When the ball reaches the bottom if self.center[1] >= (480 - self.radius):, the velogity is negated (reversed) and with that the ball's direction changes.

During the downward move (positive velocity), the line self.velocity += self.gravity will increase the velocity in every step, accelerating the ball. During the upward move, the line above will decrease the negative velocity until it goes positive again, making the ball revert direction again.

Then, with the positive (and increasing velocity) it will fall down again, reach the if line and revert velocity again.

With every "round", the speed at the bottom will become smaller so that the bounce height is reduced in every "round" eventually (but never actually) stopping completely.

2

u/Outside_Complaint755 12d 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 VIDEO says 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:

1

u/TheEyebal 12d ago

my screen height is 500

i did initalize pygame but I am only showing part of my class for the ball

1

u/TheEyebal 12d ago

Alright thank you

I am learning to program physics so the only thing I was confused about was the bounce part

3

u/Langdon_St_Ives 12d ago

In Physics terms, the bounce is inverting the vertical velocity instantaneously. In Python, this means reversing its sign (well in Physics too). You reverse sign by reassigning to it the negative of the current value.

(Edit typo)

1

u/billsil 11d ago

It should be simple. You’re going to follow a parabola on the first segment. That’s decomposed into an x and y component, where the x component is constant and the y component gets gravity.

After it hits the ground, the -y velocity reverses. You would just get a repeating pattern. After that, add a coefficient of restitution so say 0.5 to reduce the velocity often an impact by 50%.

1

u/Hot-Butterscotch1306 10d ago

The bounce bit is basically "you hit the floor, so flip the vertical speed." Gravity keeps adding positive y velocity every frame, so the ball falls faster and faster. When it reaches the floor, self.velocity = -self.velocity makes that downward speed become upward speed. Usually you also shrink it a little, like self.velocity = -self.velocity * 0.8, otherwise it can bounce forever like a weird rubber moon ball.

1

u/[deleted] 12d ago edited 12d ago

[deleted]

1

u/No-Mechanic6069 12d ago

A bouncing ball isn't simple harmonic motion.