mirror of
https://github.com/orange-cpp/omath.git
synced 2026-04-18 17:03:27 +00:00
improved walkbot
This commit is contained in:
@@ -7,26 +7,41 @@
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
|
||||
void WalkBot::update(const Vector3<float>& bot_position, const Vector3<float>& target_position,
|
||||
const float min_node_distance) const
|
||||
{
|
||||
const auto nav_mesh = m_mav_mesh.lock();
|
||||
WalkBot::WalkBot(std::shared_ptr<NavigationMesh> mesh) : m_mav_mesh(std::move(mesh)) {}
|
||||
|
||||
void WalkBot::set_nav_mesh(std::shared_ptr<NavigationMesh> mesh)
|
||||
{
|
||||
m_mav_mesh = std::move(mesh);
|
||||
}
|
||||
|
||||
void WalkBot::update(const Vector3<float>& bot_position, const Vector3<float>& target_position,
|
||||
const float min_node_distance)
|
||||
{
|
||||
if (!m_on_next_path_node.has_value())
|
||||
return;
|
||||
|
||||
const auto nav_mesh = m_mav_mesh.lock();
|
||||
if (!nav_mesh)
|
||||
return;
|
||||
|
||||
const auto path = Astar::find_path(bot_position, target_position, *nav_mesh);
|
||||
|
||||
if (path.empty())
|
||||
return;
|
||||
|
||||
if (!m_on_next_path_node.has_value())
|
||||
return;
|
||||
const auto& nearest = path.front();
|
||||
|
||||
if (path.size() > 1 && path.front().distance_to(bot_position) <= min_node_distance)
|
||||
// Record the nearest node as visited once we are close enough to it.
|
||||
if (nearest.distance_to(bot_position) <= min_node_distance)
|
||||
m_last_visited = nearest;
|
||||
|
||||
// If the nearest node was already visited, advance to the next one so
|
||||
// we never oscillate back to a node we just left.
|
||||
// If the bot was displaced (blown back), nearest will be an unvisited
|
||||
// node, so we route to it first before continuing forward.
|
||||
if (m_last_visited.has_value() && *m_last_visited == nearest && path.size() > 1)
|
||||
m_on_next_path_node->operator()(path[1]);
|
||||
else
|
||||
m_on_next_path_node->operator()(path.front());
|
||||
m_on_next_path_node->operator()(nearest);
|
||||
}
|
||||
void WalkBot::on_path(std::function<void(const Vector3<float>&)> callback)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user