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 › help with algebraic problem
Page Index Toggle Pages: 1
help with algebraic problem (Read 808 times)
help with algebraic problem
May 10th, 2009, 6:09pm
 
Im trying to figure out the best way to deal with this algebra problem. I have a class which is drawn on screen as a circle, these circles are perpetually moving. they have a smaller circle attached to them, tangentially. That I have covered no problem. The position of the smaller circle is updated on every draw loop by adding its vector to the bigger ones position and its radius.

What I want to do is have the smaller circle rotate around the bigger one to align itself with neighbouring circles' child circles...so that the direction of its vector is constantly updating itself in response to neighbours directions. The question is am I looking to average out the vectors or am I looking to average out the angles...not sure what the best route is....

any advice is really appreciated!
Re: help with algebraic problem
Reply #1 - May 10th, 2009, 6:37pm
 
let's treat the simple case of only two big circles, each with one child -- you'll then have to figure out how to extend from there as fits your purpose.

you'll see that for each big circle's child to "line up" with the other big circle's child, their centers need to lie on the line connecting the centers of the two big circles.

therefore the problem boils down to finding the angle between a big circle and it's neighbor, which is just atan2(y2-y1,x2-x1), then rotate it's child to that angle relative to the big circle's center.  assuming the other big circle does the same thing with it's child, then both children will be pointing towards each other when done.

(if you're already working with vectors, then don't actually DO the atan2 and rotate, that's the concept, but not the implementation -- instead just normalize the delta vector, scale by the sum of the radii, then add to big circle's origin, and that's the new origin for the child)

hth :)
Re: help with algebraic problem
Reply #2 - May 10th, 2009, 7:18pm
 
ok so the delta is the vector from one big circle to the next...if there are multiple big circles and I want the average of them, how would I get that? is it a simple case of adding them together and dividing by the number of these "neighbours"?
Re: help with algebraic problem
Reply #3 - May 10th, 2009, 9:54pm
 
it's not working, and I have no clue why...i have tried switching to the builtin PVector class but it didn't ork either...can you see anytihng wrong?

void updateNeighbours(){
  aqPos = new PVector(0,0,0);
 
 for (int i = 0; i < z.animals.size(); i++) {
 
 Animal a = (Animal)z.animals.get(i);
  float distance = distance(this.position.x, this.position.y, a.position.x, a.position.y);
   if(distance<100){
     PVector neighbour = new PVector(a.position.x, a.position.y, 0);
      aqPos.add(neighbour);
   
     aqPos.normalize();
     
     aqPos.mult(this.radius+aqRadius);
}


 }
Re: help with algebraic problem
Reply #4 - May 11th, 2009, 9:02am
 
onscreen and from the debugging I have been doing the vector resulting from the operations done on this vector are producing very similar vectors even though they should be different depnding on the vectors of each circles neighbours...I must be missing something very simple
Re: help with algebraic problem
Reply #5 - May 11th, 2009, 9:08am
 
the delta is "other big circle's center" minus "this big circle's center"
(that vector then points from this, to other)
then normalize that
then scale by "this big circle's radius" plus "this child's radius"
then add to "this big circle's center"
..giving "this child's center"

averaging vectors is just as with scalers -- add them all up, divide by count.  however, the resulting vector will likely not point at anything "interesting".  imagine 3 in a rougly equilateral triangle arrangement, they'll each point to the midpoint between the other two.  but what did you expect?  that's what i meant earlier about you having to figure out how to extend it - with only one child, but multiple neighbors, it's not clear what result you would expect.

hth :)
Re: help with algebraic problem
Reply #6 - May 11th, 2009, 10:34am
 
interesting - i thought adding (without dividing) gave a vector from an origin the cumulative vector...at least that's what it looks like from numerous examples on the web of high school geomtry. Im going to try subtraction now...

ok just did that and they are still all aiming in the same direction though it's different now that I have subtracted - this was the same problem before. What I should explain is that I have a gate distance to establish a neighbourhood radius and I am looking to get the average position within this locale - think of the Craig Reynold's boids algorithm... but for whatever reason all the vectors seem to be exactly the same. I wonder if it's not the math but something else with the code.

aqPos is the Vector in question which I have as a field for my Animal class. it gets updated every draw loop and is displayed on screen as an ellipse at its vector plus its parent vector...

Re: help with algebraic problem
Reply #7 - May 11th, 2009, 11:01am
 
i should also respond that yes given your example of three circles forming a triangle - I would want each one's vector that drives their child circle to be pointing to a common center...

here's my sketch if anyone is willing to comment - it must be something really obvious and small....look in the Animal tab for the area under question:

http://www.upsdn.ca/processing/animal_particles_07h/applet/Animal.pde

http://www.upsdn.ca/processing/animal_particles_07h/applet
Re: help with algebraic problem
Reply #8 - May 11th, 2009, 7:58pm
 
sorted it out - the positions I was using for each animal were not tied to the particle driving its motion. Now it works - thanks for the help
Page Index Toggle Pages: 1