Trying to make a copy of the game Tank Trouble and I have no idea how to do collision detection with the bullets and the walls/ players. The bullets are generated when the mouse is pressed but I don't know how to make them despawn once they hit an object.
Tried to make them blend into the background upon contact but game started lagging hard. Have also tried the backwards loop thing that people use to remove items from arrayLists to no avail.
PVector player = new PVector (300, 400);
ArrayList <Bullet> bullets = new ArrayList <Bullet> ();
float maxSpeed = 3; //speed of bullets
void setup() {
size(800, 600);
fill(0);
}
void draw() {
background(255);
line(20, 200, 400, 200);
rect(300, 400, 50, 50);
//creates an aiming tool for the players
PVector mouse = new PVector(mouseX, mouseY);
fill(255);
ellipse(mouse.x, mouse.y, 8, 8);
if (frameCount%5==0 && mousePressed) {
PVector dir = PVector.sub(mouse, player);
dir.normalize();
dir.mult(maxSpeed*3);
Bullet b = new Bullet(player, dir);
bullets.add(b);
}
for (Bullet b : bullets) {
b.update();
b.display();
}
}
class Bullet extends PVector {
PVector vel;
Bullet(PVector loc, PVector vel) {
super(loc.x, loc.y);
this.vel = vel.get();
}
void update() {
add(vel);
}
void display() {
fill(0);
ellipse(x, y, 5, 5);
}
float bulletX() {
return x;
}
}
Basically want the bullets to bounce 3-4 times before despawning on the last touch. If it touches the player at any point, they should both despawn.