# How Do I Go About Waypoint Interpolation?!

Hello guys, good day. I’m building a simple game where an enemy or enemies are in front of the player moving at the same speed. I need to make their positions (I only need to play with the x positions) random. Since I’m gonna have more than one enemy, I can’t make a waypoint system. So I’ll have a function in the script generate a random number and pass it on to FixedUpdate() where the value will be lerped to. Unfortunately, Mathf.Lerp() changes speed according to how far the difference between the two points and it llllacks continuity of slope and tangent. Also note that the distance between each random x value generated is 353.6m in game, so if i have an array of 3 random x values generated, on a top down view, i’ll have (random value, 353.6n) where n is the iteration of that random number generated. Look at the image( sorry it looks like crap, but try writing in MS Paint with a smooth mouse)

As you can see, I can easily make a Vector2 or Vector3 ( as in Vector3(Random.Range,0,353.6n)) from these values. I need to make cubic or quadratic interpolation between these points so that my enemy can smoothly go from one x value to another in a curve and not in a straight line. Please excuse me because im a High School junior and I’m not yet introduced to the higher concepts of Catmull-Rom and Bezier splines. I even try so hard to understand Cubic Hermite splines. So how do you think I can make my points into a smooth curve?

I’ve tried defining a quadratic function for an array of 3 points which will be concurrently updated as the game goes on. I found the differential of the equation but failed to apply the gradient because it simply doesn’t work. Check these out

. BTW, alpha is the closest point, beta is the mid point, gamma is the farthest point, all of which are precomputed and updated in an array. Then there’s lambda which is the famous 353.6m distance. Unfortunately, I just noticed that I didn’t include the distance at the present time i’m gonna be looking for. How unfortunate. I don’t even know how to include it.

Okay so i guess I need a list of ways to solve this issue:

1. Solve for the differential of a quadratic equation that satisfies my data points (which gives me gradient) and apply Transform.Translate in the direction of the gradient. That’s what I’ve been thinking of doing but can’t get my quadratic equation right. I feel so stupid http://forum.unity3d.com/images/smilies/icon_sad.gif

2. Solve a hermite or catmull-rom spline (which i dont even understand) for my data points. I dont even know where to start with this solution to be honest

3. Make a custom function (cheating http://forum.unity3d.com/images/smilies/icon_biggrin.gif ) that checks if the third number - the second number in the array is greater or less than the second - first number. So if the number is continous, then linearly interpolate. But if not, then make a lerp from + to - then lerp to the next. I mean :

.

Hello! A few points…

There is a MoveTowards() command that will move at a constant rate (used with Time.deltaTime) until you hit your waypoint. If you hit your waypoint, you can check and see how much further you should’ve gone, then MoveTowards() one more small time to make up for hitting a waypoint early.

Here, we have a Interpolate class that makes catmull-rom splines. We feed it a bunch of waypoints and it’ll spit back as many interpolated points as we want. Yeah, I know… the math isn’t nearly as interesting when someone else does it. Here, I found one if you want to take the easy (aka, professional) route =p
http://wiki.unity3d.com/index.php/Interpolate

EDIT: And a shout out for going ahead and doing the math! I totally didn’t get the math behind splines until my third year in college when studied them for class. My reliance on written-by-someone-else code is causing me to get rusty with them. >.>

first concept Biezer Curves:

The most interesting thing on that page are the animations about half way down.

What you need is a simple way to create a string of cubic biezer curves to match your points. I say this, because using a cubic biezer curve will start at one point and end at another, thus matching your points.

So here is the thoery.

use 3 points, last, current and next. and you want the handles from the current point.

1. add last and next together and multiply by 0.5. (divide by 2) to get the average. this gives you a reference point
1.a) you can alternatively just get the angle between the two and half that angle. probably a better representation, but not as fast.
2. Get the Cross of the reference point to the current versus the next point to the current. This gives you the direction of the point in the upwards direction. (that is relative, upwards is basically away from the current point and not towards either last or next)
3. get the Cross of the reference point to the current versus the last cross. This gives you a direction that is 90 degrees from the refernce, on the general last point side. Normalize this.
4. get the distance between last to current and the distance between current to next. (I would multiply them by 0.5 each) These are the distances the handles are out.
5. use a tensioner. (a number that is multiplied by the two distances to get a tight, or broad curve.
6. near handle would be current + cross * distance 1 * tensioner
7. far handle would be current - cross * distance 2 * tensioner

Can that… look at this…

http://wiki.unity3d.com/index.php/Interpolate

Hey guys, thank you so much for your replies. BigMisterb, I did review the bezier curves and found them to be good but not applicable because the range of my values are too small. I have some knowledge of beziers because before I started game programming, I did 3d modeling and animation (with 3ds max, since i was 10). Anyway, I managed to get equations which return the gradient and equation of a cubic function from 4 data points given at any given point. It worked excellently and I was surprised. I realised that the gradient was constantly changing and i realised that instead of making my enemy snap to random values by lerping, I could just lerp the random values from one to another and use those to displace the enemy on the x axis. Simpler solution, same results. Yay! Anyway, I’ve gone farther and posted a new thread under Networking titled “How do I go about Network Syncing?” Thank you guys again, happy unitying

Oh and reply if you want all the equations (for quadratic interpolation and cubic interpolation and their gradient functions) and I’ll post pictures. Later guys.