xxxxxxxxxx
String[] character = {"C","H","A","N","C","E"};
int tSize = 72;
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(1000, 600);
textSize(tSize);
textAlign(CENTER);
for (int i=1; i<charaLim+1; i++) {
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;
}
} 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;
}
}
void display() {
//fill(0);
if (showLetters) {
text(character[int(whichChara)], x, y+diameter/3);
}
stroke(255);
strokeWeight(4);
//ellipse(x, y, diameter, diameter);
}
void reOrder() {
id-=1;
}
}