# Particles, blobs and ofPolyline

Hi all,

I am attempting to make some particles (one of the MODES defined on the maths examples) interact with the blobs generated through ofxOpenCv. I am using the ofPolyLine.inside() to determine whether the particles have to react with the blob. The problem that has emerged is that when the blob is created the particles that were in the area of the blob are encapsulated, therefore the particles become “trapped” keeping vibrating (moving like 1 pixels) inside the blob. Also, some of the particles pass through the blob (perhaps this is because the blobs are not very stable). I attempted to define that if the particle’s previous and current position are inside the blob, the particle reset it position to a random place in the window. However, it seems that this approach does not work . Perhaps I am not grabbing properly the previous position. There is another strategy or solution?

here is the specific part of the code I am talking about:

``````    for (auto bIndex = 0; bIndex < contourFinder.nBlobs; bIndex++) {
ofPolyline contourLines = contourFinder.blobs[bIndex].pts;
for(unsigned int i = 0; i < p.size(); i++){
if (contourLines.inside(p[i].pos) == true && contourLines.inside(previousPos) == false) {
p[i].vel *= -1; //changing the direction of the particle
}
if (contourLines.inside(p[i].pos) == true && contourLines.inside(previousPos) == true) {
p[i].reset(); // reset the particle
}

previousPos = p[i].pos;

}
}
}

``````

thanks

hello!

C++ problem: you are overwriting `previousPos` with every pos, so the next iteration of the loop will compare the pos with the “previous” (in the sense of previous within the loop), not the previous in the sense you expect (previous position of `p[i]`). si you either need to have a `p[i].previousPos` (to maintain a previous pos for each blob), or some other structure besides it.

however there is also a functional problem: the blobs in contourFinder have no history: blob#0 in frame 1 can be blob#3 in frame 2. so even if you store the history of blobs, you cannot rely on [i] to tie them together (you have to check all the blobs vs all the blobs to maintain them).

so what you are describing is called tracking. you can start by searching for that (blob tracking) and you’ll find lots of code that looks a bit like yours, and work from there.

Hi,

thanks for the answer. It is now clear to me the problem with the loop and the previous position, but I thought that the condition “inside” for each particle was blob agnostic since it is only asks whether the particle was inside or not without being attached to the existence of any blob. I’ll search for code related with blob tracking.

thanks

Ah i understand better your goal, but then you should not compare with previous_position but with particle_position! (Which we don’t see in your snippet). So it’s not really about tracking.

When you post code, it facilitates things to post whole compilable files .h and .cpp so people can quickly run it (otherwise it can be pretty abstract).