Can someone help me convert this code from normal array[] to ArrayList

When I tried to convert it to ArrayList code, it don’t work anymore.

float fadeSpeed = 20;

float mouseStrength = 100;
float windStrength = 0.1;
float gravityStrength = 0.2;

int circleAmount = 12;

Circle[] circles = new Circle[12];
Arrow arrow;

PVector wind = PVector.fromAngle(random(0,TAU)).mult(windStrength);
PVector gravity = new PVector(0,gravityStrength);

void setup(){
  surface.setTitle("PHYSIC ENGINE");
  
  background(0);
  fullScreen();
  for(int n = 0;n < circles.length;n++){
    circles[n] = new Circle();
  }
}

void draw(){
  fill(0,fadeSpeed);
  rect(-5,-5,width + 10,height + 10);
  
  for(int n = 0;n < circles.length;n++){
    circles[n].applyForce(wind);
    circles[n].applyForce(gravity);
    circles[n].update();
    circles[n].display();
  }
  
  arrow = new Arrow(
                    new PVector(width / 2,200),
                    80,
                    wind.heading(),
                    5,
                    color(255)
                   );
  arrow.display();
  
  if(mousePressed && mouseButton == CENTER){
    wind = new PVector(mouseX - (width / 2),mouseY - 200);
    wind.normalize();
    wind.div(10);
  }else{
    wind.rotate((noise(frameCount) - 0.5) / 10);
  }
}

void mousePressed(){
  if(mouseButton == RIGHT){
    for(int n = 0;n < circles.length;n++){
      circles[n] = new Circle();
    }
  }
}

class Arrow{
  PVector position;
  float distance;
  float angle;
  float thickness;
  color colorStroke;
  
  Arrow(PVector p,float d,float a,float t,color c){
    position = p;
    distance = d;
    angle = a;
    thickness = t;
    colorStroke = c;
  }
  
  void display(){
    strokeWeight(thickness);
    stroke(colorStroke);
    
    pushMatrix();
    translate(position.x,position.y);
    rotate(angle);
    rotate(-QUARTER_PI);
    translate(distance,distance);
    line(-distance,-distance,0,0);
    rotate(HALF_PI + QUARTER_PI);
    line(0,0,20,20);
    rotate(-(PI + HALF_PI));
    line(0,0,20,20);
    popMatrix();
  }
}

class Circle{
  boolean[] collisionLayer = new boolean[15];
  
  float radius;
  color colorStroke;
  color colorFill;
  float thickness;
  
  float mass;
  float restitution;
  float friction;
  
  PVector position,velocity;
  
  Circle(boolean[] cl,float r,color cs,color cf,float t,float m,float re,float f,PVector p,PVector v){
    collisionLayer = cl;
    
    radius = r;
    colorMode(HSB);
    colorStroke = cs;
    colorFill = cf;
    thickness = t;
    
    mass = m;
    restitution = re;
    friction = f;
    
    position = p;
    velocity = v;
  }
  
  Circle(PVector p){
    collisionLayer = new boolean[]{
      true,false,false,false,false,false,false,false,false,false,false,false,false,false,false
    };
    
    radius = random(10,50);
    colorMode(HSB);
    colorStroke = color(random(0,255),255,255,random(128,255));
    colorFill = color(random(0,255),255,255,random(128,255));
    thickness = random(0,5);
    
    mass = random(-10.0,10.0);
    restitution = random(0.0,1.0);
    friction = random(0.0,1.0);
    
    position = p;
    velocity = PVector.random2D().mult(5);
  }
  
  Circle(){
    radius = random(10,50);
    colorMode(HSB);
    colorStroke = color(random(0,255),255,255,random(128,255));
    colorFill = color(random(0,255),255,255,random(128,255));
    thickness = random(0,5);
    
    mass = random(10.0,10.0);
    restitution = random(0.0,1.0);
    friction = random(0.0,1.0);
    
    position = new PVector(random(0,width),random(0,height));
    velocity = PVector.random2D().mult(5);
  }
  
  void update(){
    position.add(velocity);
    
    if(position.y > (height - radius) && velocity.y > 0){
      velocity.y = velocity.y * -restitution;
      velocity.y -= (velocity.y * friction);
      position.y = height - radius;
    }
    if(position.y < radius && velocity.y < 0){
      velocity.y = velocity.y * -restitution;
      velocity.y -= (velocity.y * friction);
      position.y = radius;
    }
    if(position.x > (width - radius) && velocity.x > 0){
      velocity.x = velocity.x * -restitution;
      velocity.x -= (velocity.x * friction);
      position.x = width - radius;
    }
    if(position.x < radius && velocity.x < 0){
      velocity.x = velocity.x * -restitution;
      velocity.x -= (velocity.x * friction);
      position.x = radius;
    }
    
    for(int i = 1;i < circles.length;i++){
      boolean collide = false;
      for(int n = 0;n < collisionLayer.length;n++){
        if(circles[i].collisionLayer[n] == collisionLayer[n]){
          collide = true;
        }
      }
      if(collide){
        float dx = circles[i].position.x - position.x;
        float dy = circles[i].position.y - position.y;
        float distance = sqrt((dx * dx) + (dy * dy));
        float minDist = (circles[i].radius) + radius;
        if(distance < minDist){ 
          float angle = atan2(dy , dx);
          float targetX = position.x + cos(angle) * minDist;
          float targetY = position.y + sin(angle) * minDist;
          PVector bounce = new PVector(targetX - circles[i].position.x,targetY - circles[i].position.y);
          bounce.mult(restitution);
          velocity.sub(bounce);
          circles[i].velocity.add(bounce);
        }
      }
    }
  }
  
  void applyForce(PVector force){
    velocity.add(force);
  }
  
  void display(){
    ellipseMode(CENTER);
    
    stroke(colorStroke);
    if(thickness == 0){
      noStroke();
    }else{
      strokeWeight(thickness);
    }
    
    fill(colorFill);
    ellipse(position.x,position.y,radius * 2,radius * 2);
  }
}
float fadeSpeed = 20;

float mouseStrength = 100;
float windStrength = 0.1;
float gravityStrength = 0.2;

int circleAmount = 12;

ArrayList<Circle> circles = new ArrayList<Circle>();
Arrow arrow;

PVector wind = PVector.fromAngle(random(0,TAU)).mult(windStrength);
PVector gravity = new PVector(0,gravityStrength);

void setup(){
  surface.setTitle("PHYSIC ENGINE");
  background(0);
  fullScreen();
  for(int n = 0;n < circleAmount;n++){
    circles.add(new Circle());
  }
}

void draw(){
  fill(0,fadeSpeed);
  rect(-5,-5,width + 10,height + 10);
  
  for(int n = 0;n < circles.size();n++){
    circles.get(n).applyForce(wind);
    circles.get(n).applyForce(gravity);
    circles.get(n).update();
    circles.get(n).display();
  }
  
  arrow = new Arrow(
                    new PVector(width / 2,200),
                    80,
                    wind.heading(),
                    5,
                    color(255)
                   );
  arrow.display();
  
  if(mousePressed && mouseButton == CENTER){
    wind = new PVector(mouseX - (width / 2),mouseY - 200);
    wind.normalize();
    wind.div(10);
  }else{
    wind.rotate((noise(frameCount) - 0.5) / 10);
  }
}

void mousePressed(){
  if(mouseButton == RIGHT){
    for(int n = 0;n < circles.size();n++){
      circles.add(new Circle());
    }
  }
}

class Arrow{
  PVector position;
  float distance;
  float angle;
  float thickness;
  color colorStroke;
  
  Arrow(PVector p,float d,float a,float t,color c){
    position = p;
    distance = d;
    angle = a;
    thickness = t;
    colorStroke = c;
  }
  
  void display(){
    strokeWeight(thickness);
    stroke(colorStroke);
    
    pushMatrix();
    translate(position.x,position.y);
    rotate(angle);
    rotate(-QUARTER_PI);
    translate(distance,distance);
    line(-distance,-distance,0,0);
    rotate(HALF_PI + QUARTER_PI);
    line(0,0,20,20);
    rotate(-(PI + HALF_PI));
    line(0,0,20,20);
    popMatrix();
  }
}

class Circle{
  ArrayList<Boolean> collisionLayer = new ArrayList<Boolean>();
  
  float radius;
  color colorStroke;
  color colorFill;
  float thickness;
  
  float mass;
  float restitution;
  float friction;
  
  PVector position,velocity;
  
  Circle(ArrayList<Boolean> cl,float r,color cs,color cf,float t,float m,float re,float f,PVector p,PVector v){
    collisionLayer = cl;
    
    radius = r;
    colorMode(HSB);
    colorStroke = cs;
    colorFill = cf;
    thickness = t;
    
    mass = m;
    restitution = re;
    friction = f;
    
    position = p;
    velocity = v;
  }
  
  Circle(PVector p){
    collisionLayer = new ArrayList<Boolean>();
      
      for(int i=0;i<15;i++){
        if(i==0)collisionLayer.add(true);
        else collisionLayer.add(false);
      }
      //true,false,false,false,false,false,false,false,false,false,false,false,false,false,false
    
    
    radius = random(10,50);
    colorMode(HSB);
    colorStroke = color(random(0,255),255,255,random(128,255));
    colorFill = color(random(0,255),255,255,random(128,255));
    thickness = random(0,5);
    
    mass = random(-10.0,10.0);
    restitution = random(0.0,1.0);
    friction = random(0.0,1.0);
    
    position = p;
    velocity = PVector.random2D().mult(5);
  }
  
  Circle(){
    radius = random(10,50);
    colorMode(HSB);
    colorStroke = color(random(0,255),255,255,random(128,255));
    colorFill = color(random(0,255),255,255,random(128,255));
    thickness = random(0,5);
    
    mass = random(10.0,10.0);
    restitution = random(0.0,1.0);
    friction = random(0.0,1.0);
    
    position = new PVector(random(0,width),random(0,height));
    velocity = PVector.random2D().mult(5);
  }
  
  void update(){
    position.add(velocity);
    
    if(position.y > (height - radius) && velocity.y > 0){
      velocity.y = velocity.y * -restitution;
      velocity.y -= (velocity.y * friction);
      position.y = height - radius;
    }
    if(position.y < radius && velocity.y < 0){
      velocity.y = velocity.y * -restitution;
      velocity.y -= (velocity.y * friction);
      position.y = radius;
    }
    if(position.x > (width - radius) && velocity.x > 0){
      velocity.x = velocity.x * -restitution;
      velocity.x -= (velocity.x * friction);
      position.x = width - radius;
    }
    if(position.x < radius && velocity.x < 0){
      velocity.x = velocity.x * -restitution;
      velocity.x -= (velocity.x * friction);
      position.x = radius;
    }
    
    for(int i = 1;i < circles.size();i++){
      boolean collide = false;
      for(int n = 0;n < collisionLayer.size();n++){
        if(circles.get(i).collisionLayer.get(n) == collisionLayer.get(n)){
          collide = true;
        }
      }
      if(collide){
        float dx = circles.get(i).position.x - position.x;
        float dy = circles.get(i).position.y - position.y;
        float distance = sqrt((dx * dx) + (dy * dy));
        float minDist = (circles.get(i).radius) + radius;
        if(distance < minDist){ 
          float angle = atan2(dy , dx);
          float targetX = position.x + cos(angle) * minDist;
          float targetY = position.y + sin(angle) * minDist;
          PVector bounce = new PVector(targetX - circles.get(i).position.x,targetY - circles.get(i).position.y);
          bounce.mult(restitution);
          velocity.sub(bounce);
          circles.get(i).velocity.add(bounce);
        }
      }
    }
  }
  
  void applyForce(PVector force){
    velocity.add(force);
  }
  
  void display(){
    ellipseMode(CENTER);
    
    stroke(colorStroke);
    if(thickness == 0){
      noStroke();
    }else{
      strokeWeight(thickness);
    }
    
    fill(colorFill);
    ellipse(position.x,position.y,radius * 2,radius * 2);
  }
}
1 Like

Thanks :smiley: :grinning: :grin: :smiling_face_with_three_hearts: :smile:

1 Like