Averaging Velocity

I have several particles in random motion, I want to maintain a regular velocity between them…

I have calculated the average velocity by using the framevelocity node, summing the output and dividing by the number of objects.

how would I implement

pvj = (pvj/n-1)-average_velocity;
return pvj/8;

where pvj is a vector representing each object’s percieved velocity and n is the number of objects??

Do I need some sort of feedback loop? When I tried this, I just got an infinite loop…

Any help would be much appreciated!

Full pseudocode here

Rule 3: Boids try to match velocity with near boids.
This is similar to Rule 1, however instead of averaging the positions of the other boids we average the velocities. We calculate a ‘perceived velocity’, pvJ, then add a small portion (about an eighth) to the boid’s current velocity.

PROCEDURE rule3(boid bJ)

	Vector pvJ

	FOR EACH BOID b
		IF b != bJ THEN
			pvJ = pvJ + b.velocity
		END IF
	END

	pvJ = pvJ / N-1

	RETURN (pvJ - bJ.velocity) / 8

END PROCEDURE