1. Overview: Biomechanical Serpentine Kinematics & Tournament Dynamics
In biological locomotion, serpentine movement represents one of nature's most energy-efficient adaptations for traversing complex, obstacle-dense terrains. Unlike rigid robotic bodies or simplified bounding-box kinematic systems, serpentine bodies operate via a continuous chain of articulated vertebrae. Each segment possesses structural mass, inertial damping, and strict angular/spatial constraints relative to its adjacent joints.
This interactive computational laboratory simulates a non-linear, multi-agent predator-prey ecosystem built upon inverse procedural kinematics ($IK$), autonomous steering force vectors, thermodynamic metabolic dissipation, and configurable **Tournament Match Win Conditions**. The green organism (controlled via high-frequency pointer vectors or autonomous demo heuristics) and the yellow autonomous organism (governed by dynamic raycasting, line-projection obstacle avoidance, and priority-weighted foraging heuristics) compete within a shared physical field.
$$\vec{x}_i(t) = \vec{x}_{i-1}(t) - d \cdot \frac{\vec{x}_{i-1}(t) - \vec{x}_i(t)}{\|\vec{x}_{i-1}(t) - \vec{x}_i(t)\|}$$
The equation above dictates the rigid distance constraint enforced across every pair of sequential body segments $i \in [1, N-1]$, where $d$ represents the invariant inter-vertebral separation distance. Concurrently, both agents experience a constant metabolic drain (hunger coefficient $\beta_{h}$), requiring continuous foraging to sustain morphological volume and structural mass. When an agent's radius outscales its competitor beyond the predatory mass threshold ($\kappa \ge 1.20$), the dynamic shifts from symmetrical competition into active apex predation.
3. Technical Details & Algorithmic Architecture
The engine utilizes a zero-dependency high-throughput HTML5 2D Canvas rendering loop optimized for all viewport profiles and high-refresh displays with device pixel ratio ($DPR$) normalization. The core architectural layers include:
A. Forward-Lag Kinematic Follower Formulation
The cephalic segment's instantaneous momentum update is modeled through first-order exponential smoothing:
$$\vec{v}_0(t + \Delta t) = \alpha \cdot \vec{v}_0(t) + (1 - \alpha) \cdot (\vec{x}_{\text{target}} - \vec{x}_0(t))$$
$$\|\vec{v}_0(t + \Delta t)\| \le v_{\text{effective}} = v_{\max} \cdot \left(\frac{r_{\text{base}}}{r_0}\right) \cdot \mu_{\text{boost}} \cdot \gamma_{\text{rubber}}$$
Here, $\alpha = 0.90$ represents the momentum damping constant, $r_{\text{base}} / r_0$ simulates inertial mass scaling (larger organisms exhibit greater momentum but reduced maximum translational velocity), $\mu_{\text{boost}}$ provides the nitro surge multiplier ($1.5\times$), and $\gamma_{\text{rubber}}$ implements dynamic rubber-banding to balance competitive pressure.
B. AI Ray-Projection Obstacle Avoidance
The autonomous yellow agent evaluates environmental targets by calculating scalar projection distances along the Euclidean path vector $\vec{D} = \vec{x}_{\text{food}} - \vec{x}_{\text{AI}}$. For each circular obstacle with radius $R_{\text{obs}}$ centered at $\vec{C}_{\text{obs}}$, the orthogonal clearance metric $d_{\perp}$ is determined by:
$$t = \text{clamp}\left(\frac{(\vec{C}_{\text{obs}} - \vec{x}_{\text{AI}}) \cdot \vec{D}}{\|\vec{D}\|^2}, 0, 1\right), \quad \vec{P}_{\text{proj}} = \vec{x}_{\text{AI}} + t \cdot \vec{D}$$
$$d_{\perp} = \|\vec{C}_{\text{obs}} - \vec{P}_{\text{proj}}\|$$
If $d_{\perp} < R_{\text{obs}} + r_{\text{AI}}$, the line-of-sight is flagged as obstructed, triggering an inversely proportional repulsive steering vector $\vec{F}_{\text{avoid}} = \sum_{k} \frac{-(\vec{C}_k - \vec{x})}{\|\vec{C}_k - \vec{x}\|^2}$ combined with a randomized perturbation routine whenever stuck states are detected over consecutive frames.
C. Web Audio Procedural Sound Synthesis
The application implements a procedural Web Audio API synthesis engine for sound effects and celebratory victory fanfares. Harmonic frequency sweeps (eating chirp: 440 Hz $\rightarrow$ 880 Hz sine wave), low-frequency triangle thump transients (elastic body collisions: 120 Hz $\rightarrow$ 40 Hz), exponential frequency modulated arpeggios (power-up surges), and major chord victory fanfares are synthesized entirely client-side.