If two objects touch


#1

Hello.

I have objects in an array list. They are rectangular, but more complex than single rectangles. They are collections of rectangles. I want something to happen if two of these “Animal” objects touch. If these were not in a list, this would be fairly easy, because they would have been initialized separately making interaction of one in terms of the other easy to describe. Since they are in an array (an array list actually… with its peculiar syntax) I am vexed on how to do this. I am looping through the “animals” arraylist with two different nested loops, but when trying to read arithmetic relations between the two objects, it does this for every object, including that object in relation with itself, so the boolean is alway true!

The following code is excessively complex, because it contains the class with all its stuff. I don’t think that what’s inside the class is important (other than the location vectors). What is important in regard to the present problem is what’s outside the class.

I will post the code on a reply to my own topic because I am exceeding the number of characters.

EDIT: I cannot post the class because the code is too large. Would you need to see the class?

Thanks a bunch.


#2
int mouseCount = 0;
ArrayList<Animal> animals;
float mutationRate = 0.2; //20% probability of mutation

int yCount;


int xPos = 0;
float yPos = 0;
int randomXOffset = 0;
float priorYPos = 0;


void setup() {
  size (1920, 701); 

  animals = new ArrayList<Animal>();
}

void draw() {
  background(#CBC9C9);

  for (int x = 0; x < width; x += 48) {
    if (x % 192 == 0) {
      strokeWeight(3);
    } else { 
      strokeWeight(1);
    }
    line(x, 0, x, height);
  }


  for (int y = 0; y < height; y += 20) {

    line(0, y, width, y);
    if (y % 140 == 120) {
      strokeWeight(3);
    } else if (y % 140 == 60) {
      strokeWeight(3);
    } else {
      strokeWeight(1);
    }
  }



  for (int i = animals.size() -1; i >= 0; i--) {
    Animal a = animals.get(i);   
    a.phenotype(); 
    a.update();    

    if (random(1) < 0.01) {
      a.velocity.x = random(-2, 2);  
      a.velocity.y = random(-2, 2);
    }
  }

  //Each animal a check if its touching or overlapping animal b. This is what isn't working!!!!
  for (int i = animals.size() -1; i >= 0; i--) {
    Animal a = animals.get(i); 

    for (int j = animals.size() -1; j >= 0; j--) {
      Animal b = animals.get(j); 

      //if (a.isTouching(b)) {

      //}
    }
  }
}

void mousePressed() {

  animals.add(new Animal()); 
  Animal a = animals.get(mouseCount);
  a.genotype();
  mouseCount ++;
}

#3

2 sketches using a double loop to check approximation of bouncing objects belonging to the same container: :cowboy_hat_face:

  1. Studio.ProcessingTogether.com/sp/pad/export/ro.9s0026dE7B8v-
  2. Studio.ProcessingTogether.com/sp/pad/export/ro.989GaZC5t7EkE

#4

I stared at those examples for a while. I do not know what to make of them.

I looked into a youtube tutorial on objects intercepting. It was for circles, not for rectangles, but I don’t think that is the problem. It was also for p5.js, not Processing. There I learnt to avoid having an object check with itself, by omitting their same index ( i != j ). From it I wrote the following code.

void draw() {
  background(bgcolor);


  for (int i = animals.size() -1; i >= 0; i--) {
    Animal a = animals.get(i);   
    a.phenotype(); 
    a.update();
    for (int j = animals.size() -1; j >= 0; j--) {
      Animal other = animals.get(j);
      if(i != j && a.intersects(other)) {
        bgcolor = 255;
    }


    if (random(1) < 0.01) {
      a.velocity.x = random(-2, 2);  
      a.velocity.y = random(-2, 2);
    }
  } 
 }

which calls the following method in the Animal class:

  boolean intersects(Animal other) {
    if (this.location.x == other.location.x) {
      return true;
    } else {
      return false;
    }
  }

Even though it is called intersect(), it really only is supposed to change the background to white if two different Animals share the same location.x value at any given moment. It is just to see if I can make the function work, afterwards I will write the algorithm for the overlapping of rectangular objects. Either way, I cannot get the thing to work. That is why I am here.