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 › cos() acting strange...
Page Index Toggle Pages: 1
cos() acting strange... (Read 437 times)
cos() acting strange...
Dec 21st, 2007, 5:45pm
 
this code is supposed to draw a line between two dots and tug them slightly towards each other, but for some reason the line "vx+=cos(a)*.01;" seems as though it's being run outside of the if() statement.  whenever I run the program all the dots accelerate to the right. switching it to "vx-=cos..." reverses the effect and changing "*.01" to "*0" nullifies it, but setting r to zero does not.  when I replace "vy+=sin..." with "vy+=cos..." the dots accelerate vertically. what's going on?

for(int j=0;j<dots.length;j++){
 if(dist(dots[j].x,dots[j].y,x,y)<=r){
   float a=atan2(dots[j].y-y,dots[j].x-x);
   vx+=cos(a)*.01;
   vy+=sin(a)*.01;
   stroke(255);
   line(dots[j].x,dots[j].y,x,y);
 }
}
Re: cos() acting strange...
Reply #1 - Dec 21st, 2007, 11:54pm
 
Is this snippet of code from a nested loop? where every dot is checking itself against every other dot?  If so, is it possible that each dot is also checking against *itself*?  In such a case dist would == 0, so your distance test would pass, but atan2(0,0) would return a constant 0 radians, accellerating each particle to the right every frame.  If there's an outer "i" loop to match that inner "j" loop, then just need to make sure that i!=j.
Re: cos() acting strange...
Reply #2 - Dec 23rd, 2007, 6:25pm
 
oh! of course!
well I feel silly. thanks for your help.
Page Index Toggle Pages: 1