cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              // create a field of sugar concentrations
var sugar = new field2D(128);
var sugar_old = sugar.clone();

var phero_food = sugar.clone();
var phero_food_old = phero_food.clone();
var phero_nest = sugar.clone();
var phero_nest_old = phero_nest.clone();

var phero_diffuse = 0.01;
var phero_decay = 0.99;

var signal_decay = 0.01;

var nest = new vec2(0.5, 0.5);
var nest_radius = 0.03;

var sensor_size = 0.5;
var sensor_noise = 0.01;

// fill it:
function reset_sugar() {
  // clear it:
  sugar.set(0);
  // add some randomized points:
  for (var i = 0; i < 10; i++) {
    var p = vec2.random(0.8).add(1).mul(0.5);
    sugar.deposit(random(8), p);
  }
  // blur it
  sugar.diffuse(sugar.clone(), sugar.width, 150);
  // fit to 0..1 range
  sugar.normalize();
  // threshold it:
  sugar.map(function(v) {
    return v > 0.1 ? 1 : 0;
  });
}
reset_sugar();

var agents = [];
for (var i = 0; i < 100; i++) {
  agents.push({
    pos: nest.clone(),
    vel: vec2.random(0.001),
    size: 0.01,
    // internal states:
    has_food: false,
    signal: 1,
  });
}

function update() {
  // update field:
  var tmp = phero_food_old;
  phero_food_old = phero_food;
  phero_food = tmp;
  // diffuse it:
  phero_food.diffuse(phero_food_old, phero_diffuse).mul(phero_decay);
  // small background noise:
  //phero_food.map(function(v) { return v + 0.01*srandom(); });

  // update field:
  var tmp = phero_nest_old;
  phero_nest_old = phero_nest;
  phero_nest = tmp;
  // diffuse it:
  phero_nest.diffuse(phero_nest_old, phero_diffuse).mul(phero_decay);
  // small background noise:
  //phero_food.map(function(v) { return v + 0.01*srandom(); });

  for (var a of agents) {
    var dir = a.vel.angle();
    // get sensor location:
    var s1 = new vec2(sensor_size, sensor_size).mul(a.size).rotate(dir).add(a.pos);
    var s2 = new vec2(sensor_size, -sensor_size).mul(a.size).rotate(dir).add(a.pos);
   
    var steer = srandom() * 0.5;
    if (a.has_food) {
      // trail:
      if (a.signal > 0) phero_food.deposit(a.signal, a.pos);
      
      // arrive?
      var atnest = a.pos.distance(nest) < nest_radius;
      if (atnest) {
        a.signal = 1;
        a.has_food = false;
        a.vel.negate();
      }
      
      var v1 = phero_nest.sample(s1);
      var v2 = phero_nest.sample(s2);
      if (v1 + v2 > sensor_noise) {
        steer = v1-v2;
      }
    } else {
      // trail:
      if (a.signal > 0) phero_nest.deposit(a.signal, a.pos);
      
      // arrive?
      var food = Math.max(0, sugar.sample(a.pos));
      if (food > 0) {
        a.signal = 1;
        sugar.deposit(-food, a.pos);
        a.has_food = true;
        a.vel.negate();

      }
      
      var v1 = phero_food.sample(s1);
      var v2 = phero_food.sample(s2);
      if (v1 + v2 > sensor_noise) {
        steer = v1-v2;
      }
    }
    
    // trail gradually weakens:
    a.signal -= signal_decay;
    
    // steering & locomotion
    a.vel.rotate(steer).len(0.005);
    a.pos.add(a.vel).wrap(1);
  }
}

function draw() {
  // draw fields
  draw2D.blend(true);
  draw2D.color("green");
  phero_food.draw();
  draw2D.color("darkred");
  phero_nest.draw();
  draw2D.color("orange");
  sugar.draw();
  draw2D.blend(false);
  // draw nest
  draw2D.color("darkred");
  draw2D.circle(nest, nest_radius * 2);
  // draw agents
  for (var a of agents) {
    draw2D.push().translate(a.pos).rotate(a.vel).scale(a.size);
    // body
    draw2D.color("darkred")
      .circle([0.3, 0]).circle([-0.3, 0]);
    // sensor (shows current state by color)
    draw2D.color(a.has_food ? "orange" : "green");
    draw2D.circle([sensor_size, sensor_size], sensor_size);
    draw2D.circle([sensor_size,-sensor_size], sensor_size);
    draw2D.pop();
  }
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console