The Path object provides a way to define a 2D path using lines and arcs. Paths can be used for the movement of objects such as enemies in a shoot-em-up game (shmup). Paths can be created in code and the FlatRedBall Editor and can easily integrate with games in edit mode.
To define a path in the FlatRedBall Editor:
Add a new object to a Screen or Entity
Select Path as the type
Select the newly-created Path and click on the Variables tab
Click the Add to Path button to add segments to the path
Paths can also be created or modified in code. Paths are built through move, line, and arc calls. Every call will set the end point which serves as the starting point for the next call. The following methods are available for modifying the path:
MoveTo - Moves the end point to the absolute positions.
MoveToRelative - Moves the end point to the argument points relative to the current end point.
LineTo - Draws a line to the absolute position.
LineToRelative - Draws a line to the position relative to the current end point.
ArcTo - Draws an arc to the absolute position.
ArcToRelative - Draws an arc to the position relative to the current end point.
Typically the Relative methods are recommend over the absolute methods, as this allows paths to be shifted. The FlatRedBall Editor only provides access to the Relative methods. The following code shows how to construct a path in code:
Paths can be visualized using EditorVisuals when the game is in Edit Mode. This example assumes a path named PathInstance.
Open the Screen or Entity which has the Path
Add a partial CustomActivityEditMode with the following code:
The Path will now update in realtime as it is edited.
Paths by themselves do not provide the ability to change the velocity of an object, but they do provide a method PointAtLength which can be used to position an object. Typically a variable is used to keep track of the distance that an object has moved along the path. This value can be modified every frame, then it can be used to determine the position of an object along the path. For example, the following code shows how to move a circle along a path. Pressing the space bar resets the distance value.
The TangentAtLength returns a Vector2 describing the unit vector parallel (tangent) to the path at the argument length. This can be used to determine the velocity of an object moving along the path. The following code shows how to draw a line displaying the tangent at length.
[subpages depth="1"]