user6179055
user6179055

Reputation:

Bouncing Ball explained

If the condition ball.getY() > getHeight() - DIAM_BALL, is true, that means, if I understood correctly that ball is touching the bottom of the screen and the thing that should happen next is bouncing, or simply the ball needs to bounce off the bottom.

So, the process or the action of bouncing, requires the ball to change the direction of the ball so now the yVel has the negative sign and it (ball) retains 90% of its previous speed. What I don't understand is how the ball is actually moving upward ? If we look at the moveBall() method, I can see that the effect of moving is happening because of ball.move(xVel, yVel) + the pause(DELEY) in the while loop. But in checkForCollision() method the code defines the necessary Y velocity and its direction in this code line yVel = -yVel * BOUNCE_REDUCE, I don't see how and where is that yVel implemented ??? I don't understand the effect of moving upwards! How does the yVel behaves in this program since it is an instance variable ?

One more thing, what tells us how far will the ball bounce, that is how to know when to start falling down again ?

import acm.graphics.*;

public class BouncingBall extends GraphicsProgram {


    /** Size (diameter) of the ball */
    private static final int DIAM_BALL = 30;

    /** Amount Y velocity is increased each cycle as a result of gravity  */
    private static final double GRAVITY = 3;

    /** AnimatIon delay or pause time between ball moves */
    private static final int DELEY = 50;

    /** Initial X and Y location of the ball */
    private static final double X_START = DIAM_BALL / 2;
    private static final double Y_START = 100;

    /** X Velocity */
    private static final double X_VEL = 5;

    /** Amount Y velocity is reduced when it bounces  */
    private static final double BOUNCE_REDUCE = 0.9;

    /** Starting X and Y velocities */
    private double xVel = X_VEL;
    private double yVel = 0.00;

    /* private instance variable */
    private GOval ball;

    public void run(){
        setup(); // Simulation ends when ball goes of right-hand-end of screen //

        while(ball.getX() < getWidth()){
            moveBall();
            checkForCollision();
            pause(DELEY);
        } 

    }

    /** Creates and place ball */
    private void setup(){
        ball = new GOval (X_START, Y_START, DIAM_BALL, DIAM_BALL);
        ball.setFilled(true);
        add(ball);
    }

    /** Update and move ball */
    private void moveBall(){
        // Increase yVelocity due to gravity on each cycle 
        yVel += GRAVITY;
        ball.move(xVel, yVel);
    }

    /** Determine if collision with floor, update velocities and location as appropriate. */
    private void checkForCollision(){

        // Determine if the ball has dropped below the floor 
        if (ball.getY() > getHeight() - DIAM_BALL){

            // Change ball's Y velocity to now bounce upwards
            yVel = -yVel * BOUNCE_REDUCE;

            // Assume bounce will move ball an amount above the
            // floor equal to the amount it would have dropped
            // below the floor 
            double diff = ball.getY() - (getHeight() - DIAM_BALL);
            ball.move(0, -2*diff);
        }
    }


}

Upvotes: 0

Views: 577

Answers (1)

eldo
eldo

Reputation: 1326

You have an yVel and that is important now only. Also you have a GRAVITY and a BOUNCE_REDUCE.

Once your ball starts falling you increase its positions by GRAVITY: (Adding gravity to make it fall since 0,0 is upper left corner)

yVel += GRAVITY;
ball.move(xVel, yVel);

Once it hit the ground yVel = -yVel will change its direction to reduce it a little so a ball wont bounce back to its original position you reduce yVel by multiplying by 0.9.

When going upward (only with 90% of the previous velocity) you are adding GRAVITY to it (its a negative velocity) so it will slow down and stop when reaching 0 vel and start falling again.

A quick example:

Drop ball at 10 unit height falling down with 3 GRAVITY it will hit the ground with 30 velocity bouncing back with -27 velocity (yVel = -yVel * BOUNCE_REDUCE;); and adding 3 GRAVITY in every iteration it will reach 9 unit height.

I hope its detailed enough.

Upvotes: 1

Related Questions