The simplest way is to make an linear interpolation between your target position and your actual position using lerp(). Take a look : https://processing.org/reference/lerp_.html
So you can do the basic trick with a fixed coefficient but the transition still be rough
float ballX = 250;
float ballY = 250;
float ballDi = 50;
float targetX,targetY;
void setup()
{
size(500,500);
}
void draw()
{
background(0);
moveball();
drawball();
}
void mouseClicked()
{
targetX = mouseX;
targetY = mouseY;
}
void moveball()
{
ballX = lerp (ballX, targetX, 0.1);
ballY = lerp (ballY, targetY, 0.1);
}
void drawball()
{
ellipse(ballX,ballY,ballDi,ballDi);
}
But if you want to have a smoother moving, with a sort of easing effect, you have to add a coef wich change during the interpolation :
void draw()
{
background(0);
if ( targetX != ballX) i ++ ;
else i = 0 ; //reset the value if target is reached
moveball();
drawball();
}
With this way, I increment i while the circle isn’t on target position.
Then I map this value between 0 and 1, or others values that are between 0 and 1 (because lerp need a coefficient between 0 and 1).
float coef = map(i,0,100,0,0.7); //100 is a abrbitrary value, but it seems near the good value to have a great easing
ballX = lerp (ballX, targetX, coef);
ballY = lerp (ballY, targetY, coef);
So, because my value coef change, the speed of the circle also.
Entire code below if you need it :
float ballX = 250;
float ballY = 250;
float ballDi = 50;
float targetX,targetY;
int i = 0;
void setup()
{
size(500,500);
}
void draw()
{
background(0);
if ( targetX != ballX) i ++ ;
else i = 0 ;
moveball();
drawball();
}
void mouseClicked()
{
targetX = mouseX;
targetY = mouseY;
}
void moveball()
{
float coef = map(i,0,100,0,0.7);
ballX = lerp (ballX, targetX, coef);
ballY = lerp (ballY, targetY, coef);
}
void drawball()
{
ellipse(ballX,ballY,ballDi,ballDi);
}