code help: unexpected token: (
in
Programming Questions
•
1 year ago
hi, i am kind of novice in processing..
i want a code help, what i am trying to do is create a 'scene' that has several 'balls' moving around bouncing off one another. Ideally this scene will also incorporate several other squares and triangles, with one hexagon repelling them all. But for now, I'm stuck on just the balls.
So far my code is in tabs, and I've coded:
(Bouncy Ball 2 tab)
import processing.opengl.*;
ArrayList bouncyBalls;
void setup() {
size (450, 900, OPENGL);
hint(ENABLE_OPENGL_4X_SMOOTH);
bouncyBalls = new ArrayList ();
for (int i = 0; i < 100; i++) {
bouncyBalls.add(new Ball());
}
}
void draw () {
background(255);
for(int i=0; i < bouncyBall.size(); i++) {
Ball ballA = (Ball) bouncyBalls.get(i);
for(int j=0; j < bouncyBall.size(); j++) {
Ball ballB = (Ball) bouncyBalls.get(j);
if(!ballA.equals(ballB) && ballA.pos.dist(ballB.pos) < ballA.rad + ballB.rad) {
bounce(ballA, ballB);
}
}
}
for(int i = 0; i < bouncyBalls.size(); i++) {
Ball theBall = (Ball) bouncyBalls.get(i);
theBall.update();
theBall.display();
}
}
<-- And this is what is highlighted when run
(Ball tab)
class Ball() {
PVector pos = new PVector();
PVector vel = new PVector();
float rad = 10;
Ball() {
pos.x = random(0, width);
pos.y = random(0, height);
vel.x = random(0, 5);
vel.y = random(0, 5);
}
void update() {
if ( pos.y + 40 + rad > height) vel.y = abs(vel.y) *-1;
if ( pos.y - rad < 0) vel.y = abs(vel.y);
if ( pos.x + rad > width) vel.x = abs(vel.y) *-1;
if ( pos.x - rad < 0) vel.x = abs(vel.y);
pos.add(vel);
}
void display () {
fill (0);
noStroke();
ellipseMode(RADIUS);
ellipse(pos.x, pos.y, rad, rad);
}
}
(Bounce tab)
void bounce(Ball ballA, Ball ballB) {
PVector ab = new PVector();
ab.set(ballA.pos);
ab.sub(ballB.pos);
ab.normalize();
while(ballA.pos.dist(ballB.pos) < ballA.rad + ballB.rad) {
ballA.pos.add.(ab);
}
}
Any help would be hugely appreciated!
Can you help?
1