Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created October 13, 2013 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enjalot/6968663 to your computer and use it in GitHub Desktop.
Save enjalot/6968663 to your computer and use it in GitHub Desktop.
worms
{"description":"worms","endpoint":"","display":"canvas","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"worms.pde":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"inlet.pde":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/IMcj1TR.gif"}
//http://media.quilime.com/?p=src/processing/vector_worms/
Worm[] w;
int num_worms = 4;
int worm_length = 65;
void setup() {
size(tributary.sw, tributary.sh);
smooth();
noStroke();
// color mode as HSB so we can cycle through the color wheel
colorMode(HSB, 360, 255, 255);
reset();
}
void reset() {
w = new Worm[num_worms];
for (int i = num_worms-1; i >= 0; i--)
w[i] = new Worm( worm_length, 20 );
}
void keyPressed()
{
switch (keyCode) {
case 38 : // up
num_worms++;
break;
case 40 : // down
num_worms--;
if (num_worms <= 0)
num_worms = 1;
break;
case 37 : // left
worm_length--;
if (worm_length <= 0)
worm_length = 1;
break;
case 39 : // right
worm_length++;
break;
}
reset();
}
int time = 0;
void draw() {
time = (time + 1) % 1000;
float t = time / 1000;
//console.log(t);
background(40);
// radius of structure
float rad = dist(mouseX, mouseY, width/2, height/2);
// angle between each worm
float angle = TWO_PI / w.length;
// get the mouse angle from center for rotation
float mouse_angle = atan2(mouseY-height/2, mouseX-width/2);
// loop through each worm and distribute them in a circle
for (int i = 0; i < w.length; i++) {
float SIN = sin(angle * i + mouse_angle);
float COS = cos(angle * i + mouse_angle);
float TAN = tan(angle * i + mouse_angle);
float dxA = lerp(2*SIN, 0, t);
float dxB = lerp(2*COS, 0, t);
float dx = width / 2 + rad * (dxA * dxB);
float dy = height / 2 + rad * SIN;
// create new force from circle code
PVector force = new PVector( dx - w[i].p.x , dy - w[i].p.y );
// mult force so it's not so extreme. Try changing this value!
force.mult( 0.2 );
w[i].v.add(force);
w[i].draw();
}
}
class Worm {
PVector p, v;
PVector[] trails;
color c;
float hueCycle = 0;
float fatness = 20;
Worm( int _length, float _fatness )
{
p = new PVector(width/2, height/2); // position
v = new PVector(); // velocity
fatness = _fatness;
// setup trails by placing them randomly
trails = new PVector[_length];
for ( int i = trails.length-1 ; i >= 0; i--) {
trails[i] = new PVector( random(width), random(height) );
}
}
void draw()
{
// drag. Try changing this!
v.mult( 0.85 );
// add the velocity to the position
p.add( v );
// update trails
// set first trail to be the object we're tracing
trails[0].x = p.x;
trails[0].y = p.y;
// loop through the trails array from LAST to FIRST
// setting each vectors position to the previous one
for ( int i = trails.length-1 ; i >= 1; i--) {
trails[i].x = trails[i-1].x;
trails[i].y = trails[i-1].y;
}
// cycle colors
hueCycle -= 5 ; // cycle speed. Try changing this!
while (hueCycle < 0 ) {
hueCycle += 360;
}
for ( int i = trails.length-1 ; i >= 0; i--) {
// loop through curHue
float curHue = hueCycle + i * 5f;
while (curHue > 360 ) {
curHue -= 360;
}
// create new color from hue cycle
c = color(hueCycle, 255, 255);
float weight = (float) i / trails.length;
float wave = sin(weight * PI);
float size = (wave * fatness);
fill( curHue, 255, 255 );
ellipse(trails[i].x, trails[i].y, size, size);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment