import toxi.math.conversion.*; import toxi.math.*; import toxi.geom.mesh2d.*; import toxi.util.datatypes.*; import toxi.util.events.*; import toxi.geom.mesh.subdiv.*; import toxi.geom.mesh.*; import toxi.math.waves.*; import toxi.util.*; import toxi.math.noise.*; import toxi.processing.*; import toxi.geom.*; import toxi.physics.*; import toxi.physics.behaviors.*; import toxi.physics.constraints.*; //import processing.opengl.*; import peasy.*; PeasyCam cam; VerletPhysics physics; AABB constraint; WETriangleMesh meshConstraint = new WETriangleMesh(); List edges = new ArrayList(); float dashSpacing = 10; Particle p1; Particle p2; ToxiclibsSupport gfx; ArrayList spicules = new ArrayList(); void setup() { size( 720, 480, P3D); noFill(); gfx = new ToxiclibsSupport(this); cam = new PeasyCam(this, width/2, height/2, 0, 1440 ); cam.setMinimumDistance( 480 ); cam.setMaximumDistance( 1440 ); physics = new VerletPhysics(); physics.addBehavior(new GravityBehavior(Vec3D.Z_AXIS.scale(-0.1f))); physics.setDrag(0.05f); // This is the center of the world Vec3D center = new Vec3D( width/2, height/2, 0 ); // these are the worlds dimensions (50%, a vector pointing out from the center in both directions) Vec3D extent = new Vec3D( width/2, height/2, height/2 ); constraint = new AABB ( center, extent ); meshConstraint.addMesh( constraint.toMesh() ); for(WingedEdge e : meshConstraint.edges.values()) { if (e.getDirection().isMajorAxis(0.01)) { edges.add(e); } } physics.setWorldBounds( constraint ); } void draw() { // Update the physics world physics.update(); background( 215 ); stroke( 100 ); for(Line3D l : edges) { drawDashedLine(l); } stroke ( 0 ); for (int i = spicules.size()-1; i >= 0; i--) { Spicule s = (Spicule) spicules.get(i); s.display(); } } void drawDashedLine(Line3D l) { List points=l.splitIntoSegments(null,dashSpacing,true); for(int i=0, num=points.size()-1; i<num; i+=2) { Vec3D p=points.get(i); Vec3D q=points.get(i+1); line(p.x,p.y,p.z,q.x,q.y,q.z); } } void keyPressed() { switch (key){ case 'r': // record = true; break; case 'g': spicules.add( new Spicule( physics, random( 75, width - 75 ), random( 75, height - 75 ), 0, random(25,75) ) ); break; } } class Particle extends VerletParticle { Particle(float x, float y, float z) { super( x, y, z ); } void display() { fill(175); stroke(0); ellipse(x,y,16,16); } } class Spicule{ Particle origin; Particle p1; Particle p2; Particle p3; VerletSpring spring1; VerletSpring spring2; VerletSpring spring3; Line3D spring1L; Line3D spring2L; Line3D spring3L; Float radius; Spicule( VerletPhysics physics, float x, float y, float z, float radius ) { origin = new Particle( x, y, z ); p1 = new Particle( x, y, z - ( radius * 2 ) ); p2 = new Particle( x-radius, y+radius, z+radius ); p3 = new Particle( x+radius, y+radius, z+radius ); physics.addParticle( origin ); physics.addBehavior( new AttractionBehavior( origin, ( radius * 2 ), -1f, 0.01f ) ); physics.addParticle( p1 ); physics.addParticle( p2 ); physics.addParticle( p3 ); spring1L = new Line3D( origin, p1 ); spring2L = new Line3D( origin, p2 ); spring3L = new Line3D( origin, p3 ); VerletSpring spring1 = new VerletSpring( origin, p1, abs( dist( origin.x, origin.y, origin.z, p1.x, p1.y, p1.z ) ), 1 ); VerletSpring spring2 = new VerletSpring( origin, p2, abs( dist( origin.x, origin.y, origin.z, p2.x, p2.y, p2.z ) ), 1 ); VerletSpring spring3 = new VerletSpring( origin, p3, abs( dist( origin.x, origin.y, origin.z, p3.x, p3.y, p3.z ) ), 1 ); VerletSpring spring4 = new VerletSpring( p1, p2, abs( dist( p1.x, p1.y, p1.z, p2.x, p2.y, p2.z ) ), 1 ); VerletSpring spring5 = new VerletSpring( p1, p3, abs( dist( p1.x, p1.y, p1.z, p3.x, p3.y, p3.z ) ), 1 ); VerletSpring spring6 = new VerletSpring( p2, p3, abs( dist( p2.x, p2.y, p2.z, p3.x, p3.y, p3.z ) ), 1 ); physics.addSpring( spring1 ); physics.addSpring( spring2 ); physics.addSpring( spring3 ); physics.addSpring( spring4 ); physics.addSpring( spring5 ); physics.addSpring( spring6 ); } void display () { line( origin.x, origin.y, origin.z, p1.x, p1.y, p1.z ); line( origin.x, origin.y, origin.z, p2.x, p2.y, p2.z ); line( origin.x, origin.y, origin.z, p3.x, p3.y, p3.z ); } }