# Make Objects in Different Classes Collide?

**URL:** https://discourse.processing.org/t/make-objects-in-different-classes-collide/41816
**Category:** Coding Questions
**Created:** [April 24, 2023, 8:05pm UTC](https://discourse.processing.org/t/make-objects-in-different-classes-collide/41816 "2023-04-24T20:05:35Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![ImLandfall](https://avatars.discourse-cdn.com/v4/letter/i/ecccb3/32.png) [@ImLandfall](https://discourse.processing.org/u/ImLandfall)
#### Post date: [April 24, 2023, 8:05pm UTC](https://discourse.processing.org/t/make-objects-in-different-classes-collide/41816/1 "2023-04-24T20:05:35Z")

</div>

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;  
}  
}

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 24, 2023, 10:10pm UTC](https://discourse.processing.org/t/make-objects-in-different-classes-collide/41816/2 "2023-04-24T22:10:56Z")

</div>

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

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

```

Call this method from draw()

---

<div class="post-metadata">

### Author: ![ImLandfall](https://avatars.discourse-cdn.com/v4/letter/i/ecccb3/32.png) [@ImLandfall](https://discourse.processing.org/u/ImLandfall)
#### Post date: [April 25, 2023, 1:04pm UTC](https://discourse.processing.org/t/make-objects-in-different-classes-collide/41816/3 "2023-04-25T13:04:07Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 25, 2023, 3:27pm UTC](https://discourse.processing.org/t/make-objects-in-different-classes-collide/41816/4 "2023-04-25T15:27:46Z")

</div>

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

> [@ImLandfall](#):
>
> if(dist( playerX, playerY, a1.deltaX, a1.deltaY) \<

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…

* * *

> [@ImLandfall](#):
>
> Also, why did use the number 22?

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

---

<div class="post-metadata">

### Author: ![ImLandfall](https://avatars.discourse-cdn.com/v4/letter/i/ecccb3/32.png) [@ImLandfall](https://discourse.processing.org/u/ImLandfall)
#### Post date: [April 25, 2023, 3:41pm UTC](https://discourse.processing.org/t/make-objects-in-different-classes-collide/41816/5 "2023-04-25T15:41:37Z")

</div>

thank you so much it worked and I understand now

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 25, 2023, 3:42pm UTC](https://discourse.processing.org/t/make-objects-in-different-classes-collide/41816/6 "2023-04-25T15:42:36Z")

</div>

Well done!

Now you can delete the ant or whatever.

* * *

This

`image(bg, 0, 0);`

can be

` background(bg);`

if I remember correctly

---

<div class="post-metadata">

### Author: ![ImLandfall](https://avatars.discourse-cdn.com/v4/letter/i/ecccb3/32.png) [@ImLandfall](https://discourse.processing.org/u/ImLandfall)
#### Post date: [April 26, 2023, 3:04pm UTC](https://discourse.processing.org/t/make-objects-in-different-classes-collide/41816/7 "2023-04-26T15:04:08Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 26, 2023, 7:13pm UTC](https://discourse.processing.org/t/make-objects-in-different-classes-collide/41816/8 "2023-04-26T19:13:05Z")

</div>

> [@ImLandfall](#):
>
> 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);

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)😉

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](http://jeffreythompson.org/collision-detection/rect-rect.php)
