Make Objects in Different Classes Collide?

ArrayList ants = new ArrayList();
int NumAnts = 30;
PImage bg;
boolean keys;
Player player;

void setup() {
size(600, 600);
frameRate(60);
player = new Player();
bg = loadImage(“grass.png”);
for (int i = 0; i < NumAnts; i++)
ants.add(new Ant());
keys = new boolean[128];
}
void draw() {
image(bg, 0, 0);

move();
for (int i = 0; i < NumAnts; i++)
ants.get(i).drawAnt();
player.drawPlayer();

}
void move() {
int xDelta = 0;
int yDelta = 0;

if (keys[‘w’])
yDelta–;
if (keys[‘s’])
yDelta++;
if (keys[‘a’])
xDelta–;
if (keys[‘d’])
xDelta++;

player.updatePlayer(xDelta, yDelta);
}

void keyPressed() {
keys[key] = true;
}
void keyReleased() {
keys[key] = false;
}

class Ant {

PVector speed;
PImage img;
public float xDelta, yDelta, w1, h1;

Ant() {
xDelta = random(50, 450);
yDelta = random(50, 450);
w1 = 30;
h1 = 30;
speed = new PVector (random(-0.8, 1), random(-0.8, 1));
img = loadImage(“ant.png”);
}

void drawAnt() {
moveAnt();
pushMatrix();
translate(xDelta + img.width/2, yDelta + img.height/2);
rotate(speed.heading() + PI/2);
translate(-img.width/2, -img.height/2);
image(img, 0, 0, w1, h1);
popMatrix();
}
void moveAnt() {
//90% of the time go striaght
//6% of the time waver right or left
//4% of the time make a larger movement left or right
float testMove = random(0, 1);
if (testMove < 0.1) {
speed.rotate(random(-0.7, 0.7));
}

if (testMove > 0.97) {
  speed.rotate(random(-1, 1));
}
xDelta = xDelta + speed.x;
yDelta = yDelta + speed.y;

if(isAntOffScreen() == true) {
  xDelta = xDelta - speed.x;
  yDelta = yDelta - speed.y;
  speed.rotate(random(-2, 2));
  
}

}
boolean isAntOffScreen() {
if(xDelta < 30 || xDelta > width - 30 ||
yDelta < 10 || yDelta > height - 30)
return true;
return false;

}
}

class Player {

public int x2, y2, w2, h2;

PImage spriteSheet;
PImage movement;
boolean inMotion;
int currentDirection;
float currentFrame;

final int UP = 0, LEFT = 1, DOWN = 2, RIGHT = 3;

Player() {
x2 = 570;
y2 = 544;
w2 = 16;
h2 = 64;
inMotion = false;
currentDirection = 1;
currentFrame = 0;

setupSprites();

}
void setupSprites() {

movement = new PImage[4][9];
spriteSheet = loadImage("theprofessorgreen.png");


for (int i =0; i < 9; i++) {
  movement[0][i] = spriteSheet.get(w2 + h2 * i, 8, 32, 56);
  movement[1][i] = spriteSheet.get(w2 + h2 * i, 72, 32, 56);
  movement[2][i] = spriteSheet.get(w2 + h2 * i, 136, 32, 56);
  movement[3][i] = spriteSheet.get(w2 + h2 * i, 200, 32, 56);
}

}

void drawPlayer() {

if (inMotion)
  image(movement[currentDirection][1 + int(currentFrame)], x2, y2);
else
image(movement[currentDirection][0], x2, y2);

}
void updatePlayer(int xDelta, int yDelta) {
currentFrame = (currentFrame + 0.3) % 8;
inMotion = true;

if (xDelta == 0 && yDelta == 0)
  inMotion = false;
else if (xDelta == -1)
  currentDirection = LEFT;
else if (xDelta == 1)
  currentDirection = RIGHT;
else if (yDelta == -1)
  currentDirection = UP;
else if (yDelta == 1)
  currentDirection = DOWN;

x2 = x2 + xDelta;
y2 = y2 + yDelta;

if(isPlayerOffScreen(x2, y2)) {
  x2 = x2 - xDelta;
  y2 = y2 - yDelta;
  
}

}

boolean isPlayerOffScreen(float x, float y) {
if (x2 < 0 || x2 > width - 30 ||
y2 < 0 || y2 > height - 56)
return true;
return false;
}
}

2 Likes

You have one player and many ants.

Do you want the player to collide with the ants?

in player class make a new method and say

for(Ant a1 : ants) { // for each
  // Check distance 
  if(dist( playerX, playerY, a1.deltaX, a1.deltaY) < 
       22) { 
           // collision 
    }
}

Call this method from draw()

1 Like

It says that the " playerX, playerY, a1.deltaX, a1.deltaY " doesn’t exist ? I am also having trouble calling it.
I put this is Player class <
void ObjCollision() {
for(Ant a1 : ants) { // for each
// Check distance
if(dist( playerX, playerY, a1.deltaX, a1.deltaY) <
22) {
// collision
}
}

}

And this in draw
< ObjCollision(); >

Let me mention that I changed the variables " x2, y2, w2, h2" to just x, y, w, h ".

Also, why did use the number 22?

Sorry for causing confusion…

Did you understand the principle concept of these lines?

When the position of your player is x2,y2 you have to use these variables of course.

So

is now

if(dist( x2, y2, a1.deltaX, a1.deltaY) <

it says: when the distance between playerx,playery to the position of the current ant (a1.deltaX, a1.deltaY) is smaller than…


Just a threshold for the distance. Change it so that it works.

2 Likes

thank you so much it worked and I understand now

Well done!

Now you can delete the ant or whatever.


This

image(bg, 0, 0);

can be

background(bg);

if I remember correctly

1 Like

I tried and it can’t. It has to be " image(bg, 0, 0); " but one thing I would like to know about

" if(dist( playerX, playerY, a1.deltaX, a1.deltaY) <
22) "

Remember when I asked what was the 22. Well I’ve been messing with it, but I can’t seem to figure out where that point is starting from such as the middle of the Ant or top right. When the Player touches the ant, the ant goes almost halfway through the Player before the player dies. Where is the " 22 " located and what else could I adjust to get a better fitting? I increased the number to 30 - 35 to where when the ant touches a side of the player it dies but lets say the ant is below the player, it won’t die until its a little past the border of the Player.

You use this code to place your ant.
It’s certainly very good code.
I think xDelta and yDelta is the center of the ant here which is good for us.

Having said that I think for the player we use the upper left corner. (Unless you use imageMode(CENTER):wink:

Hence, in the dist() command we could add the image width/2 and height/2 to the player position imho.


The dist() command is best when it’s 2 points.

Apart from the dist() command there are other ways: see Collision Detection