My goal is to move ahead along a spline 'x' world units, unfortunately there is no way to map spline points to world points.
What I do is I iterate along the spline by a small alpha amount (say 0.001), get the new spline point vs the old one and add the difference to a total up until it equals or is slightly greater than the distance I wanted to move in world space.
This works fine but there is a huge bottleneck with the sqrt, basically to calculate the distance between two vector points you need to do a square root calculation.
I feel it should be possible to just get the squared value and just use that, unfortunately I can't get the code working, here is what I have:
Where _distance
is the amount to move, Distance is the alpha value along the spline.
float distanceMoved = 0;
float oldDistance = Distance;
while (distanceMoved < (_distance))
{
Vector3 before = Position;
float oldAlpha = Alpha;
float oldDist = Distance;
Distance += _iteration;
distanceMoved += (before - Position).magnitude;
}
Notice the line with (before - Position).magnitude
, the .magnitude property performs a square root.
I've tried changing it to .sqarMagnitude and say distanceMoved < distance * distance but it has accelerated the rate of movement forwards.
Any ideas what is going wrong or how I can get rid of the square rooting or do this differently entirely?
10 + 10 + 10 != Sqrt(10*10 + 10*10 + 10*10)
? \$\endgroup\$Distance
is the alpha value, why not call itAlpha
? And… what isAlpha
, then? \$\endgroup\$