Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Comparing intersection points of array object list

$
0
0
Greetings! I had created an array of a class object in my code. I am having a bit of trouble comparing if the objects intersect each other and wondering if I can get a push in the right direction;

main loop (important elements):

  1. cellOne[] mover = new cellOne[30];
  2. void setup()
  3. {
  4.   
  5.   size(displayWidth, displayHeight, P3D);

  6.   
  7.   for (int i = 0; i < mover.length; i ++ ) { // Initialize each Car using a for loop.
  8.     mover[i] = new cellOne(); 
  9.   }

  10. }

  11. void draw(){



  12.       for (int i = 0; i < mover.length; i++) {
  13.  
  14.  
  15.   
  16.          mover[i].update();
  17.  mover[i].checkEdges();
  18.     mover[i].display();
  19.       }
  20. }


and now my class:

  1. class cellOne{
  2.   color c = color(100,50,77);
  3. float x,y;
  4.      PVector location;
  5.   PVector velocity;
  6.   PVector acceleration;
  7.   float topspeed;
  8.   float r = 48;
  9.  
  10.   cellOne(){
  11.     location = new PVector(random(displayWidth), random(displayHeight));
  12.     velocity = new PVector(0, 0);
  13.     topspeed = .8;
  14.     
  15.      x = random(2);
  16.     y = random(2);
  17.     
  18.   }
  19.     

  20.   void update() {

  21.     acceleration = PVector.random2D();
  22.     acceleration.mult(random(2));

  23.     velocity.add(acceleration);
  24.     velocity.limit(topspeed);
  25.     location.add(velocity);
  26.   }

  27.   void display() {
  28.     stroke(0);
  29.     strokeWeight(2);
  30.     fill(127);
  31.     ellipse(location.x, location.y, r,r);
  32.   }
  33.   
  34.    void filler() {
  35.     stroke(0);
  36.     strokeWeight(2);
  37.     fill(2);
  38.     ellipse(location.x, location.y, r,r);
  39.   }

  40.   void checkEdges() {


  41.      
  42.     if (location.x > displayWidth) {
  43.       location.x = 0;
  44.     } 
  45.     else if (location.x < 0) {
  46.       location.x = displayWidth;
  47.     }

  48.     if (location.y > displayHeight) {
  49.       location.y = 0;
  50.     } 
  51.     else if (location.y < 0) {
  52.       location.y = displayHeight;
  53.     }
  54.   }



  55.  boolean intersect() {
  56.     
  57.     // Objects can be passed into functions as arguments too! 
  58.     float distance = dist(x,y,location.x,location.y); // Calculate distance
  59.     //location.add(r);
  60.     // Compare distance to sum of radii
  61.     if (distance <r) {
  62.       return true;
  63.     } else {
  64.       return false;
  65.     }
  66.   }
  67. }



Viewing all articles
Browse latest Browse all 1768

Trending Articles