if (i != 0) { Particle previous = particles.get(i-1); 起首,我们须要前一个粒子的引用 VerletSpring2D spring = new VerletSpring2D(particle,previous,len,strength); 之后,我们须要在两个粒子之间创建弹簧毗连,并指定弹簧的静止长度和强度(都是浮点数) physics.addSpring(spring); 不要忘记将弹簧参加物理天下 }
示例代码5-11 柔软的钟摆
import toxi.physics2d.*;import toxi.physics2d.behaviors.*;import toxi.geom.*;// Reference to physics "world" (2D)VerletPhysics2D physics;// Our "Chain" objectChain chain;void setup() { size(640, 360); // Initialize the physics world physics=new VerletPhysics2D(); physics.addBehavior(new GravityBehavior(new Vec2D(0, 0.1))); physics.setWorldBounds(new Rect(0, 0, width, height)); // Initialize the chain chain = new Chain(180, 20, 16, 0.2);}void draw() { background(255); // Update physics physics.update(); // Update chain's tail according to mouse position chain.updateTail(mouseX, mouseY); // Display chain chain.display();}void mousePressed() { // Check to see if we're grabbing the chain chain.contains(mouseX, mouseY);}void mouseReleased() { // Release the chain chain.release();}Chain.pde
class Chain { // Chain properties float totalLength; // How long int numPoints; // How many points float strength; // Strength of springs float radius; // Radius of ball at tail // This list is redundant since we can ask for physics.particles, but in case we have many of these // it's a convenient to keep track of our own list ArrayList<article> particles; // Let's keep an extra reference to the tail particle // This is just the last particle in the ArrayList Particle tail; // Some variables for mouse dragging PVector offset = new PVector(); boolean dragged = false; // Chain constructor Chain(float l, int n, float r, float s) { particles = new ArrayList<article>(); totalLength = l; numPoints = n; radius = r; strength = s; float len = totalLength / numPoints; // Here is the real work, go through and add particles to the chain itself for(int i=0; i < numPoints; i++) { // Make a new particle with an initial starting position Particle particle=new Particle(width/2,i*len); // Redundancy, we put the particles both in physics and in our own ArrayList physics.addParticle(particle); particles.add(particle); // Connect the particles with a Spring (except for the head) if (i != 0) { Particle previous = particles.get(i-1); VerletSpring2D spring = new VerletSpring2D(particle,previous,len,strength); // Add the spring to the physics world physics.addSpring(spring); } } // Keep the top fixed Particle head=particles.get(0); head.lock(); // Store reference to the tail tail = particles.get(numPoints-1); tail.radius = radius; } // Check if a point is within the ball at the end of the chain // If so, set dragged = true; void contains(int x, int y) { float d = dist(x,y,tail.x,tail.y); if (d < radius) { offset.x = tail.x - x; offset.y = tail.y - y; tail.lock(); dragged = true; } } // Release the ball void release() { tail.unlock(); dragged = false; } // Update tail position if being dragged void updateTail(int x, int y) { if (dragged) { tail.set(x+offset.x,y+offset.y); } } // Draw the chain void display() { // Draw line connecting all points beginShape(); stroke(0); strokeWeight(2); noFill(); for (Particle p : particles) { vertex(p.x,p.y); } endShape(); tail.display(); }}Particle.pde
class Particle extends VerletParticle2D { float radius = 4; // Adding a radius for each particle Particle(float x, float y) { super(x,y); } // All we're doing really is adding a display() function to a VerletParticle void display() { fill(127); stroke(0); strokeWeight(2); ellipse(x,y,radius*2,radius*2); }}4、运行效果