I don’t understand your example… I’ve seen you post it before in an old forum post but I tried understanding and applying what I could of it and it has stopped detecting collisions completely with your .isIntersecting code.
From what I gather they’re different methods to the same end - and through looking at that code I still don’t see what is changing the colour of all the blobs vs. just the colliding blobs.
It’s probably something small and silly I just can’t see it!
This is the code:
timport org.openkinect.processing.*;
//BLOB
int blobCounter = 0;
int maxLife = 200;
float threshold = 25;
float distThreshold = 25;
ArrayList<Blob> blobs = new ArrayList<Blob>();
int trackColor;
// Kinect Library object
Kinect kinect;
PImage img;
float minThresh = 340;
float maxThresh = 475;
void setup() {
size(640, 480);
kinect = new Kinect(this);
kinect.initDepth();
img = createImage(kinect.width, kinect.height, RGB);
trackColor = color(255,255,255);
}
void draw() {
background(0);
img.loadPixels();
//minThresh = map(mouseX, 0, width, 0, 2048);
//maxThresh = map(mouseY, 0, height, 0, 2048);
//used to determine min and max for best visuals
//create image based on the raw depth
// Get the raw depth as array of integers
//KINECT
//=========================================================
int[] depth = kinect.getRawDepth();
for (int x = 0; x < kinect.width; x++) {
for (int y = 0; y < kinect.height; y++) {
int offset = x + y * kinect.width;
int d = depth[offset];
if (d > minThresh && d < maxThresh) {
img.pixels[offset] = color(255);
}else { img.pixels[offset] = color(0);
}
}
}
img.updatePixels();
image(img,0,0);
/*fill(255);
textSize(32);
text(minThresh + " " + maxThresh, 10, 64);*/
//BLOB=====================================================================
ArrayList<Blob> currentBlobs = new ArrayList<Blob>();
//begin loop to walk through every pixel
for (int x = 0; x < kinect.width; x++ ) {
for (int y = 0; y < kinect.height; y++){
int loc = x + y * kinect.width;
//what is current color
color currentColor = img.pixels[loc];
float r1 = red(currentColor);
float g1 = green(currentColor);
float b1 = blue(currentColor);
float r2 = red(trackColor);
float g2 = green(trackColor);
float b2 = blue(trackColor);
float d = distSq(r1, g1, b1, r2, g2, b2);
if (d < threshold*threshold){
boolean found = false;
for (Blob b : currentBlobs){
if (b.isNear(x,y)){
b.add(x,y);
found = true;
break;
}
}
if (!found){
Blob b = new Blob (x,y);
currentBlobs.add(b);
}
}
}
}
/*for (int i = currentBlobs.size()-1; i>=0; i++){
if(currentBlobs.get(i).size() <500){
currentBlobs.remove(i);
}
}*/
//There is no blobs!
if (blobs.isEmpty() && currentBlobs.size() > 0){
println ("Adding Blobs!");
for (Blob b : currentBlobs){
b.id = blobCounter;
blobs.add(b);
blobCounter++;
}
} else if (blobs.size() <= currentBlobs.size()){
//match whatever blobs you can match)
for (Blob b : blobs){
float recordD = 1000;
Blob matched = null;
for (Blob cb : currentBlobs){
PVector centerB = b.getCenter();
PVector centerCB = cb.getCenter();
float d = PVector.dist(centerB, centerCB);
if (d < recordD && !cb.taken){
recordD = d;
matched = cb;
}
}
matched.taken = true;
b.become(matched);
}
// Whatever is leftover make new blobs
for (Blob b : currentBlobs) {
if (!b.taken) {
b.id = blobCounter;
blobs.add(b);
blobCounter++;
}
}
} else if (blobs.size() > currentBlobs.size()) {
for (Blob b : blobs) {
b.taken = false;
}
// Match whatever blobs you can match
for (Blob cb : currentBlobs) {
float recordD = 1000;
Blob matched = null;
for (Blob b : blobs) {
PVector centerB = b.getCenter();
PVector centerCB = cb.getCenter();
float d = PVector.dist(centerB, centerCB);
if (d < recordD && !b.taken) {
recordD = d;
matched = b;
}
}
if (matched != null) {
matched.taken = true;
// matched.counter = 100;
matched.become(cb);
}
}
for (int i = blobs.size() - 1; i >= 0; i--) {
Blob b = blobs.get(i);
if (!b.taken) {
//if (b.checkCounter()){
blobs.remove(i);
// }
}
}
}
**for (Blob b : blobs) {
** b.show();
**
** if (b.checkCollision() == true){
** b.isColliding();
** } else if (b.checkCollision() == false){
** b.noCollision();
** } b.display();
**}
}
float distSq(float x1, float y1, float x2, float y2) {
float d = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
return d;
}
float distSq(float x1, float y1, float z1, float x2, float y2, float z2) {
float d = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) +(z2-z1)*(z2-z1);
return d;
}
...
with the asterix-ed area being where all the fun happens.
And this is the Blob class:
// Daniel Shiffman
// http://codingtra.in
class Blob {
float minx;
float miny;
float maxx;
float maxy;
float distance;
int id = 0;
int counter = 100;
boolean taken = false;
Blob(float x, float y) {
minx = x;
miny = y;
maxx = x;
maxy = y;
}
void show() {
stroke(0);
fill(255, 100);
strokeWeight(2);
rectMode(CORNERS);
rect(minx, miny, maxx, maxy);
textAlign(CENTER);
textSize(40);
fill(0,255,0);
text(id, minx + (maxx-minx)*0.5, maxy - 10);
textSize(25);
text(counter, minx + (maxx-minx)*0.5, miny - 10);
}
boolean checkCounter(){
counter--;
if (counter < 0){
return true;
} else {
return false;
}
}
void add(float x, float y) {
minx = min(minx, x);
miny = min(miny, y);
maxx = max(maxx, x);
maxy = max(maxy, y);
}
void become(Blob other) {
minx = other.minx;
maxx = other.maxx;
miny = other.miny;
maxy = other.maxy;
}
float size() {
return (maxx-minx)*(maxy-miny);
}
PVector getCenter() {
float x = (maxx - minx)* 0.5 + minx;
float y = (maxy - miny)* 0.5 + miny;
return new PVector(x,y);
}
** void display(){
** float x = (maxx - minx)* 0.5 + minx;
** float y = (maxy - miny)* 0.5 + miny;
** ellipse(x,y,(maxx-minx) + 30, (maxy-miny) + 30);
** }
** void noCollision(){
** fill(0,255,0);
** }
**void isColliding(){
** fill(255,0,0);
**}
**boolean checkCollision() {
** float cx = maxx-minx;
** float cy = maxy-miny;
** for (int i = 0; i < blobs.size() - 1; i++) {
** for (int j = i + 1; j <blobs.size(); j++) {
** if (dist(blobs.get(i).minx, blobs.get(i).miny, blobs.get(j).minx, blobs.get(j).miny) <= 80) {
** return true;
** } else {
** }
** }
** } return false;
** }
boolean isNear(float x, float y) {
float cx = max(min(x, maxx), minx);
float cy = max(min(y, maxy), miny);
float d = distSq(cx, cy, x, y);
if (d < distThreshold*distThreshold) {
return true;
} else {
return false;
}
}
}
What is going wrong to make all the blobs change colour???