Question for loop


#1

Hi there, I am currently working on an animation that for example if you create 4 balls, then ball1 going to link ball2, ball3, ball4 and ball2 going to link ball3 and ball4, keep going…
The problem is that every time I want to add a new ball, then I have to add a bunch of arrays for containing points, directions… I know I need to add several loops there but still not figure out where to put those loops, here is my code currently, please help!

ball[] balls = new ball[4];
//for ball0
//create an array for points that move from this ball
PVector[] pointsBall0 = new PVector[3];
//create an array for directions
PVector[] directionsBall0 = new PVector[3];
//create an array for accelerations
PVector[] accelerationBall0 = new PVector[3];
//create an array for velocities
PVector[] velocityBall0 = new PVector[3];

//for ball1
PVector[] pointsBall1 = new PVector[2];
//create an array for direction
PVector[] directionsBall1 = new PVector[2];
//create an array for acceleration
PVector[] accelerationBall1 = new PVector[2];
//create an array for velocity
PVector[] velocityBall1 = new PVector[2];

//for ball2
PVector[] pointsBall2 = new PVector[1];
//create an array for direction
PVector[] directionsBall2 = new PVector[1];
//create an array for acceleration
PVector[] accelerationBall2 = new PVector[1];
//create an array for velocity
PVector[] velocityBall2 = new PVector[1];

float magVelocity = 0.1;
//no need for ball3

void setup(){
  size(900,600);
  smooth();
  //draw balls
  for(int i=0; i<balls.length; i++){
    balls[i] = new ball(new PVector(random(width), random(height)),10);
    }
  
  //draw points from Ball0
  for(int i=0; i<directionsBall0.length; i++){
    pointsBall0[i] = new PVector(balls[0].location.x, balls[0].location.y);
    }
    
  //draw directionsBall0
  for(int i=0; i<directionsBall0.length; i++){
    //the problem point
    directionsBall0[i] = PVector.sub(balls[i+1].location, balls[0].location);
    directionsBall0[i].normalize();
    accelerationBall0[i] = PVector.mult(directionsBall0[i], magVelocity);
    //initialize velocity
    velocityBall0[i] = new PVector(0,0);
  }
//-----------------------------------------------------------------------
  //draw points from Ball1
  for(int i=0; i<directionsBall1.length; i++){
    pointsBall1[i] = new PVector(balls[1].location.x, balls[1].location.y);
    }
    
  //draw directionsBall1
  for(int i=0; i<directionsBall1.length; i++){
    //i+2
    directionsBall1[i] = PVector.sub(balls[i+2].location, balls[1].location);
    directionsBall1[i].normalize();
    accelerationBall1[i] = PVector.mult(directionsBall1[i], magVelocity);
    //initialize velocity
    velocityBall1[i] = new PVector(0,0);
  }
//------------------------------------------------------------------------
   //draw points from Ball2
  for(int i=0; i<directionsBall2.length; i++){
    pointsBall2[i] = new PVector(balls[2].location.x, balls[2].location.y);
    }
    
  //draw directionsBall2
  for(int i=0; i<directionsBall2.length; i++){
    //i+3
    directionsBall2[i] = PVector.sub(balls[i+3].location, balls[2].location);
    directionsBall2[i].normalize();
    accelerationBall2[i] = PVector.mult(directionsBall2[i], magVelocity);
    //initialize velocity
    velocityBall2[i] = new PVector(0,0);
  }
}


void draw(){
  background(155);
  for(int i=0; i<balls.length; i++){
    balls[i].draw();
  }
  //checkCollision
  drawLinesBall0();
  drawLinesBall1();
  drawLinesBall2();
}

//drawLinesBall0
void drawLinesBall0(){
for(int i=0; i<directionsBall0.length; i++){
  velocityBall0[i].add(accelerationBall0[i]);
  pointsBall0[i].add(velocityBall0[i]);
}
for(int i=0; i<directionsBall0.length; i++){
  line(balls[0].location.x, balls[0].location.y, pointsBall0[i].x, pointsBall0[i].y );
}
}

//drawLinesBall1
void drawLinesBall1(){
  for(int i=0; i<directionsBall1.length; i++){
  velocityBall1[i].add(accelerationBall1[i]);
  pointsBall1[i].add(velocityBall1[i]);
}
for(int i=0; i<directionsBall1.length; i++){
  line(balls[1].location.x, balls[1].location.y, pointsBall1[i].x, pointsBall1[i].y );
}
}

//drawLinesBall2
void drawLinesBall2(){
  for(int i=0; i<directionsBall2.length; i++){
  velocityBall2[i].add(accelerationBall2[i]);
  pointsBall2[i].add(velocityBall2[i]);
}
for(int i=0; i<directionsBall2.length; i++){
  line(balls[2].location.x, balls[2].location.y, pointsBall2[i].x, pointsBall2[i].y );
}
}
class ball{
  PVector location;
  float r;
  ball(PVector l, float radian){
    location = l.get();
    this.r = radian;
  }
  
  void draw(){
    ellipse(location.x, location.y, r,r);
  }
}


#2

This sketch has a class that draws a line from every ball to every ball. I didn’t have time to dig into the animation you’re doing but, if I understand what you’re trying to do, your question is “where to put those loops” and the code linked above is moving toward the answer: "all the loops become methods of the class.

Note: that code is pretty sloppy and outside good OO (the class is referencing the global Ball array] but I didn’t want to complicate the approach I’m suggesting, which is that you put your loops in one class and then call them. Note that this is also creating overlapping lines since every ball is drawing to every ball.


#3

Hi Adam,
Thank you for your answers. Since the balls’ positions are static however the lines will keep growing, I separate the balls and lines into two different classes and as you said set the loops as methods of the lines class. Now it is working and that helps me better understand working with objects. Thank you so much for your help.