Bouncing Ball解释道

时间:2016-10-19 12:33:11

标签: java animation

如果条件ball.getY() > getHeight() - DIAM_BALL为真,那就意味着,如果我理解正确的球触及屏幕的底部并且下一个应该发生的事情就是弹跳,或者只是球需要反弹底部。

因此,弹跳的过程或动作需要球改变球的方向,所以现在yVel有负号并且它(球)保持其先前速度的90%。我不明白的是球是如何向上移动的?如果我们查看moveBall()方法,我可以看到移动的影响是因为while循环中的ball.move(xVel, yVel) + pause(DELEY)而发生的。但是在checkForCollision()方法中,代码在此代码行yVel = -yVel * BOUNCE_REDUCE中定义了必要的Y速度及其方向,我不知道yVel实现的方式和位置??? 我不明白向上移动的效果! yVel如何在这个程序中表现,因为它是一个实例变量?

还有一件事,是什么告诉我们球会反弹多远,那就是知道何时再次开始摔倒?

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);
        }
    }


}

1 个答案:

答案 0 :(得分:2)

你有一个yVel,现在这个很重要。您还有一个GRAVITY和一个BOUNCE_REDUCE

一旦你的球开始下降,你的位置增加GRAVITY: (添加重力使其下降,因为0,0是左上角)

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

一旦它到达地面yVel = -yVel将改变其方向 为了减少它,使球不会弹回到原来的位置,你可以减去yVel乘以0.9。

当向上(仅有前一速度的90%)时,你向它添加GRAVITY(它的负速度),所以它会减速并在达到0 vel时停止并再次开始下降。

一个简单的例子:

在10单位高度落球,落下3 GRAVITY它将以30速度以-27速度(yVel = -yVel * BOUNCE_REDUCE;)反弹击中地面;并且在每次迭代中添加3 GRAVITY它将达到9个单位高度。

我希望它足够详细。