【发布时间】:2013-10-31 22:51:51
【问题描述】:
我正在做一个 SFML/C++ 项目,我需要生成一个图形来连接它们之间的障碍物以方便寻路,所以我对生成一个导航网格很感兴趣,我将应用 boost A* 算法。 有点像这样:
但是我在使用 Boost Graph Library 实现这一点时遇到了很多问题(如果您有一个更合适的库,我感兴趣的话)。首先,我创建一个具有适当结构的 adjacency_list:
struct WayPoint{
sf::Vector2f pos;
};
struct WayPointConnection{
float dist;
};
typedef boost::adjacency_list<
boost::listS,
boost::vecS,
boost::undirectedS,
WayPoint,
WayPointConnection
> WayPointGraph;
typedef WayPointGraph::vertex_descriptor WayPointID;
typedef WayPointGraph::edge_descriptor WayPointConnectionID;
然后我创建我的图表,并在其中添加障碍物的顶点(目前是简单的矩形):
while (i != rectangle.getPointCount()) {
sf::Vector2f pt1 (sf::Vector2f(rectangle.getPoint(i).x + mouseEvent.x, rectangle.getPoint(i).y + mouseEvent.y));
WayPointID wpID = boost::add_vertex(graph);
graph[wpID].pos = pt1;
i++;
}
现在它变得复杂了,我必须浏览所有顶点并创建这些顶点的邻居的弧,知道弧不应该进入障碍物......我不明白我怎么能使用 Boost 进行编码,我开始编码:
boost::graph_traits<WayPointGraph>::vertex_iterator vi, vi_end, next;
boost::tie(vi, vi_end) = vertices(graph);
for (next = vi; vi != vi_end; vi = next) {
//I need to create the good arcs ...
++next;
}
提前谢谢你。
【问题讨论】:
标签: c++ boost graph sfml a-star