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!