WEEK 6: Tut 9, 10, 11

by Sarah Shultz

Tutorial 9: Simple Animation

  • Show Sketch
/** @peep sketchcode */
// draw() is called every time the display is updated, gets called automaticall
 
// this code has an ellipse redrawn in each frame in a NEW position, based on the balue of the y variable 
float y = 0;
 
void setup() {
  size(200, 200);
  background(204);
}
 
void draw() {
  ellipse(width/2, y, 40, 40);
  y += 0.5;
}
  • Setting the animation speed
  • Show Sketch
/** @peep sketchcode */
void setup() {
  size(200, 200);
  background(204);
  frameRate(5);
}
 
// by chainging the float number that is multiplied by the frame rate, you can either slow down or speed up the frame rate to better observe the action happening within each frame
void draw() {
  float y = frameCount * 3.0;
  ellipse(width/2, y, 40, 40);
}

Fading the background: Edited this code to have a differnet background color and the fading tail ellipse, sometimes they match up, sometimes they dont, but variation and the randomness of each iteration is fun to explore

  • Show Sketch
/** @peep sketchcode */
float y = 0;
int framesBetweenFades = 50 ;
 
void setup() {
  size(200, 200);
  background(random(255), random(255), random(255));
  noStroke();
}
 
void draw() {
  if (frameCount % framesBetweenFades == 0) {
    fill(random(255), random(255), random(255), 8);
    rect(0, 0, width, height);
  }
  fill(random(1,255), random(1,255), random(1,255), random(100,255));
  ellipse(width/2, y, 40, 40);
  y += 0.5;
}

Moving through transformation: put ranges of random values for floats SX and SY

  • Show Sketch
/** @peep sketchcode */
float y = 50.0;
float speed = 1.0;
float radius = 15.0;
float angle = 0.0;
 
void setup() {
  size(200, 200);
  background(random(0,255));
  noStroke();
  ellipseMode(RADIUS); // change it from RADIUS to CORNER because you want to roatate the square from one of it's corners, not the center point
}
 
void draw() {
  fill(0, 12);
  rect(0, 0, width, height);
 
  fill(random(1,255),random(0,255), random(0,255));
  translate(100, y); // Set the y-coordinate of the circle
  fill(255);
  rotate(angle);
  rect(-60, -60, 120, 120);
  angle = angle + 0.02;
 
  y += speed;
  if (y > height *2 +  angle) {
    y = -height;
  }
}
  • Periodic Motion
  • Show Code
/** @peep sketch */
float angle = 0.0; // Current angle
float speed = 0.01; // Speed of motion
float radius = 60.0; // Range of motion
float sx = random(1.0,50.0);
float sy = random(1,100);
 
void setup() {
  size(200, 200);
  background(0);
  noStroke();
}
 
void draw() {
  fill(0, 2);
  rect(0, 0, width, height);
  angle += speed; // Update the angle
  fill(255);
  // Set the position of the small circle based on new
  // values from sine and cosine
  float x = width/2 + (cos(angle) * radius);
  float y = height/2 + (sin(angle) * radius);
  ellipse(x, y, 4, 4); // Draw smaller circle
  // Set the position of the large circles based on the
  // new position of the small circle
  float x2 = x + cos(angle * sx) * radius / 2;
  float y2 = y + sin(angle * sy) * radius / 2;
  ellipse(x2, y2, 12, 12); // Draw larger circle
}

Phase Shifting

  • Show Code
/** @peep sketch */
float angle = 0.0; // The current angle passed to sin() to calculate the x
float speed = 2; // The angular speed (in degrees)
float shift = 30; // The shift between the phases of the circles (in degrees)
float radius = 10; // The radius of the circles to draw
 
void setup() {
  size(200, 200);
  background(0);
  noStroke();
}
 
void draw() {
  background(0);
  angle = angle + speed;
  translate(width/2, height - radius);
  float phase = 0;
  for (int i = 0; i < 100; i++) {
    float x = 20 * sin(radians(angle + phase));
    ellipse(x, 0, 2*radius, 2*radius);
    translate(0, -2*radius);
    phase += shift;
  }
}

Tutorial 10

  1. Organic Motion
  • Show Sketch
/** @peep sketchcode */
float x; // X-coordinate
float y; // Y-coordinate
 
void setup() {
  size(200, 200);
  background(0);
  x = random(width);
  y = random(height);
}
 
void draw() {
  // Fade the background
  if (frameCount % 10 == 0) {
    noStroke();
    fill(0, 2);
    rect(0, 0, width, height);
  }
  // Update the position of the point
  x += random(-3, 3); // Add a small amount to x-coordinate
  y += random(-3, 3); // Add a small amount to y-coordinate
  x = constrain(x, 0, width); // Constrain x to width of display
  y = constrain(y, 0, height); // Constrain y to height of display
  // Draw the point
  strokeWeight(random(4,10));
  stroke(random(0,255), random(0,255),random(0,255),random(100,255));
  point(x, y);
}
  1. Position and Direction
  • Show Sketch
/** @peep sketchcode */
float x; // X-coordinate
float y; // Y-coordinate
float angle; // Direction of motion
float speed; // Speed of motion
 
void setup() {
  size(200, 200);
  background(0);
  x = width/2;
  y = height/2;
  angle = random(TWO_PI);
  speed = random(0.1, 0.5);
}
 
void draw() {
  // Fade the background
  if (frameCount % 10 == 0) {
    noStroke();
    fill(0, 2);
    rect(0, 0, width, height);
  }
  // Calculate distance to move in x and y
  float dx = cos(angle) * speed;
  float dy = sin(angle) * speed;
  // Update coordinate, constrained to display window
  x = constrain(x + dx, 0, width);
  y = constrain(y + dy, 0, height);
  // Update direction that the point is going to move in
  angle += random(-0.3, 0.3);
  // Draw the point
  stroke(random(0,255), random(0,100), random(10,255));
  strokeWeight(4);
  point(x, y);
}

Comments

Nobody has said anything yet.