Archive for February, 2009

Another example of using vectors and trig

Friday, February 27th, 2009

Hi this is another example, hopefully the formatting will be less messed up.
This one is about “cars” that move along randomly generated “roads”. I use vector maths to work out the direction to move in and when to stop and trig to work out how to point the car in the right direction
Marco

import processing.opengl.*;
 
/* this sketch draws a bunch of "cars" that move along randomly generated paths or "roads"
 this shows off two bits of maths.
 The first is working out how to move in the direction of the path and stop at the end point.
 This involves calculating a velocity for the car using vector maths and then updating the position of the car using the velocity
 The second is using trigonometry to rotate the car (actually a box) to point in the direction of movement.
 I use atan2 to calculate the angle
 I generate random start and end points for each car and put them in arrays */
 
int numCars = 5;
PVector startPoints[];
PVector endPoints[];
PVector carPos[];
 
/* speed of the cars */
float speed = 1.0;
 
/* speed of the cars float speed = 1.0; */
void setup() {
  size(500, 500, OPENGL);       /* initialize a set of start and end positions for the cars */
 
  startPoints = new PVector[numCars];
  endPoints = new PVector[numCars];
  carPos = new PVector[numCars];
  for (int i = 0; i < numCars; i++)
  {
     startPoints[i] = new PVector(random(0, 600), 400, random(-800, 200));
     endPoints[i] = new PVector(random(0, 600), 400, random(-800, 200));
     carPos[i] = new PVector(startPoints[i].x, startPoints[i].y, startPoints[i].z);
  }
}
 
/* camera stuff from previous weeks */
void cameraMotion()
{
 
  /*start tranforming the camera */
  beginCamera();
 
  /* the keyboard controls forward and backward movement */
  if (keyPressed)
  {
    /* w for forward, s for back */
    if (key == 'w')
      translate(0, 0, -1);
    if (key == 's')
      translate(0, 0, 1);
 
  }
 
  /* the mouse controls rotation */
  if (mousePressed)
  {
    /* yaw */
    rotateY(-(PI/16)*float(mouseX-pmouseX)/300);
    /* pitch */
    rotateX(-(PI/16)*float(mouseY-pmouseY)/300);
  }
  endCamera();
}
 
/* this draws the "roads" that the "cars" drive
   along as grey lines
*/
void drawRoad(PVector start, PVector end)
{
   strokeWeight(10);
   stroke(127);
   line(start.x, start.y, start.z, end.x, end.y, end.z);
}
 
PVector carVel(PVector start, PVector end, PVector carPos)
{
    /* check if the distance of the car from the
       start is bigger than the distance of the end
       i.e. check if the car has overshot
    */
    if(start.dist(carPos) > start.dist(end))
    {
      /* if so return a zero velocity
         i.e. the car should stop moving
      */
      return new PVector(0.0, 0.0, 0.0);
    }
 
    /* calculate the direction from the start to the
       end
       first work out the vector between them by subtracting
    */
    PVector dir = PVector.sub(end, start);
    /* then normalise it (set it to length 1)
       so that it is a direction vector
    */
    dir.normalize();
 
    /* multiply this by the speed to get
       the velocity
    */
    PVector vel = PVector.mult(dir, speed);
    return vel;
}
 
/* rotate the car so it points in the direction it is moving in */
void rotateCar(PVector vel) {
  /* assume that the car is moving only in x and z. 
  use atan2 to calculate the angle of rotation of the car*/
  float angle = atan2(vel.x, vel.z);       
 
  /* rotate about the Y axis as its moving in X and Z */
  rotateY(angle);
}
 
/* transforms and draws the car*/
void drawCar(PVector start, PVector end, PVector carPos) {
  strokeWeight(4);
  stroke(0);
  pushMatrix();
 
  /* calculate the velocity of the car and add it to the current position */
  PVector vel = carVel(start, end, carPos);
  carPos.add(vel);   
 
  /* translate based on the new position*/
  translate(carPos.x, carPos.y, carPos.z); 
 
  /* rotate the car so it points in the direction it is moving in */
  rotateCar(vel);
 
  /* draw it as a box */
  box(20, 20, 40);
  popMatrix();
}
void draw() {
  background(255);  
 
  /* make the camera work*/
  cameraMotion();    
 
  /* loop through all the cars drawing their paths and the cars themselves */
  for (int i = 0; i < numCars; i++)   {
    drawRoad(startPoints[i], endPoints[i]);
    drawCar(startPoints[i], endPoints[i], carPos[i]);
  }
}
PDF Creator    Send article as PDF to

Example of using vectors

Friday, February 27th, 2009

Hi, I’ve been asked for a few more examples of using maths in Processing programs.
This is one example, in which 3D boxes are drawn, but they get bigger depending on how far they are from the middle. I calculate the distance from the midpoint using vectors then apply it to the scale of the box.
Marco

 
import processing.opengl.*;
/* This program show how to draw objects of different heights depending on their distance away from a point
 it shows how to use vectors, and the PVector.dist method */
PVector startPoint = new PVector(0, 0, -300);
void setup() {
  size(500, 500, OPENGL);   /* initial camera setup */   
  camera(300, 300, 1000, 300, 300, 0, 0, 1, 0); 
}
void draw() {
  background(255);
  strokeWeight(5);     /* camera stuff from previous weeks     
  //start tranforming the camera*/
  beginCamera(); 
  /* the keyboard controls forward and backward movement*/
  if (keyPressed)   {
    /* w for forward, s for back*/     
    if (key == 'w')       
      translate(0, 0, -1);     
    if (key == 's')       
      translate(0, 0, 1);               
  }
  /* the mouse controls rotation */  
  if (mousePressed)   {
    /* yaw */     
    rotateY(-(PI/16)*float(mouseX-pmouseX)/300);   
  }   
  endCamera();      
  /* go from a negative position to a positive one  */
  for (int x = -1000; x < 1000; x += 100)   { 
    /* the position of the box   */   
    PVector pos = new PVector(x, 0, -300);          
    /* find the distance between the start point and      
    the box      
    we will use this as the height of the box  */   
    float d = PVector.dist(pos, startPoint);         
    /* draw the box    */  
    pushMatrix();    
    /* move to the correct position      */
    translate(pos.x, pos.y, pos.z);      
    /* scale so its height is multiplied by d     */ 
    scale(100, d, 100);      
    box(1, 1, 1);      
    popMatrix();   
  } 
}
PDF Printer    Send article as PDF to

Creative Computing 1 Assignment 3

Wednesday, February 25th, 2009

Hi,
here is the spec for the third (3D) assignment. It is due on the 6th Feb
In this assignment you will be creating a 3D scene in Processing. This should include:
* A number of shapes * Some shapes should move * Some camera movement
You should submit:
(a) A written report on your implementation, explaining what method you used for each stage, and giving reflections on what went well and what went badly in the assignment
(b) images of your program running
(c) The code files, with comments, explaining each part of the code.
Marks will be assessed in the following way:
* 50% for technical implementation of a basic 3D scene with moving objects and camera * 15% for the aesthetic look of the scene * 15% for an technically challenging extensions * 20% for the report

PDF    Send article as PDF to

Processing Monsters

Tuesday, February 17th, 2009

Here’s a link to an open source Processing project that any one of you could do. Very interesting, and could be the basis of a larger project using agents perhaps…

Basically, it’s a gallery of interactive monsters!!!! Notice that they are all coherent in one particular way…they’re all black and white. This is a good design choice when working on a small project.

Check out the link HERE.

PDF    Send article as PDF to

FLOSSART

Thursday, February 12th, 2009

FLOSS+Art is the first book to cover Free/Libre and Open Source software (FLOSS) for artists and designers not just as a technical tool, but as an artistic medium that works differently and has its own philosophy, politics and aesthetics.

More information, book order, download:

goto10.org/flossart

PDF Creator    Send article as PDF to

Andrew Shoben

Tuesday, February 10th, 2009

I hope you all enjoyed Andrew’s talk on monday.

More information here:

www.greyworld.org

“In 1993, Andrew Shoben founded art group Greyworld in Paris. Their goal is to create works that articulate public spaces, allowing some form of self-expression in areas of the city that people can see every day but would normally exclude and ignore. Greyworld are now based in London, with permanent works in many major cities around the world.”

Taken from the Greyworld Wikipedia page.

en.wikipedia.org/wiki/Greyworld

Andrew in the Guardian

www.guardian.co.uk/theguardian/2003/jun/21/weekend7.weekend

Create PDF    Send article as PDF to

cc assignment

Monday, February 9th, 2009

Assignment3.doc
This is the spec for the 3rd Creative Computing 1 assignment. It is due on the 6th of March
Marco

Create PDF    Send article as PDF to

MaxMSPJitter Introduction

Tuesday, February 3rd, 2009

All of you should find this introduction to MaxMSPJitter of interest. The archive below contains a powerpoint introduction, and some beginning example files. You will all be learning MaxMSP from now on, and some of you will find it very useful, particularly if you want to realise advanced projects in interactive audiovisual media quickly and in small groups. Check it out.

Click here

Create PDF    Send article as PDF to

Impossible Gallery

Tuesday, February 3rd, 2009

Scientific American has an interesting gallery of impossible objects, including Lego versions of M C Escher’s impossible stairs illusion. have a look around. While your there, take a look at some of the free articles. They should give you some good ideas.

Click Here

PDF    Send article as PDF to

Flash Spider Animation

Tuesday, February 3rd, 2009

This animation of a spider seems to have a lot of people interested. It has a number of behaviours and attributes that are user editable. It’s been created in Adobe Flash, and makes use of Flash’s Java-like actionscript. Take a look:

www.onemotion.com/flash/spider/

PDF Printer    Send article as PDF to