cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              var normal_speed = 0.001;
var turn_rate = 4;
var turn_max = 0.5;
var sensor_length = 0.5;
var phero_decay = 0.98;
var nest = new vec2(0.5, 0.5);
var nest_radius = 0.05;

var agents = [];
for (var i = 0; i < 10; i++) {
  agents.push({
    pos: nest.clone(),
    vel: vec2.random(normal_speed),
    size: 0.01 * (1+random()),
    
    food: 0,
  });
}

var phero_nest = new field2D(64);
var phero_nest_old = phero_nest.clone();
var phero_food = new field2D(64);
var phero_food_old = phero_food.clone();

var food = new field2D(64);
food.set(function(r, c) { return random(); });
food.diffuse(food, 40);
food.normalize();
food.set(function(r, c) {
  var pos = new vec2(r/food.height, c/food.width);
  if (pos.distance(nest) < 0.3) return 0;
  var C = food.get(r, c);
  if (C > 0.6) { 
    return 1; 
  } else { 
    return 0; 
  }
})

var food_old = food.clone();

var source = {
  pos: new vec2(0.5, 0.5)
};

function mouse(e, pt) {
  if (e == "down") {
    agents[0].pos.set(pt);
  } 
}

function update() {

  
  var tmp = phero_nest;
  phero_nest = phero_nest_old;
  phero_nest_old = tmp;
  phero_nest.diffuse(phero_nest_old, 0.001);
  phero_nest.mul(0.99);
  
  var tmp = phero_food;
  phero_food = phero_food_old;
  phero_food_old = tmp;
  phero_food.diffuse(phero_food_old, 0.001);
  phero_food.mul(phero_decay);
  
  for (var a of agents) {
    
    // compute location of sensors in world:
    var sensor1 = new vec2(sensor_length, +sensor_length)
      .mul(a.size)
      .rotate(a.vel.angle())
      .add(a.pos);
    var sensor2 = new vec2(sensor_length, -sensor_length)
      .mul(a.size)
      .rotate(a.vel.angle())
      .add(a.pos);
    
    var speed = normal_speed;
    var turn = turn_rate * 0.01 * srandom();
    
    if (a.food > 0) {
      // head home
      // look for nest pheromone trail
      
      // read field at these locations:
      var sense1 = phero_nest.sample(sensor1);
      var sense2 = phero_nest.sample(sensor2);
      
      //speed += normal_speed * (sense1 + sense2);
      turn += turn_rate * (sense1 - sense2);
      
      // did we get home?
      if (a.pos.distance(nest) < nest_radius) {
        // we got home:
        a.food = 0;
        a.vel.negate();
      }
      
      // leave a food pheromone trail
      phero_food.deposit(1, a.pos);
    
    } else {
      // keep foraging
      // look for food pheromone trail
      var sense1 = phero_food.sample(sensor1);
      var sense2 = phero_food.sample(sensor2);
      
      //speed += normal_speed * (sense1 + sense2);
      turn += turn_rate * (sense1 - sense2);
      
      // is there food?
      var food_here = Math.max(food.sample(a.pos), 0);
      if (food_here > 0) {
        food.deposit(-food_here, a.pos);
        a.food = 1;// += food_here;
        a.vel.negate();
      }
      
      // leave a nest pheromone trail
      phero_nest.deposit(1, a.pos);
    }

    
    // limit turns:
    if (turn > turn_max) { turn = turn_max; }
    else if (turn < -turn_max) { turn = -turn_max; }
    
    // turn that into steering:
    a.vel.rotate(turn).len(speed);

    // move it
    a.pos.add(a.vel).wrap(1);
  }
}

function draw() {

  draw2D.blend(true);
  draw2D.color("green");
  food.draw();
  draw2D.color(0.5, 0.5, 0);
  phero_food.draw();
  draw2D.color(0.5, 0, 0);
  phero_nest.draw();
  draw2D.blend(false);
  
  // nest
  draw2D.circle(nest, nest_radius*2);
  
  for (var a of agents) {
    draw2D.push()
      .translate(a.pos)
      .rotate(a.vel.angle())
      .scale(a.size);
    draw2D.color(1, a.food, 0).rect();
    draw2D.color("pink")
      .circle([sensor_length, +sensor_length], 0.8)
      .circle([sensor_length, -sensor_length], 0.8);
    draw2D.pop();
  }
}
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console