Loading...
Logo
Processing Forum
Hi there, 
Im a beginner with processing and really want to sink my teeth into it, im having a problem with the sketch im using,im trying to use the x y data from the color tracking within the particle class afterwards.
I think im close to getting it working but ive got stuck as im being told that im mixing active and static modes
Really need some help with this and it will be huuuugely appreciated.

Heres the code:

Copy code
  1. /**
  2. * Color Tracking
  3. * by Justin Brooks
  4. *
  5. *
  6. * based on original code:
  7. *   Brightness Tracking 
  8. *   by Golan Levin. 
  9. * Tracks the tracks pixel closest in color to specified color.
  10. */
  11. boolean lines = false;
  12. /*
  13. gravity toggle
  14. toggle this by pressing 'g'
  15. */
  16. boolean gravity = true;
  17. /* Particle count */
  18. int particleCount = 500;
  19. /* Particle array */
  20. Particle[] particles = new Particle[particleCount+1];

  21. import processing.video.*;

  22. Capture video;

  23. void setup() {
  24.  size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
  25.  // Uses the default video input, see the reference if this causes an error
  26.  video = new Capture(this, width, height, 30);
  27.  noStroke();
  28.  smooth();
  29.  colorMode(RGB, 400);
  30.   /* Frame rate is 30 */
  31.   frameRate(30);
  32.   /* The fill color is set to black (0) for the boundaries (drawn as a quad) */
  33.   fill (0);
  34.   coordx = new Particle (colorX);
  35.   coordy = new Particle (colorY);
  36.   /* We loop through our particle count and initialize each */
  37.   for (int x = particleCount; x >= 0; x--) {
  38.     particles[x] = new Particle();
  39.   }
  40. }

  41. void draw() {
  42.  if (video.available()) {
  43.    video.read();
  44.    background(0);
  45.   /* The stroke color is set to white for the border. */
  46.   stroke (400);
  47.   /* The border; it is 10 pixels from all sides */
  48.   quad (10,10,10,height-10,width-10,height-10,width-10,10); 
  49.   /* All of our particles are looped through, and updated */
  50.   for (int i = particleCount; i >= 0; i--) {
  51.     Particle particle = (Particle) particles[i];
  52.     particle.update(i);
  53.   }
  54.    int colorX = 0; // X-coordinate of the closest in color video pixel
  55.    int colorY = 0; // Y-coordinate of the closest in color video pixel
  56.    float closestColor = 10000; //we set this to be abritrarily large, once program runs, the first pixel it scans will be set to this value
  57.    // Search for the closest in color pixel: For each row of pixels in the video image and
  58.    // for each pixel in the yth row, compute each pixel's index in the video
  59.    video.loadPixels();
  60.    int index = 0;
  61.    for (int ay = 0; ay < video.height; ay++) {
  62.      for (int ax = 0; ax < video.width; ax++) {
  63.        // Get the color stored in the pixel
  64.        color pixelValue = video.pixels[index];
  65.        // Determine the color of the pixel
  66.        float colorProximity = abs(red(pixelValue)-0)+abs(green(pixelValue)-255)+abs(blue(pixelValue)-0);
  67.        // If that value is closer in color value than any previous, then store the
  68.        // color proximity of that pixel, as well as its (x,y) location
  69.        if (colorProximity < closestColor) {
  70.          closestColor = colorProximity;
  71.          closestColor=closestColor-10; //thoguht behind this is that it once it "locks" on to an object of color, it wont let go unless something a good bit better (closer in color) comes along
  72.          colorY = ay;
  73.          colorX = ax;
  74.        }
  75.        index++;
  76.      }
  77.    }
  78.    // Draw a large, yellow circle at the brightest pixel
  79.    fill(255, 204, 0, 128);
  80.    ellipse(colorX, colorY, 200, 200);
  81.  }
  82. }

  83. class Particle {
  84.   /*
  85.   Global class variables
  86.   These are kept track of, and aren't lost when the class particle is finished operating.
  87.   */
  88.   /* the x and y are our coordinates for each particles */
  89.   Particle () {
  90.     float coordx;
  91.   float coordy;

  92.      mx = coordx; 
  93. my = coordy;
  94.   }
  95. }
  96.   float x;
  97.   float y;
  98.   /* vx and vy are the velocities along each axis the particles are traveling */
  99.   float vx;
  100.   float vy;
  101.   /* Particle initialization. We define the beginning properties of each particle here. */
  102.   Particle() {
  103.     x = random(10,width-10);
  104.     y = random(10,height-10);
  105.   }
  106.   /* These are called everytime we check with the other particle's distances  */
  107.   float getx() {
  108.     return x;
  109.   }
  110.   float gety() {
  111.     return y;
  112.   }
  113.   void update(int num) {
  114.    /* Friction is simulated here */
  115.    vx *= 0.84;
  116.    vy *= 0.84;
  117.    /* Here, every particle is looped through and checked to see if they're close enough to have effect on the current particle. */
  118.    for (int i = particleCount; i >= 0; i--)  {
  119.      /* Check to see if the particle we're checking isn't the current particle. */
  120.      if (i != num) {
  121.         /* drawthis boolean is initialized. it determines whether the particle is close enough to the other particles for relationship lines to be drawn, assuming it's enabled */
  122.         boolean drawthis = false;
  123.         /*
  124.         Integers are used to keep track of the shade for each particle
  125.         The red shade shows opposition
  126.         The blue shade shows attraction
  127.         */
  128.         float redshade = 20;
  129.         float blueshade = 500;
  130.         /* We set our particle */
  131.         Particle particle = (Particle) particles[i];
  132.         /* The x and y coordinates of the particle is found, so we can compare distances with our current particle. */
  133.         float tx = particle.getx();
  134.         float ty = particle.gety();
  135.         /* The radius or distance between both particles are determined */
  136.         float radius = dist(x,y,tx,ty);
  137.         /* Is the radius small enough for the particle to have an effect on our current one? */
  138.         if (radius < 35) {
  139.            /* We've the determine that the particle is close enough for relationship lines, so we set drawthis to true. */
  140.            drawthis = true;
  141.            /* If so, we proceed to calculate the angle. */
  142.            float angle = atan2(y-ty,x-tx);
  143.            /* Is the radius close enough to be deflected? */
  144.            if (radius < 30) {
  145.              /* Is relationship lines toggled? */
  146.              if (lines) {
  147.                /*
  148.                Redshade is increased by the distance * 5. The distance is multiplied by 10 because our radius will be within 40 pixels,
  149.                30 * 13.33... is 400
  150.                We invert it so it gets redder as the particle approaches the current particle, rather than vice versa.
  151.                */
  152.                redshade = 13 * (30 - radius);
  153.              }
  154.              /*
  155.              Here, we calculate a coordinate at a angle opposite of the direction where the other particle is.
  156.              0.07 is the strength of the particle's opposition, while (40 - radius) is how much it is effected, according to how close it is.
  157.              */
  158.              vx += (30 - radius) * 0.07 * cos(angle);
  159.              vy += (30 - radius) * 0.07 * sin(angle);
  160.            }
  161.            /*
  162.            Here we see if the particle is within 25 and 35 pixels of the current particle
  163.            (we already know that the particle is under 35 pixels distance, since this if statement is nested
  164.            in if (radius < 35), so rechecking is unnecessary
  165.            */
  166.            if (radius > 25) {
  167.              /* check to see if relationship lines are toggled */
  168.              if (lines) {
  169.                /* The blue shade, between 0 and 400, is used to show how much each particle is attracted */
  170.                blueshade = 40 * (35 - radius);
  171.              }
  172.              /* This does the opposite of the other check. It pulls the particle towards the other.  */
  173.              vx -= (25 - radius) * 0.005 * cos(angle);
  174.              vy -= (25 - radius) * 0.005 * sin(angle);
  175.            }
  176.         }
  177.         /* Check to see if relationship lines are enabled */
  178.         if (lines) {
  179.           /* Check to see if the two particles are close enough. */
  180.           if (drawthis) {
  181.              /* Set the stroke color */
  182.              stroke (redshade, 10, blueshade);
  183.              /* draw the line */
  184.              line(x,y,tx,ty);
  185.           }
  186.         }
  187.       }
  188.     }
  189.     
  190.     /* Check to see if the user is clicking */
  191.     if (mousePressed) {
  192.       /* The cursor's x and y coordinates. */
  193.       float tx = mx;
  194.       float ty = my;
  195.       /* the distance between the cursor and particle */
  196.       float radius = dist(x,y,tx,ty);
  197.       if (radius < 100) {
  198.         /* Calculate the angle between the particle and the cursor. */
  199.         float angle = atan2(ty-y,tx-x);
  200.         /* Are we left-clicking or not? */
  201.         if (mouseButton == LEFT) {
  202.           /* If left, then the particles are deflected */
  203.           vx -= radius * 0.07 * cos(angle);
  204.           vy -= radius * 0.07 * sin(angle);
  205.           /* The stroke color is red */
  206.           stroke(radius * 4,0,0);
  207.         }
  208.         else {
  209.           /* If right, the particles are attracted. */
  210.           vx += radius * 0.07 * cos(angle);
  211.           vy += radius * 0.07 * sin(angle);
  212.           /* The stroke color is blue */
  213.           stroke(0,0,radius * 4);
  214.         }
  215.         /* If line relationships are enabled, the lines are drawn. */
  216.         if (lines) line (x,y,tx,ty);
  217.       }
  218.     }  
  219.     /* Previox x and y coordinates are set, for drawing the trail */
  220.     int px = (int)x;
  221.     int py = (int)y;
  222.     /* Our particle's coordinates are updated. */
  223.     x += vx;
  224.     y += vy;
  225.     /* Gravity is applied. */
  226.     if (gravity == true) vy += 2;
  227.     /* Border collisions */
  228.     if (x > width-11) {
  229.       /* Reverse the velocity if towards wall */
  230.       if (abs(vx) == vx) vx *= -1.0;
  231.       /* The particle is placed back at the wall */
  232.       x = width-11;
  233.     }
  234.     if (x < 11) {
  235.       if (abs(vx) != vx) vx *= -1.0;
  236.       x = 11;
  237.     }
  238.     if (y < 11) {
  239.       if (abs(vy) != vy) vy *= -1.0;
  240.       y = 11;
  241.     }
  242.     if (y > height-11) {
  243.       if (abs(vy) == vy) vy *= -1.0;
  244.       vx *= 0.6;
  245.       y = height-11;
  246.     }
  247.     /* if relationship lines are disabled */
  248.     if (!lines) {
  249.       /* The stroke color is set to white */
  250.       stroke (400);
  251.       /* The particle is drawn */
  252.       line(px,py,int(x),int(y));
  253.     }
  254.   }
  255. /* User presses a key */
  256. void keyPressed() {
  257.   /* if they press g */
  258.   if (key == 'g' || key == 'G') {
  259.     /* we toggle gravity */
  260.     if (gravity) gravity = false;
  261.     else gravity = true;
  262.   }
  263.   /* otherwise */
  264.   else {
  265.     /* we toggle line relationships */
  266.     if (lines) lines = false;
  267.     else lines = true;
  268.   }
  269.   
  270. }
Many Thanks
Miles

Replies(20)

You've got an extra right curly bracket at line 103, which Processing reads as the end of that block; the rest of the class ends up on its own, and won't work. That should sort the active/static issue.
Hope that helps. 
Thank you, thats awesome,
now i just need to sort out the naming, it says that itcant find the coordx and coordy starting in setup()
Sincerely
Miles
Could you describe what you are trying to do? coordx and coordy are not defined anywhere above setup.
Um, OK, sorry, there are a few different issues going on here, not just naming. This seems like a big, complicated example if you haven't done any object oriented programming before- the Particle class has a lot of stuff going on. Not trying to put you down but I would suggest stripping the Particle class right back: for example, can you write a class that just follows the brightest pixel? Once you've got that you can start adding additional stuff and interactions between particles and so on. 

edit to add: It's probably also worth starting with just one or two particles, before putting them into an array as that adds an extra layer of complexity. 

If you haven't done so, have a look at the  Objects tutorial from the Processing site.
If you have specific questions I'll try and help, but there's too much going on there to be able to sort it out without rewriting a lot of your code. 
Hope that helps. 
Ah ok, thanks

yeah im used to max/msp so i had been going about it by putting parts together, but that doesnt work in processing.

I had this which worked as a sketch but not quite as i need it to.
The circle is tracking the closest pixel to pure green and im trying to use the x y tracking data to act as the mouse in the particle class, is that not possible with a couple of adaptations to the code? seen as both parts work simultaniously
sorry for sounding stubborn, im just running out of time for this.
obviously if it isnt possible then i will get stripping.

Copy code
  1. /**
  2. * Color Tracking
  3. * by Justin Brooks
  4. *
  5. *
  6. * based on original code:
  7. *   Brightness Tracking 
  8. *   by Golan Levin. 
  9. * Tracks the tracks pixel closest in color to specified color.
  10. */
  11. boolean lines = false;
  12. /*
  13. gravity toggle
  14. toggle this by pressing 'g'
  15. */
  16. boolean gravity = true;
  17. /* Particle count */
  18. int particleCount = 500;
  19. /* Particle array */
  20. Particle[] particles = new Particle[particleCount+1];

  21. import processing.video.*;

  22. Capture video;

  23. void setup() {
  24.  size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
  25.  // Uses the default video input, see the reference if this causes an error
  26.  video = new Capture(this, width, height, 30);
  27.  noStroke();
  28.  smooth();
  29.  colorMode(RGB, 400);
  30.   /* Frame rate is 30 */
  31.   frameRate(30);
  32.   /* The fill color is set to black (0) for the boundaries (drawn as a quad) */
  33.   fill (0);
  34.   /* We loop through our particle count and initialize each */
  35.   for (int x = particleCount; x >= 0; x--) {
  36.     particles[x] = new Particle();
  37.   }
  38. }

  39. void draw() {
  40.  if (video.available()) {
  41.    video.read();
  42.    background(0);
  43.   /* The stroke color is set to white for the border. */
  44.   stroke (400);
  45.   /* The border; it is 10 pixels from all sides */
  46.   quad (10,10,10,height-10,width-10,height-10,width-10,10); 
  47.   /* All of our particles are looped through, and updated */
  48.   for (int i = particleCount; i >= 0; i--) {
  49.     Particle particle = (Particle) particles[i];
  50.     particle.update(i);
  51.   }
  52.    int colorX = 0; // X-coordinate of the closest in color video pixel
  53.    int colorY = 0; // Y-coordinate of the closest in color video pixel
  54.    float closestColor = 10000; //we set this to be abritrarily large, once program runs, the first pixel it scans will be set to this value
  55.    // Search for the closest in color pixel: For each row of pixels in the video image and
  56.    // for each pixel in the yth row, compute each pixel's index in the video
  57.    video.loadPixels();
  58.    int index = 0;
  59.    for (int ay = 0; ay < video.height; ay++) {
  60.      for (int ax = 0; ax < video.width; ax++) {
  61.        // Get the color stored in the pixel
  62.        color pixelValue = video.pixels[index];
  63.        // Determine the color of the pixel
  64.        float colorProximity = abs(red(pixelValue)-0)+abs(green(pixelValue)-255)+abs(blue(pixelValue)-0);
  65.        // If that value is closer in color value than any previous, then store the
  66.        // color proximity of that pixel, as well as its (x,y) location
  67.        if (colorProximity < closestColor) {
  68.          closestColor = colorProximity;
  69.          closestColor=closestColor-10; //thoguht behind this is that it once it "locks" on to an object of color, it wont let go unless something a good bit better (closer in color) comes along
  70.          colorY = ay;
  71.          colorX = ax;
  72.        }
  73.        index++;
  74.      }
  75.    }
  76.    // Draw a large, yellow circle at the brightest pixel
  77.    fill(255, 204, 0, 128);
  78.    ellipse(colorX, colorY, 200, 200);
  79.  }
  80. }

  81. class Particle {
  82.   /*
  83.   Global class variables
  84.   These are kept track of, and aren't lost when the class particle is finished operating.
  85.   */
  86.   /* the x and y are our coordinates for each particles */
  87.   float x;
  88.   float y;
  89.   /* vx and vy are the velocities along each axis the particles are traveling */
  90.   float vx;
  91.   float vy;
  92.   /* Particle initialization. We define the beginning properties of each particle here. */
  93.   Particle() {
  94.     x = random(10,width-10);
  95.     y = random(10,height-10);
  96.   }
  97.   /* These are called everytime we check with the other particle's distances  */
  98.   float getx() {
  99.     return x;
  100.   }
  101.   float gety() {
  102.     return y;
  103.   }
  104.   void update(int num) {
  105.    /* Friction is simulated here */
  106.    vx *= 0.84;
  107.    vy *= 0.84;
  108.    /* Here, every particle is looped through and checked to see if they're close enough to have effect on the current particle. */
  109.    for (int i = particleCount; i >= 0; i--)  {
  110.      /* Check to see if the particle we're checking isn't the current particle. */
  111.      if (i != num) {
  112.         /* drawthis boolean is initialized. it determines whether the particle is close enough to the other particles for relationship lines to be drawn, assuming it's enabled */
  113.         boolean drawthis = false;
  114.         /*
  115.         Integers are used to keep track of the shade for each particle
  116.         The red shade shows opposition
  117.         The blue shade shows attraction
  118.         */
  119.         float redshade = 20;
  120.         float blueshade = 500;
  121.         /* We set our particle */
  122.         Particle particle = (Particle) particles[i];
  123.         /* The x and y coordinates of the particle is found, so we can compare distances with our current particle. */
  124.         float tx = particle.getx();
  125.         float ty = particle.gety();
  126.         /* The radius or distance between both particles are determined */
  127.         float radius = dist(x,y,tx,ty);
  128.         /* Is the radius small enough for the particle to have an effect on our current one? */
  129.         if (radius < 35) {
  130.            /* We've the determine that the particle is close enough for relationship lines, so we set drawthis to true. */
  131.            drawthis = true;
  132.            /* If so, we proceed to calculate the angle. */
  133.            float angle = atan2(y-ty,x-tx);
  134.            /* Is the radius close enough to be deflected? */
  135.            if (radius < 30) {
  136.              /* Is relationship lines toggled? */
  137.              if (lines) {
  138.                /*
  139.                Redshade is increased by the distance * 5. The distance is multiplied by 10 because our radius will be within 40 pixels,
  140.                30 * 13.33... is 400
  141.                We invert it so it gets redder as the particle approaches the current particle, rather than vice versa.
  142.                */
  143.                redshade = 13 * (30 - radius);
  144.              }
  145.              /*
  146.              Here, we calculate a coordinate at a angle opposite of the direction where the other particle is.
  147.              0.07 is the strength of the particle's opposition, while (40 - radius) is how much it is effected, according to how close it is.
  148.              */
  149.              vx += (30 - radius) * 0.07 * cos(angle);
  150.              vy += (30 - radius) * 0.07 * sin(angle);
  151.            }
  152.            /*
  153.            Here we see if the particle is within 25 and 35 pixels of the current particle
  154.            (we already know that the particle is under 35 pixels distance, since this if statement is nested
  155.            in if (radius < 35), so rechecking is unnecessary
  156.            */
  157.            if (radius > 25) {
  158.              /* check to see if relationship lines are toggled */
  159.              if (lines) {
  160.                /* The blue shade, between 0 and 400, is used to show how much each particle is attracted */
  161.                blueshade = 40 * (35 - radius);
  162.              }
  163.              /* This does the opposite of the other check. It pulls the particle towards the other.  */
  164.              vx -= (25 - radius) * 0.005 * cos(angle);
  165.              vy -= (25 - radius) * 0.005 * sin(angle);
  166.            }
  167.         }
  168.         /* Check to see if relationship lines are enabled */
  169.         if (lines) {
  170.           /* Check to see if the two particles are close enough. */
  171.           if (drawthis) {
  172.              /* Set the stroke color */
  173.              stroke (redshade, 10, blueshade);
  174.              /* draw the line */
  175.              line(x,y,tx,ty);
  176.           }
  177.         }
  178.       }
  179.     }
  180.     /* Check to see if the user is clicking */
  181.     if (mousePressed) {
  182.       /* The cursor's x and y coordinates. */
  183.       float tx = mouseX;
  184.       float ty = mouseY;
  185.       /* the distance between the cursor and particle */
  186.       float radius = dist(x,y,tx,ty);
  187.       if (radius < 100) {
  188.         /* Calculate the angle between the particle and the cursor. */
  189.         float angle = atan2(ty-y,tx-x);
  190.         /* Are we left-clicking or not? */
  191.         if (mouseButton == LEFT) {
  192.           /* If left, then the particles are deflected */
  193.           vx -= radius * 0.07 * cos(angle);
  194.           vy -= radius * 0.07 * sin(angle);
  195.           /* The stroke color is red */
  196.           stroke(radius * 4,0,0);
  197.         }
  198.         else {
  199.           /* If right, the particles are attracted. */
  200.           vx += radius * 0.07 * cos(angle);
  201.           vy += radius * 0.07 * sin(angle);
  202.           /* The stroke color is blue */
  203.           stroke(0,0,radius * 4);
  204.         }
  205.         /* If line relationships are enabled, the lines are drawn. */
  206.         if (lines) line (x,y,tx,ty);
  207.       }
  208.     }  
  209.     /* Previox x and y coordinates are set, for drawing the trail */
  210.     int px = (int)x;
  211.     int py = (int)y;
  212.     /* Our particle's coordinates are updated. */
  213.     x += vx;
  214.     y += vy;
  215.     /* Gravity is applied. */
  216.     if (gravity == true) vy += 2;
  217.     /* Border collisions */
  218.     if (x > width-11) {
  219.       /* Reverse the velocity if towards wall */
  220.       if (abs(vx) == vx) vx *= -1.0;
  221.       /* The particle is placed back at the wall */
  222.       x = width-11;
  223.     }
  224.     if (x < 11) {
  225.       if (abs(vx) != vx) vx *= -1.0;
  226.       x = 11;
  227.     }
  228.     if (y < 11) {
  229.       if (abs(vy) != vy) vy *= -1.0;
  230.       y = 11;
  231.     }
  232.     if (y > height-11) {
  233.       if (abs(vy) == vy) vy *= -1.0;
  234.       vx *= 0.6;
  235.       y = height-11;
  236.     }
  237.     /* if relationship lines are disabled */
  238.     if (!lines) {
  239.       /* The stroke color is set to white */
  240.       stroke (400);
  241.       /* The particle is drawn */
  242.       line(px,py,int(x),int(y));
  243.     }
  244.   }
  245. /* User presses a key */
  246. void keyPressed() {
  247.   /* if they press g */
  248.   if (key == 'g' || key == 'G') {
  249.     /* we toggle gravity */
  250.     if (gravity) gravity = false;
  251.     else gravity = true;
  252.   }
  253.   /* otherwise */
  254.   else {
  255.     /* we toggle line relationships */
  256.     if (lines) lines = false;
  257.     else lines = true;
  258.   }
  259.   
  260. }
Many Thanks
Miles
I move this topic to Core libraries as it involves Capture (lot of people just cannot run your code as it needs a webcam).
I deleted the duplicate topic you created as it got no answers (except mine) and it was more imprecise than this one.
OK, having something working is an excellent place to start. I've not looked at your colour tracking, but getting the attractor to follow the mouse seems reasonable. I've just changed a couple of things, and I put my comments IN CAPS so you can see what I've done:

Copy code

  1. /**
  2.  * Color Tracking
  3.  * by Justin Brooks
  4.  *
  5.  *
  6.  * based on original code:
  7.  *   Brightness Tracking 
  8.  *   by Golan Levin. 
  9.  * 
  10.  * Tracks the tracks pixel closest in color to specified color.
  11.  */
  12. boolean lines = false;
  13. /*
  14. gravity toggle
  15.  toggle this by pressing 'g'
  16.  */
  17. boolean gravity = false;
  18. /* Particle count */
  19. int particleCount = 500;
  20. /* Particle array */
  21. Particle[] particles = new Particle[particleCount+1];

  22. //DECLARE THESE HERE SO THEY'RE ACCESSIBLE GLOBALLY
  23. int colorX = 0; // X-coordinate of the closest in color video pixel
  24. int colorY = 0; // Y-coordinate of the closest in color video pixel

  25. import processing.video.*;

  26. Capture video;

  27. void setup() {
  28.   size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
  29.   // Uses the default video input, see the reference if this causes an error
  30.   video = new Capture(this, width, height, 30);
  31.   noStroke();
  32.   smooth();
  33.   colorMode(RGB, 400);
  34.   /* Frame rate is 30 */
  35.   frameRate(30);
  36.   /* The fill color is set to black (0) for the boundaries (drawn as a quad) */
  37.   fill (0);
  38.   /* We loop through our particle count and initialize each */
  39.   for (int x = particleCount; x >= 0; x--) {
  40.     particles[x] = new Particle();
  41.   }
  42. }

  43. void draw() {
  44.   if (video.available()) {
  45.     video.read();
  46.     background(0);
  47.     /* The stroke color is set to white for the border. */
  48.     stroke (400);
  49.     /* The border; it is 10 pixels from all sides */
  50.     quad (10,10,10,height-10,width-10,height-10,width-10,10); 
  51.     /* All of our particles are looped through, and updated */
  52.     for (int i = particleCount; i >= 0; i--) {
  53.       Particle particle = (Particle) particles[i];
  54.       particle.update(i);
  55.     }
  56.     
  57.     //REMOVE THE INTS SINCE WE INITIALISED THEM BEFORE SETUP
  58.     colorX = 0; // X-coordinate of the closest in color video pixel
  59.     colorY = 0; // Y-coordinate of the closest in color video pixel
  60.     float closestColor = 10000; //we set this to be abritrarily large, once program runs, the first pixel it scans will be set to this value
  61.     // Search for the closest in color pixel: For each row of pixels in the video image and
  62.     // for each pixel in the yth row, compute each pixel's index in the video
  63.     video.loadPixels();
  64.     int index = 0;
  65.     for (int ay = 0; ay < video.height; ay++) {
  66.       for (int ax = 0; ax < video.width; ax++) {
  67.         // Get the color stored in the pixel
  68.         color pixelValue = video.pixels[index];
  69.         // Determine the color of the pixel
  70.         float colorProximity = abs(red(pixelValue)-0)+abs(green(pixelValue)-255)+abs(blue(pixelValue)-0);
  71.         // If that value is closer in color value than any previous, then store the
  72.         // color proximity of that pixel, as well as its (x,y) location
  73.         if (colorProximity < closestColor) {
  74.           closestColor = colorProximity;
  75.           closestColor=closestColor-10; //thoguht behind this is that it once it "locks" on to an object of color, it wont let go unless something a good bit better (closer in color) comes along
  76.           colorY = ay;
  77.           colorX = ax;
  78.         }
  79.         index++;
  80.       }
  81.     }
  82.     // Draw a large, yellow circle at the brightest pixel
  83.     fill(255, 204, 0, 128);
  84.     ellipse(colorX, colorY, 200, 200);
  85.   }
  86. }

  87. class Particle {
  88.   /*
  89.   Global class variables
  90.    These are kept track of, and aren't lost when the class particle is finished operating.
  91.    */
  92.   /* the x and y are our coordinates for each particles */
  93.   float x;
  94.   float y;
  95.   /* vx and vy are the velocities along each axis the particles are traveling */
  96.   float vx;
  97.   float vy;
  98.   /* Particle initialization. We define the beginning properties of each particle here. */
  99.   Particle() {
  100.     x = random(10,width-10);
  101.     y = random(10,height-10);
  102.   }
  103.   /* These are called everytime we check with the other particle's distances  */
  104.   float getx() {
  105.     return x;
  106.   }
  107.   float gety() {
  108.     return y;
  109.   }
  110.   void update(int num) {
  111.     /* Friction is simulated here */
  112.     vx *= 0.84;
  113.     vy *= 0.84;
  114.     /* Here, every particle is looped through and checked to see if they're close enough to have effect on the current particle. */
  115.     for (int i = particleCount; i >= 0; i--) {
  116.       /* Check to see if the particle we're checking isn't the current particle. */
  117.       if (i != num) {
  118.         /* drawthis boolean is initialized. it determines whether the particle is close enough to the other particles for relationship lines to be drawn, assuming it's enabled */
  119.         boolean drawthis = false;
  120.         /*
  121.         Integers are used to keep track of the shade for each particle
  122.          The red shade shows opposition
  123.          The blue shade shows attraction
  124.          */
  125.         float redshade = 20;
  126.         float blueshade = 500;
  127.         /* We set our particle */
  128.         Particle particle = (Particle) particles[i];
  129.         /* The x and y coordinates of the particle is found, so we can compare distances with our current particle. */
  130.         float tx = particle.getx();
  131.         float ty = particle.gety();
  132.         /* The radius or distance between both particles are determined */
  133.         float radius = dist(x,y,tx,ty);
  134.         /* Is the radius small enough for the particle to have an effect on our current one? */
  135.         if (radius < 35) {
  136.           /* We've the determine that the particle is close enough for relationship lines, so we set drawthis to true. */
  137.           drawthis = true;
  138.           /* If so, we proceed to calculate the angle. */
  139.           float angle = atan2(y-ty,x-tx);
  140.           /* Is the radius close enough to be deflected? */
  141.           if (radius < 30) {
  142.             /* Is relationship lines toggled? */
  143.             if (lines) {
  144.               /*
  145.                Redshade is increased by the distance * 5. The distance is multiplied by 10 because our radius will be within 40 pixels,
  146.                30 * 13.33... is 400
  147.                We invert it so it gets redder as the particle approaches the current particle, rather than vice versa.
  148.                */
  149.               redshade = 13 * (30 - radius);
  150.             }
  151.             /*
  152.              Here, we calculate a coordinate at a angle opposite of the direction where the other particle is.
  153.              0.07 is the strength of the particle's opposition, while (40 - radius) is how much it is effected, according to how close it is.
  154.              */
  155.             vx += (30 - radius) * 0.07 * cos(angle);
  156.             vy += (30 - radius) * 0.07 * sin(angle);
  157.           }
  158.           /*
  159.            Here we see if the particle is within 25 and 35 pixels of the current particle
  160.            (we already know that the particle is under 35 pixels distance, since this if statement is nested
  161.            in if (radius < 35), so rechecking is unnecessary
  162.            */
  163.           if (radius > 25) {
  164.             /* check to see if relationship lines are toggled */
  165.             if (lines) {
  166.               /* The blue shade, between 0 and 400, is used to show how much each particle is attracted */
  167.               blueshade = 40 * (35 - radius);
  168.             }
  169.             /* This does the opposite of the other check. It pulls the particle towards the other.  */
  170.             vx -= (25 - radius) * 0.005 * cos(angle);
  171.             vy -= (25 - radius) * 0.005 * sin(angle);
  172.           }
  173.         }
  174.         /* Check to see if relationship lines are enabled */
  175.         if (lines) {
  176.           /* Check to see if the two particles are close enough. */
  177.           if (drawthis) {
  178.             /* Set the stroke color */
  179.             stroke (redshade, 10, blueshade);
  180.             /* draw the line */
  181.             line(x,y,tx,ty);
  182.           }
  183.         }
  184.       }
  185.     }
  186.     /* Check to see if the user is clicking */
  187.     if (mousePressed) {
  188.       /* The cursor's x and y coordinates. */
  189.       //CHANGE FROM MOUSE COORDINATES TO colorX and colorY WHICH DEFINE THE ELLIPSE'S LOCATION ABOVE (IN DRAW)
  190.       float tx = colorX;
  191.       float ty = colorY;
  192.       /* the distance between the cursor and particle */
  193.       float radius = dist(x,y,tx,ty);
  194.       if (radius < 100) {
  195.         /* Calculate the angle between the particle and the cursor. */
  196.         float angle = atan2(ty-y,tx-x);
  197.         /* Are we left-clicking or not? */
  198.         if (mouseButton == LEFT) {
  199.           /* If left, then the particles are deflected */
  200.           vx -= radius * 0.07 * cos(angle);
  201.           vy -= radius * 0.07 * sin(angle);
  202.           /* The stroke color is red */
  203.           stroke(radius * 4,0,0);
  204.         }
  205.         else {
  206.           /* If right, the particles are attracted. */
  207.           vx += radius * 0.07 * cos(angle);
  208.           vy += radius * 0.07 * sin(angle);
  209.           /* The stroke color is blue */
  210.           stroke(0,0,radius * 4);
  211.         }
  212.         /* If line relationships are enabled, the lines are drawn. */
  213.         if (lines) line (x,y,tx,ty);
  214.       }
  215.     }  
  216.     /* Previox x and y coordinates are set, for drawing the trail */
  217.     int px = (int)x;
  218.     int py = (int)y;
  219.     /* Our particle's coordinates are updated. */
  220.     x += vx;
  221.     y += vy;
  222.     /* Gravity is applied. */
  223.     if (gravity == true) vy += 2;
  224.     /* Border collisions */
  225.     if (x > width-11) {
  226.       /* Reverse the velocity if towards wall */
  227.       if (abs(vx) == vx) vx *= -1.0;
  228.       /* The particle is placed back at the wall */
  229.       x = width-11;
  230.     }
  231.     if (x < 11) {
  232.       if (abs(vx) != vx) vx *= -1.0;
  233.       x = 11;
  234.     }
  235.     if (y < 11) {
  236.       if (abs(vy) != vy) vy *= -1.0;
  237.       y = 11;
  238.     }
  239.     if (y > height-11) {
  240.       if (abs(vy) == vy) vy *= -1.0;
  241.       vx *= 0.6;
  242.       y = height-11;
  243.     }
  244.     /* if relationship lines are disabled */
  245.     if (!lines) {
  246.       /* The stroke color is set to white */
  247.       stroke (400);
  248.       /* The particle is drawn */
  249.       line(px,py,int(x),int(y));
  250.     }
  251.   }
  252. /* User presses a key */
  253. void keyPressed() {
  254.   /* if they press g */
  255.   if (key == 'g' || key == 'G') {
  256.     /* we toggle gravity */
  257.     if (gravity) gravity = false;
  258.     else gravity = true;
  259.   }
  260.   /* otherwise */
  261.   else {
  262.     /* we toggle line relationships */
  263.     if (lines) lines = false;
  264.     else lines = true;
  265.   }
  266. }
There's only three points where I've changed/added anything.
Hope that helps
Cheers

Thank you so much
that is just what im trying to do
i should be able to use the same process so it can have a threshold for the color tracking which can act as the right mouse click
cheers
Miles
Hi there, thanks again for your help,
Ive managed to use osc now to export the xy data, but really i need to export the amount of attracted particles, 
Im not really too sure if there is any point where this number exists within the code

Copy code


  1. /**
  2.  * Color Tracking
  3.  * by Justin Brooks
  4.  *
  5.  *
  6.  * based on original code:
  7.  *   Brightness Tracking 
  8.  *   by Golan Levin. 
  9.  * 
  10.  * Tracks the tracks pixel closest in color to specified color.
  11.  */
  12. boolean lines = false;
  13. /*
  14. gravity toggle
  15.  toggle this by pressing 'g'
  16.  */
  17. boolean gravity = false;
  18. boolean rightclick = false;
  19. /* Particle count */
  20. int particleCount = 500;
  21. int pcounter = 0;
  22. /* Particle array */
  23. Particle[] particles = new Particle[particleCount+1];

  24. //DECLARE THESE HERE SO THEY'RE ACCESSIBLE GLOBALLY
  25. int colorX = 0; // X-coordinate of the closest in color video pixel
  26. int colorY = 0; // Y-coordinate of the closest in color video pixel
  27. import oscP5.*;
  28. import netP5.*;
  29.   
  30. OscP5 oscP5;
  31. NetAddress myRemoteLocation;
  32. import processing.video.*;

  33. Capture video;

  34. void setup() {
  35.   size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
  36.   // Uses the default video input, see the reference if this causes an error
  37.   video = new Capture(this, width, height, 30);
  38.   noStroke();
  39.   smooth();
  40.   colorMode(RGB, 400);
  41.   /* Frame rate is 30 */
  42.   frameRate(30);
  43.   /* The fill color is set to black (0) for the boundaries (drawn as a quad) */
  44.   fill (0);
  45.   oscP5 = new OscP5(this,7474);
  46.   myRemoteLocation = new NetAddress("127.0.0.1",7474);
  47.   /* We loop through our particle count and initialize each */
  48.   for (int x = particleCount; x >= 0; x--) {
  49.     particles[x] = new Particle();
  50.   }
  51. }

  52. void draw() {
  53.   if (video.available()) {
  54.     video.read();
  55.     background(0);
  56.     /* The stroke color is set to white for the border. */
  57.     stroke (400);
  58.     /* The border; it is 10 pixels from all sides */
  59.     quad (10,10,10,height-10,width-10,height-10,width-10,10); 
  60.     /* All of our particles are looped through, and updated */
  61.     for (int i = particleCount; i >= 0; i--) {
  62.       Particle particle = (Particle) particles[i];
  63.       particle.update(i);
  64.     }
  65.     
  66.     //REMOVE THE INTS SINCE WE INITIALISED THEM BEFORE SETUP
  67.     colorX = 0; // X-coordinate of the closest in color video pixel
  68.     colorY = 0; // Y-coordinate of the closest in color video pixel
  69.     float closestColor = 10000; //we set this to be abritrarily large, once program runs, the first pixel it scans will be set to this value
  70.     // Search for the closest in color pixel: For each row of pixels in the video image and
  71.     // for each pixel in the yth row, compute each pixel's index in the video
  72.     video.loadPixels();
  73.     int index = 0;
  74.     for (int ay = 0; ay < video.height; ay++) {
  75.       for (int ax = 0; ax < video.width; ax++) {
  76.         // Get the color stored in the pixel
  77.         color pixelValue = video.pixels[index];
  78.         // Determine the color of the pixel
  79.         float colorProximity = abs(red(pixelValue)-0)+abs(green(pixelValue)-255)+abs(blue(pixelValue)-0);
  80.         // If that value is closer in color value than any previous, then store the
  81.         // color proximity of that pixel, as well as its (x,y) location
  82.         if (colorProximity < closestColor) {
  83.           closestColor = colorProximity;
  84.           closestColor=closestColor-10; //thoguht behind this is that it once it "locks" on to an object of color, it wont let go unless something a good bit better (closer in color) comes along
  85.           colorY = ay;
  86.           colorX = ax;
  87.           rightclick = true;
  88.         }
  89.         index++;
  90.       }
  91.     }
  92.     OscMessage myMessage = new OscMessage("/test");     
  93.     myMessage.add((float)colorX);
  94.     myMessage.add(1. - (float)colorY);
  95.     myMessage.add(2. - (float)pcounter);    
  96.     /* send the message */
  97.     oscP5.send(myMessage, myRemoteLocation);
  98.     // Draw a large, yellow circle at the brightest pixel
  99.     fill(255, 204, 0, 128);
  100.     ellipse(colorX, colorY, 200, 200);
  101.   }
  102. }

  103. class Particle {
  104.   /*
  105.   Global class variables
  106.    These are kept track of, and aren't lost when the class particle is finished operating.
  107.    */
  108.   /* the x and y are our coordinates for each particles */
  109.   float x;
  110.   float y;
  111.   /* vx and vy are the velocities along each axis the particles are traveling */
  112.   float vx;
  113.   float vy;
  114.   /* Particle initialization. We define the beginning properties of each particle here. */
  115.   Particle() {
  116.     x = random(10,width-10);
  117.     y = random(10,height-10);
  118.   }
  119.   /* These are called everytime we check with the other particle's distances  */
  120.   float getx() {
  121.     return x;
  122.   }
  123.   float gety() {
  124.     return y;
  125.   }
  126.   void update(int num) {
  127.     /* Friction is simulated here */
  128.     vx *= 0.84;
  129.     vy *= 0.84;
  130.     /* Here, every particle is looped through and checked to see if they're close enough to have effect on the current particle. */
  131.     for (int i = particleCount; i >= 0; i--) {
  132.       /* Check to see if the particle we're checking isn't the current particle. */
  133.       if (i != num) {
  134.         /* drawthis boolean is initialized. it determines whether the particle is close enough to the other particles for relationship lines to be drawn, assuming it's enabled */
  135.         boolean drawthis = false;
  136.         /*
  137.         Integers are used to keep track of the shade for each particle
  138.          The red shade shows opposition
  139.          The blue shade shows attraction
  140.          */
  141.         float redshade = 20;
  142.         float blueshade = 500;
  143.         /* We set our particle */
  144.         Particle particle = (Particle) particles[i];
  145.         /* The x and y coordinates of the particle is found, so we can compare distances with our current particle. */
  146.         float tx = particle.getx();
  147.         float ty = particle.gety();
  148.         /* The radius or distance between both particles are determined */
  149.         float radius = dist(x,y,tx,ty);
  150.         /* Is the radius small enough for the particle to have an effect on our current one? */
  151.         if (radius < 35) {
  152.           /* We've the determine that the particle is close enough for relationship lines, so we set drawthis to true. */
  153.           drawthis = true;
  154.           /* If so, we proceed to calculate the angle. */
  155.           float angle = atan2(y-ty,x-tx);
  156.           /* Is the radius close enough to be deflected? */
  157.           if (radius < 30) {
  158.             /* Is relationship lines toggled? */
  159.             if (lines) {
  160.               /*
  161.                Redshade is increased by the distance * 5. The distance is multiplied by 10 because our radius will be within 40 pixels,
  162.                30 * 13.33... is 400
  163.                We invert it so it gets redder as the particle approaches the current particle, rather than vice versa.
  164.                */
  165.               redshade = 13 * (30 - radius);
  166.             }
  167.             /*
  168.              Here, we calculate a coordinate at a angle opposite of the direction where the other particle is.
  169.              0.07 is the strength of the particle's opposition, while (40 - radius) is how much it is effected, according to how close it is.
  170.              */
  171.             vx += (30 - radius) * 0.07 * cos(angle);
  172.             vy += (30 - radius) * 0.07 * sin(angle);
  173.           }
  174.           /*
  175.            Here we see if the particle is within 25 and 35 pixels of the current particle
  176.            (we already know that the particle is under 35 pixels distance, since this if statement is nested
  177.            in if (radius < 35), so rechecking is unnecessary
  178.            */
  179.           if (radius > 25) {
  180.             /* check to see if relationship lines are toggled */
  181.             if (lines) {
  182.               /* The blue shade, between 0 and 400, is used to show how much each particle is attracted */
  183.               blueshade = 40 * (35 - radius);
  184.             }
  185.             /* This does the opposite of the other check. It pulls the particle towards the other.  */
  186.             vx -= (25 - radius) * 0.005 * cos(angle);
  187.             vy -= (25 - radius) * 0.005 * sin(angle);
  188.           }
  189.         }
  190.         /* Check to see if relationship lines are enabled */
  191.         if (lines) {
  192.           /* Check to see if the two particles are close enough. */
  193.           if (drawthis) {
  194.             /* Set the stroke color */
  195.             stroke (redshade, 10, blueshade);
  196.             /* draw the line */
  197.             line(x,y,tx,ty);
  198.           }
  199.         }
  200.       }
  201.     }
  202.     /* Check to see if the user is clicking */
  203.     if (rightclick = true) {
  204.       /* The cursor's x and y coordinates. */
  205.       //CHANGE FROM MOUSE COORDINATES TO colorX and colorY WHICH DEFINE THE ELLIPSE'S LOCATION ABOVE (IN DRAW)
  206.       float tx = colorX;
  207.       float ty = colorY;
  208.       /* the distance between the cursor and particle */
  209.       float radius = dist(x,y,tx,ty);
  210.       if (radius < 100) {
  211.         /* Calculate the angle between the particle and the cursor. */
  212.         float angle = atan2(ty-y,tx-x);
  213.         /* Are we left-clicking or not? */
  214.         if (mouseButton == LEFT) {
  215.           /* If left, then the particles are deflected */
  216.           vx -= radius * 0.07 * cos(angle);
  217.           vy -= radius * 0.07 * sin(angle);
  218.           /* The stroke color is red */
  219.           stroke(radius * 4,0,0);
  220.         }
  221.         else {
  222.           /* If right, the particles are attracted. */
  223.           vx += radius * 0.07 * cos(angle);
  224.           vy += radius * 0.07 * sin(angle);
  225.           /* The stroke color is blue */
  226.           stroke(0,0,radius * 4);
  227.         }
  228.         /* If line relationships are enabled, the lines are drawn. */
  229.         if (lines) line (x,y,tx,ty);
  230.       }
  231.     }  
  232.     /* Previox x and y coordinates are set, for drawing the trail */
  233.     int px = (int)x;
  234.     int py = (int)y;
  235.     /* Our particle's coordinates are updated. */
  236.     x += vx;
  237.     y += vy;
  238.     /* Gravity is applied. */
  239.     if (gravity == true) vy += 2;
  240.     /* Border collisions */
  241.     if (x > width-11) {
  242.       /* Reverse the velocity if towards wall */
  243.       if (abs(vx) == vx) vx *= -1.0;
  244.       /* The particle is placed back at the wall */
  245.       x = width-11;
  246.     }
  247.     if (x < 11) {
  248.       if (abs(vx) != vx) vx *= -1.0;
  249.       x = 11;
  250.     }
  251.     if (y < 11) {
  252.       if (abs(vy) != vy) vy *= -1.0;
  253.       y = 11;
  254.     }
  255.     if (y > height-11) {
  256.       if (abs(vy) == vy) vy *= -1.0;
  257.       vx *= 0.6;
  258.       y = height-11;
  259.     }
  260.     /* if relationship lines are disabled */
  261.     if (!lines) {
  262.       /* The stroke color is set to white */
  263.       stroke (400);
  264.       /* The particle is drawn */
  265.       line(px,py,int(x),int(y));
  266.     }
  267.   }
  268. /* User presses a key */
  269. void keyPressed() {
  270.   /* if they press g */
  271.   if (key == 'g' || key == 'G') {
  272.     /* we toggle gravity */
  273.     if (gravity) gravity = false;
  274.     else gravity = true;
  275.   }
  276.   /* otherwise */
  277.   else {
  278.     /* we toggle line relationships */
  279.     if (lines) lines = false;
  280.     else lines = true;
  281.   }
  282. }



Regards
Miles
OK, the number of affected particles isn't there as such but since you're already testing whether the particle is affected it's simple to add a counter. I've added it in below; again, my additions are commented in caps. Only four or five lines, 
Copy code



  1. /**
  2.  * Color Tracking
  3.  * by Justin Brooks
  4.  *
  5.  *
  6.  * based on original code:
  7.  *   Brightness Tracking 
  8.  *   by Golan Levin. 
  9.  * 
  10.  * Tracks the tracks pixel closest in color to specified color.
  11.  */
  12. boolean lines = false;
  13. /*
  14. gravity toggle
  15.  toggle this by pressing 'g'
  16.  */
  17. boolean gravity = false;
  18. boolean rightclick = false;
  19. /* Particle count */
  20. int particleCount = 500;
  21. int pcounter = 0;
  22. /* Particle array */
  23. Particle[] particles = new Particle[particleCount+1];

  24. //DECLARE THESE HERE SO THEY'RE ACCESSIBLE GLOBALLY
  25. int colorX = 0; // X-coordinate of the closest in color video pixel
  26. int colorY = 0; // Y-coordinate of the closest in color video pixel
  27. import oscP5.*;
  28. import netP5.*;

  29. OscP5 oscP5;
  30. NetAddress myRemoteLocation;
  31. import processing.video.*;

  32. Capture video;
  33. int affectedCount=0;//DECLARE HERE SO ACCESSIBLE EVERYWHERE

  34. void setup() {
  35.   size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
  36.   // Uses the default video input, see the reference if this causes an error
  37.   video = new Capture(this, width, height, 30);
  38.   noStroke();
  39.   smooth();
  40.   colorMode(RGB, 400);
  41.   /* Frame rate is 30 */
  42.   frameRate(30);
  43.   /* The fill color is set to black (0) for the boundaries (drawn as a quad) */
  44.   fill (0);
  45.   oscP5 = new OscP5(this,7474);
  46.   myRemoteLocation = new NetAddress("127.0.0.1",7474);
  47.   /* We loop through our particle count and initialize each */
  48.   for (int x = particleCount; x >= 0; x--) {
  49.     particles[x] = new Particle();
  50.   }
  51. }

  52. void draw() {
  53.   if (video.available()) {
  54.     video.read();
  55.     background(0);
  56.     affectedCount=0;//RESET COUNTER AT THE START OF EACH FRAME
  57.     /* The stroke color is set to white for the border. */
  58.     stroke (400);
  59.     /* The border; it is 10 pixels from all sides */
  60.     quad (10,10,10,height-10,width-10,height-10,width-10,10); 
  61.     /* All of our particles are looped through, and updated */
  62.     for (int i = particleCount; i >= 0; i--) {
  63.       Particle particle = (Particle) particles[i];
  64.       particle.update(i);
  65.     }

  66.     //REMOVE THE INTS SINCE WE INITIALISED THEM BEFORE SETUP
  67.     colorX = 0; // X-coordinate of the closest in color video pixel
  68.     colorY = 0; // Y-coordinate of the closest in color video pixel
  69.     float closestColor = 10000; //we set this to be abritrarily large, once program runs, the first pixel it scans will be set to this value
  70.     // Search for the closest in color pixel: For each row of pixels in the video image and
  71.     // for each pixel in the yth row, compute each pixel's index in the video
  72.     video.loadPixels();
  73.     int index = 0;
  74.     for (int ay = 0; ay < video.height; ay++) {
  75.       for (int ax = 0; ax < video.width; ax++) {
  76.         // Get the color stored in the pixel
  77.         color pixelValue = video.pixels[index];
  78.         // Determine the color of the pixel
  79.         float colorProximity = abs(red(pixelValue)-0)+abs(green(pixelValue)-255)+abs(blue(pixelValue)-0);
  80.         // If that value is closer in color value than any previous, then store the
  81.         // color proximity of that pixel, as well as its (x,y) location
  82.         if (colorProximity < closestColor) {
  83.           closestColor = colorProximity;
  84.           closestColor=closestColor-10; //thoguht behind this is that it once it "locks" on to an object of color, it wont let go unless something a good bit better (closer in color) comes along
  85.           colorY = ay;
  86.           colorX = ax;
  87.           rightclick = true;
  88.         }
  89.         index++;
  90.       }
  91.     }
  92.     println(affectedCount);//DO WHATEVER WITH THIS 
  93.     OscMessage myMessage = new OscMessage("/test");     
  94.     myMessage.add((float)colorX);
  95.     myMessage.add(1. - (float)colorY);
  96.     myMessage.add(2. - (float)pcounter);    
  97.     /* send the message */
  98.     oscP5.send(myMessage, myRemoteLocation);
  99.     // Draw a large, yellow circle at the brightest pixel
  100.     fill(255, 204, 0, 128);
  101.     ellipse(colorX, colorY, 200, 200);
  102.   }
  103. }

  104. class Particle {
  105.   /*
  106.   Global class variables
  107.    These are kept track of, and aren't lost when the class particle is finished operating.
  108.    */
  109.   /* the x and y are our coordinates for each particles */
  110.   float x;
  111.   float y;
  112.   /* vx and vy are the velocities along each axis the particles are traveling */
  113.   float vx;
  114.   float vy;
  115.   /* Particle initialization. We define the beginning properties of each particle here. */
  116.   Particle() {
  117.     x = random(10,width-10);
  118.     y = random(10,height-10);
  119.   }
  120.   /* These are called everytime we check with the other particle's distances  */
  121.   float getx() {
  122.     return x;
  123.   }
  124.   float gety() {
  125.     return y;
  126.   }
  127.   void update(int num) {
  128.     /* Friction is simulated here */
  129.     vx *= 0.84;
  130.     vy *= 0.84;
  131.     /* Here, every particle is looped through and checked to see if they're close enough to have effect on the current particle. */
  132.     for (int i = particleCount; i >= 0; i--) {
  133.       /* Check to see if the particle we're checking isn't the current particle. */
  134.       if (i != num) {
  135.         /* drawthis boolean is initialized. it determines whether the particle is close enough to the other particles for relationship lines to be drawn, assuming it's enabled */
  136.         boolean drawthis = false;
  137.         /*
  138.         Integers are used to keep track of the shade for each particle
  139.          The red shade shows opposition
  140.          The blue shade shows attraction
  141.          */
  142.         float redshade = 20;
  143.         float blueshade = 500;
  144.         /* We set our particle */
  145.         Particle particle = (Particle) particles[i];
  146.         /* The x and y coordinates of the particle is found, so we can compare distances with our current particle. */
  147.         float tx = particle.getx();
  148.         float ty = particle.gety();
  149.         /* The radius or distance between both particles are determined */
  150.         float radius = dist(x,y,tx,ty);
  151.         /* Is the radius small enough for the particle to have an effect on our current one? */
  152.         if (radius < 35) {
  153.           /* We've the determine that the particle is close enough for relationship lines, so we set drawthis to true. */
  154.           drawthis = true;
  155.           /* If so, we proceed to calculate the angle. */
  156.           float angle = atan2(y-ty,x-tx);
  157.           /* Is the radius close enough to be deflected? */
  158.           if (radius < 30) {
  159.             /* Is relationship lines toggled? */
  160.             if (lines) {
  161.               /*
  162.                Redshade is increased by the distance * 5. The distance is multiplied by 10 because our radius will be within 40 pixels,
  163.                30 * 13.33... is 400
  164.                We invert it so it gets redder as the particle approaches the current particle, rather than vice versa.
  165.                */
  166.               redshade = 13 * (30 - radius);
  167.             }
  168.             /*
  169.              Here, we calculate a coordinate at a angle opposite of the direction where the other particle is.
  170.              0.07 is the strength of the particle's opposition, while (40 - radius) is how much it is effected, according to how close it is.
  171.              */
  172.             vx += (30 - radius) * 0.07 * cos(angle);
  173.             vy += (30 - radius) * 0.07 * sin(angle);
  174.           }
  175.           /*
  176.            Here we see if the particle is within 25 and 35 pixels of the current particle
  177.            (we already know that the particle is under 35 pixels distance, since this if statement is nested
  178.            in if (radius < 35), so rechecking is unnecessary
  179.            */
  180.           if (radius > 25) {
  181.             /* check to see if relationship lines are toggled */
  182.             if (lines) {
  183.               /* The blue shade, between 0 and 400, is used to show how much each particle is attracted */
  184.               blueshade = 40 * (35 - radius);
  185.             }
  186.             /* This does the opposite of the other check. It pulls the particle towards the other.  */
  187.             vx -= (25 - radius) * 0.005 * cos(angle);
  188.             vy -= (25 - radius) * 0.005 * sin(angle);
  189.           }
  190.         }
  191.         /* Check to see if relationship lines are enabled */
  192.         if (lines) {
  193.           /* Check to see if the two particles are close enough. */
  194.           if (drawthis) {
  195.             /* Set the stroke color */
  196.             stroke (redshade, 10, blueshade);
  197.             /* draw the line */
  198.             line(x,y,tx,ty);
  199.           }
  200.         }
  201.       }
  202.     }
  203.     /* Check to see if the user is clicking */
  204.     if (rightclick = true) {
  205.       /* The cursor's x and y coordinates. */
  206.       //CHANGE FROM MOUSE COORDINATES TO colorX and colorY WHICH DEFINE THE ELLIPSE'S LOCATION ABOVE (IN DRAW)
  207.       float tx = colorX;
  208.       float ty = colorY;
  209.       /* the distance between the cursor and particle */
  210.       float radius = dist(x,y,tx,ty);
  211.       if (radius < 100) { //THIS IS THE TEST OF WHETHER THE PARTICLE IS AFFECTED
  212.         affectedCount++; // IF IT IS, INCREASE THE COUNTER
  213.         /* Calculate the angle between the particle and the cursor. */
  214.         float angle = atan2(ty-y,tx-x);
  215.         /* Are we left-clicking or not? */
  216.         if (mouseButton == LEFT) {
  217.           /* If left, then the particles are deflected */
  218.           vx -= radius * 0.07 * cos(angle);
  219.           vy -= radius * 0.07 * sin(angle);
  220.           /* The stroke color is red */
  221.           stroke(radius * 4,0,0);
  222.         }
  223.         else {
  224.           /* If right, the particles are attracted. */
  225.           vx += radius * 0.07 * cos(angle);
  226.           vy += radius * 0.07 * sin(angle);
  227.           /* The stroke color is blue */
  228.           stroke(0,0,radius * 4);
  229.         }
  230.         /* If line relationships are enabled, the lines are drawn. */
  231.         if (lines) line (x,y,tx,ty);
  232.       }
  233.     }  
  234.     /* Previox x and y coordinates are set, for drawing the trail */
  235.     int px = (int)x;
  236.     int py = (int)y;
  237.     /* Our particle's coordinates are updated. */
  238.     x += vx;
  239.     y += vy;
  240.     /* Gravity is applied. */
  241.     if (gravity == true) vy += 2;
  242.     /* Border collisions */
  243.     if (x > width-11) {
  244.       /* Reverse the velocity if towards wall */
  245.       if (abs(vx) == vx) vx *= -1.0;
  246.       /* The particle is placed back at the wall */
  247.       x = width-11;
  248.     }
  249.     if (x < 11) {
  250.       if (abs(vx) != vx) vx *= -1.0;
  251.       x = 11;
  252.     }
  253.     if (y < 11) {
  254.       if (abs(vy) != vy) vy *= -1.0;
  255.       y = 11;
  256.     }
  257.     if (y > height-11) {
  258.       if (abs(vy) == vy) vy *= -1.0;
  259.       vx *= 0.6;
  260.       y = height-11;
  261.     }
  262.     /* if relationship lines are disabled */
  263.     if (!lines) {
  264.       /* The stroke color is set to white */
  265.       stroke (400);
  266.       /* The particle is drawn */
  267.       line(px,py,int(x),int(y));
  268.     }
  269.   }
  270. /* User presses a key */
  271. void keyPressed() {
  272.   /* if they press g */
  273.   if (key == 'g' || key == 'G') {
  274.     /* we toggle gravity */
  275.     if (gravity) gravity = false;
  276.     else gravity = true;
  277.   }
  278.   /* otherwise */
  279.   else {
  280.     /* we toggle line relationships */
  281.     if (lines) lines = false;
  282.     else lines = true;
  283.   }
  284. }
Thank you,
Thats brilliant ive now got that adapting audio within Max/MSP through OSC
Ive been having troubles for a while attempting to create a colour threshold (line 96), to make click = true, for use later in the particle class (line 214)

Copy code




  1. /**
  2.  * Color Tracking
  3.  * by Justin Brooks
  4.  *
  5.  *
  6.  * based on original code:
  7.  *   Brightness Tracking 
  8.  *   by Golan Levin. 
  9.  * 
  10.  * Tracks the tracks pixel closest in color to specified color.
  11.  */
  12. boolean lines = false;
  13. /*
  14. gravity toggle
  15.  toggle this by pressing 'g'
  16.  */
  17. boolean gravity = false;
  18. boolean click = false;
  19. /* Particle count */
  20. int particleCount = 500;
  21. int pcounter = 0;
  22. /* Particle array */
  23. Particle[] particles = new Particle[particleCount+1];

  24. //DECLARE THESE HERE SO THEY'RE ACCESSIBLE GLOBALLY
  25. int colorX = 0; // X-coordinate of the closest in color video pixel
  26. int colorY = 0; // Y-coordinate of the closest in color video pixel
  27. import oscP5.*;
  28. import netP5.*;

  29. OscP5 oscP5;
  30. NetAddress myRemoteLocation;
  31. import processing.video.*;

  32. Capture video;
  33. int affectedCount=0;//DECLARE HERE SO ACCESSIBLE EVERYWHERE

  34. void setup() {
  35.   size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
  36.   // Uses the default video input, see the reference if this causes an error
  37.   video = new Capture(this, width, height, 30);
  38.   noStroke();
  39.   smooth();
  40.   colorMode(RGB, 400);
  41.   /* Frame rate is 30 */
  42.   frameRate(30);
  43.   /* The fill color is set to black (0) for the boundaries (drawn as a quad) */
  44.   fill (0);
  45.   oscP5 = new OscP5(this,7474);
  46.   myRemoteLocation = new NetAddress("127.0.0.1",7474);
  47.   /* We loop through our particle count and initialize each */
  48.   for (int x = particleCount; x >= 0; x--) {
  49.     particles[x] = new Particle();
  50.   }
  51. }

  52. void draw() {
  53.   if (video.available()) {
  54.     video.read();
  55.     background(0);
  56.     affectedCount=0;//RESET COUNTER AT THE START OF EACH FRAME
  57.     /* The stroke color is set to white for the border. */
  58.     stroke (400);
  59.     /* The border; it is 10 pixels from all sides */
  60.     quad (10,10,10,height-10,width-10,height-10,width-10,10); 
  61.     /* All of our particles are looped through, and updated */
  62.     for (int i = particleCount; i >= 0; i--) {
  63.       Particle particle = (Particle) particles[i];
  64.       particle.update(i);
  65.     }

  66.     //REMOVE THE INTS SINCE WE INITIALISED THEM BEFORE SETUP
  67.     colorX = 0; // X-coordinate of the closest in color video pixel
  68.     colorY = 0; // Y-coordinate of the closest in color video pixel
  69.     float closestColor = 10000; //we set this to be abritrarily large, once program runs, the first pixel it scans will be set to this value
  70.     // Search for the closest in color pixel: For each row of pixels in the video image and
  71.     // for each pixel in the yth row, compute each pixel's index in the video
  72.     video.loadPixels();
  73.     int index = 0;
  74.     for (int ay = 0; ay < video.height; ay++) {
  75.       for (int ax = 0; ax < video.width; ax++) {
  76.         // Get the color stored in the pixel
  77.         color pixelValue = video.pixels[index];
  78.         // Determine the color of the pixel
  79.         float colorProximity = abs(red(pixelValue)-0)+abs(green(pixelValue)-255)+abs(blue(pixelValue)-0);
  80.         // If that value is closer in color value than any previous, then store the
  81.         // color proximity of that pixel, as well as its (x,y) location
  82.         if (colorProximity < closestColor) {
  83.           closestColor = colorProximity;
  84.           closestColor=closestColor-10; //thoguht behind this is that it once it "locks" on to an object of color, it wont let go unless something a good bit better (closer in color) comes along
  85.           colorY = ay;
  86.           colorX = ax;
  87.         }
  88.         index++;
  89.       }
  90.     }
  91.     println(affectedCount);//DO WHATEVER WITH THIS 
  92.     OscMessage myMessage = new OscMessage("/test");     
  93.     myMessage.add((float)colorX);
  94.     myMessage.add(1. - (float)colorY);
  95.     myMessage.add(2. - (int)affectedCount);    
  96.     /* send the message */
  97.     oscP5.send(myMessage, myRemoteLocation);
  98.     // Draw a large, yellow circle at the brightest pixel
  99.     fill(255, 204, 0, 128);
  100.     ellipse(colorX, colorY, 200, 200);
  101.   }
  102. }

  103. class Particle {
  104.   /*
  105.   Global class variables
  106.    These are kept track of, and aren't lost when the class particle is finished operating.
  107.    */
  108.   /* the x and y are our coordinates for each particles */
  109.   float x;
  110.   float y;
  111.   /* vx and vy are the velocities along each axis the particles are traveling */
  112.   float vx;
  113.   float vy;
  114.   /* Particle initialization. We define the beginning properties of each particle here. */
  115.   Particle() {
  116.     x = random(10,width-10);
  117.     y = random(10,height-10);
  118.   }
  119.   /* These are called everytime we check with the other particle's distances  */
  120.   float getx() {
  121.     return x;
  122.   }
  123.   float gety() {
  124.     return y;
  125.   }
  126.   void update(int num) {
  127.     /* Friction is simulated here */
  128.     vx *= 0.84;
  129.     vy *= 0.84;
  130.     /* Here, every particle is looped through and checked to see if they're close enough to have effect on the current particle. */
  131.     for (int i = particleCount; i >= 0; i--) {
  132.       /* Check to see if the particle we're checking isn't the current particle. */
  133.       if (i != num) {
  134.         /* drawthis boolean is initialized. it determines whether the particle is close enough to the other particles for relationship lines to be drawn, assuming it's enabled */
  135.         boolean drawthis = false;
  136.         /*
  137.         Integers are used to keep track of the shade for each particle
  138.          The red shade shows opposition
  139.          The blue shade shows attraction
  140.          */
  141.         float redshade = 20;
  142.         float blueshade = 500;
  143.         /* We set our particle */
  144.         Particle particle = (Particle) particles[i];
  145.         /* The x and y coordinates of the particle is found, so we can compare distances with our current particle. */
  146.         float tx = particle.getx();
  147.         float ty = particle.gety();
  148.         /* The radius or distance between both particles are determined */
  149.         float radius = dist(x,y,tx,ty);
  150.         /* Is the radius small enough for the particle to have an effect on our current one? */
  151.         if (radius < 35) {
  152.           /* We've the determine that the particle is close enough for relationship lines, so we set drawthis to true. */
  153.           drawthis = true;
  154.           /* If so, we proceed to calculate the angle. */
  155.           float angle = atan2(y-ty,x-tx);
  156.           /* Is the radius close enough to be deflected? */
  157.           if (radius < 30) {
  158.             /* Is relationship lines toggled? */
  159.             if (lines) {
  160.               /*
  161.                Redshade is increased by the distance * 5. The distance is multiplied by 10 because our radius will be within 40 pixels,
  162.                30 * 13.33... is 400
  163.                We invert it so it gets redder as the particle approaches the current particle, rather than vice versa.
  164.                */
  165.               redshade = 13 * (30 - radius);
  166.             }
  167.             /*
  168.              Here, we calculate a coordinate at a angle opposite of the direction where the other particle is.
  169.              0.07 is the strength of the particle's opposition, while (40 - radius) is how much it is effected, according to how close it is.
  170.              */
  171.             vx += (30 - radius) * 0.07 * cos(angle);
  172.             vy += (30 - radius) * 0.07 * sin(angle);
  173.           }
  174.           /*
  175.            Here we see if the particle is within 25 and 35 pixels of the current particle
  176.            (we already know that the particle is under 35 pixels distance, since this if statement is nested
  177.            in if (radius < 35), so rechecking is unnecessary
  178.            */
  179.           if (radius > 25) {
  180.             /* check to see if relationship lines are toggled */
  181.             if (lines) {
  182.               /* The blue shade, between 0 and 400, is used to show how much each particle is attracted */
  183.               blueshade = 40 * (35 - radius);
  184.             }
  185.             /* This does the opposite of the other check. It pulls the particle towards the other.  */
  186.             vx -= (25 - radius) * 0.005 * cos(angle);
  187.             vy -= (25 - radius) * 0.005 * sin(angle);
  188.           }
  189.         }
  190.         /* Check to see if relationship lines are enabled */
  191.         if (lines) {
  192.           /* Check to see if the two particles are close enough. */
  193.           if (drawthis) {
  194.             /* Set the stroke color */
  195.             stroke (redshade, 10, blueshade);
  196.             /* draw the line */
  197.             line(x,y,tx,ty);
  198.           }
  199.         }
  200.       }
  201.     }
  202.     /* Check to see if the user is clicking */
  203.     if (click = true) {
  204.       /* The cursor's x and y coordinates. */
  205.       //CHANGE FROM MOUSE COORDINATES TO colorX and colorY WHICH DEFINE THE ELLIPSE'S LOCATION ABOVE (IN DRAW)
  206.       float tx = colorX;
  207.       float ty = colorY;
  208.       /* the distance between the cursor and particle */
  209.       float radius = dist(x,y,tx,ty);
  210.       if (radius < 100) { //THIS IS THE TEST OF WHETHER THE PARTICLE IS AFFECTED
  211.         affectedCount++; // IF IT IS, INCREASE THE COUNTER
  212.         /* Calculate the angle between the particle and the cursor. */
  213.         float angle = atan2(ty-y,tx-x);
  214.         /* Are we left-clicking or not? */
  215.         if (mouseButton == LEFT) {
  216.           /* If left, then the particles are deflected */
  217.           vx -= radius * 0.07 * cos(angle);
  218.           vy -= radius * 0.07 * sin(angle);
  219.           /* The stroke color is red */
  220.           stroke(radius * 4,0,0);
  221.         }
  222.         else {
  223.           /* If right, the particles are attracted. */
  224.           vx += radius * 0.07 * cos(angle);
  225.           vy += radius * 0.07 * sin(angle);
  226.           /* The stroke color is blue */
  227.           stroke(0,0,radius * 4);
  228.         }
  229.         /* If line relationships are enabled, the lines are drawn. */
  230.         if (lines) line (x,y,tx,ty);
  231.       }
  232.     }  
  233.     /* Previox x and y coordinates are set, for drawing the trail */
  234.     int px = (int)x;
  235.     int py = (int)y;
  236.     /* Our particle's coordinates are updated. */
  237.     x += vx;
  238.     y += vy;
  239.     /* Gravity is applied. */
  240.     if (gravity == true) vy += 2;
  241.     /* Border collisions */
  242.     if (x > width-11) {
  243.       /* Reverse the velocity if towards wall */
  244.       if (abs(vx) == vx) vx *= -1.0;
  245.       /* The particle is placed back at the wall */
  246.       x = width-11;
  247.     }
  248.     if (x < 11) {
  249.       if (abs(vx) != vx) vx *= -1.0;
  250.       x = 11;
  251.     }
  252.     if (y < 11) {
  253.       if (abs(vy) != vy) vy *= -1.0;
  254.       y = 11;
  255.     }
  256.     if (y > height-11) {
  257.       if (abs(vy) == vy) vy *= -1.0;
  258.       vx *= 0.6;
  259.       y = height-11;
  260.     }
  261.     /* if relationship lines are disabled */
  262.     if (!lines) {
  263.       /* The stroke color is set to white */
  264.       stroke (400);
  265.       /* The particle is drawn */
  266.       line(px,py,int(x),int(y));
  267.     }
  268.   }
  269. /* User presses a key */
  270. void keyPressed() {
  271.   /* if they press g */
  272.   if (key == 'g' || key == 'G') {
  273.     /* we toggle gravity */
  274.     if (gravity) gravity = false;
  275.     else gravity = true;
  276.   }
  277.   /* otherwise */
  278.   else {
  279.     /* we toggle line relationships */
  280.     if (lines) lines = false;
  281.     else lines = true;
  282.   }
  283. }


Your a superstar, i definitely would miss my uni deadline if it wasn't for your help
Hail the beauties of forums and open source

Regards
Miles
I'm not sure I get you. Are you just looking for something like this?
Copy code
  1. if(colorX<=threshold){
  2.       doWhatWeDidForLeftClick();
  3. } else {
  4.        doWhatWeDidForRightClick();
  5. }
That would mean there is always attraction/repulsion, with no neutral state? Where are you getting the threshold value from?
im trying to have it so that if the green isnt green enough then the sketch looks as if its in a neutral state,
so when the green is registered as being green enough the particles will attract, if that makes sense.
ive been trying to put in this kind of threshold style thing at around line 94, as i was expecting to be able to use the green(pixelValue) as something to compare to my threshold of maybe 200.

a bit like this, but correct...

if(green(pixelValue)>200{
attract
}
else{
no click
}

cheers
Miles


OK, first declare a global boolean (true/false) before setup(), which will replace the mouseClick check:
Copy code
  1. boolean active=false;

Between line 95 and 96 add in a check, something like:

Copy code
  1. if(green(pixelValue)>200){
  2. active=true;
  3. }
  4. else{
  5. active=false;
  6. }

We can then condense lines 215-240 into the following. 


Copy code
  1. if (active = true) {
  2.       /* The cursor's x and y coordinates. */
  3.       //CHANGE FROM MOUSE COORDINATES TO colorX and colorY WHICH DEFINE THE ELLIPSE'S LOCATION ABOVE (IN DRAW)
  4.       float tx = colorX;
  5.       float ty = colorY;
  6.       /* the distance between the cursor and particle */
  7.       float radius = dist(x,y,tx,ty);
  8.       if (radius < 100) { //THIS IS THE TEST OF WHETHER THE PARTICLE IS AFFECTED
  9.         affectedCount++; // IF IT IS, INCREASE THE COUNTER
  10.         /* Calculate the angle between the particle and the cursor. */
  11.         float angle = atan2(ty-y,tx-x);

  12.          
  13.           vx += radius * 0.07 * cos(angle);
  14.           vy += radius * 0.07 * sin(angle);
  15.           /* The stroke color is blue */
  16.           stroke(0,0,radius * 4);
  17.         }

I've not tested this but that should give you the right idea, I think. 
Cheers for that, i think thats nearly it but
It doesnt seem to work, i think the sketch uses something else to initialize it but im not sure and I can't work it out,
this is the code i have at the moment:

Copy code




  1. /**
  2.  * Color Tracking
  3.  * by Justin Brooks
  4.  *
  5.  *
  6.  * based on original code:
  7.  *   Brightness Tracking 
  8.  *   by Golan Levin. 
  9.  * 
  10.  * Tracks the tracks pixel closest in color to specified color.
  11.  */
  12. boolean lines = false;
  13. boolean active = false;
  14. /*
  15. gravity toggle
  16.  toggle this by pressing 'g'
  17.  */
  18. boolean gravity = false;
  19. /* Particle count */
  20. int particleCount = 500;
  21. int pcounter = 0;

  22. /* Particle array */
  23. Particle[] particles = new Particle[particleCount+1];

  24. //DECLARE THESE HERE SO THEY'RE ACCESSIBLE GLOBALLY
  25. int colorX = 0; // X-coordinate of the closest in color video pixel
  26. int colorY = 0; // Y-coordinate of the closest in color video pixel
  27. import oscP5.*;
  28. import netP5.*;

  29. OscP5 oscP5;
  30. NetAddress myRemoteLocation;
  31. import processing.video.*;

  32. Capture video;
  33. int affectedCount=0;//DECLARE HERE SO ACCESSIBLE EVERYWHERE

  34. void setup() {
  35.   size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
  36.   // Uses the default video input, see the reference if this causes an error
  37.   video = new Capture(this, width, height, 30);
  38.   noStroke();
  39.   smooth();
  40.   colorMode(RGB, 400);
  41.   /* Frame rate is 30 */
  42.   frameRate(30);
  43.   /* The fill color is set to black (0) for the boundaries (drawn as a quad) */
  44.   fill (0);
  45.   oscP5 = new OscP5(this,7474);
  46.   myRemoteLocation = new NetAddress("127.0.0.1",7474);
  47.   /* We loop through our particle count and initialize each */
  48.   for (int x = particleCount; x >= 0; x--) {
  49.     particles[x] = new Particle();
  50.   }
  51. }

  52. void draw() {
  53.   if (video.available()) {
  54.     video.read();
  55.     background(0);
  56.     affectedCount=0;//RESET COUNTER AT THE START OF EACH FRAME
  57.     /* The stroke color is set to white for the border. */
  58.     stroke (400);
  59.     /* The border; it is 10 pixels from all sides */
  60.     quad (10,10,10,height-10,width-10,height-10,width-10,10); 
  61.     /* All of our particles are looped through, and updated */
  62.     for (int i = particleCount; i >= 0; i--) {
  63.       Particle particle = (Particle) particles[i];
  64.       particle.update(i);
  65.     }

  66.     //REMOVE THE INTS SINCE WE INITIALISED THEM BEFORE SETUP
  67.     colorX = 0; // X-coordinate of the closest in color video pixel
  68.     colorY = 0; // Y-coordinate of the closest in color video pixel
  69.     float closestColor = 10000; //we set this to be abritrarily large, once program runs, the first pixel it scans will be set to this value
  70.     // Search for the closest in color pixel: For each row of pixels in the video image and
  71.     // for each pixel in the yth row, compute each pixel's index in the video
  72.     video.loadPixels();
  73.     int index = 0;
  74.     for (int ay = 0; ay < video.height; ay++) {
  75.       for (int ax = 0; ax < video.width; ax++) {
  76.         // Get the color stored in the pixel
  77.         color pixelValue = video.pixels[index];
  78.         // Determine the color of the pixel
  79.         float colorProximity = abs(red(pixelValue)-0)+abs(green(pixelValue)-255)+abs(blue(pixelValue)-0);
  80.         // If that value is closer in color value than any previous, then store the
  81.         // color proximity of that pixel, as well as its (x,y) location
  82.         if (green(pixelValue) > 200){
  83. active = true;
  84.         }
  85. else{
  86. active = false;
  87. }
  88.         
  89.         if (colorProximity < closestColor) {
  90.           closestColor = colorProximity;
  91.           
  92.           

  93.           closestColor=closestColor-10; //thoguht behind this is that it once it "locks" on to an object of color, it wont let go unless something a good bit better (closer in color) comes along
  94.           colorY = ay;
  95.           colorX = ax;
  96.         }
  97.         index++;
  98.       }
  99.     }
  100.     println(affectedCount);//DO WHATEVER WITH THIS 
  101.     OscMessage myMessage = new OscMessage("/test");     
  102.     myMessage.add((float)colorX);
  103.     myMessage.add(1. - (float)colorY);
  104.     myMessage.add(2. - (int)affectedCount);    
  105.     /* send the message */
  106.     oscP5.send(myMessage, myRemoteLocation);
  107.     // Draw a large, yellow circle at the brightest pixel
  108.   }
  109. }

  110. class Particle {
  111.   /*
  112.   Global class variables
  113.    These are kept track of, and aren't lost when the class particle is finished operating.
  114.    */
  115.   /* the x and y are our coordinates for each particles */
  116.   float x;
  117.   float y;
  118.   /* vx and vy are the velocities along each axis the particles are traveling */
  119.   float vx;
  120.   float vy;
  121.   /* Particle initialization. We define the beginning properties of each particle here. */
  122.   Particle() {
  123.     x = random(10,width-10);
  124.     y = random(10,height-10);
  125.   }
  126.   /* These are called everytime we check with the other particle's distances  */
  127.   float getx() {
  128.     return x;
  129.   }
  130.   float gety() {
  131.     return y;
  132.   }
  133.   void update(int num) {
  134.     /* Friction is simulated here */
  135.     vx *= 0.84;
  136.     vy *= 0.84;
  137.     /* Here, every particle is looped through and checked to see if they're close enough to have effect on the current particle. */
  138.     for (int i = particleCount; i >= 0; i--) {
  139.       /* Check to see if the particle we're checking isn't the current particle. */
  140.       if (i != num) {
  141.         /* drawthis boolean is initialized. it determines whether the particle is close enough to the other particles for relationship lines to be drawn, assuming it's enabled */
  142.         boolean drawthis = false;
  143.         /*
  144.         Integers are used to keep track of the shade for each particle
  145.          The red shade shows opposition
  146.          The blue shade shows attraction
  147.          */
  148.         float redshade = 20;
  149.         float blueshade = 500;
  150.         /* We set our particle */
  151.         Particle particle = (Particle) particles[i];
  152.         /* The x and y coordinates of the particle is found, so we can compare distances with our current particle. */
  153.         float tx = particle.getx();
  154.         float ty = particle.gety();
  155.         /* The radius or distance between both particles are determined */
  156.         float radius = dist(x,y,tx,ty);
  157.         /* Is the radius small enough for the particle to have an effect on our current one? */
  158.         if (radius < 35) {
  159.           /* We've the determine that the particle is close enough for relationship lines, so we set drawthis to true. */
  160.           drawthis = true;
  161.           /* If so, we proceed to calculate the angle. */
  162.           float angle = atan2(y-ty,x-tx);
  163.           /* Is the radius close enough to be deflected? */
  164.           if (radius < 30) {
  165.             /* Is relationship lines toggled? */
  166.             if (lines) {
  167.               /*
  168.                Redshade is increased by the distance * 5. The distance is multiplied by 10 because our radius will be within 40 pixels,
  169.                30 * 13.33... is 400
  170.                We invert it so it gets redder as the particle approaches the current particle, rather than vice versa.
  171.                */
  172.               redshade = 13 * (30 - radius);
  173.             }
  174.             /*
  175.              Here, we calculate a coordinate at a angle opposite of the direction where the other particle is.
  176.              0.07 is the strength of the particle's opposition, while (40 - radius) is how much it is effected, according to how close it is.
  177.              */
  178.             vx += (30 - radius) * 0.07 * cos(angle);
  179.             vy += (30 - radius) * 0.07 * sin(angle);
  180.           }
  181.           /*
  182.            Here we see if the particle is within 25 and 35 pixels of the current particle
  183.            (we already know that the particle is under 35 pixels distance, since this if statement is nested
  184.            in if (radius < 35), so rechecking is unnecessary
  185.            */
  186.           if (radius > 25) {
  187.             /* check to see if relationship lines are toggled */
  188.             if (lines) {
  189.               /* The blue shade, between 0 and 400, is used to show how much each particle is attracted */
  190.               blueshade = 40 * (35 - radius);
  191.             }
  192.             /* This does the opposite of the other check. It pulls the particle towards the other.  */
  193.             vx -= (25 - radius) * 0.005 * cos(angle);
  194.             vy -= (25 - radius) * 0.005 * sin(angle);
  195.           }
  196.         }
  197.         /* Check to see if relationship lines are enabled */
  198.         if (lines) {
  199.           /* Check to see if the two particles are close enough. */
  200.           if (drawthis) {
  201.             /* Set the stroke color */
  202.             stroke (redshade, 10, blueshade);
  203.             /* draw the line */
  204.             line(x,y,tx,ty);
  205.           }
  206.         }
  207.       }
  208.     }
  209.     /* Check to see if the user is clicking */
  210.     if (active = true) {
  211.       /* The cursor's x and y coordinates. */
  212.       //CHANGE FROM MOUSE COORDINATES TO colorX and colorY WHICH DEFINE THE ELLIPSE'S LOCATION ABOVE (IN DRAW)
  213.       float tx = colorX;
  214.       float ty = colorY;
  215.       /* the distance between the cursor and particle */
  216.       float radius = dist(x,y,tx,ty);
  217.       if (radius < 100) { //THIS IS THE TEST OF WHETHER THE PARTICLE IS AFFECTED
  218.         affectedCount++; // IF IT IS, INCREASE THE COUNTER
  219.         /* Calculate the angle between the particle and the cursor. */
  220.         float angle = atan2(ty-y,tx-x);
  221.         /* Are we left-clicking or not? */
  222.           /* If right, the particles are attracted. */
  223.           vx += radius * 0.07 * cos(angle);
  224.           vy += radius * 0.07 * sin(angle);
  225.           /* The stroke color is blue */
  226.           stroke(0,0,radius * 4);
  227.        
  228.         /* If line relationships are enabled, the lines are drawn. */
  229.         if (active = true) {
  230.         if (lines) line (x,y,tx,ty);
  231.       } 
  232.   } 
  233.     }
  234.     /* Previox x and y coordinates are set, for drawing the trail */
  235.     int px = (int)x;
  236.     int py = (int)y;
  237.     /* Our particle's coordinates are updated. */
  238.     x += vx;
  239.     y += vy;
  240.     /* Gravity is applied. */
  241.     if (gravity == true) vy += 2;
  242.     /* Border collisions */
  243.     if (x > width-11) {
  244.       /* Reverse the velocity if towards wall */
  245.       if (abs(vx) == vx) vx *= -1.0;
  246.       /* The particle is placed back at the wall */
  247.       x = width-11;
  248.     }
  249.     if (x < 11) {
  250.       if (abs(vx) != vx) vx *= -1.0;
  251.       x = 11;
  252.     }
  253.     if (y < 11) {
  254.       if (abs(vy) != vy) vy *= -1.0;
  255.       y = 11;
  256.     }
  257.     if (y > height-11) {
  258.       if (abs(vy) == vy) vy *= -1.0;
  259.       vx *= 0.6;
  260.       y = height-11;
  261.     }
  262.     /* if relationship lines are disabled */
  263.     if (!lines) {
  264.       /* The stroke color is set to white */
  265.       stroke (400);
  266.       /* The particle is drawn */
  267.       line(px,py,int(x),int(y));
  268.     }
  269.   }
  270. /* User presses a key */
  271. void keyPressed() {
  272.   /* if they press g */
  273.   if (key == 'g' || key == 'G') {
  274.     /* we toggle gravity */
  275.     if (gravity) gravity = false;
  276.     else gravity = true;
  277.   }
  278.   /* otherwise */
  279.   else {
  280.     /* we toggle line relationships */
  281.     if (lines) lines = false;
  282.     else lines = true;
  283.   }
  284. }
Regards
Miles
Can you be more specific? I just tested your code and it did what I expected. The more information you give the easier it is for us to help.
Bear in mind that the method you're using to track the greenest pixel may not always give the result you expect: calling green() on a white pixel (255, 255, 255) will get a higher result than calling it on a green pixel, e.g.(0, 250, 0).
ye of course, sorry about that,
i did wonder if that might be the case seen as white is all colours,
ill be getting a green laser pen through the post soon and the background will be black, so hopefully there will be no issue,
im using quite cheap webcams at the moment, so the colours aren't hugely true, could this become a problem?

for me it seems to stay active, so it is always attracting particles,
is that not the same for you then?

thanks
Miles
Oh God, I'm such an idiot. The logic in lines 224 and 243 should be
Copy code
  1.  if(active==true)
 not 
Copy code
  1. if(active=true)
That explains why that whole section isn't working. Er... sorry!

As for the colour tracking, it seems OK but it's hard to say. I have no idea about algorithms for colour tracking.  You'll need to experiment with your equipment. I saw the other thread you posted asking about that- I'd recommend asking as specific a question as possible. Explain what isn't working, and maybe post or highlight the relevant section of the code separately, as to be quite frank it's not easy to read 300 odd lines of code. 

Hope that helps.

Of course,
im a bit new to this all,
Yeah thanks for that, its now nearly working
the active true/false part seems to not work properly yet, i think its due to the way that the pixelValue is calculated,
its difficult to test this seen as when i print the values it slows everything down, im guessing its because its printing the value from each pixel 30 times a second(framerate).
Hey there, 
Ive tried to stop littering the forums, 
Ive put a new post up which i hope is a bit more direct and i would be very greatful if you could cast your wise eye over it
Cheers
Miles