PSystem ps; int num_x = 8; int num_y = 8; float grv = 0.8; HScrollbar myscrollbar; Particle[][] myparticle; void setup() { size(800, 500); myscrollbar = new HScrollbar(0, 20, width/2, 10, 3*5+1); ps = (PSystem)loadPlugin("PSystem"); ps.setGravity(grv); ps.defaultSpringRestLength = 5; ps.defaultSpringStrength = 0.1; myparticle = new Particle[num_x][num_y]; for (int i=0; i0) { ps.addSpring(myparticle[i][j], myparticle[i-1][j]); } if(j>0) { ps.addSpring(myparticle[i][j], myparticle[i][j-1]); } } } // default positions for mesh corners myparticle[0][0].setPos(50,200,0); myparticle[0][num_x-1].setPos(width-50,200,0); myparticle[num_y-1][0].setPos(100,220,100); myparticle[num_y-1][num_x-1].setPos(width-100,220,100); myparticle[0][0].fix(); myparticle[0][num_x-1].fix(); myparticle[num_y-1][0].fix(); myparticle[num_y-1][num_x-1].fix(); } void loop() { background(200,220,200); if(keyPressed) { if(key == 'g') { // flip gravity forces upside down grv *= -1; ps.setGravity(grv); } if(mousePressed) { if(key == '1') myparticle[0][0].setPos(mouseX,mouseY,0); else if(key == '2') myparticle[num_y-1][0].setPos(mouseX,mouseY,100); else if(key == '3') myparticle[0][num_x-1].setPos(mouseX,mouseY,0); else if(key == '4') myparticle[num_y-1][num_x-1].setPos(mouseX,mouseY,100); else if(key == 'f') myparticle[4][4].setPos(mouseX,mouseY,50); } } ps.drag = myscrollbar.getPos()/2000; myscrollbar.update(); myscrollbar.draw(); fill(255,255,255); ps.draw(); } // NEW CLASS FOR SCROLLBAR COPIED FROM EXAMPLE class HScrollbar { int swidth, sheight; // width and height of bar int xpos, ypos; // x and y position of bar float spos, newspos; // x position of slider int sposMin, sposMax; // max and min values of slider int loose; // how loose/heavy boolean over; // is the mouse over the slider? boolean locked; float ratio; HScrollbar (int xp, int yp, int sw, int sh, int l) { swidth = sw; sheight = sh; int widthtoheight = sw - sh; ratio = (float)sw / (float)widthtoheight; xpos = xp; ypos = yp-sheight/2; spos = xpos + swidth/2 - sheight/2; newspos = spos; sposMin = xpos; sposMax = xpos + swidth - sheight; loose = l; } void update() { if(over()) { over = true; } else { over = false; } if(mousePressed && over) { locked = true; } if(!mousePressed) { locked = false; } if(locked) { newspos = constrain(mouseX-sheight/2, sposMin, sposMax); } if(abs(newspos - spos) > 1) { spos = spos + (newspos-spos)/loose; } } int constrain(int val, int minv, int maxv) { return min(max(val, minv), maxv); } boolean over() { if(mouseX > xpos && mouseX < xpos+swidth && mouseY > ypos && mouseY < ypos+sheight) { return true; } else { return false; } } void draw() { fill(255); rect(xpos, ypos, swidth, sheight); if(over || locked) { fill(200, 0, 0); // color while active } else { fill(220, 220, 220); // color while inactive } rect(spos, ypos, sheight, sheight); } float getPos() { // convert spos to be values between // 0 and the total width of the scrollbar return spos * ratio; } }