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.

              // simple vehicle model adapted from http://www.red3d.com/cwr/steer/gdc99/
var a = {
  // action:
  sense_range: 0.4, // how far it can see
  // the current desired steering 
  steering: new vec2(),
  // clipping:
  max_force: 1/2000,
  max_speed: 1/100,
  // physics:
  pos: new vec2(0.1),
  vel: new vec2(),
  // visual properties
  size: 1/40, 

// the location of interest (moved with mouse)
var target = new vec2(0.8);

var obstacle = {
  pos: new vec2(0.5),
  size: 0.2

function mouse(e, p) {
  // click to toggle seek/flee:
  if (e == "down") {
  } else {

// 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

// get desired steering to approach a target:
function steering_seek(a, target) { 
  // we want our velocity to get us 
  // from the current location to the target
  var rel = target.clone().sub(a.pos);
  // but it can't go any faster than our top speed:
  var desired_velocity = rel.limit(a.max_speed);
  // steering is relative to current velocity
  a.steering = desired_velocity.sub(a.vel);

function steering_avoid(a, o) {
  // get current heading:
  var heading = a.vel.angle();
  // get vector to obstacle:
  var rel = o.pos.clone().sub(a.pos);
  // rotate into our current view frame:
  // is the obstacle in our path of collision?
  var in_front = rel[0] > 0;
  var in_range = rel[0] < a.sense_range;
  // abs() captures left and right:
  var in_path = Math.abs(rel[1]) < (o.size + a.size)/2; 
  if (in_front && in_range && in_path) {
    // zero forward component and negate side component:
    var desired_velocity = rel.mul([0, -1]);
    // rotate back to global:
    // usual limit & add to steering:

function update() {
  // absent: action selection (currently just mouse)  
  // steering calculation
  steering_seek(a, target);
  // adjust to avoid obstacle:
  steering_avoid(a, obstacle);
  // turn into motion:

function draw() {
  // draw obstacle:
  draw2D.circle(obstacle.pos, obstacle.size);
  // push into agent coordinate frame
    // simple body and "eyes"
    draw2D.rect([0.4, 0.4], 0.2);
    draw2D.rect([0.4,-0.4], 0.2);
  // done drawing agent

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.


Loading ..................