//------------------------------------------------------------ // Metaball Class //------------------------------------------------------------ class MetaSystem { int W = 640; int H = 480; int METABALLS_N = 1; // Total numbers of Metaballs float ISOVALUE = 12.0f; // seems to affect size of metaball, lower value increases blobiness float Viscosity = 18.0; float Viscosity_min = 1.0; float Viscosity_max = 20.0; // Try 100 ;-) float dViscosity = (Viscosity_max - Viscosity_min)/250.0; int SQUARES_NX = 64; // seems to affect decay rate int SQUARES_NY = 64; int RES = 640/SQUARES_NX ; // RES_X & RES_Y , dimensions where grid is calculated int GRIDSIZE_X = SQUARES_NX+1; int GRIDSIZE_Y = SQUARES_NY+1; float[][] MetaGrid = new float[GRIDSIZE_X][GRIDSIZE_Y]; float MetaGridTrgt = 0; float Meta_Nb = METABALLS_N; //Metaball[] MetaB = new Metaball[METABALLS_N]; boolean switch1 = true; Metaball MetaB; ArrayList ballZ; color METACOLOR, METACENTERCOLOR, GRIDCOLOR; int[][] squareEdge = { { 0,1 } , { 1,2 } , { 2,3 } , { 3,0 } }; int[][] offset = { { 0,0 } , { 1,0 } , { 1,1 } , { 0,1 } }; int[][] Line = { { -1, -1, -1, -1, -1 } , { 0, 3, -1, -1, -1 } , { 0, 1, -1, -1, -1 } , { 3, 1, -1, -1, -1 } , { 1, 2, -1, -1, -1 } , { 1, 2, 0, 3, -1 } , { 0, 2, -1, -1, -1 } , { 3, 2, -1, -1, -1 } , { 3, 2, -1, -1, -1 } , { 0, 2, -1, -1, -1 } , { 3, 2, 0, 2, -1 } , { 1, 2, -1, -1, -1 } , { 3, 1, -1, -1, -1 } , { 0, 1, -1, -1, -1 } , { 0, 3, -1, -1, -1 } , { -1, -1, -1, -1, -1 } }; int[][] squareFlag = new int[GRIDSIZE_X][GRIDSIZE_Y]; int square_idx; float isoP1_x, isoP1_y, isoP2_x, isoP2_y; int P1_idx, P2_idx; float Val1, Val2, temp; float temp_, isoP1_x_, isoP1_y_, isoP2_x_, isoP2_y_; int P1_idx_, P2_idx_, i_, j_, square_idx_; float dVal, dIso; float prevX = 0.0f; float prevY = 0.0f; float xoff = 0.0f; float xoff2 = 0.0f; /* Vector3D a; Vector3D v; Vector3D l; */ Vector3D a = new Vector3D(0.0,0.0); Vector3D v = new Vector3D(0.0,0.0); Vector3D l = new Vector3D(0.0,0.0); int num; //METACOLOR = color(175,175,175); MetaSystem() // num corresponds to the # of Metaballs { METACOLOR = color(200,0,0); METACENTERCOLOR = color(200,0,0); GRIDCOLOR = color(220,220,220); ballZ = new ArrayList(); // array List for balls, each ball will have it's own a, v, & l ballZ.ensureCapacity(5); //METABALLS_N = _num; //Meta_Nb = METABALLS_N; int num = 4; //a = _a.copy(); //v = _v.copy(); //l = _l.copy(); for (int i=0; i 300) { mb.remove(0); } */ void makeBalls() { for (int il = ballZ.size()-1; il >= 0; il--) { Metaball mb = (Metaball) ballZ.get(il); for (int y=0; y= 0; i--) { float nx = noise(xoff) * width; float ny = noise(xoff2) * height; //Metaball mb = (Metaball) ballZ.get(i); mb.update(); mb.setCenterTo(l.x,l.y); // the l position could be problematic mb.setStrength(30000); if (mousePressed && mouseY <= 480 && mouseX <= 639) { //Vector3D a = new Vector3D(0.0,0.125); //Vector3D v = new Vector3D(0.0,0.0); //Vector3D l = new Vector3D(width/2,2); //MetaB.setCenterTo(mouseX,mouseY); // Compute difference vector between mouse and object location // 3 steps -- (1) Get Mouse Location, (2) Get Difference Vector, (3) Normalize difference vector Vector3D m = new Vector3D(mouseX,mouseY); Vector3D diff = Vector3D.sub(m,mb.getLoc()); diff.normalize(); float factor = 1.3; // Magnitude of Acceleration (not increasing it right now) diff.mult(factor); //object accelerates towards mouse mb.setAcc(diff); //MetaB.setCenterTo(l.x,l.y); //prevX = l.x; //prevY = l.y; //println("diff.x - "+ diff.x); //println("diff.y - "+ diff.y); } else { //MetaB.setAcc(new Vector3D(0,0)); xoff += .01; xoff2 += .01; // Set this to perlin noise position Vector3D lm = new Vector3D(nx, ny); Vector3D diff2 = Vector3D.sub(lm,mb.getLoc()); diff2.normalize(); mb.setAcc(diff2); //mb.setCenterTo(l.x,l.y); } if (keyPressed) { switch (key) { case '1' : creation(); //Meta_Nb = METABALLS_N - 3; //ballZ.set(0,new Metaball(a, v, l)); //Vector3D l2 = new Vector3D(0.0,200.0); //ballZ.set(0, new Metaball(a, v, l)); //Metaball mb_1 = (Metaball) ballZ.get(0); //mb_1.setCenterTo(100, 200); break; case '2' : //Meta_Nb = METABALLS_N - 2; //ballZ.add(1,new Metaball(a, v, l)); Metaball mb_2 = (Metaball) ballZ.get(1); //mb_2.setCenterTo(120, 320); break; case '3' : Vector3D a3 = new Vector3D(0.0,0.0); Vector3D v3 = new Vector3D(0.0,0.0); Vector3D l3 = new Vector3D(0.0,120); //if (key == '3' && switch1 != false) ballZ.set(0,new Metaball(a, v, l3)); //Meta_Nb = METABALLS_N - 1; Metaball mb_3 = (Metaball) ballZ.get(0); //ballZ.trimToSize(); //mb_3.setCenterTo(100, 400); //switch1 = false; break; case '4' : //Meta_Nb = METABALLS_N; ballZ.set(0,new Metaball(a, v, l)); Metaball mb_4 = (Metaball) ballZ.get(0); //mb_4.setCenterTo(50, 400); break; case '5' : //Meta_Nb = METABALLS_N; Metaball mb_5 = (Metaball) ballZ.get(4); mb_5.setCenterTo(320, 300); break; } } println("num - "+ num); println("ballZ Size - "+ ballZ.size()); } } boolean creation() { //Vector3D a3 = new Vector3D(0.0,0.0); //Vector3D v3 = new Vector3D(0.0,0.0); Vector3D l3 = new Vector3D(width,random(0,height)); ballZ.set(0,new Metaball(a, v, l3)); Metaball mb_3 = (Metaball) ballZ.get(0); return true; } // A method to test if the particle system still has particles boolean dead() { if (ballZ.isEmpty()) { return true; } else { return false; } } /* void keyPressed() { switch (key) { case '4' : //ballZ.add(3,new Metaball(a, v, l)); Meta_Nb = METABALLS_N; break; case '3' : //ballZ.add(new Metaball(a, v, l)); //Meta_Nb = METABALLS_N - 3; Metaball mb_3 = (Metaball) ballZ.get(1); mb_3.setCenterTo(200, 200); //switch1 = false; break; case '2' : ballZ.add(3,new Metaball(a, v, l)); //Meta_Nb = METABALLS_N - 2; break; case '1' : ballZ.add(3,new Metaball(a, v, l)); Meta_Nb = METABALLS_N - 3; break; } } */ //------------------------------------------------------------ // drawMetaCenter() //------------------------------------------------------------ /* void drawMetaCenter() { int i,j; for (int n=0; n=0 && MetaB.center_x <256 && MetaB.center_y >=0 && MetaB.center_y <256) { i = MetaB.getSquareCoords_i(RES, RES); j = MetaB.getSquareCoords_j(RES, RES); stroke(red(METACENTERCOLOR), green(METACENTERCOLOR), blue(METACENTERCOLOR)); line( (int) (Math_Clip((i*RES),0,255)), (int) (Math_Clip((j*RES),0,255)), (int) (Math_Clip(((i+1)*RES),0,255)), (int) (Math_Clip(((j+1)*RES),0,255)) ); line( (int) (Math_Clip(((i+1)*RES),0,255)), (int) (Math_Clip((j*RES),0,255)), (int) (Math_Clip((i*RES),0,255)), (int) (Math_Clip(((j+1)*RES),0,255)) ); } } } */ //------------------------------------------------------------ // Math_Clip() //------------------------------------------------------------ float Math_Clip(float val, float lo, float hi) { if (valhi) return hi; return val; } int Math_Clip(int val, int lo, int hi) { if (valhi) return hi; return val; } }