2020-01-10 11:22:34 +00:00
|
|
|
/**************************************************************************/
|
2020-02-11 13:01:43 +00:00
|
|
|
/* nav_map.h */
|
2020-01-10 11:22:34 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
2020-02-11 13:01:43 +00:00
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
2020-01-10 11:22:34 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
2022-05-18 12:53:52 +00:00
|
|
|
#ifndef NAV_MAP_H
|
|
|
|
#define NAV_MAP_H
|
2020-01-10 11:22:34 +00:00
|
|
|
|
|
|
|
#include "nav_rid.h"
|
2023-06-13 14:56:21 +00:00
|
|
|
#include "nav_utils.h"
|
2020-01-10 11:22:34 +00:00
|
|
|
|
|
|
|
#include "core/math/math_defs.h"
|
2022-07-23 17:12:41 +00:00
|
|
|
#include "core/object/worker_thread_pool.h"
|
2020-01-10 11:22:34 +00:00
|
|
|
|
2023-01-10 06:14:16 +00:00
|
|
|
#include <KdTree2d.h>
|
|
|
|
#include <KdTree3d.h>
|
2023-06-13 14:56:21 +00:00
|
|
|
#include <RVOSimulator2d.h>
|
2023-01-10 06:14:16 +00:00
|
|
|
#include <RVOSimulator3d.h>
|
2020-01-10 11:22:34 +00:00
|
|
|
|
2022-01-30 23:39:52 +00:00
|
|
|
class NavLink;
|
2020-01-10 11:22:34 +00:00
|
|
|
class NavRegion;
|
2023-02-01 21:11:10 +00:00
|
|
|
class NavAgent;
|
2023-01-10 06:14:16 +00:00
|
|
|
class NavObstacle;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
|
|
|
class NavMap : public NavRid {
|
2023-07-17 10:20:09 +00:00
|
|
|
RWLock map_rwlock;
|
|
|
|
|
2020-01-10 11:22:34 +00:00
|
|
|
/// Map Up
|
2020-05-12 15:01:17 +00:00
|
|
|
Vector3 up = Vector3(0, 1, 0);
|
2020-01-10 11:22:34 +00:00
|
|
|
|
|
|
|
/// To find the polygons edges the vertices are displaced in a grid where
|
2023-06-13 11:36:05 +00:00
|
|
|
/// each cell has the following cell_size and cell_height.
|
|
|
|
real_t cell_size = 0.25; // Must match ProjectSettings default 3D cell_size and NavigationMesh cell_size.
|
|
|
|
real_t cell_height = 0.25; // Must match ProjectSettings default 3D cell_height and NavigationMesh cell_height.
|
2020-01-10 11:22:34 +00:00
|
|
|
|
2024-02-04 20:31:07 +00:00
|
|
|
// For the inter-region merging to work, internal rasterization is performed.
|
|
|
|
float merge_rasterizer_cell_size = 0.25;
|
|
|
|
float merge_rasterizer_cell_height = 0.25;
|
|
|
|
// This value is used to control sensitivity of internal rasterizer.
|
|
|
|
float merge_rasterizer_cell_scale = 1.0;
|
|
|
|
|
2023-03-31 23:49:43 +00:00
|
|
|
bool use_edge_connections = true;
|
2020-01-10 11:22:34 +00:00
|
|
|
/// This value is used to detect the near edges to connect.
|
2022-06-03 16:47:54 +00:00
|
|
|
real_t edge_connection_margin = 0.25;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
2022-01-30 23:39:52 +00:00
|
|
|
/// This value is used to limit how far links search to find polygons to connect to.
|
|
|
|
real_t link_connection_radius = 1.0;
|
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
bool regenerate_polygons = true;
|
|
|
|
bool regenerate_links = true;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
2022-01-30 23:39:52 +00:00
|
|
|
/// Map regions
|
2022-07-28 17:24:14 +00:00
|
|
|
LocalVector<NavRegion *> regions;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
2022-01-30 23:39:52 +00:00
|
|
|
/// Map links
|
|
|
|
LocalVector<NavLink *> links;
|
|
|
|
LocalVector<gd::Polygon> link_polygons;
|
|
|
|
|
2020-01-10 11:22:34 +00:00
|
|
|
/// Map polygons
|
2022-07-28 17:24:14 +00:00
|
|
|
LocalVector<gd::Polygon> polygons;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
2023-01-10 06:14:16 +00:00
|
|
|
/// RVO avoidance worlds
|
|
|
|
RVO2D::RVOSimulator2D rvo_simulation_2d;
|
|
|
|
RVO3D::RVOSimulator3D rvo_simulation_3d;
|
|
|
|
|
|
|
|
/// avoidance controlled agents
|
|
|
|
LocalVector<NavAgent *> active_2d_avoidance_agents;
|
|
|
|
LocalVector<NavAgent *> active_3d_avoidance_agents;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
2023-01-10 06:14:16 +00:00
|
|
|
/// dirty flag when one of the agent's arrays are modified
|
|
|
|
bool agents_dirty = true;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
|
|
|
/// All the Agents (even the controlled one)
|
2023-02-01 21:11:10 +00:00
|
|
|
LocalVector<NavAgent *> agents;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
2023-01-10 06:14:16 +00:00
|
|
|
/// All the avoidance obstacles (both static and dynamic)
|
|
|
|
LocalVector<NavObstacle *> obstacles;
|
|
|
|
|
|
|
|
/// Are rvo obstacles modified?
|
|
|
|
bool obstacles_dirty = true;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
|
|
|
/// Physics delta time
|
2020-05-12 15:01:17 +00:00
|
|
|
real_t deltatime = 0.0;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
|
|
|
/// Change the id each time the map is updated.
|
2023-11-01 02:02:59 +00:00
|
|
|
uint32_t iteration_id = 0;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
2023-01-10 06:14:16 +00:00
|
|
|
bool use_threads = true;
|
|
|
|
bool avoidance_use_multiple_threads = true;
|
|
|
|
bool avoidance_use_high_priority_threads = true;
|
|
|
|
|
2022-12-30 04:19:15 +00:00
|
|
|
// Performance Monitor
|
|
|
|
int pm_region_count = 0;
|
|
|
|
int pm_agent_count = 0;
|
|
|
|
int pm_link_count = 0;
|
|
|
|
int pm_polygon_count = 0;
|
|
|
|
int pm_edge_count = 0;
|
|
|
|
int pm_edge_merge_count = 0;
|
|
|
|
int pm_edge_connection_count = 0;
|
|
|
|
int pm_edge_free_count = 0;
|
2024-06-25 02:50:43 +00:00
|
|
|
int pm_obstacle_count = 0;
|
2022-12-30 04:19:15 +00:00
|
|
|
|
2020-01-10 11:22:34 +00:00
|
|
|
public:
|
2022-04-18 19:57:23 +00:00
|
|
|
NavMap();
|
|
|
|
~NavMap();
|
2020-01-10 11:22:34 +00:00
|
|
|
|
2023-11-01 02:02:59 +00:00
|
|
|
uint32_t get_iteration_id() const { return iteration_id; }
|
|
|
|
|
2020-01-10 11:22:34 +00:00
|
|
|
void set_up(Vector3 p_up);
|
|
|
|
Vector3 get_up() const {
|
|
|
|
return up;
|
|
|
|
}
|
|
|
|
|
2023-03-07 14:16:07 +00:00
|
|
|
void set_cell_size(real_t p_cell_size);
|
|
|
|
real_t get_cell_size() const {
|
2020-01-10 11:22:34 +00:00
|
|
|
return cell_size;
|
|
|
|
}
|
|
|
|
|
2023-06-13 11:36:05 +00:00
|
|
|
void set_cell_height(real_t p_cell_height);
|
|
|
|
real_t get_cell_height() const { return cell_height; }
|
|
|
|
|
2024-02-04 20:31:07 +00:00
|
|
|
void set_merge_rasterizer_cell_scale(float p_value);
|
|
|
|
float get_merge_rasterizer_cell_scale() const {
|
|
|
|
return merge_rasterizer_cell_scale;
|
|
|
|
}
|
|
|
|
|
2023-03-31 23:49:43 +00:00
|
|
|
void set_use_edge_connections(bool p_enabled);
|
|
|
|
bool get_use_edge_connections() const {
|
|
|
|
return use_edge_connections;
|
|
|
|
}
|
|
|
|
|
2023-03-07 14:16:07 +00:00
|
|
|
void set_edge_connection_margin(real_t p_edge_connection_margin);
|
|
|
|
real_t get_edge_connection_margin() const {
|
2020-01-10 11:22:34 +00:00
|
|
|
return edge_connection_margin;
|
|
|
|
}
|
|
|
|
|
2023-03-07 14:16:07 +00:00
|
|
|
void set_link_connection_radius(real_t p_link_connection_radius);
|
|
|
|
real_t get_link_connection_radius() const {
|
2022-01-30 23:39:52 +00:00
|
|
|
return link_connection_radius;
|
|
|
|
}
|
|
|
|
|
2020-01-10 11:22:34 +00:00
|
|
|
gd::PointKey get_point_key(const Vector3 &p_pos) const;
|
|
|
|
|
2022-10-05 23:24:45 +00:00
|
|
|
Vector<Vector3> get_path(Vector3 p_origin, Vector3 p_destination, bool p_optimize, uint32_t p_navigation_layers, Vector<int32_t> *r_path_types, TypedArray<RID> *r_path_rids, Vector<int64_t> *r_path_owners) const;
|
2020-02-18 16:08:34 +00:00
|
|
|
Vector3 get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const;
|
|
|
|
Vector3 get_closest_point(const Vector3 &p_point) const;
|
|
|
|
Vector3 get_closest_point_normal(const Vector3 &p_point) const;
|
2022-02-13 15:07:01 +00:00
|
|
|
gd::ClosestPointQueryResult get_closest_point_info(const Vector3 &p_point) const;
|
2020-02-18 16:08:34 +00:00
|
|
|
RID get_closest_point_owner(const Vector3 &p_point) const;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
|
|
|
void add_region(NavRegion *p_region);
|
|
|
|
void remove_region(NavRegion *p_region);
|
2022-07-28 17:24:14 +00:00
|
|
|
const LocalVector<NavRegion *> &get_regions() const {
|
2020-01-10 11:22:34 +00:00
|
|
|
return regions;
|
|
|
|
}
|
|
|
|
|
2022-01-30 23:39:52 +00:00
|
|
|
void add_link(NavLink *p_link);
|
|
|
|
void remove_link(NavLink *p_link);
|
|
|
|
const LocalVector<NavLink *> &get_links() const {
|
|
|
|
return links;
|
|
|
|
}
|
|
|
|
|
2023-02-01 21:11:10 +00:00
|
|
|
bool has_agent(NavAgent *agent) const;
|
|
|
|
void add_agent(NavAgent *agent);
|
|
|
|
void remove_agent(NavAgent *agent);
|
|
|
|
const LocalVector<NavAgent *> &get_agents() const {
|
2020-01-10 11:22:34 +00:00
|
|
|
return agents;
|
|
|
|
}
|
|
|
|
|
2023-02-01 21:11:10 +00:00
|
|
|
void set_agent_as_controlled(NavAgent *agent);
|
|
|
|
void remove_agent_as_controlled(NavAgent *agent);
|
2020-01-10 11:22:34 +00:00
|
|
|
|
2023-01-10 06:14:16 +00:00
|
|
|
bool has_obstacle(NavObstacle *obstacle) const;
|
|
|
|
void add_obstacle(NavObstacle *obstacle);
|
|
|
|
void remove_obstacle(NavObstacle *obstacle);
|
|
|
|
const LocalVector<NavObstacle *> &get_obstacles() const {
|
|
|
|
return obstacles;
|
|
|
|
}
|
|
|
|
|
2023-10-23 16:16:05 +00:00
|
|
|
Vector3 get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const;
|
|
|
|
|
2020-01-10 11:22:34 +00:00
|
|
|
void sync();
|
|
|
|
void step(real_t p_deltatime);
|
|
|
|
void dispatch_callbacks();
|
|
|
|
|
2022-12-30 04:19:15 +00:00
|
|
|
// Performance Monitor
|
|
|
|
int get_pm_region_count() const { return pm_region_count; }
|
|
|
|
int get_pm_agent_count() const { return pm_agent_count; }
|
|
|
|
int get_pm_link_count() const { return pm_link_count; }
|
|
|
|
int get_pm_polygon_count() const { return pm_polygon_count; }
|
|
|
|
int get_pm_edge_count() const { return pm_edge_count; }
|
|
|
|
int get_pm_edge_merge_count() const { return pm_edge_merge_count; }
|
|
|
|
int get_pm_edge_connection_count() const { return pm_edge_connection_count; }
|
|
|
|
int get_pm_edge_free_count() const { return pm_edge_free_count; }
|
2024-06-25 02:50:43 +00:00
|
|
|
int get_pm_obstacle_count() const { return pm_obstacle_count; }
|
2022-12-30 04:19:15 +00:00
|
|
|
|
2020-01-10 11:22:34 +00:00
|
|
|
private:
|
2023-02-01 21:11:10 +00:00
|
|
|
void compute_single_step(uint32_t index, NavAgent **agent);
|
2023-01-10 06:14:16 +00:00
|
|
|
|
|
|
|
void compute_single_avoidance_step_2d(uint32_t index, NavAgent **agent);
|
|
|
|
void compute_single_avoidance_step_3d(uint32_t index, NavAgent **agent);
|
|
|
|
|
2022-10-05 23:24:45 +00:00
|
|
|
void clip_path(const LocalVector<gd::NavigationPoly> &p_navigation_polys, Vector<Vector3> &path, const gd::NavigationPoly *from_poly, const Vector3 &p_to_point, const gd::NavigationPoly *p_to_poly, Vector<int32_t> *r_path_types, TypedArray<RID> *r_path_rids, Vector<int64_t> *r_path_owners) const;
|
2023-01-10 06:14:16 +00:00
|
|
|
void _update_rvo_simulation();
|
|
|
|
void _update_rvo_obstacles_tree_2d();
|
|
|
|
void _update_rvo_agents_tree_2d();
|
|
|
|
void _update_rvo_agents_tree_3d();
|
2024-02-04 20:31:07 +00:00
|
|
|
|
|
|
|
void _update_merge_rasterizer_cell_dimensions();
|
2020-01-10 11:22:34 +00:00
|
|
|
};
|
|
|
|
|
2022-05-18 12:53:52 +00:00
|
|
|
#endif // NAV_MAP_H
|