2 ArrayList with polymorphism and 2 kinds of object interaction?

So I know how to get ArrayList objects to collide among themselves or objects from 2 ArrayLists to collide, but how can I get objects from within an ArrayList to interact among themselves AND with objects from another list? The following code has 3 polymorphed particles moving and colliding but now I need them to also bounce off of the static particles from a separate class. Can't figure out how to construct the 3rd loop.

ArrayList<Particle> particles = new ArrayList<Particle>();
ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
int count;
void setup() {
  size(600, 600);
  stroke(0, 128);
  strokeWeight(2);

  //Particle(float xPos, float yPos, float xVel, float yVel, float dia)

  for (int i = 0; i < 3; i++) {
    Particle newParticle = new Particle(random(16, width - 16), random(16, height - 16), 5, 5, 5);
    particles.add(newParticle);
  }

  for (int i = 0; i < count+2; i++) {
    Particle newParticle = new BigParticle(random(16, width - 16), random(16, height - 16), 4, 4, color(0, 0, 255), 50);
    particles.add(newParticle);
  }

  for (int i = 0; i<count+6; i++) {
    Particle newParticle = new Square (random(16, width - 16), random(16, height - 16), 3, 3, color(0, 255, 0), 20);
    particles.add(newParticle);
  }

  for (int i = 0; i<count +3; i++) {
    Obstacle newObstacle = new Obstacle (new PVector(random(0, 550), random (0, 550)), new PVector (50, 50));
    obstacles.add(newObstacle);
  }
}

void draw() {
  background(255);
  for (int i = 0; i < particles.size(); i++) {
    Particle curParticle = particles.get(i);

    for (int j = i +1; j<particles.size(); j++) {
      Particle otherParticle = particles.get(j);

      if (curParticle.detectCollision(otherParticle)) {
        curParticle.resolveCollision(otherParticle);
      }
    }
    curParticle.update();
    curParticle.drawMe();
  }

  for (int i = 0; i < obstacles.size(); i++) {
    Obstacle curObstacle = obstacles.get(i);
    curObstacle.render();
  }
}
//////////////////
class BigParticle extends Particle {
  color c;

  BigParticle(float xPos, float yPos, float xVel, float yVel, color c, float dia) {
    super(xPos, yPos, xVel, yVel, dia);
    this.c = c;
    this.dia = dia;
  }

  void update() {
    super.update();

  }
  void drawMe() {
    fill(c);
    pushMatrix();
    translate(xPos, yPos);
    ellipse(0, 0, dia, dia);
    popMatrix();
  }
}
////////////////////
class Obstacle {

  PVector pos, dim;

  Obstacle(PVector pos_, PVector dim_) {
    pos = pos_;
    dim = dim_;
  }

  void render() {
    pushMatrix();
    translate (pos.x, pos.y);
    fill(0);
    rect(0, 0, dim.x, dim.y);
    popMatrix();
  }
}
/////////////////////////
class Particle {
  float dia;
  float xPos, yPos, xVel, yVel;
  Particle(float xPos, float yPos, float xVel, float yVel, float dia) {
    this.xPos = xPos; 
    this.yPos = yPos; 
    this.xVel = xVel; 
    this.yVel = yVel;
    this.dia = dia;
  }

    //check collision
  boolean detectCollision(Particle other) {
    if (abs(xPos - other.xPos) < dia/2 + other.dia/2 &&
        abs(yPos - other.yPos) < dia/2 + other.dia/2){
      return true;
    }
    return false;
  }
  void resolveCollision(Particle other) {
    particles.remove(other);
  }

  void update() {
    xPos += xVel;
    yPos += yVel;

    if (xPos - dia/2 < 0 ) {
      xPos = dia/2;
      xVel *= -1;
    }
    if (xPos + dia/2 > width) {
      xPos = width - dia/2;
      xVel *= -1;
    }
    if (yPos - dia/2 < 0) {
      yPos = dia/2;
      yVel *= -1;
    }
    if (yPos + dia/2 > height) {
      yPos = height - dia/2;
      yVel *= -1;
    }
  }

  void drawMe() {
    fill(255, 0, 0, 128);
    pushMatrix();
    translate(xPos, yPos);
    ellipse(0, 0, dia, dia);
    popMatrix();
  }
}
////////////////////////////
class Square extends BigParticle {

  Square (float xPos, float yPos, float xVel, float yVel, color c, float dia) {
    super(xPos, yPos, xVel, yVel, c, dia);
  }

  void update() {
    super.update();

  }
  void drawMe() {
    fill(c);
    pushMatrix();
    translate(xPos, yPos);
    rectMode(CENTER);
    rect(0, 0, dia, dia);
    popMatrix();
  }
}

Answers

  • how can I get objects from within an ArrayList to interact among themselves

    I believe you did that already with your first two for loops.

    AND with objects from another list

    Are you referring to the missing interaction between particles and obstacles?

    Taking a guess, you want o make sure the Obstacle class implements some behavior similar to the one you see in the Particle class. The obstacle class should handle collisions and it should also resolve them. One way to do this is using Abstract classes or maybe an Interface.

    Kf

  • Thanks kfrajer! So, I'm newbing around and wrote the Obstacle class as:

    class Obstacle {
    
      PVector pos, dim;
    
      Obstacle(PVector pos_, PVector dim_) {
        pos = pos_;
        dim = dim_;
      }
      //check collision
      boolean detectObstacleCollision(Particle p) {
        if (abs(pos.x - p.xPos) < dim.x/2 + p.dia/2 &&
          abs(pos.y - p.yPos) < dim.y/2 + p.dia/2) {
          return true;
        }
        return false;
      }
      void resolveObstacleCollision(Particle p) {
        particles.remove(p);
      }
    
      void render() {
        pushMatrix();
        translate (pos.x, pos.y);
        fill(0);
        rect(0, 0, dim.x, dim.y);
        popMatrix();
      }
    }
    

    and draw() now looks like:

    void draw() {
      background(255);
      for (int i = 0; i < particles.size(); i++) {
        Particle curParticle = particles.get(i);
    
        for (int j = i +1; j<particles.size(); j++) {
          Particle otherParticle = particles.get(j);
    
          if (curParticle.detectCollision(otherParticle)) {
            curParticle.resolveCollision(otherParticle);
          }
        }
        curParticle.update();
        curParticle.drawMe();
      }
      for (Obstacle obstacle : obstacles) {
        obstacle.render();
        if (obstacle.detectObstacleCollision(particles)) {
          obstacle.resolveObstacleCollision(particles);
        }
      }
    }
    

    but get the error "The function "detectObstacleCollision()" expects parameters like: "detectObstacleCollision(Particle)". Not sure where to go from here.

  • edited July 2017

    WooHoo got it:

        ArrayList<Particle> particles = new ArrayList<Particle>();
        ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
        int count;
    
        void setup() {
          size(1000, 800);
          stroke(0, 128);
          strokeWeight(2);
    
          for (int i = 0; i < 5; i++) {
            particles.add(new Particle(new PVector(random(width-100), random(height - 100)), new PVector(10, 10), new PVector(random(5), random(5)), color(255, 0, 0)));
          }
          for (int i = 0; i < count+2; i++) {
            particles.add(new BigParticle(new PVector(random(width-100), random(height - 100)), new PVector(50, 50), new PVector(random(5), random(5)), color(0, 0, 255)));
          }
    
          for (int i = 0; i < count+3; i++) {
            particles.add (new BigParticle(new PVector(random(width-100), random(height - 100)), new PVector(50, 50), new PVector(5, 5), color(0, 255, 255)));
          }
    
          for (int i = 0; i<count+3; i++) {
            particles.add(new Square(new PVector(random(width-100), random(height - 100)), new PVector(20, 20), new PVector(random(5), random(5)), color(0, 200, 0)));
          }
    
          for (int i = 0; i<count +3; i++) {
            obstacles.add (new Obstacle (new PVector(random(100, width - 100), random (100, height - 100)), new PVector (50, 50)));
          }
        }
    
        void draw() {
          background(255);
          for (int i = 0; i < particles.size(); i++) {
            Particle curParticle = particles.get(i);
    
            for (int j = i +1; j<particles.size(); j++) {
              Particle otherParticle = particles.get(j);
    
              if (curParticle.detectCollision(otherParticle)) {
                curParticle.resolveCollision(otherParticle);
              }
            }
    
            curParticle.update();
          }
          for (int i = 0; i<obstacles.size(); i++) {
            Obstacle currObstacles = obstacles.get(i);
            currObstacles.update();
          }
        }
        /////////////////////////
        class BigParticle extends Particle {
          color c;
    
          BigParticle(PVector pos_, PVector dim_, PVector vel_, color c_) {
            super(pos_, dim_, vel_, c_);
            c = c_;
          }
    
          void update() {
            super.update();
            drawMe();
          }
    
          void drawMe() {
            pushMatrix();
            fill(c);
            translate(pos.x, pos.y);
            ellipse(0, 0, dim.x, dim.y);
            popMatrix();
          }
        }
        ///////////////////////////////
        class Obstacle {
    
          PVector pos, dim;
    
          Obstacle(PVector pos_, PVector dim_) {
            pos = pos_;
            dim = dim_;
          }
    
          void update() {
            for (int i = 0; i < particles.size(); i++) {
              Particle particle = particles.get(i);
              if (particle.detectBump(this)) {
                particle.resolveBump(this);
              }
            }
            render();
          }
    
          void render() {
            pushMatrix();
            translate (pos.x, pos.y);
            fill(100);
            rectMode(CENTER);
            rect(0, 0, dim.x, dim.y);
            popMatrix();
          }
        }
        ////////////////////////////
        class Particle {
          PVector pos, dim, vel, acc;
          color c;
    
          Particle(PVector pos_, PVector dim_, PVector vel_, color c_) {
            acc = new PVector(3, 3);
            pos = pos_;
            dim = dim_;
            vel = vel_;
            c = c_;
          }
    
          boolean detectCollision(Particle other) {
            if (abs(pos.x - other.pos.x) < dim.x /2 + other.dim.x/2 &&
              abs(pos.y - other.pos.y) < dim.y/2 + other.dim.y/2) {
              return true;
            }
            return false;
          }
    
          void resolveCollision(Particle other) {
            float angle = atan2(pos.y - other.pos.y, pos.x - other.pos.x);
            float avgSpeed = (vel.mag() + other.vel.mag())/3;
            vel.x = avgSpeed * cos(angle);
            vel.y = avgSpeed * sin(angle);
            other.vel.x = avgSpeed * cos(angle - PI);
            other.vel.y = avgSpeed * sin(angle - PI);
          }
    
          boolean detectBump(Obstacle other) {
            return abs(pos.x - other.pos.x) < dim.x /2 + other.dim.x/2 &&
              abs(pos.y - other.pos.y) < dim.y/2 + other.dim.y/2;
          }
    
          void resolveBump(Obstacle obstacles) {
            println(" Bump Bra!");
            float angle = atan2(pos.y - obstacles.pos.y, pos.x - obstacles.pos.x);
            float avgSpeed = (vel.mag() + 2);
            vel.x = avgSpeed * cos(angle);
            vel.y = avgSpeed * sin(angle);
          }
    
          void update() {
            pos.add(vel);
    
            if (pos.x - dim.x /2 < 0 ) {
              pos.x = dim.x /2;
              vel.x *= -1;
            }
            if (pos.x + dim.x /2 > width) {
              pos.x = width - dim.x /2;
              vel.x *= -1;
            }
            if (pos.y - dim.y/2 < 0) {
              pos.y = dim.y/2;
              vel.y *= -1;
            }
            if (pos.y + dim.y/2 > height) {
              pos.y = height - dim.y/2;
              vel.y *= -1;
            }
    
            drawMe();
          }
    
          void drawMe() {
            pushMatrix();
            fill(c);
            translate(pos.x, pos.y);
            ellipse(0, 0, dim.x, dim.y);
            popMatrix();
          }
        }
        //////////////////////////////////
        class Square extends BigParticle {
    
    
          Square (PVector pos_, PVector dim_, PVector vel_, color c_) {
            super(pos_, dim_, vel_, c_);
          }
    
          void update() {
            super.update();
            drawMe();
          }
          void drawMe() {
            pushMatrix();
            fill(c);
            translate(pos.x, pos.y);
            rectMode(CENTER);
            rect(0, 0, dim.x, dim.y);
            popMatrix();
          }
        }
    
Sign In or Register to comment.