Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »


In this simple programme, we animate an ellipse to always chase the mouse. TheĀ ellipse only ever moves a percentage of the distance between it and the mouse in each step.

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

PVector pos;
PVector targetPos;
float  easingFactor = 0.1;
   
void setup() {
  size(800, 600); 
  fill(0);
  pos = new PVector();
  targetPos = new PVector();
}
   
void draw() { 
  background(255);
  targetPos.set(mouseX,mouseY);
  targetPos = PVector.sub(targetPos, pos);  // find the distance between mouse and position on the x an y axis
  targetPos = PVector.mult(targetPos, easingFactor); // multiply the distance by the easingFactor to slow it down
  if(targetPos.mag() > .5) { 
//magnitude is always an absolute number, so we don't need to use the abs() function 
  pos = PVector.add(pos,targetPos);
  } 
  ellipse(pos.x, pos.y, 60, 60);
}