OpenCV Blobs Collision and Gravity Coding
in
Contributed Library Questions
•
2 years ago
Hi Everyone!
I've been practicing some OOP and OpenCV with processing and i've been doing ok so far.
This sketch is working and its a Work in Progress.
Im drawing the Blobs and i want to have a picture fall from height = 0 width = random and when it collides with a blob, i want it to jump up and start falling again, and so on.
My Questions are:
How do I code the colliding of the picture and the blob? You can recommend some examples and i'll try to learn from them :)
and
How would you code Gravity in such a way that, when colliding with a blob i would just use a method like move(Speed, Angle, Acceleration); and the image would rise till speed = 0 and fall again until it hits a blob or y = height.
keep in mind that i really want to understand the code so if you point me to some documentation / tutorials / examples or even some commented code i'll be glad :)
Thanks all.
Heres the sketch so far:
- import hypermedia.video.*;
- Item_Image i;
- Container c;
- OpenCV opencv;
- HScrollbar scrollMaxSize;
- HScrollbar scrollMinSize;
- void setup(){
- size(800,600);
- i = new Item_Image("image.jpg");
- c = new Container(i,(width/2)-65,0,130,130);
- scrollMaxSize = new HScrollbar(15,15,100,10,1);
- scrollMinSize = new HScrollbar(15,45,100,10,1);
- opencv = new OpenCV(this);
- opencv.capture(width/2,height/2);
- }
- void draw(){
- background(50);
- c.display();
- opencv.read();
- opencv.convert(OpenCV.GRAY);
- opencv.threshold(80);
- opencv.blur( OpenCV.GAUSSIAN, 20 );
- opencv.threshold(80);
- image(opencv.image(),(width/2)-(width/4),height/2);
- //Scrollbar settings
- float blobMaxSize = (scrollMaxSize.getPos()-3)*200;
- float blobMinSize = (scrollMinSize.getPos()-10)*200;
- fill(255,255,255);
- text("blobMaxSize: "+int(blobMaxSize), 130, 20);
- text("blobMinSize: "+int(blobMinSize), 130, 50);
- scrollMaxSize.update();
- scrollMinSize.update();
- scrollMaxSize.display();
- scrollMinSize.display();
- //coloca as blobs num array
- Blob[] blobs = opencv.blobs( int(blobMinSize), int(blobMaxSize), 5, false, OpenCV.MAX_VERTICES*2 );
- translate((width/2)-(width/4), height/2);
- //percore as blobs todas
- for(int i = 0; i < blobs.length; i++){
- //cor do preenchimento das blobs
- fill(124,175,122);
- //desenha as blobs
- beginShape();
- for(int j=0; j < blobs[i].points.length; j++ ) {
- vertex( blobs[i].points[j].x, blobs[i].points[j].y );
- }
- endShape(CLOSE);
- }
- }
- void mouseClicked(){
- opencv.remember();
- }
- class Container {
- float x,y,w,h;
- itemInterface content;
- Container(itemInterface _content, float _x, float _y, float _w, float _h){
- x = _x;
- y = _y;
- w = _w;
- h = _h;
- content = _content;
- }
- void display(){
- content.display(x,y,w,h);
- }
- /*
- void gravity(){
- //GRAVIDADE APLICADA AO CONTAINER
- if(velocidade <= 10){
- velocidade = velocidade*gravidade;
- }else{
- velocidade = velocidade*1;
- }
- x += velocidade;
- println("aceleraçao da gravidade: " + velocidade);
- }
- */
- }
- interface itemInterface {
- void display( float _x, float _y, float _w, float _h);
- }
- class Item_Image implements itemInterface {
- PImage img;
- float x,y,w,h;
- String directory;
- Item_Image(String _dir){
- directory = _dir;
- img = loadImage(directory);
- }
- void display(float _x,float _y, float _w, float _h){
- x = _x;
- y = _y;
- w = _w;
- h = _h;
- image(img, x,y,w,h);
- }
- }
- //SCROLLBAR CLASS, FROM PROCESSING EXAMPLES
- class HScrollbar
- {
- int swidth, sheight; // width and height of bar
- int xpos, ypos; // x and y position of bar
- float spos, newspos; // x position of slider
- int sposMin, sposMax; // max and min values of slider
- int loose; // how loose/heavy
- boolean over; // is the mouse over the slider?
- boolean locked;
- float ratio;
- HScrollbar (int xp, int yp, int sw, int sh, int l) {
- swidth = sw;
- sheight = sh;
- int widthtoheight = sw - sh;
- ratio = (float)sw / (float)widthtoheight;
- xpos = xp;
- ypos = yp-sheight/2;
- spos = xpos + swidth/2 - sheight/2;
- newspos = spos;
- sposMin = xpos;
- sposMax = xpos + swidth - sheight;
- loose = l;
- }
- void update() {
- if(over()) {
- over = true;
- } else {
- over = false;
- }
- if(mousePressed && over) {
- locked = true;
- }
- if(!mousePressed) {
- locked = false;
- }
- if(locked) {
- newspos = constrain(mouseX-sheight/2, sposMin, sposMax);
- }
- if(abs(newspos - spos) > 1) {
- spos = spos + (newspos-spos)/loose;
- }
- }
- int constrain(int val, int minv, int maxv) {
- return min(max(val, minv), maxv);
- }
- boolean over() {
- if(mouseX > xpos && mouseX < xpos+swidth &&
- mouseY > ypos && mouseY < ypos+sheight) {
- return true;
- } else {
- return false;
- }
- }
- void display() {
- fill(255);
- rect(xpos, ypos, swidth, sheight);
- if(over || locked) {
- fill(153, 102, 0);
- } else {
- fill(102, 102, 102);
- }
- rect(spos, ypos, sheight, sheight);
- }
- float getPos() {
- // Convert spos to be values between
- // 0 and the total width of the scrollbar
- return spos * ratio;
- }
- }
1