I'm trying to get a ball to follow my mouse. The ball is supposed to move at a constant speed.

PVector location = new PVector();
PVector velocity = new PVector();

void setup() {
  size(1024, 576);
}

void draw() {
  background(0);
  PVector mouse = new PVector(mouseX, mouseY);

  mouse.sub(location);
  velocity.x = mouse.x / (mouse.x + mouse.y);
  velocity.y = mouse.y / (mouse.x + mouse.y);

  location.add(velocity);

  fill(255);
  ellipse(location.x, location.y, 40, 40);
}
Tagged:

Answers

Sign In or Register to comment.