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

Re : Comparing intersection points of array object list

$
0
0
Hey goToLoop,

Here is the revised code that attempts to have different class object detection. Its not compiling correctly, though.



class:

  1. class cellOne {
  2.   final PVector location = new PVector(random(width), random(height));
  3.   final PVector velocity = new PVector();
  4.   final PVector acceleration = new PVector();
  5.   
  6. final PVector attraction = new PVector(mouseX, mouseY);
  7.   
  8.   final static float TOP_SPEED = 2;
  9.   final static short DIAM = 20;

  10.   final static short DIM = 5, MIN_DIST = 60, MAX_SPD = 5;
  11.   
  12.   float objectColor = 0200;

  13.   void update() {
  14.     PVector.random2D(acceleration).mult(noise(3));

  15.     velocity.add(acceleration);
  16.     velocity.limit(TOP_SPEED);

  17.     location.add(velocity);
  18.     
  19.   }

  20.   void display() {
  21.     stroke(0);
  22.     strokeWeight(2);
  23.     fill(objectColor);
  24.     ellipse(location.x, location.y, DIAM, DIAM);
  25.     


  26.   }

  27.   void checkEdges() {
  28.     if (location.x > width)   location.x = 0;
  29.     else if (location.x < 0)  location.x = width;

  30.     if (location.y > height)  location.y = 0;
  31.     else if (location.y < 0)  location.y = height;
  32.     
  33.   // if(location.x == mouseX
  34.   }

  35.   boolean isNear(cellOne other) {
  36.     //return dist(x, y, other.x, other.y) < MIN_DIST;
  37.     //return sq(abs(other.x - x)) + sq(abs(other.y - y)) < MIN_DIST*MIN_DIST;
  38.     
  39.     return abs(other.location.x - location.x) < MIN_DIST 
  40.       && abs(other.location.y - location.y) < MIN_DIST;
  41.   }

  42.   void drawLine(cellOne other) {
  43.     stroke(4);
  44.     line(location.x, location.y, other.location.x, other.location.y);
  45. location.x += 3; //move opposite direction to not intersect
  46.     location.y += 3;
  47.     
  48.     objectColor = 0;
  49. }

  50.   void drawLine2(cellFast other) {
  51.     stroke(4);
  52.     line(location.x, location.y, other.location.x, other.location.y);
  53. location.x += 3; //move opposite direction to not intersect
  54.     location.y += 3;
  55.     
  56.     objectColor = 0;
  57. }

  58. boolean intersect( cellFast b)
  59. {
  60. return abs(b.location.x - location.x) < MIN_DIST 
  61.       && abs(b.location.y - location.y) < MIN_DIST;



  62. }


main:



  1. mainCell player1;
  2. HEALTH healthBar;
  3. Animation animation1;

  4. final static int NUM = 30;
  5. final static int NUM5 = 5;
  6. int frames = 60;

  7. final static cellOne[] mover = new cellOne[NUM];
  8. final static cellFast[] speedy = new cellFast[NUM5];

  9. boolean isPaused;

  10. void setup(){
  11. //stuff
  12. }

  13. void draw(){


  14.  for (int i = 0; i != NUM; connectPoints(i++)) {
  15.     mover[i].update();
  16.     mover[i].checkEdges();  
  17.     mover[i].display();
  18.   }

  19.       

  20.  for (int i = 0; i != NUM5; connectPointsSpeed(i++)) {

  21.   speedy[i].update1();
  22.  speedy[i].checkEdges1();
  23.    speedy[i].displayFast();
  24.       }
  25.       
  26.       for (int j =0; j < mover.length; compareTwo(j++)){
  27.         for (int k=0; k < speedy.length; compareTwo(j++)){
  28.           
  29.         mover[j].update();
  30.     mover[j].checkEdges();  
  31.     mover[j].display();
  32.     
  33.      speedy[k].update1();
  34.  speedy[k].checkEdges1();
  35.    speedy[k].displayFast();

  36. }


  37. final static void connectPoints(int i) {
  38.   for (int z = i+1; z != NUM; z++){
  39.         
  40.     if ( mover[i].isNear(mover[z]) )  mover[i].drawLine(mover[z]);
  41.       
  42.     
  43.   }
  44. }
  45.     
  46.     final static void connectPointsSpeed(int i) {
  47.   for (int z = i+1; z != NUM5; z++){
  48.         
  49.     if ( speedy[i].isNear(speedy[z]) )  speedy[i].drawLine(speedy[z]);
  50.       
  51.     
  52.   }
  53.     }
  54.  void compareTwo(int j){
  55.     
  56.    for(int m = j+1; m != mover.length; m++){
  57.         if ( mover[j].intersect(speedy[m]) )  mover[j].drawLine2(speedy[m]);

  58.    }
  59.     
  60. }



Viewing all articles
Browse latest Browse all 1768

Trending Articles