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);
  for(int i=0; i<40; i++)  


void draw()
// centerX[0]=centerX[0]+step;
// centerY[0]=centerY[0]+step;
 for(int i=0; i<40; i++)  //copia
 centerX[0]=centerX[0]+stepX;  // actualiza la primera el.lipse
 for(int i=1; i<40; i++)  // actualiza la resta
 for(int i=0; i<40; i++)
 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)

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:

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


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

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