Page 1 of 1

How to make a ball bounce in a parabola?

Posted: Sat May 27, 2006 7:00 am
by jasonkidd007
How to make a ball bounce in a parabola after the ball reaches the bottom of the screen?

I wanted it to reach the bottom, bounce back up but not as high as the previous one and then reach the bottom of the screen and bounce back up in a parabola but this time lower than the previous height until the ball reaches the right end of the screen and stop and prints itself without dissapearing.

here is my coding but it only bounces up and down:

Code: Select all

REM This program makes a ball bounce less on each successive bounce
REM until it bounces not longer

REM Variable Dictionary
REM y.......y coordinate
rem x.......x coordinate
REM bounce..bounces to a certain height
REM count...counts to a certain range



bounce = 20
y = 30
x = 21


  y = y + 1
  CIRCLE (x, y), 20, 14
  PAINT (x, y), 14, 14
  FOR count = 1 TO 5000
  NEXT count
  CIRCLE (x, y), 21, 0
  CIRCLE (x, y), 0, 0
  IF y = 460 THEN
      y = y - 1
      CIRCLE (x, y), 20, 14
      PAINT (x, y), 14, 14
      FOR count = 1 TO 5000
      NEXT count
      CIRCLE (x, y), 21, 0
      PAINT (x, y), 14, 0
      IF bounce = y THEN EXIT DO
    bounce = bounce + 60
    IF bounce > 460 THEN EXIT DO



Posted: Sat May 27, 2006 6:11 pm
by Theophage
Sorry I never got back to you, Jason :oops:

Posted: Tue May 30, 2006 12:51 pm
by Quibbler
Maybe this will give you some ideas

Code: Select all

WHILE bounce < 1000
IF x >= 620 THEN ix = -.1: ic = 0: ib = 1
IF x <= 0 THEN ix = .1: ic = 0: ib = 1
x = x + ix
IF y >= 480 THEN iy = -1: ic = ib
IF y <= ic THEN
iy = 1
ib = ib + 40
bounce = bounce + 1
y = y + iy * (y - ic + 5) * .01
CIRCLE (xo, yo), 20, 0
CIRCLE (x, y), 20, 14
xo = x: yo = y
FOR i = 1 TO 1000: NEXT i

Posted: Tue May 30, 2006 1:19 pm
by AlienQB
I think I can help you out a bit here. :)

You need to let go of the (x,y) thinking. A ball is not simply described by where it is, but where it's going and how fast that's changing. It's these relationships that causes the parabola to appear. This can be accomplished in 3 steps as shown below.

Step 1
What you need to do is to implement position, velocity and acceleration as individual coordinates (that's vectors in mathematics). To do this we're going to need some stuff as usual; a Vector TYPE. This will do:

Code: Select all

TYPE Vector
Step 2
Now when that's in place, we can start writing our program. We need:

[*]Constants like what the gravity is, how large the ball is and how much of the velocity that will be preserved after impact with the ground.
[*]Declare the ball's position, velocity and acceleration.

Code: Select all

CONST G = 9.82          'Gravity on earth 9.82 m/s^2
CONST RA = .25          'Fairly large ball with a diameter of 1 meter
CONST S = 20            '1 meter equals 100 pixels on the screen as we draw
CONST T = .002          'Step time 2 ms each loop
CONST E = .8            '80% bounce efficiency

DIM r AS Vector         'Location
DIM v AS Vector         'Velocity
DIM a AS Vector         'Acceleration

r.x = 0                 'Position the ball in the center,
r.y = 20                'ten meters above the ground.

v.x = .1                'Let the ball have a small initial
v.y = 0                 'velocity to the right

a.x = 0                 'No initial acceleration
a.y = 0
Notice here that we're measuring in meters. This is generally a good thing to do, and we can scale the graphics later on as we draw them.

Step 3
We are now ready to write the general bounce method, so let's jump right into it:

Code: Select all


   'Gravity causes the ball to get acceleration
   a.y = -G

   'Acceleration causes the ball to get a new velocity
   v.x = v.x + T * a.x
   v.y = v.y + T * a.y

   'A new velocity causes the ball to move
   r.x = r.x + T * v.x
   r.y = r.y + T * v.y

   'Check for bounce
   IF r.y < 0 THEN
      r.y = 0           'The ball can't go through the ground
      v.y = -v.y * E    'Reverse the velocity of the y-coordinate
			'and lose some speed to bounce efficiency


   'Draw the ball and scale, but leave the old one in place,
   'so one can view the parabola trail left by the ball.
   CIRCLE (S + 320 * r.x, 400 - S * r.y), RA * S, 14

That's pretty much all there's to it. A possible addition is the implementation of coefficients of restitution, but that's a bit over our heads right now. Hope this helps out not only with the bouncing ball but delivers some insight into organizing code for applications.

Now this program runs beautifully and looks like this:


Posted: Tue May 30, 2006 1:31 pm
by Quibbler
Don't you think you're over-complicating this.
To get a reasonable bouncing ball just keep x-velocity constant and cause the y-velocity to decrease to zero going up and increase going down.

Posted: Wed May 31, 2006 3:40 am
by AlienQB
Isn't that exactly what my program does? :P

Besides, if one writes apps systematically, they're easily expandable to include not only bouncing balls but a whole bunch of dynamics stuff.

Posted: Wed May 31, 2006 3:29 pm
by Theophage
Does anyone else think "MMMmmmmm McDonalds," when they see the screen shot above?