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.004;
var turn_rate = 4;
var turn_max = 0.5;
var sensor_length = 0.5;
var agents = [];

function make_agent() {
  agents.push({
    pos: new vec2(random(), random()),
    vel: vec2.random(normal_speed),
    size: 0.02 * (1 + random()),

    energy: 1,
  });
}

for (var i = 0; i < 10; i++) {
  make_agent();
}

var light = new field2D(64);
//light.set(function(r, c) { return random(); });
light.diffuse(light, 40);
light.normalize();
var light_old = light.clone();

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

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

var i = 10;
while (i--) {
  console.log(i);
}

function update() {
  // swap old & current fields:
  var tmp = light;
  light = light_old;
  light_old = tmp;
  // smoothen:
  light.diffuse(light_old, 0.03);
  light.mul(0.998);
  // add:
  light.deposit(1, source.pos);
  // move source:
  source.pos.add(vec2.random(0.02));

  var i = agents.length;
  
  if (i < 1) {
    make_agent();
    i++;
  }
  
  while (i--) {
    var a = agents[i];
    var food = 0.5 * Math.max(light.sample(a.pos), 0);
    
    if (a.energy < 0.1) {
      // die
      agents.splice(i, 1);
      continue; // go to next iteration of loop
    }
    
   
    if (a.energy < 0.15 && food < 0.1) {
      // hibernating
      a.energy *= 0.999;
      continue;
    }
    
    a.energy *= 0.95;

    // eat:
    light.deposit(-food, a.pos);

    a.energy += food * 0.5; // inefficient digestion

    
    
    if (a.energy > 1) {
      a.energy = a.energy * 0.4;
      // have a baby:
      agents.push({
        pos: a.pos.clone(),
        vel: vec2.random(normal_speed),
        size: 0.02 * (1 + random()),

        energy: a.energy,
      });
    }

    // 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);
    // read field at these locations:
    var sense1 = light.sample(sensor1);
    var sense2 = light.sample(sensor2);

    //console.log(sense1, sense2);
    var speed = normal_speed * (sense1 + sense2 + 0.1);
    var turn = turn_rate * (sense1 - sense2 + 0.01 * srandom());
    // 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() {

  light.draw();

  for (var a of agents) {
    draw2D.push()
      .translate(a.pos)
      .rotate(a.vel.angle())
      .scale(a.size);
    draw2D.color("red").rect([0, 0], 1);
    draw2D.color("yellow").rect([0, 0], a.energy);
    draw2D.color("pink")
      .circle([sensor_length, +sensor_length], 0.3)
      .circle([sensor_length, -sensor_length], 0.3);
    draw2D.pop();
  }
}
            
          
!
999px
Loading ..................

Console