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 › broken() program
Page Index Toggle Pages: 1
broken(?) program (Read 577 times)
broken(?) program
Oct 2nd, 2005, 10:46am
 
This code is adapted from the Gravity Particles applet by Keith Peters.  It ran fine for a while, but when I changed "force = 100/distSQ" to 100*distSQ and tried to run it, the text window at the bottom of the editor went crazy and processing crashed.  Now it seems to do it no matter what for this one program.  I can run other programs I've written, but this one crashes it.  The text window displays a whole lot of messages, and I can't see most of them because the environment freezes, but many of them look like
"at java.lang.Thread.run(Unknown Source)" or
"at processing.core.PApplet.run(PApplet.java:951)" or
"at processing.core.PGraphics.requestDisplay(PGraphics.java:362)"
Those three messages have occurred a lot with only the number at the end changing.  Here's the code:

int numParticles = 100;
float[] positionX = new float[numParticles];
float[] positionY = new float[numParticles];
float[] velocityX = new float[numParticles];
float[] velocityY = new float[numParticles];
int w = 500;
int h = 500;
void setup() {
 size(w, h);
 smooth();
 strokeWeight(2);
 stroke(0);
 for (int i=0; i<numParticles; i++){
   positionX[i] = random(w);
   positionY[i] = random(h);
   velocityX[i] = 0;
   velocityY[i] = 0;
 }
}

void draw(){

 background(255);
 
 for (int i=0; i<numParticles; i++){
   float dx = mouseX - positionX[i];
   float dy = mouseY - positionY[i];
   float distSQ = dx*dx + dy*dy;
   float dist = sqrt(distSQ);
   float force = 100/distSQ;
   
   velocityX[i] += force * dx/dist;
   velocityY[i] += force * dy/dist;
   velocityX[i] = min(velocityX[i], 20);
   velocityX[i] = max(velocityX[i], -20);
   velocityY[i] = min(velocityY[i], 20);
   velocityY[i] = max(velocityY[i], -20);
   
   positionX[i] += velocityX[i];
   positionY[i] += velocityY[i];
   
   if(positionX[i] > w){
     positionX[i] = w;
     velocityX[i] *= -1;}
   if(positionX[i] < 0){
     positionX[i] = 0;
     velocityX[i] *= -1;}
   if(positionY[i] > h){
     positionY[i] = h;
     velocityY[i] *= -1;}
   if(positionY[i] < 0){
     positionY[i] = 0;
     velocityY[i] *= -1;}
   
   point(positionX[i], positionY[i]);
 }
}

The operation I changed is in line 30.  I had another question about this program before the glitch, though.  Is there a way to make the program more efficient?  I have a more than capable system, but this program lags sometimes. I can't remember how many particles I put into it but I remember thinking that it should have been able to handle more when it was completely bogged down.  So I'm wondering if there are simpler ways to make these calculations, or maybe a different way to structure the program.  Any ideas there would be helpful.  Thanks!
Re: broken(?) program
Reply #1 - Oct 2nd, 2005, 8:02pm
 
my guess would be that you're getting a division by zero. that whenever distSQ is zero, your program is gonna crash. and my guess is that the error being reported is just wrong (perhaps you're on osx?)
Page Index Toggle Pages: 1