Article Preview
Buy Now
| Print: | |
| PDF: |
The Topographic Apprentice
Path Animation
Tell your Object3Ds where to go!
Issue: 2.6 (July/August 2004)
Author: Joe Nastasi
Article Description: No description available.
Article Length (in bytes): 7,109
Starting Page Number: 38
RBD Number: 2619
Resource File(s): None
Related Link(s): None
Known Limitations: None
Excerpt of article text...
This month we will discuss the path file format, introduce a couple of new features and also take a look at a new technique for coding real-time tasks! New Stuff The Path Animation library was initially designed to be used for 2D movement only, but the code now includes the ability to define 3D waypoints. This resulted in a change to the API, namely, the DestX and DestY parameters are now replaced with a Vector3D that includes all three axes. Turn, Turn, Turn, But When? Additionally, the library now has the option to turn before movement or turn during movement. A separate turn, which is enabled by setting the Path Class' SeparateTurn property to true, forces the model to turn in place towards the new waypoint, then move towards it. An integrated turn, which is enabled when the SeparateTurn property is false, turns the model as it moves towards the waypoint. The integrated turn results in wide curved paths, especially when there are only a few waypoints defined. Path Speed The speed at which models turn and move on a path is defined by two or three properties. First is the interval that the path update method is called by the main animation loop. In our case, we used a Timer Control with a period of 50 milliseconds. Next, a step divides the movement between two waypoints by a selected value. In our example, a step size of 10 will move the model from one waypoint to another in 500 milliseconds, or 10 steps X 50 milliseconds. If an integrated turn is being performed, the turn is also divided by the same value. When a path uses separate turns, a step size dedicated to turns divides the turning action, allowing adjustment of the speed of the turn. Path Description File Format We will store all paths in a text file, just like we did for the character animation library. The format is simple and strictly defined. General Rules The EndOfLine method separates lines, so make sure that the text file has line breaks that match the OS on which the program is running. An extension of ".path", must be appended to the file name. The file must be in the same directory as the project running the Path Animation Library. The colon character (":") is used as a field delimiter. Path Definition A path definition must always appear before any of the waypoints that it uses. Field One always contains the label "Path" to mark it as a path definition. Field Two contains the name of the path. This field may be empty if the path is not to be accessed by name. Field Three always contains either a "T" or an "F" to indicate if the path is to be looped or not. Field Four always contains either a "T" or an "F" to indicate if turning will occur separately or as part of the forward movement. Field Five contains a number, greater than zero, that represents the number of steps the model's movement will be divided into. Field Six contains a number, greater than zero, that represents the number of steps the model's turn will be divided into when the path is defined as having separate turns. If integrated turns are being used, then this field must contain a zero. Waypoint Definition A waypoint definition must always follow the path definition that will use it. Field One always contains the label "Waypoint" to mark it as a waypoint definition. Field Two contains the name of the waypoint. Field Three contains a number that represents the X coordinate of the waypoint's position. Field Four contains a number that represents the Y coordinate of the waypoint's position. Field Five contains a number that represents the Z coordinate of the waypoint's position. Here is an example of a path that defines a square as defined in the animation file syntax: Path:Square:T:T:10:5 Waypoint:Zero:400:60:0 Waypoint:One:400:60:400 Waypoint:Two:-400:60:400 Waypoint:Three:-400:60:0 The Path Animation Class contain the LoadAnimation method which reads in the .path file, parses it and creates the appropriate instances of the path and waypoint classes. In addition to loading during initialization, you can provide the option to reload the file which simplifies editing a .path file by allowing you to preview changes quickly. Finite State Machines The path animation update method borrows from a technique used in embedded systems programming. Finite State Machines or FSM, are not a new type of terror robot from the Terminator series, but a technique that divides real-time processes into separate states or modes. This has two advantages. First, dividing a task up by any means always makes the resulting code more robust and easier to understand. Second, when logic requires waiting for a count to go to zero or some other time or event-dependent status change, simply sitting in a loop is wasteful of the processor's time. In a FSM, we define a state that checks that particular status. If it's not true, we simply exit from the loop and allow the program to do other work. If it is true, then the FSM changes to the next state. Main Path Loop We'll end by describing the main loop logic in detail. There are four states handled by the main loop's FSM. They are defined as constants in the Waypoint Class: wpStart, wpTurn, wpMove, wpDone. First we determine if a loop animation is playing. Once that is established, here's how it all works: Get local instances of current path, waypoint, Object3D and separate/integrated turn property. Calculate the target orientation the model needs to be in when it reaches the waypoint. Get the current state of the current waypoint. If the current waypoint state is wpStart: Calculate step size or distance for each step. For example, if there are 100 units between two waypoints and the Step property is 10, the model will move 10 units each step. Get the total number of steps and turn steps for use as counters. Set the current waypoint state to wpTurn. If the current waypoint state is wpTurn: If this path is using an integrated turn, set the current waypoint state to wpMove, effectively skipping wpTurn. If this path is using a separate turn: If there are more turn steps to take, turn by an angular amount calculated using the current turn step. Decrement the current turn step counter. If there are no more turn steps left, set the state to wpMove. If the current waypoint state is wpMove: If there are more movement steps to take, move model forward by the step distance previously calculated. Decrement the movement step counter. If there are no more movement steps left, set the state to wpDone. If the current waypoint state is wpDone: Set the current waypoint state to wpStart. Get the next waypoint, resetting to the first waypoint when the last one is reached. If path is not to be looped, then set the IsPlaying property to false, effectively stopping the update until a new path is started. Next Issue In the next installment, we'll will see the code in detail.
...End of Excerpt. Please purchase the magazine to read the full article.
Article copyrighted by REALbasic Developer magazine. All rights reserved.
|










