- Home /
Navigate Curve at Constant Speed
Hi, all!
I've seen and read a lot of threads concerning iTween and traversing through paths at a constant speed. I have an example up and running right now that works perfectly, moving all objects around a curve at a constant rate.
The path in question is a flat oval (Much like a running track). For an object placed directly on a path, this works perfectly. I can have it move at, say, 15 units per second and it'll do that.
However, when I incorporate an offset with the curve, all objects sitting on the offset complete the curve in the same amount of time, because the math is tied to a time and not a speed in reality. It simply takes the speed and divides it by the length of the base curve and multiplies that by delta time. So although it purports to operate at a consistent speed, this is not quite true.
I know this because if I change the path length math to update the length in the equation based on the offset, it operates perfectly as a state function, but not in between start and end. As in; when a full revolution is complete, all of the objects are where they should be. But the spaces between them occur over the course of the run.
This should not be happening. The advances in spacing should technically only occur on the curves of the oval and not the straight sides, because when they are all moving at the 'same speed' in a straight line, no advances should be made.
I don't quite know how to deal with this. The code I borrowed from another thread works mainly with iTween Path Length and PutOnPoint functions.
I've gotten as far as calculating the maximum number of units an object should be able to travel in a given FixedUpdate. The next step should be to find what point on the curve from the current point is that number of units away. I am versed in calculus and am not afraid to use it but I am not quite sure about how to construct an algorithm to represent the curve.
Any help would be appreciated.
TL;DR: I want to move something along a curve in a way that keeps it at a steady units/second, taking into account any offset from the curve.
Thanks! - YA
Answer by youngapprentice · Aug 15, 2015 at 06:37 AM
So I figured it out by doing this:
Do some percentage math to clamp the distance travelled pretty close to what you want. Do math to find the exact distance you actually want to travel. Get the normalized vector in that direction and travel that exact distance.
Done :) Works like a charm.
Answer by cjdev · Aug 14, 2015 at 10:33 PM
From what I've seen of the tweening libraries, the curved paths are actually just a series of waypoints close enough together that they approximate the curve. If you find yourself needing to change the curve dynamically and update the speed you could always switch to a tweening function based on time and calculate the speed yourself based on the length of your new curve. All you'd need is the sum of the linear distance of the waypoints divided by your desired speed to give you the time used in the tween.
I'd rather not change the speed dynamically, even if it does work for this reason: If I am traveling at 15mph, then I am definitely covering a very exact distance every second (Pretending my fixed update was every second). This is, in spirit, a little different than creating a fixed point change and fluctuating speed to make it look like the rate is consistent. I want to make it move along the curve, but also only cover a certain amount of real world units every update, ins$$anonymous$$d of a percentage of the curve.
You wouldn't be finding a variable speed, only finding the new time for the tween based on the distance of the modified curve. Since distance = rate * time, with a constant rate (speed) you need to find the new time when the distance changes.
Okay I follow that and that makes good sense. However, when if I do that and find a new time, this new time will be applied over the entire curve ins$$anonymous$$d of just on the parts where the distance discrepancy should be occurring. This will make gaps appear between objects on the straight points where these gaps wouldn't be appearing in real life if they were all moving at identical speeds.
However, I think your solution could work if there was a way to splice the curve from moment to moment. For instance; the next 2 units of the curve ins$$anonymous$$d of the whole curve. Then it would work. I just can't figure out how to do this.
In real life if two paths diverged in a curve, one to the outside and one to the inside, and two agents went at constant speeds along the curves to meet at a straight path the agent on the outside would be behind the agent on the inside. It's why racecar drivers always like to take the inside line.