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 agents = [];

function make_agent() {
  var a = {
    pos: new vec2(random(), random()),
    vel: vec2.random(0.001),
    size: 1/30,
    max_force: 1/400,
    max_speed: 1/100,
    view_range: 0.3,
  };
  agents.push(a);
}
for (var i=0; i<100; i++) make_agent();

var target = {
  pos: new vec2(random(), random()),
  pos_prev: new vec2(),
  vel: new vec2(),
  size: 0.1,
};
function mouse(e, p) {
  if (e == "down") {
    var pos_prev = target.pos.clone();
    target.pos.set(p);
    target.vel = target.pos.clone().sub(pos_prev);
  }
}

function update() {
  var targetspd = target.vel.len();
  
  for (var a of agents) {
    // given the position of the light (target.pos)
    // how do I compute the required steering?
    // target from the agent's point of view
    var rel = target.pos.clone().sub(a.pos);
    rel.relativewrap(1);
    
    // limit this to our top speed
    var desired_velocity = rel;
    desired_velocity.limit(a.max_speed);
    
    // run away?
    if (targetspd > 0.01) desired_velocity.negate();
    
    for (var n of agents) {
      // don't check self:
      if (a != n) {
        var rel = n.pos.clone().sub(a.pos);
        rel.relativewrap(1);
        
        // where is the target in the agent's field of view?
        var heading = a.vel.angle(); // our direction in radians
        var relview = rel.clone().rotate(-heading);
        // will we collide?
        var in_front = relview[0] > 0;
        var in_range = relview[0] < a.view_range;
        var in_path = Math.abs(relview[1]) < (a.size/2 + n.size/2);
        if (in_front && in_range && in_path) {
          // take evasive action!
          var avoid = new vec2(0, -relview[1]);
          avoid.rotate(heading);
          desired_velocity.add(avoid);
        }
      }
    }
    
    // locomotion:
    
    // subtract our current velocity
    var steering = desired_velocity.sub(a.vel);
    // forward Euler integration
    //var steering = vec2.random(0.001);
    var acceleration = steering.limit(a.max_force);
    a.vel.add(acceleration).limit(a.max_speed);
    a.pos.add(a.vel).wrap(1);
    
  }
}

function draw() {
  draw2D.color("yellow");
  draw2D.circle(target.pos, target.size);
  
  for (var a of agents) {
    draw2D.push()
      .translate(a.pos)
      .rotate(a.vel)
      .scale(a.size);
      draw2D.color("green");
      draw2D.circle();
      draw2D.color("white");
      draw2D.circle([0.25, 0.25], 0.2);
      draw2D.circle([0.25,-0.25], 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