Particle Interaction

Hi All,

I’m new to OpenGL, but I’m trying to write a GLSL shader that will simulate a particle system. I’ve managed to get a particle system working with forces that act on all the particles, but I’m stuck on how to get particles to interact with each other. I’d like to create a repelling force between particles within a certain radius of each other. Here is my current semi-functional fragment shader attempt:

 
#extension GL_ARB_draw_buffers : enable

uniform sampler2D src_tex_unit0; // Position texture

void main(void)
{
	vec2 tex_coord = gl_TexCoord[0].st;

	// Updating particle position.
	vec2 old_pos = texture2D(src_tex_unit0, tex_coord).xy;
    
	vec2 new_pos;	
	
	//compare particle to all other particles to test repulsion / attraction. 
	for(int i = 0; i < 50 ; i++ ){

		vec2 tex_coord1 = vec2(i,i);
		vec2 p1_position = texture2D(src_tex_unit0, tex_coord1).xy;
		
		if(p1_position != old_pos){

			float distance1 = distance(p1_position , old_pos);
			vec2 direction1 = p1_position - old_pos;

			if(distance1 < 300.0){
				new_pos = old_pos - 3.0*normalize(direction1)  ;
			} else {
				new_pos = old_pos ;
			}
		} else {
			new_pos = old_pos;
		}
	}

	gl_FragData[0] = vec4(new_pos, 0.0, 1.0);
}

It works in that all the particles within a certain radius of another particle are pushed away, but for a reason I don’t understand all the particles are just pushed away from a single other particle instead of iterating over several particles and pushing away from these multiple points. I should also point out that I’m not trying to iterate over all particle positions in the above code, but it seems like I should be getting repulsion from at least more than one, like say specifically 50 particles. It is as if it chooses the first particle but then gets stuck and doesn’t repel from any other particle locations.

Any help would be greatly appreciated.

The problem is that you are not updating old_pos with new_pos so new_pos will only contain info from the last particle in the loop.

I’m curious to know why you chose to use GLSL as your simulation platform? What sort of speed-up do you get over a software implementation? My initial reaction would be to use a platform that is more suited for general computation, like CUDA, at least for the simulation, and then perhaps use GLSL for visualization.