Collision between particles

Hi,
I am trying to make a collision between two Particle Emitters: fire and a water jet.

I have this code but it doesn’t work…

var water : ParticleEmitter;
var fire : ParticleEmitter;

function OnParticleCollision (other : GameObject) {
	var water = other.particleEmitter;
	if (water) {
		Destroy(fire);
	}
}

Another problem is that the fire is a prefab so I would like to stop emit only the hit fire particle.

Could you please suggest me a code snippet to make it work?

Have you attached a World Particle Collider to each of them?

Particle systems don’t collide with each other. Particles have no volume, so it’s not really possible.

–Eric

So there is no such way to make it work???

Not using the built-in particle system, no. You could create a custom system using actual objects, or move colliders around based on the positions of the particles in an existing system.

–Eric

anyone has a code snippet? or a sample project…

hi pipposanta, interesting problem!

Like the others said, the particles won’t collide in the normal sense, but you could loop through the particles testing for closeness and still get an equivalent effect. If our two enemy particles get too close, we kill one or both or do whatever.

The downside is it’s testing array against array, so will scale quite badly, but there are some tricks you could do to improve that. Here is a very quick and dirty example I knocked together to show the concept :

http://www.timdawson.com/unity/watervsfire.html

At first I just had the water and fire particles dying when they touched, but that seemed boring so I added steam puffs at the point where the water died.

Here’s the code that does the work :

var waterEmitter : Transform;
var fireEmitter : Transform;
var tollerance : float = 0.1;

var steamEmitter : Transform;

function Update () {
    var waterParticles = waterEmitter.particleEmitter.particles;
	var fireParticles = fireEmitter.particleEmitter.particles;
	
    for (var i=0; i<waterParticles.Length; i++) {
		for (var j=0; j<fireParticles.Length; j++) {
			if (waterParticles[i].energy != 0  fireParticles[j].energy != 0){
				if ((waterParticles[i].position - fireParticles[j].position).sqrMagnitude < tollerance) { // for accurate results, we should use "magnitude" but sqrMagnitude gives acceptable results in this case
					waterParticles[i].energy = 0;
					fireParticles[j].energy = 0;
					
					if (steamEmitter.gameObject.active)
						steamEmitter.particleEmitter.Emit(waterParticles[i].position, Random.insideUnitSphere *3, 0.5, 2, Color.black);
				}
			}
		}
	}
	
	waterEmitter.particleEmitter.particles = waterParticles;
	fireEmitter.particleEmitter.particles = fireParticles;
}

The logic is very simple, just get both particle emitters, get both arrays of particles, then run through them. If you have a lot of particles, this will get very slow, but you could probably do some simple broad phase checks to reduce the work done.

I spawn a steam particle when they hit as an example but other obvious ways to extend this would be to make either the fire or water particle only lose some of their energy on collision - you could have a fire particle that takes multiple water particles to kill it, or visa versa.

Hope this is helpful!

wow, that was nice.

thanks man, that’s helpful to me as well.

Glad you liked it hypnoslave, but on re-reading I think I misunderstood what pipposanta wanted!

if the idea is just to turn off the fire when water hits it’s base, that’s much easier, your code was nearly right. Just change it to :

var water : ParticleEmitter; 
var fire : ParticleEmitter; 

function OnParticleCollision (other : GameObject) { 
   if (water == other.particleEmitter) { 
		fire.emit = false;
   } 
}

this will give you something like this - http://www.timdawson.com/unity/watervsfire2.html

Note that I turned off the emitter instead of destroying it - this stops the particles that are already spawned from just popping out of existance. If it’s important to destroy the emitter, just enable Autodestruct and they’ll kill themselves when they’re done.

Just been trying to implement this code into my environment, attached the script to a fire and my rain is attached to my camera, which is the water, I am not sure why, but it seems to work sometimes but far from everytime and I am really not sure what is the problem, I turn my rain on, walk over to the fire and after sometime it will turn the fire off so to speak. But maybe out of 10 attempts it has worked less than 3 times, I am not sure what is going on here, any ideas?

Regards

Its been 11 years
is it possible now?

1 Like

No, sadly. As usual, unity fails to update things that are really needed.

1 Like

No unfortunately, if it happens with raymarching shader we can easily create fluid simulations like water, smoke etc.

I believe it would be possible to place a proxy gameobject with a sphere collider at every particle position and do Physics.OverlapSphere to get a very decent performing collision detection. The proxy gameobjects should be pooled as instantiating them would probably be quite slow as they’ll need to be added to the Physics engine’s internal data structure (Quadtree)…

You could also use Graphics.DrawMeshInstanced to create your own particle system with blackjack and hookers!.