2
\$\begingroup\$

In Unity, I have a Spline that I'm using to move an object. During the game, the player can add new knots to this Spline as the object moves forward. e.g if they want the object to go right, they can add a knot to the right.

If it was just a single Spline that couldn't be extended, the time t to be used for evaluating the curve could be calculated as, for example, t=Time.time/20.0f (if I wanted it to last 20 seconds)

I'm evaluating the spline at every frame.

But now when I'm adding the functionality extend the Splind dynamically, how can I ensure that the object will still move on the Spline smoothly?

The problem is that if the curve gets longer, then at any time t, f(t) will be further along the curve than when the curve was smaller.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

You can recalculate position based on the spline length. I believe it should works: t += Speed*Time.deltaTime/Length.

\$\endgroup\$
0
\$\begingroup\$

It sounds like the function \$f(p)\$ is taking a value from 0 to 1 representing percentage of traversal and returning a position. Using time elapsed divided by total time for traversal works in simple cases, but as you discovered, it doesn't accommodate changes to the spline in a manner that works for your animation.

To find a solution, let's look at a couple of simple examples and notice where & how things go wrong.

Borrowing some from your question, let's assume you have a spline that's 100 unit long, you want the total traversal time to be 20 seconds and you've been traveling for 5 seconds already. Then we should have: $$ \begin{align} &f_1(5/20) \\ =\text{ } &f_1(1/4) \\ =\text{ } &25 \end{align} $$ The result is 25 units travelled because we've travelled along one fourth of it thus far. We're effectively moving along the spline at 5 units per second, so if we were to travel for an additional second, the result would be 30.

Now let's say that you're increasing the spline by adding another section that increases the total length by 75 units. The second version of the spline is now 175 units. So now we have this:

$$ \begin{align} &f_2(5/20) \\ =\text{ } &f_2(1/4) \\ =\text{ } & 43.75 \end{align} $$

The result jumps up because we're one fourth of the way along a spline that measures 175. Not only has our relative position changed, but the rate of travel has also changed. We are now effectively moving at 8.75 units per second.

It turns out that this ratio is the key to correcting the situation. Consider a much simpler situation: if we were at the end of spline 1 and we were to double it, suddenly we're only at the halfway point. Similarly, we know that if we doubled the length of the spline, the distance covered by \$p\$ is now twice as much. If we're going 2x as fast as we want, scaling \$p\$ by 1/2 brings us back to where we want to be.

More generally, if \$L_1\$ is the length of the original spline and \$L_2\$ is the length of the extended spline, then multiplying \$p\$ by \$\frac{L_1}{L_2}\$ (the reciprocal of the change) before evaluating \$f()\$ compensates for the change.

Let's check this against things from our very first example. At the time of 5 seconds we'd get: $$ p_1 \times \frac{L_1}{L_2} \\ \frac{5}{20} \times \frac{100}{175} \\ \frac{1}{7} \\ $$ If we've travelled one seventh of a spline measuring 175 units, we covered a distance of 25. So our displacement on the extended spline is correct. And if we check to see where we would be a second later, we'd get: $$ p_1 \times \frac{L_1}{L_2} \\ \frac{6}{20} \times \frac{100}{175} \\ \frac{6}{35} \\ $$ If we've travelled \$\frac{6}{35}\$ of a spline measuring 175 units, we covered a distance of 30. So the rate of change is also correct.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .