← back to the observatory

The Cohesion Force Formula

A symbol-by-symbol guide with a concrete worked example

The Equation
F_cohesion (i)  =  αc  ·  ( neighbors  −  rᵢ )

Each colored symbol is explained below

What each symbol means

This formula calculates the pull that draws one agent toward the center of its neighbors — like flocking birds or schooling fish being drawn toward the group.

F
F subscript "cohesion", with argument (i)
Cohesion force on agent i
The output of the formula — a force vector (direction + strength) telling agent i how hard to pull toward its neighbors, and in which direction. It's a vector, meaning it has both a magnitude (how strong) and a direction (which way).
Unit: same as the simulation's force unit
i
Subscript / index variable
The agent we're calculating for
A label (like a name tag) for the specific agent we're interested in right now. In a flock of 50 birds, i might be bird #7. The formula is the same for every bird — only i changes. In programming terms, think of it as the loop variable.
Dimensionless index (just a label)
αc
Greek letter alpha, subscript c
Cohesion gain / strength
A tuning knob — an arbitrary which dictates the degree to which agents are attracted to each other. Double αc and the cohesion force doubles. Set it to zero and cohesion is turned off entirely. The subscript c just distinguishes it from other alpha values (like α for alignment or separation).
Unit: scalar multiplier (this value is arbitrary)
r with a bar over it, subscript "neighbors"
Average position of neighbors
The centroid — the geometric center — of all nearby agents. The bar over r is standard notation meaning "the average of r". So if agent i has 3 neighbors at positions A, B, C, then r̄ = (A + B + C) / 3. This is the target agent i is being pulled toward.
Unit: position vector (e.g. meters in 2D or 3D space)
rᵢ
r subscript i
Agent i's current position
Where agent i actually is right now in space. The subscript i ties this position specifically to our agent of interest. This is subtracted from r̄ to find the direction and distance to the group center.
Unit: position vector (same space as r̄)
Reading the notation

A few conventions that appear here are worth demystifying on their own.

Notation decoder

F_cohesion(i) Function notation. The (i) means "for a specific agent i". It's like a function call — plug in a given agent and get that agent's force back.
r̄ (r-bar) Bar notation always means average. r̄_neighbors is shorthand for "the mean of all neighbors' position vectors". This is common throughout statistics and physics.
rᵢ (subscript i) Subscript indexing. The little i below and after r picks out one specific agent's position from a whole list. r₁ = agent 1's position, r₂ = agent 2's, and so on.
(r̄ − rᵢ) Vector subtraction. Subtracting two positions gives a displacement vector — an arrow pointing from rᵢ toward r̄, whose length is the distance between them. This is how direction and distance are encoded together in one operation.
αc · ( … ) Scalar multiplication. Multiplying a vector by a plain number (scalar) stretches or shrinks it without changing direction. αc scales the pull — same direction, different strength.

What the subtraction (r̄ − rᵢ) looks like geometrically

neighbor 1 neighbor 2 neighbor 3 r̄ (centroid) rᵢ (agent i) (r̄ − rᵢ) direction + distance to group center F_cohesion (scaled by αc)

The arrow (r̄ − rᵢ) points from agent i toward the group center. Multiplying by αc scales how far agent i is nudged in that direction.

Worked example — three birds in a flock

We have a simple 2D flock. Agent i (bird #3) wants to know how strongly to pull toward its two neighbors.

Given:   αc = 0.4  |  Neighbor 1 at (6, 8)  |  Neighbor 2 at (10, 4)  |  Agent i at (3, 2)
Goal: Find F_cohesion(i) — the force vector pulling agent i toward the group center.

1
Find the average position of the neighbors — r̄_neighbors
Average the x-coordinates and average the y-coordinates separately. This gives the centroid — the "center of mass" of the group.
r̄_x = (6 + 10) / 2 = 8.0
r̄_y = (8 + 4) / 2 = 6.0

r̄_neighbors = (8.0, 6.0)
💡 In 2D, positions are pairs (x, y). Averaging a vector just means averaging each component independently.
2
Subtract agent i's position — (r̄ − rᵢ)
Subtract agent i's coordinates from the centroid. The result is a displacement vector pointing from agent i toward the group center.
(r̄ − rᵢ)_x = 8.03 = +5.0
(r̄ − rᵢ)_y = 6.02 = +4.0

(r̄ − rᵢ) = (5.0, 4.0)
💡 Both components are positive, which makes sense — the group center is up and to the right of agent i. This vector is an arrow pointing from (3,2) toward (8,6).
3
Multiply by the cohesion strength αc
Scale the displacement vector by αc = 0.4. This doesn't change the direction — only how strong the resulting force is.
F_x = 0.4 × 5.0 = 2.0
F_y = 0.4 × 4.0 = 1.6

F_cohesion(i) = (2.0, 1.6)
💡 αc = 0.4 means the force is 40% of the raw displacement — a moderate pull. With αc = 1.0 the agent would be pulled to the centroid in one step; αc = 0.1 would be a gentle nudge.
4
Interpret the result
The force vector (2.0, 1.6) tells us:
Direction: northeast — toward the group center
Magnitude: √(2.0² + 1.6²) = √(4.0 + 2.56) = ≈ 2.56 units

Agent i will be nudged 2.56 units in the direction of its neighbors.
💡 The magnitude (length of the vector) is how hard agent i is pulled. The direction is always toward the group center — that's what makes cohesion work as a flocking rule.

A note on vectors in this formula

Unlike the harmonic oscillator (which worked with plain numbers), this formula uses vectors — quantities that carry both a size and a direction. In 2D, a vector is just a pair of numbers (x, y). In 3D it's a triple (x, y, z).

When we calculate the difference between two positions (r̄ − rᵢ), we get an arrow — it points from "where you are" to "where you want to go", and its length dictates how far to travel. Multiplying by αc then controls how aggressively an agent converges on that target.
In plain English, the formula says:
"Find the center of my neighbors.
Draw an arrow from me to that center.
Scale that arrow by αc to decide how hard I'm pulled.
That scaled arrow is my cohesion force."
The formula is computed fresh each timestep as agents move, so the pull constantly updates to chase the evolving group center.