// The particle class controls all movements & transformation class Particle { float x, y, dx, dy, tx, ty, px, py, speed, tangle; float angle = random(-0.3, 0.3); float terrRadius = random(20, 80); // territory radius, used for backOff() float initSize, typeSize, tSize; // tSize = target size float maxSize, charSpace; // sets spacing for displayed text float maxSizeScalar = 4.0; // sets the maximum size float resizeSpd = 0.05; boolean inString = false; // set if the character is used to display text Particle(float xpos, float ypos, float spd, float s) { x = xpos; y = ypos; tx = xpos; ty = ypos; px = xpos; py = ypos; speed = spd; initSize = s; tSize = initSize; maxSize = initSize*maxSizeScalar; charSpace = initSize; } void update() { dx = mouseX - x; dy = mouseY - y; float d = dist(x, y, mouseX, mouseY); // back off when mouse comes too close if (d < terrRadius) { backOff(); } else { // idle movement: move sideways to cursor, random vertical movement idleMovement(); } // attack if (mousePressed) { attack(); } // update size if (abs(typeSize-tSize)>0.1) { typeSize += (tSize - typeSize)*resizeSpd; } // store previous position px = x; py = y; } void reset(float x, float y, int fs, float as){ float angle = random(-TWO_PI, TWO_PI); float rx = random(fs/2); float ry = random(fs/3); speed = random(as)+1.0; tx = x+rx*cos(angle); ty = y+ry*sin(angle); inString = false; decSize(); } void backOff(){ angle = (atan2(dy, dx)+PI); x += cos(angle) * speed * random(1, 2); y += sin(angle) * speed * random(1, 2); } void idleMovement(){ // slow movement towards mouseX if (!inString) { tx += (mouseX - tx)*0.001+random(-3, 3); ty += (mouseY - ty)*0.001+random(-3, 3); // random movement tx += random(-1, 1)*0.5; ty += random(-1, 1)*0.2; } else { // random movement tx += random(-1, 1); ty += random(-1, 1); } x += (tx - x)*speed*0.01; y += (ty - y)*speed*0.01; } void attack(){ tangle = atan2(dy, dx); angle += (tangle-angle)*0.8; x += cos(angle) * speed * random(1, 6); y += sin(angle) * speed * random(1, 6); tSize = initSize; } void setTargetPos(int c, int s) { int charNum = c; int stringLength = s; tx = width/2 - stringLength*charSpace/2 + charSpace*c; ty = height/2 - tSize*2; } void incSize() { if (tSize < maxSize) { tSize += maxSize-initSize; charSpace = tSize/1.5; } } void decSize() { if (typeSize > initSize) { tSize = initSize; charSpace = initSize; } } void setInString() { inString = true; } }