ok, i went back to the original
http://processing.org/learning/topics/follow3.html
which cleared a few things up.
given that you wanted 3 snakes it didn't make sense to have them all following the mouse, so i had 1 follow the mouse and 1 follow the bouncing ball thing you had going on.
i can't decide what finalX and finalY is for - it's not in the original - so i've ignored it 8)
have used String.toCharArray() to split the string into characters and changed segment() to take a single char
have taken out a lot of stuff - you were new() this and new() that all over the place which was a lot of extra work. you need to read up on that and on variable scope.
anyway:
Code:
/**
* Follow 3.
* Based on code from Keith Peters
*
* A segmented line follows the mouse. The relative angle from
* each segment to the next is calculated with atan2() and the
* position of the next is calculated with sin() and cos().
*/
PFont fontA;
// bouncing ball
float xpos, ypos; // Starting position of shape
float xspeed = 2.8; // Speed of the shape
float yspeed = 2.2; // Speed of the shape
int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom
int radius = 10; // renamed from size which clashes with size() method name
//String[] lines; // unused
Snake SnakeSN1;
Snake SnakeSN2;
void setup() {
size(1200, 400);
// lines aren't used
// lines = loadStrings("dialogos.txt");
fontA = loadFont("AmericanTypewriter-24.vlw");
textFont(fontA, 30);
smooth();
strokeWeight(5);
stroke(0, 100);
xpos = width/2;
ypos = height/2;
SnakeSN1 = new Snake(); // use default constructor
// SnakeSN1 = new Snake(0, (xpos + radius / 2), (ypos + radius / 2), "String1");
SnakeSN2 = new Snake(0, (xpos + radius / 2), (ypos + radius / 2), "String2"); // use other constructor
}
void draw() {
background(50);
// update bouncing ball
xpos = xpos + ( xspeed * xdirection );
ypos = ypos + ( yspeed * ydirection );
if (xpos > width - radius || xpos < 0) {
xdirection *= -1;
}
if (ypos > height - radius || ypos < 0) {
ydirection *= -1;
}
// show current bouncer position
ellipse(xpos, ypos, radius, radius);
// draw snakes
SnakeSN1.draw(mouseX, mouseY); // first snake follows mouse
SnakeSN2.draw(xpos, ypos); // second snake follows bouncing ball
}
class Snake {
int b; // unused
float xFinal; // don't know
float yFinal; // don't know
// String texto; // text - not needed - just use textoArray instead
// segment positions
float[] xPosition;
float[] yPosition;
int snLength;
float segLength = 15; // letter spacing
char[] textoArray; // letters
Snake() {
// pass defaults to 'main' constructor
this(0, mouseX, mouseY, "dopedope21");
}
// Contructor - all this is done only once
Snake(int id, float xMain, float yMain, String textIn) {
b = id;
xFinal = xMain; // don't know what these are for
yFinal = yMain;
snLength = textIn.length();
println("snLength: " + snLength);
// create positions arrays
xPosition = new float[snLength];
yPosition = new float[snLength];
// toCharArray will split string into characters
textoArray = textIn.toCharArray();
textoArray = reverse(textoArray);
}
// draw this snake
void draw(float xPos, float yPos){
// first segment follows passed in values (mouse or ball)
dragSegment(0, xPos, yPos, textoArray[0]);
// other segments follow the previous segment
for(int i = 1; i < snLength; i++) {
dragSegment(i, xPosition[i - 1], yPosition[i - 1], textoArray[i]);
}
ellipse (xFinal, yFinal, snLength, snLength); // ??? what is this? why snlength?
}
void dragSegment(int i, float xin, float yin, char textoLetra) {
// dx and dy are differences from the current piece to the passed in values
// (which are either the mouse, ball or the previous segment)
float dx = xin - xPosition[i];
float dy = yin - yPosition[i];
float angle = atan2(dy, dx);
xPosition[i] = xin - cos(angle) * segLength;
yPosition[i] = yin - sin(angle) * segLength;
segment(xPosition[i], yPosition[i], angle, textoLetra);
}
// draw a single letter
void segment(float x, float y, float a, char textoLetra) {
pushMatrix();
translate(x, y);
rotate(a);
line(0, 0, segLength, 0);
text(textoLetra, 0, 0);
popMatrix();
}
}