Constraining Particles

The ParticleBlinkScript node can be used to constrain particle positions. As an example, this kernel constrains particles to the surface of a sphere. Each particle is moved outwards or inwards to the radius of the sphere and its velocity rotated to point tangent to the sphere’s surface.

The resulting effect of the following particle constraining kernel

Note:  A more realistic version of this kernel would involve the gradual moving of the particles, rather than doing it instantaneously.

Example node graph for the following kernel

kernel ParticleSphereKernel : ImageComputationKernel<ePixelWise>

{

Image<eReadWrite> p_position;

Image<eReadWrite> p_velocity;

 

param:

float3 _centre;

float _radius;

void define() {

defineParam(_centre, "pa_position", float3(0.0f, 0.0f, 0.0f));

defineParam(_radius, "pa_radius", 1.0f);

}

void process() {

float3 d = p_position()-_centre;

float r = length(d);

if ( r != 0.0f ) {

d /= r;

// Move the particle to the sphere surface

p_position() = _centre + d*_radius;

 

// Change the velocity to follow the surface

float3 v = p_velocity();

float lv = length(v);

if ( lv != 0.0f ) {

v /= lv;

p_velocity() = cross(cross(d, v), d)*lv;

}

}

}

};