/**
* Motion blur on an animated square.
*
*
Elie Zananiri
* CART 253, Winter 2008
*/
/* global variables */
int rectX = 0;
int rectY = 50;
int rectSize = 50;
// this value is used to calculate the distance of the step but
// also to determine the minimum offset to reach the target
int easeFactor = 10;
int targetLeft;
int targetRight;
int currTarget;
/* built-in functions */
void setup() {
size(300, 300);
noStroke();
smooth();
targetLeft = 0;
targetRight = width-rectSize;
setTarget(targetLeft);
}
void draw() {
fill(0, 30);
rect(0, 0, width, height);
moveSquare();
drawSquare();
}
/* custom functions */
// sets the target x position of the square
void setTarget(int newTarget) {
currTarget = newTarget;
}
// moves the square
void moveSquare() {
// find the distance from the target to the square
int dX = currTarget-rectX;
// ease towards the target
int stepX = dX/easeFactor;
// set the new square position
rectX += stepX;
// check if the step is small enough to consider it having arrived at the target
if ((rectX-targetLeft) <= easeFactor) {
setTarget(targetRight);
} else if ((targetRight-rectX) <= easeFactor) {
setTarget(targetLeft);
}
}
// draws the square
void drawSquare() {
fill(255, 128, 0); // orange
rect(rectX, rectY, rectSize, rectSize);
}