Hello,
I try to move the particles from the shape of a String to the next one…
I guess I should remove a part of the particles and create new ones for the new String but I don’t manage to figure out how?!
Thank you very much for your help in advance.
Best wishes,
L
<!--
String[] wordsList= new String [4];
PGraphics pg;
Particle[]particles;
PFont pf;
int index=0; // for wordsList
float x = 0;
float y = 0;
int lastWordTime; // for timer
int tileGap =5;
int tileSize = 5;
int gridHorizontal = 150;
int gridVertical = 150;
int particleNum = 0;
color textCol = color(0);
int nbParts = 0;
int nbPartsPerLetter = 200;
float startTime = 0;
float timeT = 0;
void setup() {
size(500, 500);
startTime = millis();
// frameRate(15);
background(0);
pf = createFont("ARIAL.TTF", 100);
wordsList[0] =("Hello");
wordsList[1] = ("You");
wordsList[2] = ("There");
wordsList[3] = ("Here");
nextString(wordsList[index]);
}
void draw() {
background(0);
translate(10, 100);
timeT= millis()-startTime;
for (int i= 0; i<nbParts; i++) {
particles[i].display();
particles[i].update();
}
if (millis()-startTime>2000) {
index++;
nextString(wordsList[index%4]);
startTime = millis();
}
if (index>=wordsList.length) {
index = 0;
}
}//func
void nextString(String word) {
nbParts = nbPartsPerLetter*word.length();
particles = new Particle [nbParts];
pg = createGraphics(width, height);
pg.beginDraw();
pg.textSize(140);
pg.textLeading(100);
pg.textAlign(LEFT, TOP);
pg.fill(textCol);
pg.text(word, 100, 100);
pg.endDraw();
pg.loadPixels();
for (int i = 0; i<nbParts; i++) {
particles[i] = new Particle();
int particleCount = particles.length;
int particleIndex = 0;
if (pg.pixels[int(width*y+x)] !=0) {
Particle newParticle;
if (particleIndex<particleCount) {
newParticle = particles[i];
particleIndex+=1;
} else {
newParticle = new Particle();
newParticle.pos.x = random(width);
newParticle.pos.y = random(height);
//particles = (Particle[])append(particles, newParticle);
}
newParticle.target.x = x;
newParticle.target.y = y;
}
}
//pg.updatePixels();
}
/*void mousePressed() {
if (mouseButton == LEFT) {
index += 1;
if (index>=wordsList.length) {
index = 0;
}
nextString(wordsList[index]);
}
}*/
class Particle {
PVector pos = new PVector(0, 0);
PVector origin = new PVector(0, 0);
PVector target = new PVector(0, 0);
PVector acc = new PVector(0, 0);
PVector vel = new PVector(0, 0);
boolean found = false;
color col = color(255);
float rad = random(10);
float newX, newY;
int MARGIN = 0;
float maxForce = 0.1;
float maxSpeed = 4;
float closeEnough = 50;
Particle() {
while (!found) {
int x= (int)random(width);
int y = (int) random(height);
if (pg.pixels[y*width+x]==textCol) {
pos = new PVector(x, y);
origin = pos;
found = true;
}
}
}
void update() {
float proximityMult =1.0;
float distance = dist(this.pos.x, this.pos.y, this.target.x, this.target.y);
if (distance < closeEnough) {
proximityMult = distance/closeEnough;
}
// Add force towards target
PVector toTarget = new PVector(this.target.x, this.target.y);
toTarget.sub(this.pos);
toTarget.normalize();
toTarget.mult(this.maxSpeed*proximityMult);
// Calculate steer force
PVector steer= new PVector(toTarget.x, toTarget.y);
steer.sub(this.vel);
steer.normalize();
steer.mult(this.maxForce);
acc.add(steer); //Add steer force to acc
//Move particle;
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
/*pos.x+=noise(pos.x*cos(TWO_PI)*random(-10, 10))*random(-15, 15);
pos.y+=noise(pos.y*sin(TWO_PI)*random(-10, 10))*random(-15, 15);*/
}
void home() {
pos.x = lerp(pos.x, x, 0.05);
pos.y = lerp(pos.y, y, 0.05);
}
void display() {
pos.add(acc);
if (pos.x < MARGIN) {
pos.x = MARGIN;
acc.x*=-1;
}
if (pos.x > width-MARGIN) {
pos.x = width-MARGIN;
acc.x*=-1;
}
if (pos.y < MARGIN) {
pos.y = MARGIN;
acc.y*=-1;
}
if (pos.y > height-MARGIN) {
pos.y = height-MARGIN;
acc.y*=-1;
}
fill(col);
ellipse(this.pos.x, this.pos.y, this.rad, this.rad);
}
}
-->