The GetPositionAtLengthAlongSpline method can be used to determine a position along the Spline after having traveled a certain distance.
Splines are used to define the path that an object moves along. Splines define both a curved path to follow as well as the velocities at each point. Therefore, when using the GetPositionAtTime method to move an object along a Spline, the object may speed up and slow down according to the velocity at a given spot along the Spline.
In some cases you may want to move along the Spline at a constant speed. To do this, you will want to use the path defined by the Spline, but not its velocity. If your object moves at a constant rate along a Spline, then the amount of time spent moving along the Spline is directly related to the length it has traveled along the Spline. The GetPositionAtLengthAlongSpline can be used to convert this length along a given Spline to a position.
The following example loads a SplineList, pulls the first Spline out of it, then gets the position along the Spline at a given length. Add the following using statement:
Add the following at class scope:
Add the following to initialize after initializing FlatRedBall:
Add the following to Update:
A Spline is a curved path that you can use to create curved motion. Splines can be created in code or in tools like the SplineEditor. The default file format for a spline is .splx.
The following example instantiates a Spline, adds four points, calculates the velocity and acceleration values, then finally makes the Spline visible. Visibility is usually used for tools and debugging. Add the following using statement:
Add the following to your Screen's CustomInitialize:
The following code creates a Spline by loading a .splx file. First you should add the file to your project. Information on adding files can be found here. File used:ExampleSpline.splx Add the following using statement:
Add the following to Initialize after initializing FlatRedBall:
A .splx file can contain multiple Splines. In this case the .splx file only contains one. Splines implement the INameable interface, so you can find Splines in the loaded list by their name if your .splx file contains multiple Splines.
In this example I added the .splx to my project's "Content" folder. Therefore, the file name is prepended with "Content\". Make sure to modify this according to where you place your file.
In FlatRedBall XNA and FlatRedBall MDX, this file should be loaded "from file". In FlatSilverBall, it should be set as Content. For more information, see this article on adding files.
.splx files store Velocities, so you do not need to call CalculateVelocities. In fact, it is possible for .splx files to store velocities that have been modified if the tool that has created the .splx supports handles. To preserve the original content, you should only call CalculateAccelerations after the Spline is loaded.
Objects can be easily positioned using a Spline. The GetPositionAtTime method returns a Vector3. It takes a time argument which is the amount of time from the beginning of the Spline. The following code positions a Sprite according to how much time has passed since the user has pressed the space bar. This code example also skips over the steps to create a valid Spline and Sprite. Add the following at class scope:
Add the following in Update:
Even though Splines provide a variety of methods and properties to treat the Spline as a single object, the Spline object also implements the IList<SplinePoint> interface. That means that you can directly work with a Spline as a set of points. For example, to move the SplinePoint at index 2 to the right by 4 units, you would write:
Of course, if you ever make changes to a Spline like moving its points you will also want to recalculate velocities and accelerations:
Did this article leave any questions unanswered? Post any question in our forums for a rapid response.
There are a few things to keep in mind: