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 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 ideal_energy_total = 800;

var agents = [];
var agent_energy_min = 0.1;
var agent_energy_replicate = 1.;
var agent_eating_efficiency = 0.5;
var agent_age_factor = 0.001;
var agent_population_max = 50;

function make_agent() {
  return {
    pos: new vec2(random(), random()), //.add(0.5),
    vel: vec2.random(1/2000),
    size: 0.03,
    energy: 1 + random(),
    sense: 0,
    age: 0,
  }
}

var food = new field2D(64);
for (var i = 0; i < ideal_energy_total; i++) {
  food.deposit(1, random(), random());
}
var food_past = food.clone();

function update() {

  var world_energy_total = food.sum();

  var i = agents.length;
  while (i--) {
    var a = agents[i];

    world_energy_total += a.energy;

    // lose energy
    // lose it faster as we get older
    a.age += dt;
    a.energy -= agent_age_factor * a.age;

    var f = food.sample(a.pos) * agent_eating_efficiency;
    // sanity check
    f = Math.max(f, 0);
    a.sense = f;
    // remove from field
    food.deposit(-f * 0.5, a.pos);

    // gain energy from food:
    a.energy += f * 0.2;

    // die?
    if (a.energy <= agent_energy_min) {
      agents.splice(i, 1);
    } else {
      if (a.energy > agent_energy_replicate) {
        if (agents.length < agent_population_max) {
          // reproduce:
          a.energy *= 0.4;
          
          var child = make_agent();
          child.pos.set(a.pos);
          child.energy = a.energy;
          
          agents.push(child);
        } else {
          // limit:
          a.energy = agent_energy_replicate;
        }
      }

      // random walk
      a.vel.rotate(0.3 * (random() - 0.5));
      // locomotion
      a.pos.add(a.vel).wrap(1);
    }
  }

  // if the world all dies out, try again:
  if (agents.length < 1) {
    agents.push(make_agent());
  }

  // want to keep total food steady:
  var deficit = ideal_energy_total - world_energy_total

  // rain some food:
  while (deficit > 100) {
    food.deposit(deficit, random(), random());
    deficit=0;
  }

  var tmp = food;
  food = food_past;
  food_past = tmp;
  food.diffuse(food_past, 0.03)
}

function mouse(e, p) {
  if (e == "down") {
    // move agent zero
    agents[0].pos.set(p);
  } else {
    // drop sugar
    food.deposit(1, p);
  }
}

  function draw() {
    draw2D.color("darkgreen");
    food.draw();

    for (var a of agents) {
      
      draw2D.push()
        .translate(a.pos)
        .rotate(a.vel.angle())
        .scale(a.size);
      
      var hue = 0.1;
      draw2D.hsl(hue, 1./(1+a.age), 0.3)
        .circle();
      draw2D.hsl(hue, 1./(1+a.age), 0.7)
        .circle([0,0], a.energy/agent_energy_replicate);
      draw2D.hsl(hue, 0.5, a.sense*10)
        .circle([0.4,+0.2], 0.2)
        .circle([0.4,-0.2], 0.2);
      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