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 & HelpSyntax Questions › how to get average position of item in array
Page Index Toggle Pages: 1
how to get average position of item in array (Read 781 times)
how to get average position of item in array
Sep 1st, 2008, 4:15am
 
Hi

I am messing around with the boids example provided by Daniel Shiffman - and I am trying to render visible the vector that is referred to as the "cohesion" vector which I am understanding to be the average position of all Boids in the array. I would like to put an X or a + there...but I am not sure how to either call that vector or alternatively average all xpos and ypos of the boids in the array and place this graphic element at that position.

help? thanks!
Re: how to get average position of item in array
Reply #1 - Sep 1st, 2008, 3:05pm
 
All you need to do is pass that function the array of Boid objects and then use the resulting vector as the location to draw something.

Code:

Vector3D centroid = cohesion(boids);
ellipse(centroid.x,centroid.y,10,10);


You may want to pull the cohesion function out of the Boid class itself depending on exactly how you are implementing this.  The example is rather inefficient as it calculates the centroid multiple times for each boid (where it could really just do it once since the center doesn't change across all the boids.)  


Re: how to get average position of item in array
Reply #2 - Sep 2nd, 2008, 7:23pm
 
thanks for responding. I am just starting with processing so some of the obvious things like where to place the code you psted is over my head. I tried placing it in the void flock(ArrayList boids) {...function - I noticed there was already a vector there like your "centroid" -
Vector3D coh = cohesion(boids);   // Cohesion

when I tried drawing that ellipse using it, it drew it at 0,0...what am I missing? I tried also placing the code in the main draw function but that didnt work either.

As for your suggestion about a more efficient way to handle processing the vectors for the array, I would like to know more...If I were to bring this "rule" out of the boid class where would it go? its own class?
Re: how to get average position of item in array
Reply #3 - Sep 8th, 2008, 3:02am
 
That vector that comes out of the cohesion function is not the centroid, it's the steering vector towards the centroid (an acceleration for the boid).  The centroid ends up in the variable called "sum" (in the cohesion() function) I believe.

Also, if you move this function out of the Boid class (which makes a lot of sense), the best location is probably the Flock class, i.e. the Flock would have the ability to calculate the center of all of its members.
Re: how to get average position of item in array
Reply #4 - Sep 9th, 2008, 8:16pm
 
OK. I hate to be a nuisance but I am still having trouble figuring this one out...

I found the variable in the cohesion function but I don't know how to access it from outside the function.

and I was playing around with moving the function over to the Flock class and it inevitably made sense to move all the steering functions over and somewhere in the process I got confused about how to get the things going again...I really need to learn OOP properly I guess...
Page Index Toggle Pages: 1