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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

              // try increasing this, see if long antennae are a good idea!
var sensor_size = 4/5; 
var min_sense = 0.01; // sensors never report zero

var max_steer = 0.1;
var base_speed = 1/200;
var min_speed = base_speed/10;

// create a field of sugar concentrations
var light = new field2D(64);
var light_old = light.clone();

// create some vehicles
var agents = [];
for (var i=0; i<40; i++) {
		pos: new vec2(random(), random()),
		vel: vec2.random(min_speed),
		size: 1/(50 + random(50)),

// a sugar deposit point
var source = {
  pos: new vec2(0.5, 0.5),
	vel: vec2.random(),

// mouse moves source:
function mouse(e, pt) {

function update() {
  // apply source agent to sugar:
  light.deposit(2, source.pos);
  // and then move it (random walk):
  // update field:
  var tmp = light_old;
  light_old = light;
  light = tmp;
  // diffuse it:
  light.diffuse(light_old, 0.1);
  // nervous system:
	for (var a of agents) {
    // get agent heading:
    var dir = a.vel.angle();
    // get sensor locations:
    var sensor1 = new vec2(sensor_size, sensor_size).mul(a.size).rotate(dir).add(a.pos);
    var sensor2 = new vec2(sensor_size, -sensor_size).mul(a.size).rotate(dir).add(a.pos);
    // get the sugar levels at these locations:
		var sense1 = light.sample(sensor1);
		var sense2 = light.sample(sensor2);
    // clamp:
    sense1 = Math.max(Math.min(sense1, 1), min_sense);
    sense2 = Math.max(Math.min(sense2, 1), min_sense);
    // convert to locomotion (direct wiring):
    a.wheel1 = sense2;
    a.wheel2 = sense1;
  // implement locomotion
  for (var a of agents) {
    var speed = base_speed * (a.wheel1 + a.wheel2)/2;
    var steer = (a.wheel2 - a.wheel1) / (a.wheel1 + a.wheel2);
    // add a little random walk:
    steer += srandom()*0.1;
    // limit:
    if (speed < min_speed) { speed = min_speed; }
    if (steer > max_steer) { steer = max_steer; }
    else if (steer < -max_steer) { steer = -max_steer; }
    // apply (TODO -- as desired force...):
    // locomotion
function draw() {
  // draw field
  light.smooth = true;  // don't want pixels
  // draw agents
  for (var a of agents) {
    // sensor (shows current state by color)
    draw2D.color(1, 0.5, 0.5)
  	  .circle([sensor_size, sensor_size], 0.5)
  	  .circle([sensor_size,-sensor_size], 0.5);
  	// body
  		.rect([0., 0], 1.5, 1);
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................