The Easiest Way to Save and Share Code Snippets on the web

code twiddlings

java

last edit: Feb, 2nd 2012 | jump to bottom

//  by stephen monslow 2011. vimeo.com/magicmonz   www.stephen.monslow.co.uk
//  based on follow mouse tutorial by jose sanchez
//<one line to give the program's name and a brief idea of what it does.>
  /*  Copyright (C) 2011  Stephen Monslow Magic Monz VJ 
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program 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 General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
 
import processing.opengl.*;
import javax.media.opengl.*;
import toxi.geom.*;
 
boolean lins = true;
 
 
//import jsyphon.*; // Syphon
 
//JSyphonServer mySyphon;
//PGraphicsOpenGL pgl;
//GL gl;
//int[] texID;
 
boolean go = true;
 
 
int num = 20;
int u = num-1;
 
Agent[] a = new Agent[num];
 
 
 
void setup() {
 
  size(640, 400, P3D);
  smooth();
 
    frameRate(60);
//  pgl = (PGraphicsOpenGL) g;
//  gl = pgl.gl;
//  initSyphon(gl,"Syphon - Processing");
 
 
  for (int i =0; i < num; i++ ) {
    Vec3D pos = new Vec3D(random(width), random(height), 0);
    a[i] = new Agent(pos, random(1,5 ), random(1, 100), random(10,9000));
  }
}
 
 
 
 
void draw() {
 
  background(0);
 
 
  if(mousePressed){
   num=(int) random(1,20); 
  }
 
if(  go ) {
  for (int i =1 ; i < 5; i++) {
    float rad = noise(frameCount*PI/(i*90))*width*2;
    noFill();
    strokeWeight(i*2);
    ellipse(width/2, height/2, rad, rad );
  }
 
  for (int i =0; i < num; i++ ) {
    a[i].run();
 
 
 
    //  a[i].limiter = noise(frameCount*PI/300) * 40;
  }
 
int out = (int) random(1,num-1);
  for (int i =5; i < num; i++ ) {
 
 
    strokeWeight(i/5);
 
    if( lins ){
    line(a[i].loc.x, a[i].loc.y, a[i-1].loc.x, a[i-1].loc.y);
    line(a[i-2].loc.x, a[i-2].loc.y, a[i-3].loc.x, a[i-3].loc.y);
    line(a[i-3].loc.x, a[i-3].loc.y, a[i-1-4].loc.x, a[i-1-4].loc.y);
  line(a[i-4].loc.x, a[i-4].loc.y, a[i-5].loc.x, a[i-5].loc.y);
    }
 
        a[i].outside = a[out].loc;
    //        
    //       u--;
    //      if(u< 1) u = num-1;
  }
 
 
}
 
 
 
//    renderTexture(pgl.gl);
}
 
 
 
 
//
//void initSyphon(GL gl, String theName) {
//    if(mySyphon!=null) {
//      // in case you are using
//      //  hint(DISABLE_OPENGL_2X_SMOOTH); or hint(ENABLE_OPENGL_4X_SMOOTH);
//      // setup will be called a second or third time and consequently initSyphon(), too.
//      // Therefore, in case a Syphon server is running, we stop it here, and
//      // inform the listening clients to remove the server from their render list.
//      // in the next step then we create a new server.
//      mySyphon.stop();
//    }
//    mySyphon = new JSyphonServer();
//    mySyphon.test();
//    mySyphon.initWithName(theName);
//
//    // copy to texture, to send to Syphon.
//    texID = new int[1];
//
//    gl.glGenTextures(1, texID, 0);
//    gl.glBindTexture(gl.GL_TEXTURE_RECTANGLE_EXT, texID[0]);
//    gl.glTexImage2D(gl.GL_TEXTURE_RECTANGLE_EXT, 0, gl.GL_RGBA8, width, height, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, null);
//} 
//
//void renderTexture(GL gl) {
//  gl.glBindTexture(gl.GL_TEXTURE_RECTANGLE_EXT, texID[0]);
//  gl.glCopyTexSubImage2D(gl.GL_TEXTURE_RECTANGLE_EXT, 0, 0, 0, 0, 0, width, height); 
//  mySyphon.publishFrameTexture(texID[0], gl.GL_TEXTURE_RECTANGLE_EXT, 0, 0, width, height, width, height, false);
//}
//
//public void stop() {
//  // processing 1.5 does not override and call stop anymore.
//  // looking at the PApplet source code, it says "this [super()] used to shut down the sketch, 
//  // but that code has been moved to dispose()", so we use dispose() instead of stop()
//  // for procesing pre 1.5 call dispose() from here
//  dispose();
//}
//
//void dispose() {
//  // if a syphon server is not stopped when the sketch is closed, the server would
//  // remain visible on the syphon client (Simple Client application) side.
//
//  println("\n\nabout to stop sketch ...");
//  println("deleting textures");
//  gl.glDeleteTextures(1, texID, 0);
//  if(mySyphon!=null) {
//    println("stopping the syphon server");
//    mySyphon.stop();
//  }
//  println("sketch stopped, done.");
//}
//
 
 
 
void keyPressed(){
 if(key == ' '){
  go = !go;
 
 } 
 
  if( key == 'c'){
  lins = !lins;
  }
 
 
 
}
 
 
 
 
class Agent{
 
  Vec3D loc = new Vec3D();
  Vec3D vel = new Vec3D();
  Vec3D acc = new Vec3D();
 
  Vec3D outside = new Vec3D();
 
  float limiter;
  float rad;
  float seed;
 
  Agent( Vec3D _loc , float _limiter, float _rad  ,float _seed){
 
    loc = _loc;
    limiter = _limiter;
    rad = _rad;
    seed = _seed;
  }
 
 
 
  void run(){
    display();
    update();
    follow();
  }
 
 
  void follow(){
    Vec3D target = new Vec3D(outside.x,outside.y,0);
    Vec3D dif = target.sub(loc);
    dif.normalize();
    dif.scaleSelf(noise(frameCount*PI/seed,frameCount*PI/seed*2)*(mouseX*0.01));
    acc.addSelf(dif);
  }
 
 
  void update(){
 
    vel.addSelf(acc);
    vel.limit(limiter+(noise(frameCount*PI/seed)*(mouseY/4)));
//vel.limit(mouseX/4);
    loc.addSelf(vel);
    acc = new Vec3D();
 
 
    if(loc.x > width) loc.x = 0;
    if(loc.x<0) loc.x= width;
    if(loc.y > height) loc.y = 0;
    if(loc.y < 0) loc.y = height;
 
//    rad = noise(frameCount*PI/seed) * 200;
 
  }
 
 
  void display(){
 
  stroke(255);
  strokeWeight(5);
 
//  noFill();
  fill(255);
 
  ellipse( loc.x, loc.y, rad,rad);
 
  }
 
 
}
73 views