Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Here we use vectors to create a force of attraction. The force's effect is accumulative, always adding to the velocity of our ellipse. But since the force is sometimes negative, depending on if the ellipse is above, below, left, or right of the mouse, the ellipse is subject to an oscillating effect. 

Code Block
PVector pos = new PVector(200,200);
PVector velocity = new PVector(1,0);

void setup() {
  size(600,600);
  fill(255);
}

void draw() {
  background(0);
  PVector direction = new PVector(mouseX, mouseY); 
  direction.sub(pos);  // by subtracting the pos from mouse coordinates, we end up with a vector between the two points
  //
  direction.normalize(); // now we have a vector with the length of 1, this tells us the direction we want to push out ellipse in
  direction.mult(.1); // we shorten the vector (magnitude), this gives us our speed
  velocity.add(direction); //we add the direction to our velocity, so the changes accumulate over time
  pos.add(velocity); // pos + velocity give us our new position!
  ellipse(pos.x, pos.y,30,30);
};

...