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.

Quick-add: + add another resource

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.

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
Loading ..................