Intersection between two items in separate arrayLists?
in
Programming Questions
•
6 months ago
Hello everyone!
I am wanting to create a sketch where a rectangle from arrayList A will remove itself from the list when it collides with a rectangle from arrayList B. I am so puzzled on how to do this.
Here is my class code from which I make the arrayLists:
class Ghost {
float x;
float y;
int xSpeed = -1;
int r = 50;
Ghost(float x, float y) {
this.x = x;
this.y = y;
}
void display() {
fill(200, 0, 0);
stroke(255);
rect(x, y, r, r);
x = x + xSpeed;
}
void finished() {
////This is the part I'm trying to figure out//////////
///collision
if (x < knights.get(x)+knights.get(r) && y < knights.get(y) + knights.get(r) && y+r > knights.get(y)) {
///remove the ghost
ghosts.remove(this);
}
}
}
The other arraylist is the exact same, aside from it moving in the opposite direction, and without the collision code.
As for my sketch,
It simply draws them while I add them in with keyPressed.
void keyPressed() {
if (key == 'c') {
ghostcount = ghostcount +1;
ghosts.add(new Ghost(width, random(50,height-50)));
}
if (key == 'z') {
knightcount = knightcount +1;
knights.add(new Knight(0, random(50,height-50)));
}
}
Thanks in advance for all your suggestions. This will help me out greatly! Let me know if you need anymore information!
1