I am going to try my best to describe what I am trying to make so I can hopefully receive some help. I am trying to make each character in a string accelerate towards the mouse. With the code I have currently written the entire string accelerates towards my mouse rather than each individual character moving independently towards the mouse.
Mover m;
void setup() {
size(1000,800);
m = new Mover();
}
void draw() {
background(0);
m.display();
}
void mouseMoved() {
m.move();
}
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
Mover() {
location = new PVector(random(100,400),random(100,300));
velocity = new PVector(0,0);
}
void display() {
PFont f = createFont("Helvetica", 50);
String s = "to be or\nnot to be.";
textFont(f);
textSize(50);
float x = 10;
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
text(c, x + location.x, location.y );
x = x + textWidth(c);
}
}
void move() {
//acceleration = PVector.random2D();
//acceleration.mult(random(5));
PVector mouse = new PVector(mouseX,mouseY);
PVector direction = PVector.sub(mouse,location);
acceleration = direction;
direction.normalize();
direction.mult(.01);
velocity.limit(2);
velocity.add(acceleration);
location.add(velocity);
}
}
Since you want to make each letter move separately you could make a class Letter with a char and a pos and a velocity and how many millis pass until it starts flying
Before setup have an array of that class
In setup fill the array in a for loop with your word (use charAt)
Any more explanation on how to set a timer for how much time to pass until it starts moving? or some psudo code for how you would approach it, not going to lie im still very new to processing.
like you said, i want the string to start as a word and then break off into each character (like this but i just added an array of a’s to demonstrate.
Also, to use a for loop in setup to make the array, you want to pass a char to the constructor.
Hence, you need to change your constructor in the class
Maybe you want to read the tutorial on objects
To reduce the class to be responsible not for the entire sentence, but for one letter only, I removed things from it and put it to setup.
Chrisir
This is the beginning of my full running sketch:
String s = "to be or \nnot to be.";
Mover[] m = new Mover[s.length()];
void setup() {
size(1000, 800);
PFont f = createFont("Helvetica", 50);
textFont(f);
textSize(50);
float x = 40;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
m[i] = new Mover(c, x, 200);
x = x + textWidth(c);
}
}
void draw() {
background(0);
for (int i = 0; i < s.length(); i++) {
....
....