# Binning based particles system and precise collision numbering

Hi there,
I’m really hesitating between beginner and advanced post but I chose advanced, forgive me if you consider it as stupid…

I digged a lot the Kyle’s binning based system exposed especially here: Quadtree Particle System

Actually, this is that code that I’m studying:

The function `void ParticleSystem::addForce(float targetX, float targetY, float radius, float scale)` contains the whole logic about collision detection:

``````void ParticleSystem::addForce(float targetX, float targetY, float radius, float scale) {
float minX = targetX - radius;
float minY = targetY - radius;
float maxX = targetX + radius;
float maxY = targetY + radius;
if(minX < 0)
minX = 0;
if(minY < 0)
minY = 0;
unsigned minXBin = ((unsigned) minX) >> k;
unsigned minYBin = ((unsigned) minY) >> k;
unsigned maxXBin = ((unsigned) maxX) >> k;
unsigned maxYBin = ((unsigned) maxY) >> k;
maxXBin++;
maxYBin++;
if(maxXBin > xBins)
maxXBin = xBins;
if(maxYBin > yBins)
maxYBin = yBins;
float xd, yd, length, maxrsq;

float xhalf;
int lengthi;

for(int y = minYBin; y < maxYBin; y++) {
for(int x = minXBin; x < maxXBin; x++) {
vector<Particle*>& curBin = bins[y * xBins + x];
int n = curBin.size();
for(int i = 0; i < n; i++) {
Particle& curParticle = *(curBin[i]);
xd = curParticle.x - targetX;
yd = curParticle.y - targetY;
length = xd * xd + yd * yd;
if(length > 0 && length < maxrsq) {

// WHERE THE DETECTION OCCUR

xhalf = 0.5f * length;
lengthi = *(int*) &length;
lengthi = 0x5f3759df - (lengthi >> 1);
length = *(float*) &lengthi;
length *= 1.5f - xhalf * length * length;
xd *= length;
yd *= length;
length = 1 / length;
length = (1 - length);
length *= scale;
xd *= length;
yd *= length;
curParticle.xf += xd;
curParticle.yf += yd;
}
}
}
}
``````

}

if(length > 0 && length < maxrsq) defines the condition where the collision detection occurs.

I’d like to trigger something at each collision detected, especially, some OSC messages triggering.
Including ofxOSC but especially triggering OSC messages in the ParticleSystem class system doesn’t work (thread issues I guess)

So I tried to create a boolean and to make it true when a collision is detected, then testing it in testApp and if true I trigger a message then I make it false etc etc.
It works… but for each collision, I have at least 10 messages triggered (actually, like if 10 collisions detected)

This is a very specific question, but I’d like to figure out how I could handle this.

so would you like to have 1 OSC message triggered if any number of particles have a collision?

then, maybe it’s easy to write a loop like:

``````bool bAnyHit = false;
for (int i = 0; i < particles.size(); i++){
if (particles[i].bHit == true) bAnyHit = true;
}
if (bAnyHit == true){
; // send message
}
``````

hope this helps,
zach

my problem seems to be more about how the collision are detected in that binning system.

Indeed, when I draw on the screen a big cross showing a collision after the if(length > 0 && length < maxrsq) { in the code, I see a lot of cross, but with that process I have not a message popped out for each cross (less messages than cross) so I guess I miss things…

ah you should probably ping @kylemcdonald for this…

just hope he can help me here.
can I ping him from here ? trying on twitter…

I think he’ll get a specific email now that I used his @ name…

also sent him a short message.
hope it can help.