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 › Object Programming, where am I going wrong
Page Index Toggle Pages: 1
Object Programming, where am I going wrong? (Read 494 times)
Object Programming, where am I going wrong?
Jul 26th, 2009, 8:10pm
 
Hey, this is my first post. I've been learning processing over the last few days, giving myself a project to try and build in order to learn the language. This site has been great, thanks.

What I want to happen is, when the mouse is clicked, a circle is drawn around that point outward in a golden spiral. I've got this far.

Code:

Ball[] balls;
int numBalls = 50;
int currentBall = 0;

float phi = (sqrt(5)+1)/2; // Calculate Phi
float angle = 0.0; // Current angle

void setup () {
 size(500, 500);
 smooth();
 noStroke();
 balls = new Ball[numBalls]; // Create the array
 for (int i = 0; i < numBalls; i++) {
   balls[i] = new Ball(); // Create each ball
 }
}

void draw() {
 fill(0, 16);
 rect(0, 0, width, height);
 fill (255);
 for (int i = 0; i < numBalls; i++) {
   balls[i].display();
 }
}

// Click to create a new Ball
void mousePressed() {
 balls[currentBall].start(mouseX, mouseY);
 currentBall++;
 if (currentBall >= numBalls) {
   currentBall = 0;
 }
}

// Ball Class
class Ball {
 float sx, sy;
 float speed = 0.05;

 boolean on = false;
 
 void start(float xpos, float ypos) {
   sx = xpos;
   sy = ypos;
   on = true;
 }

 void display() {
   if (on == true) {
       float sinval = sin(angle);
       float cosval = cos(angle);
      angle += speed; //Update the angle
      float x = cosval * (pow(phi, (2/PI)*(angle)));
      float y = sinval * (pow(phi, (2/PI)*(angle)));
      ellipse(x + sx, y + sy, 4, 4); // Draw circle
     }
  }
}


So, what happens NOW is when the mouse is clicked, the ball will start to spiral from that point, but when the mouse is clicked again, the ball that appears is already in motion, matching the current balls path relative to where the mouse was just clicked!

If anyone understands what is wrong, please let me know. I will continue my research until I find an answer! I will learn this language!
Re: Object Programming, where am I going wrong?
Reply #1 - Jul 26th, 2009, 8:54pm
 
Because angle is global, each Ball is increasing the global value of angle. If you move angle into the ball class so that each object looks after it's own angle then it will get closer to what you are looking for.
Re: Object Programming, where am I going wrong?
Reply #2 - Jul 26th, 2009, 9:53pm
 
Success! Who knew something so simple was causing the problem. I also condensed the move() and display() methods. Thank you!
Next item: instead of a ball, what about a particle system that flows outward around wear the golden spiral would be? Maybe some OpenGl effects...hmm....

Thanks again.
Code:
Ball[] balls;
int numBalls = 50;
int currentBall = 0;
float phi = (sqrt(5)+1)/2; // Calculate Phi


void setup () {
 size(500, 500);
 smooth();
 noStroke();
 balls = new Ball[numBalls]; // Create the array
 for (int i = 0; i < numBalls; i++) {
   balls[i] = new Ball(); // Create each ball
 }
}

void draw() {
 fill(0, 5);
 rect(0, 0, width, height);
 fill (255);
 for (int i = 0; i < numBalls; i++) {
   balls[i].display();
 }
}

// Click to create a new Ball
void mousePressed() {
 balls[currentBall].start(mouseX, mouseY);
 currentBall++;
 if (currentBall >= numBalls) {
   currentBall = 0;
 }
}

// Ball Class
class Ball {
 float sx, sy;
 float speed = 0.05;
 boolean on = false;
 float x;
 float y;
 float angle = 0.0; // Current angle
 
 void start(float xpos, float ypos) {
   sx = xpos;
   sy = ypos;
   on = true;
 }
 
 void display() {
   if (on == true) {
     angle += speed; //Update the angle
     float sinval = sin(angle);
     float cosval = cos(angle);
     float x = cosval * (pow(phi, (2/PI)*(angle)));
     float y = sinval * (pow(phi, (2/PI)*(angle)));
     ellipse(x + sx, y + sy, 4, 4); // Draw circle
       if (x > width) {
        on = false;
        angle = 0;
      }
    }
  }
 

}

Page Index Toggle Pages: 1