Slow Movement of player to mouse position on mouse click

Hello, I am new to processing and I have to make a game were the user clicks somewhere in the window and the ball will move to the position. It is pretty simple, but I cant figure it out :sweat_smile:
I havent found something similar on the web, so hopefully this new topic is useful for everyone^^

float plposX;
float plposY;
float easing = 0.05;

void setup() {
  //fullScreen();
  size(600, 600);
  plposX = width/2;
  plposY = height/2;
  cursor(CROSS);
}

void draw() {
  background(0);
  
  //direction line
  strokeWeight(5);
  stroke(255, 255, 255, 100);
  line(plposX, plposY, mouseX, mouseY);

  //player movement
  fill(255);
  noStroke();
  circle(plposX, plposY, 40);
  if (plposX<mouseX && plposY<mouseY) {
    plposX+= (mouseX-plposX)*easing;
    plposY+= (mouseY-plposY)*easing;
  } else {
    plposX-= (plposX-mouseX)*easing;
    plposY-= (plposY-mouseY)*easing;
  }
}

Hey, welcome to the forum!

Great to have you here!

Your Sketch is good!

The “if” is not really necessary.

Also, in my version you can click the mouse to start the player.

Warm regards,

Chrisir

here is my version :

float plposX;
float plposY;

float mouseXDesired, mouseYDesired; 

float easing = 0.05;

void setup() {
  //fullScreen();
  size(600, 600);
  plposX = width/2;
  plposY = height/2;
  mouseXDesired=plposX;
  mouseYDesired=plposY; 

  cursor(CROSS);
}

void draw() {
  background(0);

  //direction line
  strokeWeight(5);
  stroke(255, 255, 255, 100);
  line(plposX, plposY, mouseXDesired, mouseYDesired);

  //player display
  fill(255);
  noStroke();
  ellipse(plposX, plposY, 40, 40);

  //player movement
  plposX+= (mouseXDesired-plposX)*easing;
  plposY+= (mouseYDesired-plposY)*easing;
}

void mousePressed() {
  mouseXDesired=mouseX;
  mouseYDesired=mouseY;
}
1 Like

My god huge thanks, I can finally continue on the project!^^

1 Like