Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                var numBv = 30;   //we want to have 30 objects / Braitenberg Vehicles
var bv = [];      //create array to store the objects

//graphic layers
var trailLayer;
var lightLayer;
var vehicleLayer;

//image
var img;
function preload() {
  img = loadImage("https://media.cdnws.com/_i/6533/8687/2017/88/ananas.png");
}
function setup() {
  createCanvas(776, 776);
  background(0);
  angleMode(DEGREES);
  
  trailLayer   = createGraphics(width, height);
  lightLayer   = createGraphics(width, height);
  vehicleLayer = createGraphics(width, height);
  

  
  //create vehicles
  for(var i = 0; i < numBv; i++){
     bv[i] = new Vehicle(random(width), random(height));
  }
}

function draw() {

  //clear layers
  vehicleLayer.clear();
  lightLayer.background(0);

  //draw a light to the light layer
  var n = 50;
  for(var i = 0; i < n; i++) {
    var d = map(i, 0, n, 0, 500);
    lightLayer.fill(255, 10);
    lightLayer.noStroke();
    lightLayer.ellipse(mouseX, mouseY, d);
  }
  

  //update draw vehicles
  for(var i = 0; i < numBv; i++){
    bv[i].updateSensorPosition();
    bv[i].senseAndOrient(img);
    bv[i].move();
    //bv[i].draw();
    bv[i].trail();
  }
  
  //draw the layers
  //image(lightLayer, 0,0);
  image(img, 0,0);
  image(vehicleLayer, 0,0);
  image(trailLayer, 0,0);
  
}

function Vehicle(x,y){
  //properties
  var x = x;
  var y = y;
  var px;
  var py;
  var size = 56;
  var angle = random(360);
  var speed = 1;
  
  //sensor properties
  var sensorSize = 5;
  var sensorAngle = 15;
  var sensorDistance = size / 2 + 10;
  var sensorInfluence = 100;
  
  var sensor1x;
  var sensor1y;
  var sensor1a;
  
  var sensor2x;
  var sensor2y;
  var sensor2a;
  
  //functions
  this.draw = function() {
    
    //style
    vehicleLayer.noFill();
    vehicleLayer.stroke(255);
    
    //draw the body of the vehicle
    vehicleLayer.ellipse(x, y, size);
    
    //line pointing into moving direction
    var x2 = x + sin(angle) * size;
    var y2 = y + cos(angle) * size;
    vehicleLayer.line(x, y, x2, y2);
    
    //draw sensors
    vehicleLayer.ellipse(sensor1x, sensor1y, sensorSize);
    vehicleLayer.ellipse(sensor2x, sensor2y, sensorSize);
  }
  
  this.move = function(){
    px = x;
    py = y;
    
    x += sin(angle) * speed;
    y += cos(angle) * speed;
    
    //check border
    
    if( x > width)  x = 0;
    if( x < 0)      x = width;
    if( y > height) y = 0;
    if( y < 0)      y = height;
    
    
    if( x > width)  angle+=180;
    if( x < 0)      angle+=180;
    if( y > height) angle+=180;
    if( y < 0)      angle+=180;
    
  }
  
  this.updateSensorPosition = function(){
    //sensor 1
    sensor1a = angle + sensorAngle;
    sensor1x = x + sin(sensor1a) * sensorDistance;
    sensor1y = y + cos(sensor1a) * sensorDistance;
    
    //sensor 2
    sensor2a = angle - sensorAngle;
    sensor2x = x + sin(sensor2a) * sensorDistance;
    sensor2y = y + cos(sensor2a) * sensorDistance;
  }
  
  this.senseAndOrient = function(layer){
    var c1 = layer.get(sensor1x,sensor1y);
    var b1 = brightness(c1);
    var influence1 = map(b1, 0, 255, 0,   sensorInfluence);
    
    var c2 = layer.get(sensor2x,sensor2y);
    var b2 = brightness(c2);
    var influence2 = map(b2, 0, 255, 0, - sensorInfluence);
    
    //update angle of the vehicle
    angle += influence1 + influence2;
    
    //updateSpeed
    speed = 1 + (abs(influence1) + abs(influence2)) / 20;
  }
  
  this.trail = function(){
    
    if(x<width && x>0 && y<height && y>0){ 
        trailLayer.stroke(123);
        //trailLayer.noStroke();
        //trailLayer.ellipse(x,y,1);
        trailLayer.line(px, py, x, y);
    }
  }
  
}
              
            
!
999px

Console