Integration Phase 4a: Getting and Using Paths

Once you have computed a path using an AstarQuery, you can retrieve it as a Path object by calling AstarQuery::GetPath(). The Path object is a collection of nodes and edges, through which you can iterate.

For instance, the following code retrieves the path computed by a query, and sends an arrow to the visual debugging system at a different position in each frame: [code from Tutorial_FirstIntegration.cpp]

class MyGameLevel
{
public:
    ...         
    void TestPath(Kaim::World* world);
    ...     
};

...

void MyGameLevel::TestPath(Kaim::World* world)
{
    Kaim::ScopedDisplayList displayList(world, Kaim::DisplayList_Enable);
    displayList.InitSingleFrameLifespan("Test Path", "My Game");

    if (m_astarQuery->m_processStatus == Kaim::QueryDone)
    {
        Kaim::AStarQueryResult result = m_astarQuery->GetResult();
        if( result == Kaim::ASTAR_DONE_PATH_FOUND )
        {
            Kaim::Path* resultPath = m_astarQuery->GetPath();
            if( resultPath->GetEdgeCount() == 0 )
                return;

            // Let's fake an object moving on this path
            // in a very basic way.
            static KyFloat32 ratio = 0.0f;
            ratio += 0.00005f;
            if( ratio > 1.0f )
                ratio = 0.0f;
            KyFloat32 positionRatio = ratio * resultPath->GetEdgeCount();
            KyUInt32 currentEdge = (KyUInt32)floorf(positionRatio);
            Kaim::Vec3f edgeStart = resultPath->GetNodePosition(currentEdge);
            Kaim::Vec3f edgeEnd = resultPath->GetNodePosition(currentEdge+1);
            Kaim::Vec3f objectPosition = edgeStart + (positionRatio-currentEdge)*(edgeEnd-edgeStart);

            Kaim::VisualShapeColor shapeColor;
            shapeColor.m_triangleColor = Kaim::VisualColor(255, 255, 0);
            shapeColor.m_lineColor     = Kaim::VisualColor(175, 175, 0);
            displayList.PushArrow(    objectPosition + Kaim::Vec3f(0.0f, 0.0f, 2.0f),
                                    objectPosition,
                                    0.05f, shapeColor);
        }
    }
}

Testing

When you connect the Navigation Lab to the game, you should see a yellow arrow that moves from the start point to the destination point, with a speed that varies in proportion to the length of the edge it is currently following.

Obviously, the code provided above is just a simple example of a basic way that you could access and use the paths generated by the AstarQuery in your game code for your own purposes. However, the next phase of this tutorial introduces you to the rich path following system built in to Gameware Navigation, which works out-of-the-box to get your characters computing and following paths.