In order to generate a curve, the function requires four coherent positions along the path with the weight value between 0 and 1. Since the weight increases between these two values, the curve returns the coordinates at a further path.
When the provided weight value is 0, the curve returns the correct coordinates in the second input coordinate. When the provided weight value is 1, the curve returns an exact coordinate at the third input position. However, all four values are used in these calculations to ensure a smooth path between the second and third positions and move forward to the next set of running points.
This can be illustrated in charts. If the curve is calculated using points 0, 1, 2, and 3, and the weight is 0.0, the resulting position is 1. If the weight increases to 1.0, the curve coordinate trajectory will be along the line between point 1 and point 2. When the weight reaches 1.0, it will reach point 2. Note that from this point, the curve coordinate does not return a value online, toward 0 or 3, even if they will pass through the curve function: these external points are used only to calculate the curve angle between the two points in the center.
Once the weight reaches 1.0, the curve can be moved to the next group of points, entering points 1, 2, 3, and 4. Then the weight is increased from 0.0 again to 1.0, resulting in the calculated coordinate along the line between point 2 and point 3.
From this set of points, it is impossible to return Curve Coordinates between 0 and 1, or between 4 and 5 (gray in the chart ), there are insufficient external points of these parts of the path to be processed.
By moving the path between a point and a point, a smooth curve can be reduced and passed through all defined positions. All of this can be done well in 3D space.
Tip:When defining a path, do not forget that it will take the same amount of time to move between every coherent path point. Therefore, you should try to ensure that the points are approximately same distance. Different from other large gaps, the point will lead to faster movement, and moving slowly at a fixed interval, because the distance must be moved.
In order to create a closed path, allow the plane to go through the loop, seamlessly return to its original point and start again, we must ensure that the three vertices of the final curve are the same as the first of the three vertices. When the weight of the three points reaches 1.0, the curve coordinate will finally reach the correct position of point 1 on the moving path, allowing the entire path to be tracked from the very beginning.
In paperplaneobjectCodeIt is implemented by storing two class-level variables. An int value is called _ splineindex, which defines the first index of four points for curve calculation. A floating point value is called _ splineweight, it allows us to go through the path between the defined points along the curve.
In the UPDATE function, we add a small sum to the _ splineweight variable. If it reaches or exceeds 1.0, we will reduce it by 1.0 and add _ splineindex. If _ splineindex moves the last array point of the path, it is reset to start. These updates move the plane along the curve, and when it reaches the focus of its moving path, reset it back to the starting point.
In these update modes, we call the getplaneposition function to perform curve calculations and return final flight coordinates. This function is expected to pass the curve index and curve weight value as parameters. The following code will describe it.
private vector3 getplaneposition ( int splineindex, float splineweight) {vector3 ret; // if the weight exceeds 1, reduce by 1 and move to the next index If (splineweight> 1) {splineweight-= 1; splineindex + = 1;} // keep the spline index within the Array Bounds splineindex = splineindex % _ movementpath. length; // calculate the spline position ret = vector3.catmullrom (_ movementpath [splineindex], _ movementpath [(splineindex + 1) % _ movementpathlength], _ movementpath [(splineindex + 2) % _ movementpathlength], _ movementpath [(splineindex + 3) % _ movementpathlength], splineweight ); return RET ;}
This code first checks whether the weight of the curve is greater than 1. If yes, it will subtract 1 and switch to the next curve index (we will see this reason later ). The second is to check whether the index of the cyclic curve exceeds the limit of the _ movementpath array item.
The coordinate of the curve is then calculated by transferring the four vector coordinates and the weight of the curve to the vector3.catmullrom function. Note, however, because we use exponential operators in the curve index, if they exceed the length of the array, they will loop back to the beginning. This operation allows us to implement our closed loop (which requires repeat the first three points) without actually repeating them in the array: they are only reused at the beginning, when the end of the array is reached.
With the ability to manually calculate flight coordinates, we can now set flight coordinates and move smoothly along the trajectory. This is a good start, but this is a very obvious visualization problem. When it is moving, the flight always faces the same direction. Of course, it is always moving in the direction (paper planes generally do not fly well on the side ).
Fortunately, it is very easy to make the flight look like it is flying. The first thing we need to do is to calculate another flight coordinate, just a little farther along the track. We call the getplaneposition function to complete it within 1 second. At this time, we add 0.1 to the curve weight. The other reason is that the getplaneposition function checks whether the weight exceeds 1.0, because this second can cause overflow.
The second call allows us to see where the flight is now and where it will be in the next second. The flight direction must be from these points to the next second's point, because its trajectory is moving. Therefore, we need a method to rotate the plane so that it is directed from the first position to the next second.
This rotation can be completed using another convenient static Matrix Function: createworld. The createworld function creates a world matrix (this is what we will try to do in the update method of each object at the end) so that it can be placed in a special position and face a special direction. This is what we need: the position is the first curve we have calculated, from this to the next curve point.
You can simply calculate the direction by subtracting the current position from the next position. The resulting vector preparation is passed as a parameter to createworld.
A small problem persists: The plane continues to fly to one side because its side has been defined in the sketchup mode. To solve this problem, after matrix calculation, we simply rotate the 90-degree angle to rotate it.
The complete code for calculating the location and flight direction is as follows.
// Calculate the current position and store in the position PropertyVector3 position = getplaneposition (_ splineindex, _ splineweight );// Calculate the next position too so we know which way we are movingVector3 nextposition = getplaneposition (_ splineindex, _ splineweight + 0.1f );// FIND THE MOVEMENT DIRECTIONVector3 Delta = nextposition-position;// Create the world matrix for the planeTransformation = matrix. createworld (Position, Delta, vector3.up );// The plane needs to be rotated 90 degrees so that it points// Forward, so apply a rotationApplytransformation (matrix. createrotationy (mathhelper. toradians (-90 )));
The final result is a smooth and realistic flight around the scene between houses. You can see the actual effect by running the chasecam project-the original view uses a camera that does not chase the flight track, but slowly wraps around the scene, allowing you to easily see the flight track.