collision detection with image subtraction
in
Programming Questions
•
2 years ago
hi all
EDIT: i found the first error: i could check only perimeter not the whole rect!
PS: ANY ADVICE OR ANY KIND OF CONSIDERATIONS ON THE CODE AND HOW TO DEBUG IT IS REALLY APPRECIATED
i'm trying to implement this sketch by jared tarbell:
http://www.complexification.net/gallery/machines/boxFitting/
but with different collision detection algorithm
i've seen this collision tecniques in actionscript3:
the key function is BitmapData.hitTest and i want to implement it in processing.. i've looked for some image subtraction in processing but without result and my implementation (nested for loop in x,y with PGraphics.get) is really too slow and my processing.org ide contue to crash..
some advice?
EDIT 2: the right code, it works mayebe it could be more optimezed!
ps. this is my code:
- int stage = 600;
- int grow_limit = 50;
- PGraphics buffer = createGraphics(stage, stage, P2D); //createImage(stage, stage, ALPHA);
- ArrayList rects = new ArrayList();
- color bg = color(55,30,115);
- //------------------------------------------------------------------------------------- MAIN
- //------------------------------------------------------------------------------------- MAIN
- //------------------------------------------------------------------------------------- MAIN
- void setup() {
- //size(*2stage, stage);
- size(stage, stage);
- background(bg);
- smooth();
- buffer.beginDraw();
- buffer.background(0);
- buffer.endDraw();
- addRect();addRect();addRect();addRect();addRect();addRect();addRect();
- }
- void draw() {
- Rectangle r;
- //println("rests: "+rects.size());
- for (int i=0; i<rects.size(); i++) {
- r = (Rectangle) rects.get(i);
- boolean dead = r.grow();
- if (dead) {
- rects.remove(i);
- addRect();
- }
- }
- //image(buffer, stage, 0);
- }
- void addRect() {
- int x = (int)random(stage);
- int y = (int)random(stage);
- addRect(x, y);
- }
- void addRect(int x, int y) {
- //println("addRect "+rects.size());
- rects.add(new Rectangle(x, y));
- }
- boolean hittest(Rectangle r) {
- /** Return True if there is no collision. False otherways. **/
- //println("hittest");
- if (r.x+r.w > stage) return false;
- if (r.y+r.h > stage) return false;
- // check only the perimeter!!
- int i=0, j=0;
- j = r.y;
- int flag = 0;
- j = r.y -1;
- for (i=r.x; i<(r.x+r.w); i++) flag += checkPixel(i, j);
- j = r.y+r.h +1;
- for (i=r.x; i<(r.x+r.w); i++) flag += checkPixel(i, j);
- i = r.x -1;
- for (j=r.y; j<(r.y+r.h); j++) flag += checkPixel(i, j);
- i = r.x+r.w +1;
- for (j=r.y; j<(r.y+r.h); j++) flag += checkPixel(i, j);
- return flag==0;
- }
- int checkPixel(int i, int j) {
- color black = color(0);
- if ( buffer.get(i, j) == black) return 0;
- else return 1;
- }
- //------------------------------------------------------------------------------------- RECTANGLE
- //------------------------------------------------------------------------------------- RECTANGLE
- //------------------------------------------------------------------------------------- RECTANGLE
- class Rectangle {
- int x, y, w, h;
- boolean dead = false;
- float rotation = random(TWO_PI);
- float delta = random(3,6);
- public Rectangle(int x, int y) {
- println("Rectangle > .");
- this.x = x;
- this.y = y;
- this.w = this.h = 2;
- }
- boolean grow() {
- this.h+=1;
- this.w+=1;
- if ( hittest(this) ) {
- if (h>grow_limit || w>grow_limit) dead = true;
- this.draw();
- buffer.beginDraw();
- buffer.noStroke();
- buffer.fill(100);
- buffer.rect(x, y, w, h);
- buffer.endDraw();
- }
- else dead = true;
- return dead;
- }
- void draw() {
- fill(bla);
- stroke(blabla);
- rect(x, y, w, h);
- }
- }
PS: ANY ADVICE OR ANY KIND OF CONSIDERATIONS ON THE CODE AND HOW TO DEBUG IT IS REALLY APPRECIATED
1