/** * WireDrawing Test Progress * by Mike Shaub. * * This example demonstrates how to draw a line with the * Traer Physics Library. It's affected by gravity and the * endpoints can be moved around. * * Press 1 and 2 to select the beginning and end points then Click the mouse to move the points. * Press < and > to Increase and Decrease the effect of gravity on the wire. */ import traer.physics.*; ParticleSystem physics; Particle[][] particles; int lineDetail = 10; //How many segments are calculated per line float GravityAmt = 2.0; void setup() { size(400, 400); smooth(); noFill(); frameRate(60); physics = new ParticleSystem(2.0, 0.05); //(Force of gravity, Drag) Higher Gravity = pull down more, Higher Drag = Viscosity particles = new Particle[lineDetail][lineDetail]; //initial position of right end //float gridStepX = (float) ((width / 2) / lineDetail); //original line length float gridStepX = (float) (random(1,width/20)); //short line for (int i = 0; i < lineDetail; i++) //can't delete? { for (int j = 0; j < lineDetail; j++) //can't remove? { //initial position of left end //particles[i][j] = physics.makeParticle(0.2, j * gridStepX + (width / 4), i + 20, 0.0);//original particles[i][j] = physics.makeParticle(0.2, j * gridStepX + (width / 4), i + random(height/20,height/4), 0.0); if (j > 0) { physics.makeSpring(particles[i][j - 1], particles[i][j], 8.0, 0.5, gridStepX); } } } particles[0][0].makeFixed(); particles[0][lineDetail - 1].makeFixed(); } void draw() { physics.advanceTime(0.15); physics.setGravity(GravityAmt); if (mousePressed) { if (key == '1') { if (abs(mouseX - particles[0][0].position().x()) < 100 && abs(mouseY - particles[0][0].position().y()) < 30){ particles[0][0].moveTo(mouseX, mouseY, 0); particles[0][0].velocity().clear(); } } if (key == '2') { if (abs(mouseX - particles[0][lineDetail - 1].position().x()) < 100 && abs(mouseY - particles[0][lineDetail - 1].position().y()) < 30){ particles[0][lineDetail - 1].moveTo(mouseX, mouseY, 0); particles[0][lineDetail - 1].velocity().clear(); } } } background(255); beginShape(); curveVertex(particles[0][0].position().x(), particles[0][0].position().y()); for (int j = 0; j < lineDetail; j++) //Draws width of Rows { curveVertex(particles[0][j].position().x(), particles[0][j].position().y()); } curveVertex(particles[0][lineDetail - 1].position().x(), particles[0][lineDetail - 1].position().y()); endShape(); //Section to draw a second similar line that hangs a little lower (WORKS) /* beginShape(); curveVertex(particles[0][0].position().x(), particles[0][0].position().y()); for (int j = 0; j < lineDetail; j++) //Draws width of Rows { curveVertex(particles[0][j].position().x(), particles[0][j].position().y()+(lineDetail/2-abs(j-lineDetail/2))); } curveVertex(particles[0][lineDetail - 1].position().x(), particles[0][lineDetail - 1].position().y()); endShape(); */ } void mouseReleased() //changing the endpoint x,y { particles[0][lineDetail - 1].setVelocity( (mouseX - pmouseX), (mouseY - pmouseY), 0 ); } void keyPressed() { if (key == ',' || key == '<') { GravityAmt--; } if (key == '.' || key == '>') { GravityAmt++; } physics.setGravity(GravityAmt); println(GravityAmt); }