Magnet magnet; int rows = 120; int cols = 120; field[][] fields = new field[rows][cols]; // field is a grid. // there is a distinct force of every cell according to the // distance of magnet and that cell's distance Particle[] particles = new Particle[300]; Vector3D[] currentForce = new Vector3D[300]; void setup() { size(500,500); framerate(30); colorMode(RGB,250); smooth(); magnet = new Magnet(new Vector3D(width/2,height/2),new Vector3D(0,0)); float size = width/rows; for(int x = 0; x < rows; x++) { for(int y = 0; y < cols; y++) { float xpos = x*size; float ypos = y*size; fields[x][y] = new field(new Vector3D(xpos,ypos), new Vector3D(0,0), size); fields[x][y].calcForce(magnet.getLoc()); } } for (int i = 0; i < 300; i++) { particles[i] = new Particle(new Vector3D(cos(i)*100.0, sin(i)*100.0),new Vector3D(5,0),1); } } void draw() { background(250); int size = width/rows; for (int k = 0; k < 300; k++) { float x = particles[k].loc.x/size; float y = particles[k].loc.y/size; int i = constrain(int(x),0,rows-1); int j = constrain(int(y),0,cols-1); // println("i: " + i); currentForce[k] = fields[i][j].force; particles[k].add_force(currentForce[k]); particles[k].run(); particles[k].render(); particles[k].changeColor(); } } void mouseDragged(){ for (int k = 0; k < 300; k++) { particles[k].loc.setXYZ(mouseX, mouseY,0); } }