Tom Says: Safe code is boring code! Why??
Previous page:
ChucK Tricks
Next page:
GLApp
Lazy day sketch from a while back. Requires the traer.physics library. Try clicking and pressing the any key and taking out some of the //comments.
import traer.physics.*;
ParticleSystem physics;
Particle[][] particles;
void setup()
{
size( 800, 600 );
smooth();
fill( 0 );
frameRate( 24 );
ellipseMode( CENTER );
//physics = new ParticleSystem( 5.0, 0.05 );
physics = new ParticleSystem( 0, 0, 0, 0.05 );
particles = new Particle[100][10];
for(int line = 0; line < particles.length; line++) {
//particles[line][0] = physics.makeParticle( 1.0, width/2, height/2, 0 );
particles[line][0] = physics.makeParticle( 1.0, random(width), random(height), 0 );
//particles[line][0].makeFixed();
}
for(int line = 0; line < particles.length; line++) {
for ( int i = 1; i < particles[line].length; ++i )
{
particles[line][i] = physics.makeParticle( 1.0, width/2, height/2+i, 0 );
physics.makeSpring( particles[line][i-1], particles[line][i], 2.0, 0.1, 0.01 );
}
}
for(int line = 0; line < particles.length; line++)
particles[line][particles[line].length-1].setMass( 5.0 );
}
int frame = 0;
void draw()
{
frame++;
//physics.setGravity(sin(frame/10.0), cos(frame/10.0), 0);
physics.advanceTime( 1.0 );
if ( mousePressed )
{
for(int line = 0; line < particles.length; line++) {
particles[line][particles[line].length-1].moveTo( mouseX, mouseY, 0 );
particles[line][particles[line].length-1].velocity().clear();
}
}
background( 255 );
fill(0);
for(int line = 0; line < particles.length; line++) {
beginShape();
curveVertex( particles[line][0].position().x(), particles[line][0].position().y() );
for ( int i = 0; i < particles[line].length; ++i )
curveVertex( particles[line][i].position().x(), particles[line][i].position().y() );
curveVertex( particles[line][particles[line].length-1].position().x(), particles[line][particles[line].length-1].position().y() );
endShape();
ellipse( particles[line][0].position().x(), particles[line][0].position().y(), 5, 5 );
ellipse( particles[line][particles[line].length-1].position().x(), particles[line][particles[line].length-1].position().y(), 20, 20 );
}
}
void mouseReleased()
{
for(int line = 0; line < particles.length; line++)
particles[line][particles[line].length-1].setVelocity( (mouseX - pmouseX), (mouseY - pmouseY), 0 );
}
void keyPressed()
{
for(int line = 0; line < particles.length; line++)
particles[line][particles[line].length-1].setVelocity( random(-100, 100), random(-100, 100), 0 );
}Posted Jun 08, 2009, in the evening.