help witch colliding objects from two different arrays

edited September 2016 in Questions about Code

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);
  bullets.add(new Bullet(angle, ship.position.x, ship.position.y));
}

class Asteroid
{
  PVector position;
  PVector velocity;
  float radius;

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

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

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

  void update()
  {
    position.add(velocity);

    if(position.x - radius/2 < 0 || position.x + radius/2 > width)
    {
      velocity.x *= -1;
    }
    if(position.y - radius/2 < 0 || position.y + radius/2 > height)
    {
      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;
  float radius;

  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;
    radius = 5;
  }

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

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

 void update()
  {
    position.add(velocity);
    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);

      if(collision(asteroid.position.x, asteroid.position.y, asteroid.radius, 
                   bullet.position.x, bullet.position.y, bullet.radius))
      {
        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;
}

Answers

Sign In or Register to comment.