We know, that the length travelled with constant acceleration is equal to initial speed * time + half of acceleration * square of time, + the length travelled before acceleration started, but in this case it is 0, because you have constant acceleration at the point of launch.
In other words, it is
s = v0t + 1/2 * at²
.
We know the s, which is your distance, and v0 which is the initial speed(speed base).
We now need to swap it so that we have all t-s on the same side.
New form:
at²/2 + v0t - s = 0
. As you can see, this is quadratic equation, which is of form
ax² + bx + c = 0
.
In our case,
a = a/2
,
b = v0
and
c = -s
.
We solve these by first finding discriminant, which is
D
, and it is equal to
b² - 4ac
.
After filling it in, we get our finalized form
D = v0² + 2as
.
We know that if D is equal to 0, then this equation has only one solution, which is doubled(this means that your parabola only touches the x axis on graph); if D is > 0, then there are 2 real solutions(roots) for your equation.
If, however, D is < 0, there exist no real solution, and both roots are from complex numbers, which are useless in your case.
Then we need to find roots.
There is formula
x12 = (-b +- root(D)) / 2a
. So
x1 = (-b + root(D)) / 2a
and
x2 = (-b - root(D)) / 2a
.
If we fill a and b with your values, we will get:
x1 = (-v0 + root(D)) / a
and
x2 = (-v0 - root(D)) / a
.
I guess in your situation you will always be left with one x that is positive, and one that is negative, and basically you dont care about negative time.
Script:
JASS:
function GetTimeToTravel takes real startingSpeed, real acceleration, real length returns real
local real disc = startingSpeed * startingSpeed + 2 * length * acceleration
if acceleration == 0. then
return length / startingSpeed
elseif disc < 0 then
return -1.
endif
return (-startingSpeed + SquareRoot(disc)) / acceleration
endfunction
We only return x1, because x2 will be either == x1 or negative most of the time