Mixing active and static modes??? please help
in
Core Library Questions
•
2 years ago
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:
- /**
- * Color Tracking
- * by Justin Brooks
- *
- *
- * based on original code:
- * Brightness Tracking
- * by Golan Levin.
- *
- * Tracks the tracks pixel closest in color to specified color.
- */
- boolean lines = false;
- /*
- gravity toggle
- toggle this by pressing 'g'
- */
- boolean gravity = true;
- /* Particle count */
- int particleCount = 500;
- /* Particle array */
- Particle[] particles = new Particle[particleCount+1];
- import processing.video.*;
- Capture video;
- void setup() {
- size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
- // Uses the default video input, see the reference if this causes an error
- video = new Capture(this, width, height, 30);
- noStroke();
- smooth();
- colorMode(RGB, 400);
- /* Frame rate is 30 */
- frameRate(30);
- /* The fill color is set to black (0) for the boundaries (drawn as a quad) */
- fill (0);
- coordx = new Particle (colorX);
- coordy = new Particle (colorY);
- /* We loop through our particle count and initialize each */
- for (int x = particleCount; x >= 0; x--) {
- particles[x] = new Particle();
- }
- }
- void draw() {
- if (video.available()) {
- video.read();
- background(0);
- /* The stroke color is set to white for the border. */
- stroke (400);
- /* The border; it is 10 pixels from all sides */
- quad (10,10,10,height-10,width-10,height-10,width-10,10);
- /* All of our particles are looped through, and updated */
- for (int i = particleCount; i >= 0; i--) {
- Particle particle = (Particle) particles[i];
- particle.update(i);
- }
- int colorX = 0; // X-coordinate of the closest in color video pixel
- int colorY = 0; // Y-coordinate of the closest in color video pixel
- float closestColor = 10000; //we set this to be abritrarily large, once program runs, the first pixel it scans will be set to this value
- // Search for the closest in color pixel: For each row of pixels in the video image and
- // for each pixel in the yth row, compute each pixel's index in the video
- video.loadPixels();
- int index = 0;
- for (int ay = 0; ay < video.height; ay++) {
- for (int ax = 0; ax < video.width; ax++) {
- // Get the color stored in the pixel
- color pixelValue = video.pixels[index];
- // Determine the color of the pixel
- float colorProximity = abs(red(pixelValue)-0)+abs(green(pixelValue)-255)+abs(blue(pixelValue)-0);
- // If that value is closer in color value than any previous, then store the
- // color proximity of that pixel, as well as its (x,y) location
- if (colorProximity < closestColor) {
- closestColor = colorProximity;
- 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
- colorY = ay;
- colorX = ax;
- }
- index++;
- }
- }
- // Draw a large, yellow circle at the brightest pixel
- fill(255, 204, 0, 128);
- ellipse(colorX, colorY, 200, 200);
- }
- }
- class Particle {
- /*
- Global class variables
- These are kept track of, and aren't lost when the class particle is finished operating.
- */
- /* the x and y are our coordinates for each particles */
- Particle () {
- float coordx;
- float coordy;
- mx = coordx;
- my = coordy;
- }
- }
- float x;
- float y;
- /* vx and vy are the velocities along each axis the particles are traveling */
- float vx;
- float vy;
- /* Particle initialization. We define the beginning properties of each particle here. */
- Particle() {
- x = random(10,width-10);
- y = random(10,height-10);
- }
- /* These are called everytime we check with the other particle's distances */
- float getx() {
- return x;
- }
- float gety() {
- return y;
- }
- void update(int num) {
- /* Friction is simulated here */
- vx *= 0.84;
- vy *= 0.84;
- /* Here, every particle is looped through and checked to see if they're close enough to have effect on the current particle. */
- for (int i = particleCount; i >= 0; i--) {
- /* Check to see if the particle we're checking isn't the current particle. */
- if (i != num) {
- /* 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 */
- boolean drawthis = false;
- /*
- Integers are used to keep track of the shade for each particle
- The red shade shows opposition
- The blue shade shows attraction
- */
- float redshade = 20;
- float blueshade = 500;
- /* We set our particle */
- Particle particle = (Particle) particles[i];
- /* The x and y coordinates of the particle is found, so we can compare distances with our current particle. */
- float tx = particle.getx();
- float ty = particle.gety();
- /* The radius or distance between both particles are determined */
- float radius = dist(x,y,tx,ty);
- /* Is the radius small enough for the particle to have an effect on our current one? */
- if (radius < 35) {
- /* We've the determine that the particle is close enough for relationship lines, so we set drawthis to true. */
- drawthis = true;
- /* If so, we proceed to calculate the angle. */
- float angle = atan2(y-ty,x-tx);
- /* Is the radius close enough to be deflected? */
- if (radius < 30) {
- /* Is relationship lines toggled? */
- if (lines) {
- /*
- Redshade is increased by the distance * 5. The distance is multiplied by 10 because our radius will be within 40 pixels,
- 30 * 13.33... is 400
- We invert it so it gets redder as the particle approaches the current particle, rather than vice versa.
- */
- redshade = 13 * (30 - radius);
- }
- /*
- Here, we calculate a coordinate at a angle opposite of the direction where the other particle is.
- 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.
- */
- vx += (30 - radius) * 0.07 * cos(angle);
- vy += (30 - radius) * 0.07 * sin(angle);
- }
- /*
- Here we see if the particle is within 25 and 35 pixels of the current particle
- (we already know that the particle is under 35 pixels distance, since this if statement is nested
- in if (radius < 35), so rechecking is unnecessary
- */
- if (radius > 25) {
- /* check to see if relationship lines are toggled */
- if (lines) {
- /* The blue shade, between 0 and 400, is used to show how much each particle is attracted */
- blueshade = 40 * (35 - radius);
- }
- /* This does the opposite of the other check. It pulls the particle towards the other. */
- vx -= (25 - radius) * 0.005 * cos(angle);
- vy -= (25 - radius) * 0.005 * sin(angle);
- }
- }
- /* Check to see if relationship lines are enabled */
- if (lines) {
- /* Check to see if the two particles are close enough. */
- if (drawthis) {
- /* Set the stroke color */
- stroke (redshade, 10, blueshade);
- /* draw the line */
- line(x,y,tx,ty);
- }
- }
- }
- }
- /* Check to see if the user is clicking */
- if (mousePressed) {
- /* The cursor's x and y coordinates. */
- float tx = mx;
- float ty = my;
- /* the distance between the cursor and particle */
- float radius = dist(x,y,tx,ty);
- if (radius < 100) {
- /* Calculate the angle between the particle and the cursor. */
- float angle = atan2(ty-y,tx-x);
- /* Are we left-clicking or not? */
- if (mouseButton == LEFT) {
- /* If left, then the particles are deflected */
- vx -= radius * 0.07 * cos(angle);
- vy -= radius * 0.07 * sin(angle);
- /* The stroke color is red */
- stroke(radius * 4,0,0);
- }
- else {
- /* If right, the particles are attracted. */
- vx += radius * 0.07 * cos(angle);
- vy += radius * 0.07 * sin(angle);
- /* The stroke color is blue */
- stroke(0,0,radius * 4);
- }
- /* If line relationships are enabled, the lines are drawn. */
- if (lines) line (x,y,tx,ty);
- }
- }
- /* Previox x and y coordinates are set, for drawing the trail */
- int px = (int)x;
- int py = (int)y;
- /* Our particle's coordinates are updated. */
- x += vx;
- y += vy;
- /* Gravity is applied. */
- if (gravity == true) vy += 2;
- /* Border collisions */
- if (x > width-11) {
- /* Reverse the velocity if towards wall */
- if (abs(vx) == vx) vx *= -1.0;
- /* The particle is placed back at the wall */
- x = width-11;
- }
- if (x < 11) {
- if (abs(vx) != vx) vx *= -1.0;
- x = 11;
- }
- if (y < 11) {
- if (abs(vy) != vy) vy *= -1.0;
- y = 11;
- }
- if (y > height-11) {
- if (abs(vy) == vy) vy *= -1.0;
- vx *= 0.6;
- y = height-11;
- }
- /* if relationship lines are disabled */
- if (!lines) {
- /* The stroke color is set to white */
- stroke (400);
- /* The particle is drawn */
- line(px,py,int(x),int(y));
- }
- }
- }
- /* User presses a key */
- void keyPressed() {
- /* if they press g */
- if (key == 'g' || key == 'G') {
- /* we toggle gravity */
- if (gravity) gravity = false;
- else gravity = true;
- }
- /* otherwise */
- else {
- /* we toggle line relationships */
- if (lines) lines = false;
- else lines = true;
- }
- }
Miles
1