unexpected token I cannot see!!
in
Programming Questions
•
1 year ago
Can someone please have a quick glance at this code and perhaps ID why i'm getting an error in my bounce function.
I really cant see whats causing it,
I really cant see whats causing it,
Thanks
float gravity = .9;
int radiuz = 20;
ArrayList balls;
void setup() {
size (800, 450);
smooth();
ellipseMode(RADIUS);
background(30);
PVector posX;
balls = new ArrayList();
balls.add(new Ball(new PVector(random(10),random(10)), new PVector(random(10),random(10)), radiuz, int(random(255)), int(random(255)), int(random(255))));
}
void draw () {
background(30);
for (int i = balls.size()-1; i>= 0; i--) {
Ball b = (Ball) balls.get(i);
b.Udpate();
}
for(int i=1;i<balls.size();i++){
Ball A = (Ball) balls.get(i);
for(int j=0;j<i;j++){
Ball B = (Ball) balls.get(j);
bounce(ballA,ballB);
}
}
}
void mousePressed() {
balls.add(new Ball(new PVector(random(10),random(10)), new PVector(random(10),random(10)), 20, int(random(255)), int(random(255)), int(random(255))));
}
void bounce(Ball ballA, Ball ballB) {
PVector ab = new PVector();
ab.set(ballA.position);
ab.sub(ballB.position);
ab.normalize();
while(ballA.position.dist(ballB.position) < ballA.radiuz + ballB.radiuz) {
ballA.position.add.(ab); //THIS LINE IS CAUSING AN ERROR
}
}
class Ball {
PVector direction = new PVector();
PVector position = new PVector();
float r;
int R, G, B;
Ball (PVector tempposition, PVector tempdirection, int radios, int tempR, int tempG, int tempB){
R=tempR;
G=tempG;
B=tempB;
r=radios;
direction = tempdirection;
position = tempposition;
}
void Udpate(){
direction.y = direction.y + gravity;
position.x += direction.x;
position.y += direction.y;
fill (R, G, B);
ellipse (position.x, position.y, 2*r, 2*r);
// here is where the balls bounce off the four walls
if (position.x >= width - r) { //right wall
direction.x = -.9*abs(direction.x);
}
if (position.x <= 0 + r) { // left wall
direction.x = .9*abs(direction.x);
}
if (position.y > height - r) {direction.y *= -.7; // bottom
if (abs(direction.y)<1) {direction.y=0; }
position.y = height - r;
}
}
}
1