Simulating Neural Pathways and Logic Gates with Organic Flocking Algorithms
2026/9/12
Creative coding often explores the intersection of chaos and order. In this sketch, Kinetic Flocking Neural Gates, I aimed to visualize the concept of organic neural pathways self-organizing into highly structured, rigid logic gates over time.Visually, the piece resembles a microscopic view of a glowing, neon-lit neural network set against a deep violet void. Swarms of amber and pale green particles flow dynamically across the screen. As they lose momentum, they begin to snap into a rigid, geometric lattice, their connections hardening into bright, pure white geometric links. Python and py5 are ideal for this task—NumPy allows us to efficiently calculate the kinematics and distances for hundreds of particles simultaneously, while py5 easily handles the additive blending required to give the system a cinematic, bioluminescent glow.
Visual & Aesthetic Approach
The aesthetic goal was to contrast two distinct states of matter: organic flow and rigid structure. To achieve the organic flow, the particles do not move randomly; instead, they are driven by a continuous curl noise field, which gives their movement a fluid, swarm-like quality reminiscent of flocking boids. The transition to rigid structure occurs through a simple but effective simulation rule: as a particle's velocity drops below a certain threshold, a new "snap-to-grid" force takes over, pulling it toward the nearest intersection of an invisible geometric lattice. By mapping the color and stroke weight of the connections to both the distance between particles and their current speed, the visual output dynamically shifts from soft, organic amber tendrils into stark, white geometric scaffolding.Code & Technical Breakdown
The core logic of this simulation resides in how the particles update their positions and how they decide to connect to one another.1. Curl Noise and the Snap-to-Grid Mechanism
Rather than implementing a full, complex Boids algorithm (alignment, cohesion, separation), we can achieve a highly organic flocking behavior by pushing the particles through a time-varying noise field.# Update positions based on a curl noise field
for i in range(N_PARTICLES):
nx = py5.os_noise(positions[i, 0] * 0.005, positions[i, 1] * 0.005, t) * py5.TWO_PI * 2
vx = np.cos(nx) * 2
vy = np.sin(nx) * 2
# Smooth velocity interpolation for fluid movement
velocities[i] = velocities[i] * 0.95 + np.array([vx, vy]) * 0.05
positions[i] += velocities[i]
# Snap to grid force (increases when slow)
speed = np.linalg.norm(velocities[i])
if speed < 1.0:
target_x = np.round(positions[i, 0] / GRID_SIZE) * GRID_SIZE
target_y = np.round(positions[i, 1] / GRID_SIZE) * GRID_SIZE
# Pull the particle towards the grid intersection
positions[i, 0] += (target_x - positions[i, 0]) * 0.05
positions[i, 1] += (target_y - positions[i, 1]) * 0.05Here, py5.os_noise generates a smooth, continuous angle for the velocity vector. We use a momentum equation (velocities[i] * 0.95 + ...) so the particles don't change direction instantly, giving them "weight." The critical moment happens when speed < 1.0. The particle calculates the nearest node on a GRID_SIZE lattice and gently interpolates its position towards it, simulating the hardening of a logic gate.
2. Rendering Dynamic Connections
To emphasize the transition from organic to rigid, we evaluate the connections between particles dynamically during the draw loop.# Draw connections
py5.stroke_weight(2)
for i in range(N_PARTICLES):
for j in range(i + 1, N_PARTICLES):
dist = np.linalg.norm(positions[i] - positions[j])
if dist < 120:
speed_i = np.linalg.norm(velocities[i])
if speed_i < 0.5:
# Rigid white connection for "locked" particles
py5.stroke(255, 255, 255, py5.remap(dist, 0, 120, 255, 0))
else:
# Organic amber connection for "flowing" particles
py5.stroke(255, 150, 0, py5.remap(dist, 0, 120, 150, 0))
py5.line(positions[i, 0], positions[i, 1], positions[j, 0], positions[j, 1])By calculating the Euclidean distance between every pair of particles (O(N^2) complexity, easily handled by Python for 300 particles), we draw lines only between neighbors closer than 120 pixels. The stroke color and opacity depend on the distance (fading out smoothly as particles separate) and the velocity of the origin particle. This creates a mesmerizing visual where free-flowing particles trail warm amber light, while stationary particles crystallize into bright white circuits.
