Permalink
Browse files

more toxiclibs examples

  • Loading branch information...
1 parent 1c46cee commit 5232f1f744afaffd5ac4ab0f81b555036f92ca11 @shiffman shiffman committed Mar 27, 2011
View
@@ -0,0 +1 @@
+.DS_Store
View
Binary file not shown.
View
Binary file not shown.
Binary file not shown.
@@ -12,17 +12,22 @@ class Chain {
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<Particle> particles;
// Let's keep an extra reference to the tail particle
// This is just the last particle in the ArrayList
- VerletParticle2D tail;
+ 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<Particle>();
totalLength = l;
numPoints = n;
@@ -34,26 +39,28 @@ class Chain {
// 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 location
- VerletParticle2D particle=new VerletParticle2D(width/2,i*len);
+ 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) {
- VerletParticle2D previous = physics.particles.get(i-1);
- VerletSpring2D spring=new VerletSpring2D(particle,previous,len,strength);
+ 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
- VerletParticle2D head=physics.particles.get(0);
+ Particle head=particles.get(0);
head.lock();
// Store reference to the tail
- tail = physics.particles.get(numPoints-1);
+ tail = particles.get(numPoints-1);
+ tail.radius = radius;
}
// Check if a point is within the ball at the end of the chain
@@ -84,16 +91,13 @@ class Chain {
// Draw the chain
void display() {
// Draw line connecting all points
- for(int i=0; i < physics.particles.size()-1; i++) {
- VerletParticle2D p1 = physics.particles.get(i);
- VerletParticle2D p2 = physics.particles.get(i+1);
- stroke(0);
- line(p1.x,p1.y,p2.x,p2.y);
- }
-
- // Draw a ball at the tail
+ beginShape();
stroke(0);
- fill(175);
- ellipse(tail.x,tail.y,radius*2,radius*2);
+ noFill();
+ for (Particle p : particles) {
+ vertex(p.x,p.y);
+ }
+ endShape();
+ tail.display();
}
}
@@ -51,7 +51,9 @@ void draw() {
background(255);
// Update physics
- physics.update();
+ if (mousePressed) {
+ physics.update();
+ }
// Update chain's tail according to mouse location
chain.updateTail(mouseX,mouseY);
// Display chain
@@ -0,0 +1,19 @@
+// Notice how we are using inheritance here!
+// We could have just stored a reference to a VerletParticle object
+// inside the Particle class, but inheritance is a nice alternative
+
+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(175);
+ stroke(0);
+ ellipse(x,y,radius*2,radius*2);
+ }
+}
@@ -0,0 +1,61 @@
+// The Nature of Code
+// <http://www.shiffman.net/teaching/nature>
+// Spring 2010
+// Toxiclibs example: http://toxiclibs.org/
+
+// Force directed graph
+// Heavily based on: http://code.google.com/p/fidgen/
+
+class Cluster {
+
+ // A cluster is a grouping of nodes
+ ArrayList<Node> nodes;
+
+ float diameter;
+
+ // We initialize a Cluster with a number of nodes, a diameter, and centerpoint
+ Cluster(int n, float d, Vec2D center) {
+
+ // Initialize the ArrayList
+ nodes = new ArrayList<Node>();
+
+ // Set the diameter
+ diameter = d;
+
+ // Create the nodes
+ for (int i = 0; i < n; i++) {
+ // We can't put them right on top of each other
+ nodes.add(new Node(center.add(Vec2D.randomVector())));
+ }
+
+ // Connect all the nodes with a Spring
+ for (int i = 0; i < nodes.size()-1; i++) {
+ VerletParticle2D ni = nodes.get(i);
+ for (int j = i+1; j < nodes.size(); j++) {
+ VerletParticle2D nj = nodes.get(j);
+ // A Spring needs two particles, a resting length, and a strength
+ physics.addSpring(new VerletSpring2D(ni,nj,diameter,0.01));
+ }
+ }
+ }
+
+ void display() {
+ // Show all the nodes
+ for (Node n : nodes) {
+ n.display();
+ }
+ }
+
+
+ // Draw all the internal connections
+ void showConnections() {
+ stroke(0,150);
+ for (int i = 0; i < nodes.size()-1; i++) {
+ VerletParticle2D pi = (VerletParticle2D) nodes.get(i);
+ for (int j = i+1; j < nodes.size(); j++) {
+ VerletParticle2D pj = (VerletParticle2D) nodes.get(j);
+ line(pi.x,pi.y,pj.x,pj.y);
+ }
+ }
+ }
+}
@@ -0,0 +1,26 @@
+// The Nature of Code
+// <http://www.shiffman.net/teaching/nature>
+// Spring 2010
+// Toxiclibs example: http://toxiclibs.org/
+
+// Force directed graph
+// Heavily based on: http://code.google.com/p/fidgen/
+
+// Notice how we are using inheritance here!
+// We could have just stored a reference to a VerletParticle object
+// inside the Node class, but inheritance is a nice alternative
+
+class Node extends VerletParticle2D {
+
+ Node(Vec2D pos) {
+ super(pos);
+ }
+
+ // All we're doing really is adding a display() function to a VerletParticle
+ void display() {
+ fill(0,150);
+ stroke(0);
+ ellipse(x,y,16,16);
+ }
+}
+
@@ -0,0 +1,97 @@
+/**
+ * <p>Force directed graph,
+ * heavily based on: <a href="http://code.google.com/p/fidgen/">fid.gen</a><br/>
+ * <a href="http://www.shiffman.net/teaching/nature/toxiclibs/">The Nature of Code</a><br/>
+ * Spring 2010</p>
+ */
+
+/*
+ * Copyright (c) 2010 Daniel Schiffmann
+ *
+ * This demo & library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * http://creativecommons.org/licenses/LGPL/2.1/
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+import toxi.geom.*;
+import toxi.physics2d.*;
+
+// Reference to physics world
+VerletPhysics2D physics;
+
+// A list of cluster objects
+Cluster cluster;
+
+// Boolean that indicates whether we draw connections or not
+boolean showPhysics = true;
+boolean showParticles = true;
+
+// Font
+PFont f;
+
+void setup() {
+ size(300,300);
+ smooth();
+ frameRate(30);
+ f = createFont("Georgia",12,true);
+
+ // Initialize the physics
+ physics=new VerletPhysics2D();
+ physics.setWorldBounds(new Rect(10,10,width-20,height-20));
+
+ // Spawn a new random graph
+ cluster = new Cluster(8,100,new Vec2D(width/2,height/2));
+
+}
+
+void draw() {
+
+ // Update the physics world
+ physics.update();
+
+ background(255);
+
+ // Display all points
+ if (showParticles) {
+ cluster.display();
+ }
+
+ // If we want to see the physics
+ if (showPhysics) {
+ cluster.showConnections();
+ }
+
+ // Instructions
+ fill(0);
+ textFont(f);
+ text("'p' to display or hide particles\n'c' to display or hide connections\n'n' for new graph",10,20);
+}
+
+// Key press commands
+void keyPressed() {
+ if (key == 'c') {
+ showPhysics = !showPhysics;
+ if (!showPhysics) showParticles = true;
+ }
+ else if (key == 'p') {
+ showParticles = !showParticles;
+ if (!showParticles) showPhysics = true;
+ }
+ else if (key == 'n') {
+ physics.clear();
+ cluster = new Cluster(int(random(2,20)),random(10,width/2),new Vec2D(width/2,height/2));
+
+ }
+}
@@ -81,7 +81,7 @@ void draw() {
p2.x = mouseX;
p2.y = mouseY;
p2.unlock();
- }
+ }
}
@@ -0,0 +1,51 @@
+class Blanket {
+ ArrayList<Particle> particles;
+ ArrayList<Connection> springs;
+
+ Blanket() {
+ particles = new ArrayList<Particle>();
+ springs = new ArrayList<Connection>();
+
+ int w = 20;
+ int h = 20;
+
+ float len = 10;
+ float strength = 0.125;
+
+ for(int y=0; y< h; y++) {
+ for(int x=0; x < w; x++) {
+
+ Particle p = new Particle(new Vec2D(100+x*len,y*len));
+ physics.addParticle(p);
+ particles.add(p);
+
+ if (x > 0) {
+ Particle previous = particles.get(particles.size()-2);
+ Connection c = new Connection(p,previous,len,strength);
+ physics.addSpring(c);
+ springs.add(c);
+ }
+
+ if (y > 0) {
+ Particle above = particles.get(particles.size()-w-1);
+ Connection c=new Connection(p,above,len,strength);
+ physics.addSpring(c);
+ springs.add(c);
+ }
+ }
+ }
+
+ Particle topleft= particles.get(0);
+ topleft.lock();
+
+ Particle topright = particles.get(w-1);
+ topright.lock();
+ }
+
+ void display() {
+ for (Connection c : springs) {
+ c.display();
+ }
+ }
+}
+
@@ -0,0 +1,11 @@
+class Connection extends VerletSpring2D {
+ Connection(Particle p1, Particle p2, float len, float strength) {
+ super(p1,p2,len,strength);
+ }
+
+ void display() {
+ stroke(0);
+ line(a.x,a.y,b.x,b.y);
+ }
+}
+
@@ -0,0 +1,17 @@
+// Notice how we are using inheritance here!
+// We could have just stored a reference to a VerletParticle object
+// inside the Particle class, but inheritance is a nice alternative
+
+class Particle extends VerletParticle2D {
+
+ Particle(Vec2D loc) {
+ super(loc);
+ }
+
+ // All we're doing really is adding a display() function to a VerletParticle
+ void display() {
+ fill(175);
+ stroke(0);
+ ellipse(x,y,16,16);
+ }
+}
Oops, something went wrong.

0 comments on commit 5232f1f

Please sign in to comment.