We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Collisions (Read 378 times)
Collisions
Apr 24th, 2009, 1:11pm
 
We are having trouble with our blob detection code. Right now the balls are detecting the blob line, but the lines aren't detecting the balls. If there is too much movement, the balls don't see the line and fly right through. We need help creating the collision to work better, so the balls don't fly through the lines when people move quickly.  Following is our code so far. Thank you!


import processing.video.*;
import blobDetection.*;
import fullscreen.*;

FullScreen fs;

Capture cam;
BlobDetection theBlobDetection;
PImage img;
boolean newFrame=false;

Ball [] bouncing= new Ball[5];
float testBrightness;
color black = color(0);
color white = color(255);
int numPixels;

void setup()
{
smooth();
noCursor();
       // Size of applet
     size(640,480);
     // Capture
     cam = new Capture(this, 20*4, 15*4, 10);
     // BlobDetection
     // img which will be sent to detection (a smaller copy of the cam frame);
     img = new PImage(80,60);
     theBlobDetection = new BlobDetection(img.width, img.height);
     theBlobDetection.setPosDiscrimination(true);
     theBlobDetection.setThreshold(0.5f); // will detect bright areas whose luminosity > 0.2f;
fs = new FullScreen(this);
//fs.setResolution(1344,840);
fs.enter();

for(int i=0; i<bouncing.length; i++){
bouncing[i]= new Ball(random(0,640),random(0,480),random(6,8), random(8,10));

 }


}

// ==================================================
// captureEvent()
// ==================================================
void captureEvent(Capture cam)
{
     cam.read();
     newFrame = true;
}

// ==================================================
// draw()
// ==================================================
void draw()
{


 if (newFrame)
{
           newFrame=false;
background(0);
           //image(cam,0,0,width,height);
           img.copy(cam, 0, 0, cam.width, cam.height,
                       0, 0, img.width, img.height);
           //fastblur(img, 2);
           theBlobDetection.computeBlobs(img.pixels);
           drawBlobsAndEdges(true,true);
     }


int threshold = 127; // Set the threshold value
   float pixelBrightness; // Declare variable to store a pixel's color
   // Turn each pixel in the video frame black or white depending on its brightness
   loadPixels();
   for (int i = 0; i < numPixels; i++) {
     pixelBrightness = brightness(cam.pixels[i]);
     if (pixelBrightness > threshold) { // If the pixel is brighter than the
       pixels[i] = white; // threshold value, make it white
     }
     else { // Otherwise,
       pixels[i] = black; // make it black
     }
   }
   updatePixels();

for(int i=0; i<bouncing.length; i++){
bouncing[i].move();
bouncing[i].display();
 }


}

// ==================================================
// drawBlobsAndEdges()
// ==================================================
void drawBlobsAndEdges(boolean drawBlobs, boolean drawEdges)
{
     noFill();
     Blob b;
     EdgeVertex eA,eB;
     for (int n=0 ; n<theBlobDetection.getBlobNb() ; n++)
     {
           b=theBlobDetection.getBlob(n);
           if (b!=null)
           {
                 // Edges
                 if (drawEdges)
                 {
                       strokeWeight(6);
                       stroke(255,255,255);
                       for (int m=0;m<b.getEdgeNb();m++)
                       {
                       eA = b.getEdgeVertexA(m);
                       eB = b.getEdgeVertexB(m);
                         if (eA !=null && eB !=null)
                       line(
                       eA.x*width, eA.y*height,
                       eB.x*width, eB.y*height
                       );
                       }
                 }


           }

     }
}




class Ball {
 
int xpos;
int ypos;
int xspeed;
int yspeed;
float state;
int xdirection;  
int ydirection;  

Ball(float tempxpos, float tempypos, float tempxspeed, float tempyspeed){
   
  xpos=int(tempxpos);
  ypos=int(tempypos);
  xspeed=int(tempxspeed);
  yspeed=int(tempyspeed);
  state=0;
  xdirection = 1;  
  ydirection = 1;

}

void display(){
 noStroke();
 fill(21,164,170,100);
 ellipse(xpos,ypos,15,15);
}



void move(){
xpos = xpos + ( xspeed * xdirection );
 ypos = ypos + ( yspeed * ydirection );
 
 int testValue = get(xpos, ypos);
int tv2 = get(xpos+20, ypos+20);
int tv3 = get(xpos+20, ypos-20);
int tv4 = get(xpos-20, ypos+20);
int tv5 = get(xpos-20, ypos-20);
//int tv6 = get(xpos+20, ypos);
//int tv7 = get(xpos-20, ypos);
//int tv8 = get(xpos, ypos+20);
//int tv9 = get(xpos, ypos-20);
   testBrightness = brightness(testValue);
   //println (testBrightness);
   if (testBrightness == 255)  { // If the test location is brighter than the threshold set the fill to black
    xdirection *= -1;
    ydirection *= -1;
   }
 


 if (xpos > width-4 || xpos < 0) {
   xdirection *= -1;
 }
 if (ypos > height-4 || ypos < 0) {
   ydirection *= -1;
 }
}


}
Page Index Toggle Pages: 1