本文整理汇总了C++中PathNode::SetEstimatedCost方法的典型用法代码示例。如果您正苦于以下问题:C++ PathNode::SetEstimatedCost方法的具体用法?C++ PathNode::SetEstimatedCost怎么用?C++ PathNode::SetEstimatedCost使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PathNode
的用法示例。
在下文中一共展示了PathNode::SetEstimatedCost方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: UpdatePath
void Pathfinder::UpdatePath() {
//need to set all parents to null before recalculating the path.
//if not, there's an infinite loop in BuildPath()
for (std::vector<PathNode>::iterator itr = m_nodes.begin(); itr != m_nodes.end(); ++itr) {
itr->SetParent(nullptr);
}
//set StartNode and EndNode
m_startNode = NearestNode(m_StartPosition);
m_endNode = NearestNode(m_EndPosition);
//recalculate all estimated costs
int16_t cost = 0;
PathNode * node;
for (std::vector<PathNode>::iterator itr = m_nodes.begin(); itr != m_nodes.end(); ++itr) {
node = &(*itr);
if (node->GetCost() != -1) {
//estimated based on pixels distance
float cost = node->GetCost();
float estimated = (sqrt(pow((m_endNode->GetPos().mX - node->GetPos().mX), 2)
+ pow(m_endNode->GetPos().mY - node->GetPos().mY, 2)));
node->SetEstimatedCost(estimated);
node->SetCost(cost + estimated);
node->SetTotalCost(cost + estimated);
}
}
//A*
m_openNodes.clear();
m_closedNodes.clear();
m_startNode->SetCost(0);
m_startNode->SetTotalCost(0);
m_openNodes.push_back(m_startNode);
bool objectiveFound = false;
while (!m_openNodes.empty() && !objectiveFound) {
PathNode * node = *(m_openNodes.begin());
m_openNodes.erase(m_openNodes.begin());
if (node->GetPos().mX == m_endNode->GetPos().mX
&& node->GetPos().mY == m_endNode->GetPos().mY) {
BuildPath(node);
} else {
for (std::vector<Polygon::Edge>::iterator itr = node->GetPolygon()->m_edges.begin();
itr != node->GetPolygon()->m_edges.end(); ++itr) {
printf("EDGE: %d - %d\n", node->GetPolygon()->m_id, itr->m_neighbour->m_id);
PathNode * nextNode = nullptr;
for (std::vector<PathNode>::iterator pathNodeItr = m_nodes.begin();
pathNodeItr != m_nodes.end(); ++pathNodeItr) {
if (pathNodeItr->GetPolygon()->m_id == itr->m_neighbour->m_id) {
nextNode = &(*pathNodeItr);
}
}
if (nextNode == m_endNode) {
nextNode->SetParent(node);
nextNode->SetTotalCost(node->GetTotalCost() + nextNode->GetCost());
BuildPath(nextNode);
objectiveFound = true;
break;
}
if (nextNode->GetCost() != -1) {
if (node == nextNode) {
continue;
} else if (find(m_closedNodes.begin(), m_closedNodes.end(), nextNode)
!= m_closedNodes.end()) {
continue;
} else if (find(m_openNodes.begin(), m_openNodes.end(), nextNode)
!= m_openNodes.end()) {
if (nextNode->GetTotalCost() > 0
&& nextNode->GetTotalCost() >
node->GetTotalCost() + nextNode->GetCost()) {
nextNode->SetTotalCost(node->GetTotalCost() + nextNode->GetCost());
nextNode->SetParent(node);
}
} else {
nextNode->SetParent(node);
m_openNodes.push_back(nextNode);
}
}
}
std::vector<PathNode *>::iterator el = find(m_openNodes.begin(),
m_openNodes.end(), node);
if (el != m_openNodes.end()) {
m_openNodes.erase(el);
}
m_closedNodes.push_back(node);
}
}
}