/// class RythumBar begin //////////////////////////////////////////////////////////////////// // apply vector in the position of Rythumbar. class RythumBar{ float x,y,rw,rh; float rspeed; float speed; int rdirection; float tr; // target if rytum Funtion float oldHeading; float newHeading; float thea; float addthea; float ty; float m; // mass int id; // object id int coid; // collision id int coll; Bellipse barBg; Vector3D acc; Vector3D vec; // velocity Vector3D loc; // location Vector3D pause; RythumBar[] others; boolean isCollide; boolean doLine; float[] loc_x; //X temArray for the chasing Ball function float[] loc_y; //Y temArray for the chasing Ball function int chasingNum = 20; color chBall; ArcShape agepetal; RythumBar(float x_, float y_, float rw_, float rh_, int id_, RythumBar[] o_){ x = x_; rw = 100-rw_/2; rh = 100-rh_/2; y = y_; ty = y; id = id_; others = o_; speed = random(2,3); rspeed = 2; m = rh*0.5; //give the direction rdirection = 1; tr = rh; addthea = 0.15; color barColor = color(rv[id],gv[id],bv[id],70); chBall = barColor; barBg = new Bellipse(x, y, rw, rh, 1); float temVX = darkArray[id]*0.02 + softArray[id]*0.02 + random(-1,1); float temVY = locationArray[id]*0.02*(1-askFeelArray[id]); vec = new Vector3D( temVX, temVY);// loc = new Vector3D(x, y); acc = new Vector3D(0.5, 0); pause = new Vector3D(0,0); barBg.r = barColor; barBg.b = color(255,0); //chasingBall loc_x = new float[0]; loc_y = new float[0]; agepetal = new ArcShape(0, 0, 35, 255, 1); agepetal.arcFill = color(rv[id],gv[id],bv[id]); } void run(){ drawNodeline(others[coid].loc, loc ); for(int i=0; i newHeading){ thea = thea - addthea; } else if(thea > vec.heading2D()){ thea = vec.heading2D(); } if(oldHeading <= newHeading){ thea = thea + addthea; } else if(thea < vec.heading2D()){ thea = vec.heading2D(); } }// end of moveball function // wallcollision detector void wallDetect(){ if (loc.x > sw+rh/2 ){ loc.x = rh/2; vec.x *= 1; } else if ( loc.x < rh/2) { loc.x = sw+rh/2; vec.x *= 1; } if (loc.y > sh-150-rh/2){//-150 loc.y = sh-150-rh/2; vec.y *= -1; } else if( loc.y < rh/2) { loc.y = rh/2; vec.y *= -1; } }//end of walldetector //agePetal void agePetals(){ int num = ageArray[id]; float deg = TWO_PI/8; float cwv = 255/8; for(int i=0; i0){ float cv = 150/loc_x.length; switch(styleOfvisual){ case 1: if(genderArray[id] == 1){ loc_x[0] = sw/4; loc_y[0] = sh/2; } if(genderArray[id] == 2){ loc_x[0] = sw*0.75; loc_y[0] = sh/2; } for(int i=0; i< loc_x.length ; i++){ float dis = dist(loc_x[0], loc_y[0], loc_x[i], loc_y[i]); float dx = loc_x[i] - loc_x[0]; float dy = loc_y[i] - loc_y[0]; float thelta = atan2( dy, dx); stroke(chBall,cv*i); noFill(); ellipse(loc_x[0] + (dis/2)*cos(thelta), loc_y[0] + (dis/2)*sin(thelta), dis, dis ); ellipse(loc_x[0] - (dis/2)*cos(thelta), loc_y[0] - (dis/2)*sin(thelta), dis, dis ); } break; case 2: loc_x[0] = sw/2; loc_y[0] = sh/2; for(int i=1; i< loc_x.length ; i++){ beginShape(); fill(210,cv*i); vertex( loc_x[0], loc_y[0]); fill(chBall,cv*i); vertex( loc_x[i-1], loc_y[i-1]); vertex( loc_x[i], loc_y[i]); endShape(); beginShape(); noFill(); stroke(chBall,cv*i); vertex( loc_x[0], loc_y[0]); vertex( loc_x[i-1], loc_y[i-1]); vertex( loc_x[i], loc_y[i]); endShape(); } break; case 3: loc_x[0] = sw/2; loc_y[0] = sh/2; for(int i=1; i< loc_x.length ; i++){ jiglingLine(loc_x[0], loc_y[0],loc_x[i], loc_y[i]); float dis = dist( loc_x[0], loc_y[0],loc_x[i], loc_y[i])*2; stroke(chBall,cv*i); noFill(); ellipse(loc_x[0], loc_y[0], dis, dis); } break; case 4: web(); break; case 5: int wNum = 20; int hNum = count; float wlength = sw-80; float hHeight = 450; float gap = 1; float wUnit = (wlength-((wNum-1)*gap))/wNum; float hUnit = (hHeight-((hNum-1)*gap))/hNum; for(int i=0; i< loc_x.length ; i++){ fill(chBall,cv*i); noStroke(); rect(15+(wUnit+1)*i, 80+(hUnit+1)*id, wUnit, hUnit); if(i == loc_x.length-1){ rect(15+(wUnit+1)*(i+1), 80+(hUnit+1)*id, wlength-(wUnit*(i+1))-gap*i, hUnit); } } break; case 6: float cv_6 = 60/loc_x.length; for(int i=0; i< loc_x.length ; i++){ stroke(chBall); noFill(); line(sw/2, 0, loc_x[i], sh-250); line(loc_x[i], sh-250-ageArray[id]*15, loc_x[i], sh-250); rect(loc_x[i]-2.5, sh-250-ageArray[id]*15-2.5, 5, 5); } break; } } resetLocArray(); }//end of visual // add loc.x, loc.y into Array when the ball collide against others void addLoc(){ if(isCollide == true){ loc_x = append(loc_x, loc.x); loc_y = append(loc_y, loc.y); } }//end of add //reset locArray void resetLocArray(){ for(int i=0; i< others.length; i++){ mx = max(others[i].loc_x.length, others[id].loc_x.length); } if(mx > 20){ for(int i=0; i< others.length; i++){ others[i].loc_x = subset(others[i].loc_x, 0, 0); others[i].loc_y = subset(others[i].loc_y, 0, 0); mx =0; } styleOfvisual+=1; if(styleOfvisual > 6){ styleOfvisual =1; } } }//end of reset Array //jiglingLine() void jiglingLine(float stx, float sty, float tx, float ty){ int jNum = 30; float cv = 255/jNum; float[] xoff = new float[jNum]; float[] yoff = new float[jNum]; float[] x = new float[jNum]; float[] y = new float[jNum]; float xd = tx-stx ; float yd = ty-sty ; float dis = dist( stx , sty, tx , ty)/(jNum-1); float dg = atan2(yd,xd); for(int i=0; i= baseOflength){ doLine = false; } } //-- change position and velocity of ball after collision void collideAfter(Vector3D x1, Vector3D x2, Vector3D p1, Vector3D p2 ,float m1, float m2, int kk){ float dx = p1.x - p2.x; float dy = p1.y - p2.y; float dx2 = p2.x - p1.x; float dy2 = p2.y - p1.y; float d = sqrt(dx*dx+dy*dy); float r = others[kk].rh/2+rh/2+5; //locate the ball01 beside ball02 // component of velocity in the direction of (dx,dy) p1.x = p2.x + r*dx/d; p1.y = p2.y + r*dy/d; p2.x = p1.x + r*dx2/d; p2.y = p1.y + r*dy2/d; // find distance between two balls float collide_angle = atan2(dy,dx); // find magnitude of velocity float vec_mgnit01 = x1.magnitude(); float vec_mgnit02 = x2.magnitude(); //find angle of ball float vec_angle01 = x1.heading2D(); float vec_angle02 = x2.heading2D(); others[coid].oldHeading = x1.heading2D(); oldHeading = x2.heading2D(); // strat make normal vector which means turn the vecter direction be perpendicular to the line between two collided center. Vector3D newVec01 = new Vector3D(); newVec01.setXYZ(vec_mgnit01 * cos(vec_angle01 - collide_angle), vec_mgnit01 * sin(vec_angle01 - collide_angle),0); Vector3D newVec02 = new Vector3D(); newVec02.setXYZ(vec_mgnit02 * cos(vec_angle02 - collide_angle), vec_mgnit02 * sin(vec_angle02 - collide_angle),0); // conservation of momentum to get new vector Vector3D finalVec01 = new Vector3D(); finalVec01.setXYZ(((m1-m2) * newVec01.x + (m2+m2) * newVec02.x) / (m1+m2), newVec01.y,0); Vector3D finalVec02 = new Vector3D(); finalVec02.setXYZ(((m1+m1) * newVec01.x + (m2-m1) * newVec02.x) / (m1+m2), newVec02.y,0); // impliment new vetor into inital velocity of ball. x1.setXYZ(cos(collide_angle) * finalVec01.x + cos(collide_angle + HALF_PI)* finalVec01.y, sin(collide_angle) * finalVec01.x + sin(collide_angle + HALF_PI)* finalVec01.y,0); x2.setXYZ(cos(collide_angle) * finalVec02.x + cos(collide_angle + HALF_PI)* finalVec02.y, sin(collide_angle) * finalVec02.x + sin(collide_angle + HALF_PI)* finalVec02.y,0); others[coid].newHeading = x1.heading2D(); newHeading =x2.heading2D(); } // -- check the collision boolean collideCheck(Vector3D p01, Vector3D p02, Vector3D v01,Vector3D v02, int k){ float distanceLoc = Vector3D.distance(p01,p02); float r = others[k].rh/2 + rh/2; if( distanceLoc <= r ){ others[id].coid=k; doLine = true; return true; } else { return false; } } // return id int returnID(){ return id; } } /// class RythumBar begin ///////////////////////////////////////////////////////////////////////////////////////////////////////////