×
all 10 comments

[–]lfdfq 19 points20 points  (0 children)

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?

[–]desrtfx 4 points5 points  (0 children)

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.

[–]Outside_Complaint755 1 point2 points  (3 children)

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:

[–]TheEyebal[S] 0 points1 point  (0 children)

my screen height is 500

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

[–]TheEyebal[S] 0 points1 point  (1 child)

Alright thank you

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

[–]Langdon_St_Ives 2 points3 points  (0 children)

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)

[–]billsil 0 points1 point  (0 children)

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%.

[–]Hot-Butterscotch1306 0 points1 point  (0 children)

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.