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.

Quick-add: + add another resource

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.

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
Loading ..................

Console