Physarum Transport System

Slime Mold Sensor Logic

class Agent {
  getSensorValue(offsetAngle) {
    const sx = this.x + Math.cos(this.angle + offsetAngle) * this.sensorDist;
    const sy = this.y + Math.sin(this.angle + offsetAngle) * this.sensorDist;

    // Read pheromone (green channel) from canvas
    const data = ctx.getImageData(sx, sy, 1, 1).data;
    return data[1];
  }

  update() {
    const forward = this.getSensorValue(0);
    const left = this.getSensorValue(-0.5);
    const right = this.getSensorValue(0.5);

    // Turn toward highest concentration
    if (left > right) this.angle -= this.turnSpeed;
    else if (right > left) this.angle += this.turnSpeed;

    this.x += Math.cos(this.angle) * speed;
    this.y += Math.sin(this.angle) * speed;
  }
}