0
我通过其他类似的q/a读过,但需要特别的帮助,以便如何让球彼此反弹。Java“简单”撞球/碰撞练习
- 该程序具有用户输入的重绘速度和球的数量。
- 我可以让他们反弹离开墙壁就好了。
这里是我的代码:
(进口ArgsProcessor; 进口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);
}
}
}
}