I would be most grateful if anyone could offer a hint as to why my spiral class is not working.
Quote:Ball ball1;
Ball ball2;
Spiral spiral1;
void setup() {
size(1100,800);
smooth();
ball1 = new Ball(55);
ball2 = new Ball(175);
spiral1 = new Spiral(ball1.x, ball1.y, ball1.r);
}
void draw() {
background(50);
ball1.move();
ball2.move();
if (ball1.intersect(ball2)) {
ball1.highlight();
ball2.highlight();
}
spiral1.display();
ball1.display();
ball2.display();
spiral1.x = ball1.x;
spiral1.y = ball1.y;
}
class Ball {
int redc = 0;
float r;
float x,y;
float xspeed,yspeed;
color c = color(200,200);
Ball(float tempR) {
r = tempR;
x = random(width);
y = random(height);
xspeed = random(0,5);
yspeed = random(0,5);
}
void move() {
x += xspeed;
y += yspeed;
if (x > width || x < 0) {
xspeed *= -1;
}
if (y > height || y < 0) {
yspeed *= -1;
}
}
void highlight() {
c = color(redc,0,0,99);
redc = redc+1;
if (redc > 255) {
redc = redc -1;
}
}
void display() {
stroke(0);
strokeWeight(15);
fill(c);
ellipse(x,y,r*2,r*2);
c = color(0,150,0);
}
boolean intersect(Ball other) {
float distance = dist(x,y,other.x,other.y);
if (distance < r + other.r) {
return true;
} else {
return false;
}
}
}
class Spiral {
float r = 0;
float theta = 0;
float radiusLimit =0;
float x;
float y;
Spiral(float tempX,float tempY,float tempradiusLimit) {
x = tempX;
y = tempY;
tempradiusLimit = radiusLimit;
}
void display() {
if (r < radiusLimit) {
float x = r * cos(theta);
float y = r * sin(theta);
noStroke();
fill(0);
ellipse(x + width/2, y + height/2, 5, 5);
theta += 0.11;
r = r+.15;
stroke(255);
strokeWeight(2);
noFill();
ellipse(width/2,height/2,2*radiusLimit,2*radiusLimit);
}
}
}