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.

            
              var max_accel = 0.001;
var view_range = 0.01;
var flee_factor = 0.01;

// 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++) {
	agents.push({
		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) {
  source.pos.set(pt);
}

function update() {
  
  // apply source agent to sugar:
  light.deposit(2, source.pos);
  // and then move it (random walk):
  source.vel.rotate(srandom()).len(random()/50);
  source.pos.add(source.vel).wrap(1);
  
  // update field:
  var tmp = light_old;
  light_old = light;
  light = tmp;
  light_old.mul(0.995);
  // diffuse it:
  light.diffuse(light_old, 0.2);
  
  // 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 = 1. - ((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; }
    
    var newvel = a.vel.clone().rotate(steer).len(base_speed * speed);
    for (var n of agents) {
      if (a == n) continue;
      var rel = n.pos.clone().sub(a.pos).relativewrap(1);
      var distance = Math.max(0, rel.len() - a.size - n.size);
      if (distance < view_range) {
        // create a repulsion:
        var factor = 1. - (distance / view_range);
        factor = -factor;
        newvel.add(rel.clone().len(factor * flee_factor))
      }
    }
    
    // locomotion
    var acc = newvel.sub(a.vel).limit(max_accel);
    a.vel.add(acc);
		a.pos.add(a.vel).wrap(1);
	}
}
  
function draw() {
  // draw field
  light.smooth = true;  // don't want pixels
  light.draw();
  // draw agents
  for (var a of agents) {
  	draw2D.push().translate(a.pos).rotate(a.vel).scale(a.size);
    // sensor (shows current state by color)
    draw2D.color(0.7, 0.7, 0.2)
  	  .circle([sensor_size, sensor_size], 0.5)
  	  .circle([sensor_size,-sensor_size], 0.5);
  	// body
    
    draw2D.color("darkred");
  	draw2D.rect([0., 0], 1.5, 1);
  	draw2D.pop();
  }
}
            
          
!
999px
🕑 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 ..................

Console