0

There are a lot of mousefollower tutorials out there. Most of them feature a simple formula for easing the motion:

x += (tx - x) / interp;
y += (ty - y) / interp;

(tx = target position, x = actual position, interp > 1)

This makes the follower go very fast in the beginning, then decelerate slowly to the target position.

How do i have to change the formular, so that i can define a custom acceleration, custom deceleration and a maxspeed for the movement in between? For the very beginning i'd be happy with an added acceleration at all.

Thanks!

Hans

superno
  • 51
  • 3

1 Answers1

0

Acceleration is the change in velocity over time. So in 1D, to apply a constant velocity, you'd do:

v += a * dt;
x += v * dt;

where:

  • a is the acceleration (a constant)
  • v is the velocity
  • x is the x-position
  • dt is the timestep, i.e. the time between updates

You'd do something similar for deceleration, except that a would now be negative.

To set a maximum velocity, you simply need to a conditional check on v, maybe:

v = MIN(v_max, v);

where v_max is your maximum allowed velocity (a constant).

In 2D, you'd need to take into account the direction of travel:

x += v * cos(theta);
y += v * sin(theta);

I'll leave it to you to calculate theta...

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680