top of page

BOUNCING TEXT

Modified version of the code found online - by el

String[] character = {"S","H","O","U","L","D"};
int tSize = 36;

ArrayList<Chara> charas = new ArrayList<Chara>();
int charaLim = character.length;
//int charaLim = 50;
int randoChara;

float spring = 0.1;
float gravity = 0.01;
float friction = -0.9;
int charaCounter;
boolean showLetters = true;

void setup() {
  size(500, 500);
  textSize(tSize);
  textAlign(CENTER);
  //frameRate(4);
  for (int i=1; i<charaLim+1; i++) {
    //randoChara = int(random(0, charaLim));
    randoChara = i-1;
    charas.add(new Chara(i, randoChara, width/(charaLim+1)*i, random(height/2), tSize));
  }
}

void draw() {
  background(0);
  for (int i = 0; i < charas.size(); i++) {
    Chara chara = charas.get(i);
    chara.collide();
    chara.move();
    chara.display();
  }
  if (mousePressed) {
    //addOne();
  }
  for (int i = charas.size(); i > 0; i--) {
    if (charas.size() > charaLim*2) {
      //deleteOne(1);
    }
  }
  //saveFrame("images0/######.tif");
}

void mousePressed() {
  addOne();
}
void addOne() {
  //randoChara = int(random(0, charaLim));
  //for (int i=1; i<charaLim+1; i++) {
  //  randoChara = charas.size()%i;
  //}
  if (charaCounter>=charaLim) {
    charaCounter=0;
  }
  charas.add(new Chara(charas.size()+1, charaCounter, mouseX, mouseY, tSize));
  charaCounter++;
}
void deleteOne(int delid) {
  charas.remove(delid-1);
  for (int i = delid-1; i<charas.size(); i++) {
    Chara chara = charas.get(i);
    chara.reOrder();
    //println("removed one, total number: " + charas.size());
  }
}

void keyPressed(){
  if (key == ' '){
    showLetters = !showLetters;
  }
}

class Chara {
  int id;
  int whichChara;
  float x, y;
  float diameter;
  float vx = random(-0.01);
  float vy = random(0.01);
  float xx = random (0, 500);
  float xS;
  int offSet = 250;
  float r;
  float rd = 1;

  Chara(int idint, int randoChara, float xin, float yin, float din) {
    id = idint; 
    whichChara = randoChara;
    x = xin;
    y = yin;
    diameter = din;
    r = random(width/4, width/2);
  }

  void collide() {
    for (int i = 0; i < charas.size(); i++) {
      Chara others = charas.get(i);
      if (others != this) {
        if (id == others.id+1) {
          //line(x, y, others.x, others.y);
        }        
        float dx = others.x - x;
        float dy = others.y - y;
        float distance = sqrt(dx*dx + dy*dy);
        float minDist = others.diameter/4 + diameter/4;
        if (distance < minDist*8) { 
          line(x, y, others.x, others.y);
        }
        if (distance < minDist) { 
          float angle = atan2(dy, dx);
          float targetX = x + cos(angle) * minDist;
          float targetY = y + sin(angle) * minDist;
          float ax = (targetX - others.x) * spring;
          float ay = (targetY - others.y) * spring;
          vx -= ax;
          vy -= ay;
          others.vx += ax;
          others.vy += ay;
          //fill(255, 0, 0);
          //ellipse(x, y, diameter, diameter);
          //deleteOne(others.id);
          //deleteOne(id);
          //addOne();
        }
      } else {
        fill(255);
      }
    }
  }  
  void move() {
    vy += gravity;
    x += vx;
    y += vy;
    if (x + diameter/2 > width) {
      x = width - diameter/2;
      vx *= friction;
    } else if (x - diameter/2 < 0) {
      x = diameter/2;
      vx *= friction;
    }
    if (y + diameter/2 > height) {
      y = height - diameter/2;
      vy *= friction;
    } else if (y - diameter/2 < 0) {
      y = diameter/2;
      vy *= friction;
    }
    //x=(sin(radians(xx))*r)+offSet;
    //y=(cos(radians(xx))*r)+offSet;
    //xx+=xS;
    //xS = r/100;
    ////r=r+rd;
    //if (r>=width/2 || r<=0) {
    //  rd*=-1;
    //}
  } 

  void display() {
    //fill(0);
    if (showLetters) {
      text(character[int(whichChara)], x, y+diameter/3);
    }
    //text(id, x, y+diameter/3);
    //ellipse(x,y,10,10);
    //noFill();  
    stroke(255);
    //strokeWeight(4);
    //ellipse(x, y, diameter, diameter);
  }
  void reOrder() {
    id-=1;
  }
}

bottom of page