Java "simple" 颠簸 Ball/Collision 运动

Java "simple" Bumping Ball/Collision exercise

我通读了一些其他类似的内容 q/a,但需要有关如何使球相互反弹的具体帮助。

这是我的代码:

(导入参数处理器; 导入 sedgewick.StdDraw;)

public static void main(String[] args) {
    ArgsProcessor ap = new ArgsProcessor(args);
    //request from user - get refresh rate and number of balls
    int pause = ap.nextInt("Enter pause time:");
    int numBalls = ap.nextInt("How many balls will you put into play?");

    // set the scale of the coordinate system

    StdDraw.setXscale(-1.0, 1.0);
    StdDraw.setYscale(-1.0, 1.0);

    // initial values
    int[] balls = new int[numBalls];
    double[] positionX = new double[numBalls];
    double[] positionY = new double[numBalls];
    double[] velocityX = new double[numBalls];
    double[] velocityY = new double[numBalls];
    double[] radius = new double[numBalls];
    double distance = 0;

    for (int i = 0; i < numBalls; ++i) {
        balls[i] = numBalls;
        positionX[i] = Math.random();
        positionY[i] = Math.random();
        velocityX[i] = Math.random() * .01;
        velocityY[i] = Math.random() * .01;
        radius[i] = 0.05;
    }

    while (true) {
        // clear the background - draw before since it is on the bottom
        StdDraw.setPenColor(StdDraw.GRAY);
        StdDraw.filledSquare(0, 0, 1.0);

        // bounce off wall according to law of elastic collision
        for (int i = 0; i < numBalls; ++i) {

            if (Math.abs(positionX[i] + velocityX[i]) > 1.0 - radius[i]) {
                velocityX[i] = -velocityX[i];
            }
            if (Math.abs(positionY[i] + velocityY[i]) > 1.0 - radius[i]) {
                velocityY[i] = -velocityY[i];
            }
            positionX[i] = positionX[i] + velocityX[i];
            positionY[i] = positionY[i] + velocityY[i];

        //figure out the distance between
        //if balliradius + balliradius >= distance between, then reverse direction
        for (int j = 0; j < numBalls; ++j){//create a for loop

            distance = Math.sqrt(Math.pow(positionX[i] - positionX[i], 2) + Math.pow(positionY[i] - positionY[i], 2)); 

            if (distance <= radius[i] + radius[i]){ //if distance between two balls has them touching/overlapping, change direction
                positionX[i] = -positionX[i] - velocityX[i];
                positionY[i] = -positionY[i] - velocityY[i];
            } //if balliradius + balliradius < distance between, then keep moving
            // update position
            positionX[i] = positionX[i] + velocityX[i];
            positionY[i] = positionY[i] + velocityY[i]; 

            // draw ball on the screen
            StdDraw.setPenColor(StdDraw.BLUE);
            StdDraw.filledCircle(positionX[i], positionY[i], radius[i]);
        }
        // display and pause for 20 ms
        StdDraw.show(pause);
    }

    }
}

}

首先,你的距离测试应该涉及两个对象,而不是一个(事实上,距离将始终为零)注意使用 position*[j]:

distance = Math.sqrt(Math.pow(positionX[i] - positionX[j], 2) + Math.pow(positionY[i] - positionY[j], 2));

您的跳出计算也有误。对于最简单的反弹,只需撤消最新的速度调整,然后反转速度分量的符号(同样,您需要访问两个对象,而不仅仅是一个,注意 radius[j]):

if (distance <= radius[i] + radius[j]){ //if distance between two balls has them touching/overlapping, change direction
    // undo last velocity update; now the balls aren't overlapping anymore
    positionX[i] = positionX[i] - velocityX[i] ;
    positionY[i] = positionY[i] - velocityY[i] ;
    positionX[j] = positionX[j] - velocityX[j] ;
    positionY[j] = positionY[j] - velocityY[j] ;
    // now reverse all velocity components
    velocityX[i] = -velocityX[i] ;
    velocityY[i] = -velocityY[i] ;
    velocityX[j] = -velocityX[j] ;
    velocityY[j] = -velocityY[j] ;
    }