import processing.video.*; Bug[] bugs = new Bug[5]; int bugNumber = 0; boolean run = true; Capture video; float targetRed = 255.0; //set some numbers for the target you are chasing float targetGreen = 0.0; float targetBlue = 0.0; //boolean isFood = false; void setup() { video = new Capture(this,320, 240, 12); //initiate the video, resolution and frame rate size(320, 240); //give you Processing window a size framerate(30); smooth(); noStroke(); //background(255); for (int i = 0; i < bugs.length; i ++ ) { bugs[i] = new Bug(random(100),random(100),3); } } void mousePressed() { /* boolean followFirst = false; if (keyPressed) { if (keyCode == SHIFT) { followFirst = true; } } // bugs[bugNumber] = new Bug(mouseX,mouseY,3); if (bugNumber == 0) { if (bugs[bugs.length - 1] != null) { bugs[bugs.length - 1].followBug(bugs[0]); } } else if ( bugs[bugNumber - 1] != null ) { if (followFirst) { bugs[bugNumber].followBug(bugs[0]); } bugs[bugNumber-1].followBug(bugs[bugNumber]); } bugNumber++; if (bugNumber >= bugs.length) { bugNumber = 0; } */ // for (int i = 0; i < bugs.length; i ++ ) { // bugs[i].goXgoY(mouseX,mouseY); // } //isFood = true; } void mouseReleased() { run = true; } void captureEvent(Capture camera) { camera.read(); ///////////////////// } void draw() { // background(255); float worldRecord = 1000.0; //intialize the worldrecord int xFound = 0; // initialize the location of the red tracking ball int yFound = 0; for(int j=0; j < video.height; j=j+1) { //for each row for(int i=0; i < video.width; i=i+1) { //for each column //get the color of this pixels //find pixel in linear array using formula: pos = row*rowWidth+column color pix = video.pixels[j*video.width+i]; //find the difference float diff = abs(targetRed - red(pix)) + abs(targetGreen - green(pix)) + abs(targetBlue - blue(pix)); if (diff< worldRecord){ // if this is closest to our target color worldRecord = diff; yFound = j; //mark the spot for drawing it later xFound = i; } } } // println(worldRecord); image(video,0,0); //draw the video, this might be optional //after all the pixels have been tested, draw the winner if (worldRecord > 200) { // fill(255,0,0); // ellipse(xFound, yFound, 10, 10); for (int i = 0; i < bugs.length; i ++ ) { bugs[i].goXgoY(xFound,yFound); } for (int i = 0; i < bugs.length; i ++ ) { //if (bugs[i] != null) { bugs[i].update(true); bugs[i].display(); //} } } else { for (int i = 0; i < bugs.length; i ++ ) { //if (bugs[i] != null) { bugs[i].update(false); bugs[i].display(); //} } } } class Bug { float bx, by; int size; int MIN_DISTANCE = 5; int STEP_DISTANCE = 5; float angle = 0.0; Bug bugToFollow; color bugColor; float x;// = bugToFollow.bx; float y;// = bugToFollow.by; /// float wWidth; float wHeight; //float bSize; float xDirection, yDirection; /// Bug(float x, float y, int s) { bx = x; by = y; size = s; bugColor = color(0,random(255),random(255),200); } void followBug(Bug b) { bugToFollow = b; } void goXgoY(int _x, int _y) { x = _x; y = _y; } void update(boolean isFood) { //if (bugToFollow != null) { //float x = bugToFollow.bx; //float y = bugToFollow.by; if (isFood) { if ( sqrt( sq(x-bx) + sq(y-by)) > MIN_DISTANCE ) { angle = atan2(x-bx,y-by); bx += MIN_DISTANCE * sin(angle)+random(-10,10)-random(-10,10); // why does sin work for xand cos for y? by += MIN_DISTANCE * cos(angle)+random(-10,10)-random(-10,10); // why cosine? bx += noise(bx); by += noise(by); } } else { x += random(-10,10); y += random(-10,10); if ( sqrt( sq(x-bx) + sq(y-by)) > MIN_DISTANCE ) { angle = atan2(x-bx,y-by); bx += MIN_DISTANCE * sin(angle); // why does sin work for xand cos for y? by += MIN_DISTANCE * cos(angle); // why cosine? bx += noise(bx); by += noise(by); } } } void display(){ fill(bugColor); pushMatrix(); // translate(bx,by); //ellipse(0,0,size,size); noStroke(); ellipseMode(CENTER); ellipse(bx,by,40,20); ellipse(bx,by-10,10,20); ellipse(bx,by+10,10,20); /////////////eye & tail, change direction // if (xDirection > 0 && yDirection > 0) if (sin(angle) > 0 && cos(angle) > 0) { fill(255); ellipse(bx+15 ,by-8,4,4); fill(255); ellipse(bx+15,by+8,4,4); fill(0); ellipse(bx+15 ,by-8,2,2); ellipse(bx+15 ,by+8,2,2); fill(bugColor); triangle(bx,by+10,bx-60,by+random(20)-random(20),bx,by-10); } else if (sin(angle) > 0 &&cos(angle)< 0) { fill(255); ellipse(bx+15 ,by-8,4,4); fill(255); ellipse(bx+15,by+8,4,4); fill(0); ellipse(bx+15 ,by-8,2,2); ellipse(bx+15 ,by+8,2,2); fill(bugColor); triangle(bx,by+10,bx-60,by+random(20)-random(20),bx,by-10); } else if (sin(angle) < 0 &&cos(angle) > 0) { fill(255); ellipse(bx-15 ,by-8,4,4); fill(255); ellipse(bx-15,by+8,4,4); fill(0); ellipse(bx-15 ,by-8,2,2); ellipse(bx-15 ,by+8,2,2); fill(bugColor); triangle(bx,by+10,bx+60, by+random(20)-random(20),bx,by-10); } else if (sin(angle) < 0 && cos(angle) < 0) { fill(255); ellipse(bx-15 ,by-8,4,4); fill(255); ellipse(bx-15,by+8,4,4); fill(0); ellipse(bx-15 ,by-8,2,2); ellipse(bx-15 ,by+8,2,2); fill(bugColor); triangle(bx,by+10,bx+60,by+random(20)-random(20),bx,by-10); } popMatrix(); } }