使用 AstarQuery 计算路径后,便可以通过调用 AstarQuery::GetPath() 将其检索为 Path 对象。Path 对象是节点和边的集合,您可以对其进行循环访问。
例如,以下代码将检索由查询计算的路径,并在每一帧的不同位置向可视调试系统发送一个箭头:[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); } } }
将 Navigation Lab 连接到游戏时,您应该会看到一个黄色箭头从起始点移动到目标点,速度的变化与其当前所跟随的边的长度成正比。
显然,上述代码只是一个简单示例,显示了访问使用您的游戏代码由 AstarQuery 生成的路径以及将其用于自己的用途的基本方式。但是,本教程的下一个阶段将向您介绍内置于 Gameware Navigation 中的丰富路径跟随系统,可以开箱即用以获取角色计算和跟随路径。