bounce back
in
Contributed Library Questions
•
2 years ago
i'm trying to get these squares bounce off anything that is black, ie the blob lines. how?!
thanks (:
code:
import hypermedia.video.*;
OpenCV opencv;
float x = 100; // x location of square
float y = 0; // y location of square
float speed = 30; // speed of square
float gravity = 0.5;
rectan[] rectans = new rectan[12] ;
int cont = 50;
int bThreshold = 50;
int timer = 0;
void setup() {
size( 640, 480 );
smooth();
// open video stream
opencv = new OpenCV( this );
opencv.capture( 640, 480 );
for (int i = 0; i < rectans.length; i++){
rectans[i] = new rectan(color(255), cont, 50, i+1);
cont += 100;
}
/* for (int j = 0; j < rectans.length; j++){
rectans[j] = new rectan(color(255), cont, 50, j+1);
cont += 100;
}*/
}
void draw() {
background(255);
opencv.read(); // grab frame from camera
opencv.threshold(40); // set black & white threshold
opencv.invert();
// find blobs
Blob[] blobs = opencv.blobs( 10, width*height/2, 100, true, OpenCV.MAX_VERTICES*4 );
// draw blob results
for( int i=0; i<blobs.length; i++ ) {
beginShape();
for( int j=0; j<blobs[i].points.length; j++ ) {
vertex( blobs[i].points[j].x, blobs[i].points[j].y );
}
endShape(CLOSE);
}
// start bouncing squares
for (int i = 0; i < rectans.length; i++){
rectans[i].display();
rectans[i].mover();
}
}
boolean checkDarkPixel (float mx, float my) {
boolean isPixelDark = true;
//load the pixels of the display window
//so we can access the pixels array
loadPixels();
//get the location in the pixels array
int loc = int(my*width + mx);
if (loc > 0 && loc <pixels.length) {
//get its brightness
float b = brightness(pixels[loc]);
//make a boolean variable, until we test, we will caonsider the pixel light
//if the brightness of the pixel is less then the threshold
//make isPixelDark true, as we now consider is dark
if (b< bThreshold) {
isPixelDark = false;
}
}
//return the result of our test
return isPixelDark;
}
---------
class:
class rectan{
color c;
float xpos;
float ypos;
float yspeed;
//float xspeed;
//Constructor
rectan(color tempc, float tempxpos, float tempypos, float tempyspeed){
c = tempc;
xpos = tempxpos;
ypos = tempypos;
yspeed = tempyspeed;
constrain(ypos, 0, height);
}
void display(){
rectMode(CENTER);
fill(c);
rect(xpos,ypos,20,20);
}
void mover(){
ypos += yspeed;
if ( ypos > height || ypos < 0 || !checkDarkPixel(xpos,ypos)) {
yspeed = -yspeed;
}
/*xpos += xspeed;
if ( xpos > width || xpos < 0 || !checkDarkPixel(xpos,ypos)) {
xspeed = -xspeed;
} */
}
}
1