Java"简单"碰撞球/碰撞练习

时间:2016-08-02 17:02:54

标签: java collision

我在此阅读了其他一些类似的问题,但需要特别指出如何使球相互反弹。

  • 这个程序让用户输入重绘的速度和 球的数量。
  • 我可以让他们从墙上反弹就好了。

这是我的代码:

(导入ArgsProcessor; import 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);
    }

    }
}

}

1 个答案:

答案 0 :(得分:0)

首先,你的距离测试应该涉及两个对象,而不仅仅是一个(事实上,距离总是为零)注意使用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] ;
    }