Behaviors
How GOKIs's critters move — threat response, wall following, and the entomology research behind it.
GOKIs’s critters are not following a canned animation loop. Every frame, the GPU evaluates each critter’s state, picks a behavior, and updates position and velocity. The model is grounded in published research on cockroach escape behavior.
What triggers an escape
The mouse cursor is treated as a predator. Each critter measures the distance to the cursor every frame. Anything within a 180-pixel threat radius counts as a threat, and the response comes in two stages. A weak stimulus triggers an alert pause — the critter freezes and assesses. A strong one triggers an escape.
This two-stage model comes from Camhi & Nolen’s 1981 work on cockroach giant fibre responses.
How the escape moves
When a critter escapes, it does not pick a random direction. It chooses from five preferred escape angles relative to the threat, measured so that 180° points straight away from it:
| Angle | Meaning |
|---|---|
| 95° | Near-perpendicular dash |
| 125° | Diagonal away |
| 150° | Sharp turn away |
| 180° | Straight retreat |
| 205° | Overshoot to the far side |
Which of these are on the table depends on where the threat sits relative to the critter’s body: a threat in front can trigger any of 95°–180°, a threat to the side narrows it to 125°–180°, and a threat from behind uses 150°, 180°, or 205°.
Roughly 90% of escapes are “away” responses; the remaining 10% counter-intuitively head toward the threat — usually a small misfiring turn, occasionally a full charge. Both numbers match the observations in Domenici et al. (2008).
Wall following
Cockroaches feel safer when they are touching something. GOKIs models this with thigmotaxis (innate preference for wall contact, varied per individual). Critters that are not currently fleeing tend to:
- Drift toward the nearest screen edge (or text boundary, see below).
- Move along edges once they reach them.
- Linger near corners.
Each critter has its own personality — three values (thigmotaxis, boldness, activity) sampled from a Gaussian distribution at spawn time. So no two critters behave quite the same way, and the population stays interesting at any scale.
Daily vs survival behaviors
The internal state machine separates two kinds of behavior:
- Survival behaviors — ALERT (freezing) and ESCAPE. Triggered by threats. Always win over everything else.
- Daily behaviors — REST (recovering fatigue), WALL_FOLLOW (drifting along edges), EXPLORE (wandering in open space), SEEK_WALL (heading toward an edge). Selected by utility scoring when no threat is near.
The utility score balances current fatigue, contact need, recent history (you tend to keep doing what you were just doing), and individual personality.
Text boundary
A signed distance field generated from the word “GOKIs” is layered on top of the screen bounds. Critters treat the outline of the text as a wall too, so they cluster inside the letters when idle. Toggle it off from the menu bar (Show Text Boundary) if you prefer plain rectangular screen edges.
References
- Camhi, J. M. & Nolen, T. G. (1981). Properties of the escape system of cockroaches. Journal of Comparative Physiology.
- Domenici, P. et al. (2008). Cockroaches keep predators guessing by using preferred escape trajectories. Current Biology.