Intersection between objects in a array

So i'm now working on a little game to learn how intersection and atan2 works. And I need some help with the intersection between objects in a array. Why I need your help? Because I don't want the objects overlap eachother. So can you people please help me?

Heres the draw and setup

Player p;
int x = 100;
int y = 100;
int grids = 100;
int enemyCount = 1;
int bulletCount = 1;

float dEnemy;

boolean wait;
boolean isLeft, isRight, isUp, isDown;

ArrayList shootBullet = new ArrayList();
ArrayList enemies = new ArrayList();

void setup() {
  //fullScreen(P3D);
  size(750, 500, P3D);
  p = new Player();

  for(int i = 0; i < 2; i++) {
    enemies.add(new enemy());
  }
}

void draw() {
  background(100);
  stroke(255);

  for(int i = 0; i < 41; i++) {
    line(0 + (i * 100), 0, 0 + (i * 100), 4000);
  }

  for(int i = 0; i < 41; i++) {
    line(0, 0 + (i * 100), 4000, 0 + (i * 100));
  }


  if(mousePressed && wait == false) {
    for(int i = 0; i < bulletCount; i++) {
      wait = true;
      shootBullet.add(new shoot());
    }
  } else {
    if(mousePressed == false) {
      wait = false;
    }
  }

  for(int i = 0; i < shootBullet.size(); i++) {    
    shoot s = (shoot) shootBullet.get(i);
    s.display();
  }

  for(int i = 0; i < enemies.size(); i++) {
    enemy e = (enemy) enemies.get(i);
    e.display();
  }

  p.display();
  p.movement();
}

void keyPressed() {
  setMove(keyCode, true);
}

void keyReleased() {
  setMove(keyCode, false);
}

boolean setMove(int k, boolean b) {
  switch (k) {
    case UP:
    return isUp = b;

    case DOWN:
    return isDown = b;

    case LEFT:
    return isLeft = b;

    case RIGHT:
    return isRight = b;

    default:
    return b;
  }
}

Heres my Enemy class (The one that I want to check intersection between the enemies so they stop moving when they hit Eachother)

class enemy {
  PVector location;
  PVector velocity;
  float a;
  float speed = random(1, 10);
  float r = 50;
  float dPlayer;

  enemy() {
    location = new PVector(random(0, 4000), random(0, 4000));
    velocity = new PVector();
  }

  void display() {
    a = atan2(p.location.y - location.y, p.location.x - location.x);
    dPlayer = dist(location.x, location.y, p.location.x, p.location.y);

    velocity.x = cos(a);
    velocity.y = sin(a);

    velocity.x *= speed;
    velocity.y *= speed;

    if(dPlayer >= p.r) {
      location.x = location.x + velocity.x;
      location.y = location.y + velocity.y;
    }

    ellipse(location.x, location.y, r, r);
  }
}

Heres my Player class

class Player {
  PVector location;
  PVector velocity;
  float frict = 0.96;
  int r = 50;

  Player() {
    location = new PVector(2000, 2000);
    velocity = new PVector(0, 0);
  }

  void display() {
    //pushMatrix();
    colorMode(RGB);
    fill(255);
    camera(location.x, location.y, (height/2.0) / tan(PI*30.0 / 180.0), location.x, location.y, 0, 0, 1, 0); 
    ellipse(location.x, location.y, r, 50r);
    //popMatrix();
  }

  void movement() {
    location.x = location.x + velocity.x; 
    location.y = location.y + velocity.y;
    if (isLeft) velocity.x = velocity.x - frict/2;
    if (isRight) velocity.x = velocity.x + frict/2;
    if (isDown) velocity.y = velocity.y + frict/2;
    if (isUp) velocity.y  = velocity.y - frict/2;
    velocity.y = velocity.y * frict;
    velocity.x = velocity.x * frict;
    velocity.x = constrain(velocity.x, -5, 5);
    velocity.y = constrain(velocity.y, -5, 5);
  }
}

Heres my Shoot class

class shoot {
  PVector velocity;
  PVector location;

  shoot() {
    velocity = new PVector();
    location = new PVector();
    location.x = p.location.x;
    location.y = p.location.y;
    float a = atan2(mouseY - height/2, mouseX - width/2);
    velocity.x = cos(a);
    velocity.y = sin(a);
    velocity.x *= 10;
    velocity.y *= 10;
  }

  void display() {
    println(mouseY - height/2);
    println(mouseX - height/2);
    location.x += velocity.x;
    location.y += velocity.y;
    ellipse(location.x, location.y, 10, 10);
  }
}
Tagged:

Answers

Sign In or Register to comment.