CollidingBalls.java
This is the syntax highlighted version of CollidingBalls.java
from 2.5 Arrays of
Introduction to Computer Science by
Robert Sedgewick and Kevin Wayne.
/*************************************************************************
* Compilation: javac CollidingBalls.java
* Execution: java CollidingBalls N
* Dependencies: StdDraw.java
*
* Simulate N balls of uniform mass, bouncing off the four walls
* and each other according to the laws of elastic collisions.
*
* % java CollidingBalls 50
*
* Check physics?
*
*************************************************************************/
import java.awt.Color;
public class CollidingBalls {
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
double dt = 1.0;
double DIAMETER = 10.0;
// initialize positions and velocities at random
double[] px = new double[N];
double[] py = new double[N];
double[] vx = new double[N];
double[] vy = new double[N];
Color[] color = new Color[N];
for (int i = 0; i < N; i++) {
px[i] = 16 + 480 * Math.random();
py[i] = 16 + 480 * Math.random();
vx[i] = 10 * Math.random() - 5;
vy[i] = 10 * Math.random() - 5;
color[i] = Color.getHSBColor((float) Math.random(), 1.0f, 1.0f);
}
int SIZE = 512;
StdDraw.create(SIZE, SIZE);
while(true) {
StdDraw.clear(Color.black);
// detect collision with wall and reverse velocity
for (int i = 0; i < N; i++) {
if ((px[i] + dt * vx[i] > SIZE - DIAMETER / 2) || (px[i] + dt * vx[i] < DIAMETER / 2)) vx[i] = -vx[i];
if ((py[i] + dt * vy[i] > SIZE - DIAMETER / 2) || (py[i] + dt * vy[i] < DIAMETER / 2)) vy[i] = -vy[i];
}
// detect collision with other particles
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
double dx = (px[i] - px[j]) + dt * (vx[i] - vx[j]);
double dy = (py[i] - py[j]) + dt * (vy[i] - vy[j]);
// if collision swap velocities
if (Math.sqrt(dx*dx + dy*dy) <= DIAMETER) {
double tempx = vx[i];
double tempy = vy[i];
vx[i] = vx[j];
vy[i] = vy[j];
vx[j] = tempx;
vy[j] = tempy;
// StdDraw.play("pop.wav");
break; // only pairwise collisions
}
}
}
// update positions
for (int i = 0; i < N; i++) {
px[i] = px[i] + vx[i] * dt;
py[i] = py[i] + vy[i] * dt;
}
// update positions
for (int i = 0; i < N; i++) {
StdDraw.go(px[i], py[i]);
StdDraw.setColor(color[i]);
StdDraw.spot(DIAMETER);
}
StdDraw.pause(50);
}
}
}
Last updated: Sat Aug 7 07:50:01 EDT 2004
.
Copyright © 2004, Robert Sedgewick and Kevin Wayne.