We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › arrays, a tail of rectangles following your mouse
Page Index Toggle Pages: 1
arrays, a tail of rectangles following your mouse (Read 420 times)
arrays, a tail of rectangles following your mouse
Dec 3rd, 2007, 6:54pm
 
i need some help for a school-excercise:

we need to make 256 rectangles, following your mouseposition.

so i guess x[0] needs to be mouseX, and x[1] pmouseX.

but i can't find out how to solve it for 254 more... something like x[i]=x[i-1]?
so one rectangle gets the coordinates of the one before?
Re: arrays, a tail of rectangles following your mo
Reply #1 - Dec 3rd, 2007, 7:52pm
 
hi. an easy way would be to draw a rect at mouse position every frame, and overpaint it with a half-transparent background. That way you get a fading out trail.

if you stick with the arrays[], you basically need to add the current mouse position to the list and delete the oldest value. have a look at the append() and shorten() methods for arrays. Then, also in every frame, loop through the position arrays and draw the rects. There might be more elegant ways to do it, but this seems straightforward, especially if you're a beginner.
Re: arrays, a tail of rectangles following your mo
Reply #2 - Dec 4th, 2007, 4:22am
 
smuuf,

below's an example of a spring-based follow-the-leader approach to what your asking about. (Although, the example is not that simple.)
PLEASE don't hand this in as your assignment (I may know your instructor Wink)


Code:

/* Worm
demonstrates springs in a series */

// for worm
int segments = 60;
float[] x = new float[segments];
float[] y = new float[segments];
float[] accelX = new float[segments];
float[] accelY = new float[segments];
float[] springing = new float[segments];
float[] damping = new float[segments];

// for food
float fx, fy;
float fCntrX, fCntrY;
float fAngle, fSpeedX = .25, fSpeedY = .5;

void setup(){
size(400, 400);
smooth();
// initialize array values
for (int i=0; i<segments; i++){
/* need to decrease both springing and
damping values as segments increase */
springing[i] = .05*(.07*(i+1));
damping[i] = .95-(.02*i);
}

// food center
fCntrX = width/2;
fCntrY = height/2;
}

void draw(){
// repaint background
fill(0, 10);
noStroke();
rect(0, 0, width, height);

createFood();
createWorm();
}

void createFood(){
// food moves in random wave pattern
fx = fCntrX + cos(radians(fAngle))*random(25);
fy = fCntrY + sin(radians(fAngle))*random(25);

fCntrX+=fSpeedX;
fCntrY+=fSpeedY;

fAngle+=random(-6, 6);

// keep food within display window
if (fCntrX>width-15 || fCntrX<15 ){
fSpeedX*=-1;
}
if (fCntrY>height-15 || fCntrY<15 ){
fSpeedY*=-1;
}
}

void createWorm(){
float[] deltaX = new float[segments];
float[] deltaY = new float[segments];

for (int i=0; i<segments; i++){
// lead ellipse
if (i==0){

/* food position used to calculate the
initial ellipse of the worm */
deltaX[i] = (fx-x[i]);
deltaY[i] = (fy-y[i]);
}
else {
deltaX[i] = (x[i-1]-x[i]);
deltaY[i] = (y[i-1]-y[i]);
}

// create springing effect
deltaX[i] *= springing[i];
deltaY[i] *= springing[i];

accelX[i] += deltaX[i];
accelY[i] += deltaY[i];

// move worm
x[i] += accelX[i];
y[i] += accelY[i];

fill(0);
stroke(255);
// draw worm
if (i<segments/2){
ellipse(x[i], y[i], i, i);
}
else {
ellipse(x[i], y[i], segments-i, segments-i);
}
// slow down springing
accelX[i] *= damping[i];
accelY[i] *= damping[i];
}
}
Page Index Toggle Pages: 1