public double timeToHit(Particle b) { Particle a = this; if (a == b) return INFINITY; double dx = b.rx - a.rx; double dy = b.ry - a.ry; double dvx = b.vx - a.vx; double dvy = b.vy - a.vy; double dvdr = dx*dvx + dy*dvy; if (dvdr > 0) return INFINITY; double dvdv = dvx*dvx + dvy*dvy; double drdr = dx*dx + dy*dy; double sigma = a.radius + b.radius; double d = (dvdr*dvdr) - dvdv * (drdr - sigma*sigma); // if (drdr < sigma*sigma) StdOut.println("overlapping particles"); if (d < 0) return INFINITY; return -(dvdr + Math.sqrt(d)) / dvdv; } public void bounceOff(Particle that) { double dx = that.rx - this.rx; double dy = that.ry - this.ry; double dvx = that.vx - this.vx; double dvy = that.vy - this.vy; double dvdr = dx*dvx + dy*dvy; // dv dot dr double dist = this.radius + that.radius; // distance between particle centers at collison // normal force F, and in x and y directions double F = 2 * this.mass * that.mass * dvdr / ((this.mass + that.mass) * dist); double fx = F * dx / dist; double fy = F * dy / dist; // update velocities according to normal force this.vx += fx / this.mass; this.vy += fy / this.mass; that.vx -= fx / that.mass; that.vy -= fy / that.mass; // update collision counts this.count++; that.count++; }