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 a = {
  // the current desired steering 
  steering: new vec2(),
  // for random walk, current turning tendency
  wander: vec2.random(),  
  // clipping:
  max_force: 1/2000,
  max_speed: 1/200,
  // physics:
  pos: new vec2(0.5, 0.5),
  vel: vec2.random(0.001),
  // visual:
  size: 1/40
};

// simple locomotion model
function locomotion(a) {
  // interpret steering as a force to get acceleration
  var force = a.steering.limit(a.max_force);
  var acceleration = force; // divided by mass?
  // forward Euler integration & clipping
  a.vel.add(acceleration);
  a.vel.limit(a.max_speed);
  a.pos.add(a.vel);
  // keep in world
  a.pos.wrap(1);
}

// not recommended!
function steering_twitchy_wander(a) {
  a.steering = vec2.random();
}

function steering_wander(a) {
  // Reynold's recommendation:
  // random deviations to a stored value
  // constrained to lie on a circle
  a.wander.add(vec2.random(0.5)).len(0.5);
  // put this directly ahead of the agent:
  var desired_velocity = a.vel.clone().len(0.5).add(a.wander);
  // usual stuff:
  desired_velocity.limit(a.max_speed);
  a.steering = desired_velocity.sub(a.vel);
}

function update() {
  steering_wander(a);
  locomotion(a);
}
  
function draw() {
  // push into agent's local coordinate system
    draw2D.push()
      .translate(a.pos)
      .rotate(a.vel)
      .scale(a.size);
      // draw agent body:
      draw2D.color("pink");
      draw2D.rect();
      // draw agent eyes:
      draw2D.color("white");
      draw2D.circle([0.5, 0.5], 0.5);
      draw2D.circle([0.5, -0.5], 0.5);
    // done drawing agent:
    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