interface ParticleSpringGridRenderer { void draw(Particle p, int row, int col); } class ParticleSpringGrid { private ParticleSystem phys; private Particle[][] grid; private ParticleSpringGridRenderer psgr; // creates a ParticleSpringGrid with the upper left particle at (0,0) ParticleSpringGrid(int rows, int cols, int spacing, ParticleSpringGridRenderer psgr) { phys = new ParticleSystem(0, 0); grid = new Particle[rows][cols]; // create the grid of particles for(int r = 0; r < grid.length; r++) { for(int c = 0; c < grid[r].length; c++) { Particle p = phys.makeParticle(1, c*spacing, r*spacing, 0); grid[r][c] = p; } } // connect the particles with springs // springs are made by connecting the current particle with its // neighbor to the right and neighbor to the bottom for(int r = 0; r < grid.length; r++) { for(int c = 0; c < grid[r].length; c++) { // particle a, particle b, strength, damping, rest length if ( c+1 < grid[r].length ) { phys.makeSpring(grid[r][c], grid[r][c+1], 0.8, 0.4, spacing); } if ( r+1 < grid.length ) { phys.makeSpring(grid[r][c], grid[r+1][c], 0.8, 0.4, spacing); } } } grid[0][0].makeFixed(); grid[0][cols-1].makeFixed(); grid[rows-1][0].makeFixed(); grid[rows-1][cols-1].makeFixed(); this.psgr = psgr; } int rows() { return grid.length; } int cols() { return grid[0].length; } Particle get(int row, int col) { return grid[row][col]; } // finds all dead particles and move those above them into their grid position // then creates new springs void reconnect() { } // kills the particle at grid[row][col] void kill(int row, int col) { grid[row][col].kill(); } void update() { phys.tick(); } void draw() { for(int r = 0; r < grid.length; r++) { for(int c = 0; c < grid[r].length; c++) { psgr.draw(grid[r][c], r, c); } } } }