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 › Worm that eats circles/ellipses
Page Index Toggle Pages: 1
Worm that eats circles/ellipses (Read 1616 times)
Worm that eats circles/ellipses
Jun 11th, 2010, 2:01am
 
[url]Hey everyone, I am just just starting with processing, my maths teacher suggested to create a small and basic animation so I could have a better mark.

I'm writing to you because I need some help, perhaps I'm lucky and you can help me.

My idea is to create a "worm" that floats and when I click, a circle appears (food) and it runs to the food and eats it, becoming bigger.

With this code I have the "worm" and when I click it goes to where I clicked, but I cannot manage to create a circle there and I really don't know how that circle/food can follow the other circles to be a larger worm...

this is my code:(sorry if my English isn't really good). Thank you in advance!![/url]
float[] centerX = new float[40];
float[] centerY = new float[40];

float[] auxX = new float[40];
float[] auxY = new float[40];

float stepX=1;
float stepY=2;


void setup() {
 size(400, 400);
 smooth();
 background(55);
  for(int i=0; i<40; i++)  
{
 centerX[i]=200;
 centerY[i]=300;
 
}

}

void draw()
{
background(55);
// centerX[0]=centerX[0]+step;
// centerY[0]=centerY[0]+step;
 
 
 
 for(int i=0; i<40; i++)  //copia
 {
 auxX[i]=centerX[i];
 auxY[i]=centerY[i];
 }
 
 centerX[0]=centerX[0]+stepX;  // actualiza la primera el.lipse
 centerY[0]=centerY[0]+stepY;
 
 for(int i=1; i<40; i++)  // actualiza la resta
 {
 centerX[i]=auxX[i-1];
 centerY[i]=auxY[i-1];
 }
 
 noFill();  
 strokeWeight(4);
 
 for(int i=0; i<40; i++)
{
 stroke(255,125,0,255-6*i);
 ellipse(centerX[i],centerY[i],150,100);
}
 
 if (centerX[0]>width | centerX[0]<0)
{
 stepX=-1*stepX;  // A cada nou rebot, step canvia de direccio com
// ho fan els rajos de llum  
}

if (centerY[0]>height | centerY[0]<0)
{
 stepY=-1*stepY;        
}
 
}

void mouseClicked()  
{
 stepX=(mouseX-centerX[0])/50;  // Observem que la velocitat varia
 stepY=(mouseY-centerY[0])/50; // amb la distancia del punt clickat.
Re: Worm that eats circles/ellipses
Reply #1 - Jun 11th, 2010, 2:23am
 
Hi, and welcome. Dunno if i got what you need but to achieve the effect you're after, one option could be to use some Vector math. This is the easiest (thanks to the Processing API) and more efficient way to 'move' stuff.
Nothing too difficult, have a look at these pages:
http://www.shiffman.net/teaching/nature/vectors/
http://www.processing.org/reference/PVector.html

What you basically need to do, is to simulate motion, wich one could thing as of three vectors: the velocity, the position and the acceleration vectors.

velocity = velocity + acceleration
position = position + velocity

You should subtract the object (worm) initial position to the target position and apply that as a new velocity. That will move the worm toward the circle. more or less.
something like

Code:

PVector velocity = PVector.sub(targetloc,worm_origin_loc);  


but others may suggest different approaches.
Cheers
GC
Page Index Toggle Pages: 1