#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# help witch colliding objects from two different arrays

edited September 2016

Objects do not collide correctly.

``````Ship ship;
ArrayList<Bullet> bullets = new ArrayList<Bullet>();
ArrayList<Asteroid> asteroids = new ArrayList<Asteroid>();
int num = 10;

void setup()
{
size(displayWidth, displayHeight);
ship = new Ship(300, 300, 0, 0);
for(int i = 0; i < num; i++)
{
asteroids.add(new Asteroid(random(50, width - 50), random(50, height - 50),
random(5), random(5)));
}
}

void draw()
{
background(0);
ship.run();
Collision2();

for(int i = 0; i < bullets.size(); i++)
{
Bullet bullet = bullets.get(i);
bullet.run();
if(!bullet.active)
{
bullets.remove(i);
}
}

for(int a = 0; a < asteroids.size(); a++)
{
Asteroid asteroid = asteroids.get(a);
asteroid.run();
}
}

void mousePressed()
{
float angle = atan2(mouseY - ship.position.y, mouseX - ship.position.x);
}

class Asteroid
{
PVector position;
PVector velocity;

Asteroid(float posX, float posY, float velX, float velY)
{
position = new PVector(posX, posY);
velocity = new PVector(velX, velY);
}

void run()
{
draw();
update();
}

void draw()
{
pushMatrix();
translate(position.x, position.y);
popMatrix();
}

void update()
{

{
velocity.x *= -1;
}
{
velocity.y *= -1;
}
}
}

class Ship
{
float angle;
PVector position;
PVector velocity;

Ship(float posX, float posY, float velX, float velY)
{
position = new PVector(posX, posY);
velocity = new PVector(velX, velY);
}

void run()
{
draw();
}

void draw()
{
pushMatrix();
float angle = atan2(mouseY - position.y, mouseX - position.x);
translate(position.x, position.y);
rotate(angle);
triangle(20, 0, -20, -10, -20, 10);
popMatrix();
}
}

class Bullet
{
PVector position;
PVector init_position;
PVector velocity;
float angle;
float distance;
boolean active;

Bullet(float _angle, float init_posX, float init_posY)
{
angle = _angle;
position = new PVector();
init_position = new PVector(init_posX, init_posY);
velocity = new PVector(10, 0);
distance = 0.0;
active = true;
}

void run()
{
draw();
update();
}

void draw()
{
pushMatrix();
translate(init_position.x, init_position.y);
rotate(angle);
popMatrix();
}

void update()
{
distance += velocity.mag();

if(distance > 700)
{
active = false;
}
}
}

void Collision2()
{
for(int i = asteroids.size() - 1; i >= 0; i--)
{
Asteroid asteroid = asteroids.get(i);

for(int j = bullets.size() - 1; j >= 0; j--)
{
Bullet bullet = bullets.get(j);

{
asteroids.remove(i);
bullets.remove(j);
}
}
}
}

boolean collision(float x1, float y1, float r1, float x2, float y2, float r2)
{
float d = dist(x1, y1, x2, y2);
if(d <= (r1 + r2)/2)
{
return true;
}
return false;
}
``````
Tagged:

• Hi Gismas--

First, this code doesn't work. Ton of errors, including "The function Collision2()" does not exist.

1. Post a working sketch
2. Explain, when you say that they "do not collide correctly" -- what do you mean? What do you expect to happen, and what is happening instead?
• Line 152 has an obvious typo

• I suspect the problem is that you are deleting both bullet and asteroid within a nested loop.

Specifically, when you delete an asteroid you continue to check the rest of the bullets against the modified asteroid list. I think you need a break in there somewhere.

• edited September 2016

jeremydouglass, I corrected code . When you shoot down asteroids that are at the top of the screen disappears . If you shoot the asteroid , it does not disappear .

koogs, well , I will think how to fix