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

Re : Comparing intersection points of array object list

$
0
0
On 2nd thought, in my example I only care if a car is immersed within a grape or a banana.
But I don't mind if a grape is colliding with a banana

So you're gonna need a double for loop, since you need to check 1 cellOne against the other 29.

Here's a close example doing that. Although it's 1D objects checking whether they're near each other.
But it helps to understand what's need to be done for your code. I hope so. 
    /** 
     * Moving Dots (v2.22)
     * by Josem.93 (2013/Aug)
     * mod GoToLoop
     * 
     * http://forum.processing.org/topic/interaction-among-objects-in-an-array
     * http://forum.processing.org/topic/gradual-movement-within-a-for-loop
     * 
     * http://studio.processingtogether.com/sp/pad/export/ro.9Kzw4QVZGKeFZ/latest
     */
    
    final static int NUM = 30, FPS = 60;
    final static Dot[] dots = new Dot[NUM];
    
    boolean isPaused;
    
    void setup() {
      size(800, 600);
      frameRate(FPS);
      smooth();
      stroke(Dot.COLOUR);
      fill(Dot.COLOUR);
    
      for (int i = 0; i != NUM; dots[i++] = new Dot());
    }
    
    void draw() {
      background(-1);
      for (int i = 0; i != NUM; connectPoints(i++))   dots[i].script();
    }
    
    void mousePressed() {
      if (isPaused = !isPaused)   noLoop();
      else                        loop();
    }
    
    void keyTyped() {
      mousePressed();
    }
    
    final static void connectPoints(int i) {
      for (int z = i+1; z != NUM; z++)
        if ( dots[i].areNear(dots[z]) )   dots[i].drawLine(dots[z]);
    }
    
    class Dot {
      final static short DIM = 5, MIN_DIST = 30, MAX_SPD = 5;
      final static color COLOUR = 0100;
    
      float x, y;
      float spx = random(-MAX_SPD, MAX_SPD);
      float spy = random(-MAX_SPD, MAX_SPD);
    
      Dot() {
        x = width>>1;
        y = height>>1;
      }
    
      void script() {
        move();
        display();
      }
    
      void move() {
        if ((x += spx) > width  | x < 0)  spx *= -1;
        if ((y += spy) > height | y < 0)  spy *= -1;
      }
    
      void display() {
        ellipse(x, y, DIM, DIM);
      }
    
      boolean areNear(Dot other) {
        //return dist(x, y, other.x, other.y) < MIN_DIST;
        //return sq(abs(other.x - x)) + sq(abs(other.y - y)) < MIN_DIST*MIN_DIST;
        return abs(other.x - x) < MIN_DIST && abs(other.y - y) < MIN_DIST;
      }
    
      void drawLine(Dot other) {
        line(x, y, other.x, other.y);
      }
    }
    

Viewing all articles
Browse latest Browse all 1768

Trending Articles