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.

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

// fill it:
function reset_sugar() {
	// fill with uniform noise
	sugar.set(0);
}
reset_sugar();

// mouse adds sugar:
function mouse(e, pt) {
	sugar.deposit(10, pt);
}

var agents = [];
for (var i=0; i<40; i++) {
	agents.push({
		pos: new vec2(random(), random()),
		vel: vec2.random(0.001),
		size: 0.02,
    // stored recollection of previous sense
		sense_memory: 0.5,
	});
}

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

function update() {
  // update field:
  var tmp = sugar_old;
  sugar_old = sugar;
  sugar = tmp;
  sugar.mul(0.999);
  // diffuse it:
  sugar.diffuse(sugar_old, 0.1);
  // small background noise:
  //sugar.map(function(v) { return v + 0.01*srandom(); });

  // apply source agent to sugar:
  sugar.deposit(2, source.pos);
  // and then move it (random walk):
  source.vel.rotate(random()-0.5).len(random()*0.003);
  source.pos.add(source.vel).wrap(1);
  
	for (var a of agents) {
    // get the sugar level at this location:
		var sense = sugar.sample(a.pos);
    // sanity: no such thing as a negative intensity:
    sense = Math.max(0, sense);
    // update the field to show that we removed sugar here:
	sugar.deposit(-sense, a.pos);
    
    // compare to the previous one:
    var change = sense / a.sense_memory;
		if (change > 1) {
      // getting better
      // forward swimming behavior:
			a.vel
        .rotate(srandom()*0.1)
        .len(0.005);
		} else {
      // getting worse
      // tumbling behavior:
			a.vel
        .rotate(srandom())
        .len(0.0005);
		}	
		// remember for the next time:
		a.sense_memory = sense;
		
		// locomotion
		a.pos.add(a.vel).wrap(1);
	}
}
  
function draw() {
  // draw field
  sugar.draw();
  // 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(1-a.sense_memory, a.sense_memory, 0.5)
  	  .circle([0.3, 0.], 0.7);
  	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