We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
I believe you did that already with your first two for loops.
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:
and draw() now looks like:
but get the error "The function "detectObstacleCollision()" expects parameters like: "detectObstacleCollision(Particle)". Not sure where to go from here.
WooHoo got it: