Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
/
Animation

Animation

Nov 06, 2018

Analytics

Loading data...

In this simple program, we animate an ellipse to always chase a point defined by the mouse. The ellipse only ever moves a percentage of the distance between it and the target destination in each step.

The PVector object is used to handle the coordinates and the simple math operations.

PVector pos;
PVector targetPos;
PVector mousePos;
void setup() {
  size(1000, 600);
  fill(0);
  pos = new PVector(0, 0);
  targetPos = new PVector(0, 0);
  mousePos = new PVector(width/2, height/2);
}

void draw() {
  background(255);
  targetPos.set(mousePos); 
  targetPos.sub(pos);  // find the vector between mouse position and ellipse position
  targetPos.mult(.2); // each step in the animation will move this percentage of the distance to the target position
  pos.add(targetPos);
  ellipse(pos.x, pos.y, 60, 60);
}
void mousePressed() {
  mousePos.set(mouseX, mouseY);
};

Exercise 10

Add an additional ellipse that follows the mouse at a slower rate than the first ellipse. 

Modify the code so that the ellipse always follows the mouse position.  

Replace with the ellipse with another shape (like a triangle or even a car) , and keep it pointed towards the mouse.

Exercise Solution Using Arrays

 Expand source
PVector[] pos;
PVector[] targetPos;
PVector mousePos;
int arrayLength = 5;
void setup() {
  size(1000, 600);
  fill(0);

  pos = new PVector[arrayLength];
  targetPos = new PVector[arrayLength];

  for (int i = 0; i<pos.length; i++) { 
    pos[i] = new PVector(0, 0);
    targetPos[i] = new PVector(0, 0);
  }
  mousePos = new PVector(width/2, height/2);
}

void draw() {
    mousePos.set(mouseX, mouseY);
  background(255);
  
    for (int i = 0; i<pos.length; i++) { 
        targetPos[i].set(mousePos); 
        targetPos[i].sub(pos[i]);  // find the vector between mouse position and ellipse position
        targetPos[i].mult((i*0.02)+.02); // each step in the animation will move this percentage of the distance to the target position
        pos[i].add(targetPos[i]);
        ellipse(pos[i].x, pos[i].y, 60, 60);
    }
}


Solution using a class. 

classExample.zip

, multiple selections available,
For you

Programming
Results will update as you type.
  • Tiling and Repetition
  • Reactors
  • Programming Basics: Parametric and Generative Graphic Design 2016
  • Archive
  • High Scores
  • Artificial Neural Network
  • Alternatives to the Processing IDE
  • p5.js Programming
  • Programming in Processing (java)
    • Starting with Processing (en)
    • Variables (en)
    • Classes and Objects (en)
    • Events und Functions (en)
    • Writing our own Functions (en)
    • Random Numbers (en)
    • Conditionals (en)
    • Loops (en)
    • Nested Loops (en)
    • Coordinates (en)
    • Arrays and Lists (en)
    • SVG + Images (en)
    • Motion and Temporality
    • Gesture Interactions
    • Using bitmaps as modifiers
    • Vectors
    • Animation
    • Animation 2
    • Simple Collision Detection
    • Sound
    • Typography

{"serverDuration": 62, "requestCorrelationId": "57bce8fa51354d669e6d607530d1cf6f"}