make one particle destroy another?

Hi there

I’m working on a project where one set of particles eats another (predator / prey). I’ve got both particle classes interacting nicely (chasing / avoiding / etc), but I am having trouble taking the next step. When the predator catches up with the prey I want it to destroy it but at the moment it doesn’t.

I thought that by comparing their co-ordinates I’d be able to use a conditional to destroy one of the particles but it doesn’t seem to be working. If anyone could give me any pointers on how to do this I’d be most grateful. I’m new to oF so sorry if this is an obvious question! Many thanks!

if (predators[i].pos.x == prey[i].pos.x &&   
predators[i].pos.y == prey[i].pos.y {  
kill prey - not sure about the syntax in here...  

Do you mean destroying the particle object or are you having problems detecting that they are actually touching ?

If your x and y variables there are floats, depending on your simulation you are pretty unlikely to see predator->x == prey->x.

What I would do is something like:

float minDistance = 20.0f;  
float tmpa = predators[i].pos.x - prey[i].pos.x;  
float tmpb = predators[i].pos.y - prey[i].pos.y;  
if( sqrtf( tmpa*tmpa + tmpb*tmpb) < minDistance )  
    prey[i].active = false; // you'll need a var called active of course  

if the prey are in a std vector you can delete it, but it’s not advisable, it’s far more efficient to de-activate it and re-use it later.


Thanks for that - the detection is working really well now - I’m still having trouble with deactivating / kiling the prey though.

How would you go about creating this active variable? I assume it is a bool of some sort and I think it would go in the particle class something like this

void predator :: active() {  
something in here...  

but I’m very unsure about where to put it and more importantly what to put in it.

If I was to ‘deactivate’ the particle would this remove it completely? (ie my other particles (predators) wouldn’t be chasing after prey that isn’t being drawn?

Thanks a lot for the help so far!

What you can do if you want to make it efficient by only deactivate them is as you say having a bool called eg. activated. I suppose you got some loops for creating the objects and to loop thorugh to detect when they hit each other aswell as move them around. In all these loops you simply say if(predator.activated). And when you want to kill a predator, simple say activated = true, and it would’nt get counted any more. And if you want to make a new, simply look for a deactivated ovject, and reset and activate it. Hope it helps.