You should check this paper, it explains the algorithm very well, but unfortunately the logic has nothing to do with boids.
Jefff_Jones_SlimeMould.pdf (2.2 MB)
2D Arrays have a data structure that layout items in a grid - like form, making it ideal to access elements by row and columns indexes, which is the type of data structure you need when dealing with these kinds of simulations.
Here is a simple field class where I show you how you can look up what
GridAgent a particle is at by using it position:
public class Field2d
public Field2d(int columns, int rows, double resolution)
// your init logic
public GridAgent GetGridAgent(Particle particle)
// You can do the math yourself and manually check the results. You will see how this works
int column = (int) Constrain(particle.Position.X / m_resolution, 0, m_columns - 1);
int row = (int) Constrain(particle.Position.Y / m_resolution, 0, m_rows - 1);
// Retreive grid agent by index
return m_field[column, row];
private double Constrain(double x, double min, double max)
return Math.Max(min, (Math.Min(x, max)));