This discussion is archived
6 Replies Latest reply: Nov 17, 2012 9:16 PM by Melissa13

# Don't understand the code, about boucing ball

Currently Being Moderated
this is a program intends to create a ball and let the ball bouncing until it reaches the right side of the console. It's in the stanford cs106a course.
I don't the part：
// 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);
does the ball actually reaches the bottom of the console, and them let the ball moves back upwards with the distance 2diff?

thanks a lot!

beside, I'm new in this forum, how to let the code show in the code mode? Cause it seems hard to read right now.

Here is the whole program:
/*
* File: BouncingBall.java
* -----------------------
* This program graphically simulates a bouncing ball.
*/

import acm.program.*;
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 DELAY = 50;

/** Initial X and Y location of 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 Velocties */

private double xVel = X_VEL;

private double yVel = 0.0;

/* private instance variable */

private GOval ball;

public void run() {

setup();

// Simulation ends when ball goes off right hand

// end of screen

while (ball.getX() < getWidth()) {

moveBall();

checkForCollision();

pause(DELAY);

}

}
/** Create and place ball. */

private void setup() {

ball = new GOval(X_START, Y_START, DIAM_BALL, DIAM_BALL);

ball.setFilled(true);

}

/** 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 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);

}

}

}
Edited by: 967521 on 2012-10-24 下午7:11

Edited by: 967521 on 2012-10-25 上午7:43

Edited by: Melissa13 on 2012-10-26 上午7:21
• ###### 1. Re: plesase help(about boucing ball)
Currently Being Moderated
Put your code between
tags and it'll show correctly (you should edit the post now).
• ###### 2. Re: plesase help(about boucing ball)
Currently Being Moderated
I've already editted the post.
thanks a lot!
• ###### 3. Re: plesase help(about boucing ball)
Currently Being Moderated
I do not understand this statement at all: ："assume bounce will move ball an amount above the floor equal to the amount it would have dropped below the floor."

Should it read："assume bounce will move ball an amount above the floor equal to the amount it would have dropped to the floor." This would produce a constant bounce hight and distance moved in the problem.
• ###### 4. Re: plesase help(about boucing ball)
Currently Being Moderated
I'm afraind I don't understand the part"This would produce a constant bounce hight and distance moved in the problem."
Do you mean the ball wouldn't keep boucing at the original place? In this program, the ball keeps moving right when it's boucing at the same time.
• ###### 5. Re: plesase help(about boucing ball)
Currently Being Moderated
Your premiss for the assignement does not make sense to me... especially the part about going through the floor. I have no problem bouncing a ball--with gravity effects or any other, but I cannot make heads not tails of your instructions on what needs to be done.
• ###### 6. Re: plesase help(about boucing ball)
Currently Being Moderated
It's the original comment in the handout of the course.
I think going throught the floor is just an hypothesis which equals to the movement above the floor after boucing happens.