TGB/Tutorials/Basic aStar
From TDN
aStar Mini Tutorial by Steven A. Hine
First I’d like to acknowledge the person who wrote the aStar code. This is an awesome resource. Thanks Phil. After trying to implement this in a few games I finally figured it out. Here is the easiest way to use aStar. [edit] Step 1: (Creating a Project)First create a new project called aStarFollowing, add the puzzle template, and check the copy executable box. IMAGE Next we will need to add some behaviors to make the player and camera move. (You can get the behaviors from TDN network on the behaviors page.) The first one is under the movement styles category called ‘shooter’. Download, unzip, and add this behavior into the \aStarFollowing\game\behaviors folder. The next behavior will be added to the same folder, but a file will have to be created for it. Open up notepad or another txt program and copy the camera follow code from the ‘effects’ category link. Make sure to save the file as ‘cameraFollow.cs’. The .cs extension is important for TGB’s editor to see the behavior. Here is what should be in the folder: IMAGE The next step to do is to reload TGB’s editor. This can be done by going to the project menu and clicking ‘reload project’. This will add the new behaviors to the editor. Now the aStar files need to be added to the scripts folder. They should be in TGB’s installed folder: \Program Files\GarageGames\TorqueGameBuilder-1.7.4\games\AStarDemo\game\gameScripts. Copy the files aStarActor.cs and aStarPath.cs into the \aStarFollowing\game\gameScripts folder. All files are setup except the scripts we need to create. The next section will set up the scene for interaction with the player and AI. [edit] Step 2: (Creating a scene)aStar uses a tile map to process path calculations. Each tile in the map is a location in the game world that the AI can either be on or have to move around. This is set up is by using the tile editor to create values for each tile in the map. First drag a new newlayer object into the level area from the tile maps tab. Change the tile count x/y to 30 and tile size x/y to 5. Then press the size object to layer button to show all the tiles. IMAGE To add tiles to the map press the tile editing tab to reveal the edit controls: IMAGE IMAGE First, under image find the puzzleBlock_1ImageMap. Then under custom data delete ‘none’ and type in 10. This is where aStar will find the value to rate each tile. Each tile in the map does not have to be done. Only the tiles that need to have a value higher than 0 will have to be done. A value of 0 means that the AI can move on to the tile, higher values tell aStar to move around the tile. 10 is the highest value and impassable. The AI will not be able to use this tile. When a tiles custom data is set a circle will be put in the top left hand corner of the tile. IMAGE After setting up the tile create a map that looks like this one: (Of course the triangle and white ball will be put in later.) IMAGE
[edit] Step 3: (Adding the player with controls and camera)To see how well aStar works, a player will be added with keyboard controls of left, right, up, and down. Press the create button to show the ‘static images’ tab. Choose any image from the tab and drag it into the scene into the tile layer. With this image still selected press the edit button and open the ‘behaviors’ tab. This is what it looks like: IMAGE By pressing the up/down arrows the two added behaviors should appear. If they are not there, check to see if they are in the correct folder (/’project name’/behaviors/) and reload the project. When asked to save the project, save it as start.t2d. After pressing the arrows and revealing the behaviors, select Shooter Controls and press the green plus button to add the behavior to the object. Change the setting in the Shooter Controls to this: IMAGE If the scene was played now, the keyboard arrows would move the object around the screen. The controls are now set up. Next the camera will be added. Back in TGB, go to the create menu and drag an empty scene object into the scene. This will be used to keep the camera from showing blank space outside of the tile map. Size the object to the same size of the tile map. By doing this the camera behavior, that will be added in a moment, will know the boundaries of the camera. Lastly, go to the ‘scripting’ tab and enter pSceneBounds in the name option. Select the player object again and open up the behaviors menu. Select ‘Camera’ under Camera Follow and press the green button. The behavior should now be added to the panel like this: IMAGE Under ‘objectInfo’ select pSceneBounds. When the behavior is added to the scene on startup it will use the pSceneBounds object to get the boundaries of the tile map. This is because it is the same size as the map. Before going on open up the scripting tab, add pPlayer for the name and PlayerClass for the class. By doing this we can access some scripting functions and create some functions for the player. Now the player is set up. Next add the AI and script in the aStar movement. [edit] Step 4: (AI and aStar)First choose a static image and drag it into the scene inside the tile map. It should be put between the blocks in the map. Open up the scripting tab and enter this information: Name: pAI Class: AIClass Super Class: aStarActor It is always good to give object a unique name. The class will be used to add some more functionality to the object through script. Finally the super class aStarActor will add all the functions need to give control to aStar. At this point the level can be saved as ‘start.t2d’ and TGB closed. aStar works by making each tile in the tile map a node that the AI can move to. Of course, whether or not the tile is used depends on the custom data value that it has. All the blocks we added earlier have a value of 10, so they are not usable and aStar will move around them. It is assumed that any tile that does not have its custom data set has a value of zero. In this picture the AI moves in the path of the arrows , because it is the shortest path of tiles that are open. IMAGE If you need a more in depth understanding of aStar, there are many resources on the internet as well as books on the subject. All these calculations happen in script and engine code. To get aStar to work, open up a new notepad file and type in this code:
function AIClass::onLevelLoaded(%this, %sceneGraph)
{
%this.moveSpeed = 10;
%this.enableUpdateCallback();
}
function AIClass::onUpdate(%this)
{
%this.startPath(%this.getPosition(), pPlayer.getPosition());
}
First the onLevelLoaded function is used to set up the moveSpeed variable. This variable is used to tell aStar how fast the AI should move to the next tile or node in the tile map. Then the onUpdate callback is enabled, so that the player and AI position can be passed to aStar. aStar will use these to create the path. Next, type in this code below the onUpdate function.
function AIClass::startPath(%this, %start, %dest)
{
// find our path
%this.pathGrid = $pathGrid;
%newPath = %this.findDestinationPath(%dest);
if (isObject(%newPath) )
{
%this.currentPath = %newPath;
%this.followAStarPath(%newPath);
activeActorsSet.add(%this);
}
else
{
%this.isFrustrated("noPath");
}
}
This code comes from the aStar demo in the TGB documents distributed with TGB. First a function called startPath is declared for the AIClass. In it we pass a start destination (%start) and a end destination (%dest). Next a pathGrid object is created that points to the aStarPath tileLayer from the level. A %newPath is created by calling aStarActor findDestinationPath. This function was added to the AI by the super class. It tries to find a path to the destination passed to the startPath function. If a newPath is created, the code continues through the ‘if’ statement. The currentPath is updated with the newPath. Next the AI is told to follow the path by calling followAStarPath from the aStarActor supr class. Lastly, this object is added to the activeActorsSet, which will update the path if the game world has changed. When a path is not found to the object, the else statement is executed. The isFrustrated function is called, which states there is no path. The isFrustrated function can be changed to create different behaviors. The most obvious is when a path was tried several times with no conclusion. By keeping count of how many tries were made, the AI’s last try could tell it to do some other action. These actions could be to wait, random directions, or give up. Save the file as ‘Actor.cs’, and close notepad. Before any of the above code will run, TGB needs to be told to compile the new file as well as aStarActor and aStarPath. Add this code to the main.cs file in the ‘game’ directory of the project. It goes below the ‘Exec game scripts’ line:
exec("./gameScripts/aStarActor.cs");
exec("./gameScripts/aStarPath.cs");
exec("./gameScripts/actor.cs");
These lines should be self explanatory. Save the file and close notepad. At this point the scene can be run. Open up TGB and run the scene. The AI should chase the player around the scene avoiding the blocks in the tile map.
|



