Pachicito
YaBB Newbies
Offline
Posts: 6
Re: star nursery
Reply #2 - Jun 25th , 2009, 10:08am
Part 2:
Code: // Adjust the general velocity of the brightness if(totalBright == 0) { // Avoid divide by 0 if(firstFrame) { brightX[i][j] = i * (width/8) + (width/16); brightY[i][j] = j * (height/6) + (height/12); } } else { tempx = (tempx / totalBright + .5) * (width / 40.0); tempy = (tempy / totalBright + .5) * (height / 30.0); if(firstFrame) { brightX[i][j] = tempx; brightY[i][j] = tempy; } else { brightX[i][j] += brightVelX[i][j]; brightY[i][j] += brightVelY[i][j]; brightVelX[i][j] = ((tempx - brightX[i][j]) - brightVelX[i][j]) * .2; brightVelY[i][j] = ((tempy - brightY[i][j]) - brightVelY[i][j]) * .2; } } } } } // // // Star // // // class Star { float x, y, xv, yv; float diameter, inside, minD, maxD; int id, age; boolean connected[] = new boolean[maxStars]; float red, green, blue; Star(int iid, float ix, float iy, float ixv, float iyv) { x = ix; y = iy; xv = ixv; yv = iyv; id = iid; age = (int)random(500); minD = 20; maxD = 60; } void update() { // Decay the inside diameter if(inside > 0) inside -= 1; // React to video input if(x >= 0 && x < width && y >= 0 && y < height) { color tempPixel = video.pixels[(int)x + (int)y*width]; // Diameter grows with brightness float multiplier = 255.0 / (brightest - darkest); float bright = constrain(((brightness(tempPixel) - darkest) * multiplier) / 255, 0, 1); float sizeMult = sin(age * .05) * .4 + .6; diameter += ( (((1 - bright) * (maxD - minD) + minD) * sizeMult) - diameter ) * .2; // Influence by movement of general brightness xv += brightVelX[(int)floor(x / (width/8.0))][(int)floor(y / (height/6.0))] * 1; yv += brightVelY[(int)floor(x / (width/8.0))][(int)floor(y / (height/6.0))] * 1; } // Check for new connections for(int i=0; i < nStars; i++) { if(i != id) {// && touching < 1 && stars[i].touching < 1) { // If it's not me float xd = stars[i].x - x; float yd = stars[i].y - y; float diff = xd*xd + yd*yd; float radii = stars[i].diameter/2 + diameter/2; // If touching if(diff < radii*radii) { springAdd(stars[i].x, stars[i].y, radii); // Assimilate Velocity xv += (stars[i].xv - xv) * .01; yv += (stars[i].yv - yv) * .01; // Color changes with distance float dist = sqrt(diff); if(dist < 30) { stroke(255, ((30 - dist) * 6) * (min(inside, stars[i].inside) / 60)); line(x, y, stars[i].x, stars[i].y); } if(!connected[i]) { inside += diameter / (maxD / 5); connected[i] = true; } } else { connected[i] = false; } } } inside = constrain(inside, 0, 60); } void display() { ellipseMode(CENTER_DIAMETER); if(keyPressed && key == '`') { ellipse(x, y, diameter, diameter); } if(inside > 1) { imageMode(CENTER_DIAMETER); tint(255, (inside-4)*64); pushMatrix(); translate(0,0,-1); image(basicStar, x, y, inside, inside); popMatrix(); } //line(x, y, x+xv, y+yv); x += xv; y += yv; // Constrain to screen float d2 = diameter/2; if(x < d2) { x = d2; } if(x > width - d2) { x = width - d2; } if(y < d2) { y = d2; } if(y > height - d2) { y = height - d2; } xv *= .8; yv *= .8; age++; } float dx, dy, mag, ext; void springAdd(float sx, float sy, float rest) { dx = sx - x; dy = sy - y; if(dx != 0 || dy != 0) { // Prevent / by zero mag = sqrt(dx*dx + dy*dy); ext = mag - rest; xv += (dx / mag * ext) * .1; yv += (dy / mag * ext) * .1; } } } void brightToAlpha(PImage b) { b.format = ARGB; for(int i=0; i < b.pixels.length; i++) { b.pixels[i] = color(255,255,255,brightness(b.pixels[i])); } }