# Accessing next spread value -- Boids project -- ATTRACTOR

Hey! Sorry for what may be a stupid q but I am a complete newbie…

I am trying to implement a minimum distance check in a thing I am doing. Basically I have a spread of 2d vectors and I want ensure they stay a certain distance apart while in movement. I was just wondering how you can access the N+1 position in a spread?

I want to do a euclidean distance so I need (Xn,Yn) and (Xn+1,Yn+1). So how do you do this for all the spreads?

Any help would be much appreciated!

edit I just found differential! :)

[edit 2](edit 2) Ok I found differential but using this gives distances 1->2, 2->3, etc.

will have to modify it to scan through each spread maybe… Any ideas??

I am trying to do an implementation of the boids algorithm for a college project.

I need this part for the second rule… Pseudo code is below!

Rule 2: Boids try to keep a small distance away from other objects (including other boids).

The purpose of this rule is to for boids to make sure they don’t collide into each other. I simply look at each boid, and if it’s within a defined small distance (say 100 units) of another boid move it as far away again as it already is. This is done by subtracting from a vector c the displacement of each boid which is near by. We initialise c to zero as we want this rule to give us a vector which when added to the current position moves a boid away from those near it.

In pseudocode:

``````PROCEDURE rule2(boid bJ)

Vector c = 0;

FOR EACH BOID b
IF b != bJ THEN
IF |b.position - bJ.position| < 100 THEN
c = c - (b.position - bJ.position)
END IF
END IF
END

RETURN c

END PROCEDURE
``````

Thanks so much!

hello si.

if your a bit familiar with coding, you should just check out the particles plugin. its a mix between a particle system and the boids algorithm, with only a few changes, it should do what you want.

as you have to check each object with each other, it can lead to very high spreadcounts, so writing a plugin is much better for performance too. see here howto write plugins.

vvvv is not that cool for things that work best with a procedural approach like this; in this case, mostly because you’d have to create a spread with n*n slices to compare every boid with every other boid. this is obviously not so computationally smart, but still possible.

however, something very similar has been done by dottore in his BoublesRadius (2D Spreads) module; it might give you some ideas.

Thanks a million guys, will check these things out.

Would the resample node work for this diki? If I set it to point mode it would create n repetitions of each value so I could compare them that way?

exactly! :)
also nice: Normalize (3d Vector) for getting the length of a vector (e.g. the distance between the boids).

Cool! Thanks, will try that…

Could I use the
Attractor Node (2d) for this problem?

With :
Radius = Min distance between boids
Strength = < 1
Power = No of Boids (eg 10)
X,Y = Boid locations (10)
Attractor X,Y = Resampled Boid locations (100)

I tried this with the attractor node but I get a “max float” output… What does this mean?

hey!

the standard attractor can be used for this task.

note that i didn’t feedback the positions computed by the attractor. so this patch is probably only a small part of the solution. however it achieves to keep particles apart from each other.

if you want to ensure that particles never jump then you would need to store the newly computed positions and feed them back via framedelay. rule 1 then will probably compute new positions depending on previous boid positions.

so forget the randomspread and lfo part…

attractor (value) is the only node you need. attractor (2d) and (self) can be patched (and seem to be buggy). so probably those should be replaced by legacy modules in coming releases.

rule2.v4p (7.5 kB)

Thanks a lot gregsn! I will check that out when I get home, in college at the moment…

Appreciate the help

Si