I’m trying to develop a game.

I have a starting point with and starting vector (blue), next I draw on screen the path (black) which I want to follow with a certain inertia, or limited angle and step each turn that should result a red line.
Do you have any tips on how to program such algorithm?
You can just create a differential scheme, i.e. model speed and coordinate of the source point at discrete moments in time. Say you fix some
dt = 0.1 secfor example, starting speed is given by blue vector asv0. We start atx0.Say
y[j]are the points of the black path.Let
x1 = x0 + v1 * dt, wherev1 = v0 + (y[k(x0)+1] - x0) * f(abs(y[k(x0)+1] - x_0)). Wherek(x0)is the index of the nearest tox0point amongy[j],f(x)is a function characterizing the ‘force’ pulling your trajectory to that of the defined path. It defined for nonnegativexes only.This models pulling your trajectory to the next point in the defined path to that closest to current modeled position on the trajectory.
A good example of
f(x)could be one modeling the gravitational force:f(x) = K/(x * x), whereKshould be adjusted experimentally to be giving natural desired results.Then
x2 = x1 + v2 * dt, wherev2 = v1 + (y[k(x1) + 1] - x1) * f(abs(y[k(x1) + 1] - x_1))and so on:x[n+1] = x[n] + v[n+1] * dt, wherev[n+1] = v[n] + (y[k(x[n]) + 1] - x[n]) * f(abs(y[k(x[n])+1] - x[n]))…You’ll have to adjust
dtandKhere.dtshould be small enough for the trajectory to be smooth. BiggerKmakes trajectory more close to defined precisely, smallerKmakes it more relaxed.Edit now actually when I thought a little bit, I understand that selection of the force function
fwas not good, as gravitational force allows space velocities, i.e. ability for your trajectory to fly away from the desired one infinitely if the initial speed is too big. So you should construct another function, possibly just something along the lines off(x) = K xorf(x) = K x ^ alpha, wherealpha > 0. You see, this scheme is quite general, so you should experiment.