feat: godot-engine-source-4.3-stable
This commit is contained in:
parent
c59a7dcade
commit
7125d019b5
11149 changed files with 5070401 additions and 0 deletions
481
engine/modules/navigation/2d/godot_navigation_server_2d.cpp
Normal file
481
engine/modules/navigation/2d/godot_navigation_server_2d.cpp
Normal file
|
|
@ -0,0 +1,481 @@
|
|||
/**************************************************************************/
|
||||
/* godot_navigation_server_2d.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "godot_navigation_server_2d.h"
|
||||
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
#include "nav_mesh_generator_2d.h"
|
||||
#endif // CLIPPER2_ENABLED
|
||||
|
||||
#include "servers/navigation_server_3d.h"
|
||||
|
||||
#define FORWARD_0(FUNC_NAME) \
|
||||
GodotNavigationServer2D::FUNC_NAME() { \
|
||||
return NavigationServer3D::get_singleton()->FUNC_NAME(); \
|
||||
}
|
||||
|
||||
#define FORWARD_0_C(FUNC_NAME) \
|
||||
GodotNavigationServer2D::FUNC_NAME() \
|
||||
const { \
|
||||
return NavigationServer3D::get_singleton()->FUNC_NAME(); \
|
||||
}
|
||||
|
||||
#define FORWARD_1(FUNC_NAME, T_0, D_0, CONV_0) \
|
||||
GodotNavigationServer2D::FUNC_NAME(T_0 D_0) { \
|
||||
return NavigationServer3D::get_singleton()->FUNC_NAME(CONV_0(D_0)); \
|
||||
}
|
||||
|
||||
#define FORWARD_1_C(FUNC_NAME, T_0, D_0, CONV_0) \
|
||||
GodotNavigationServer2D::FUNC_NAME(T_0 D_0) \
|
||||
const { \
|
||||
return NavigationServer3D::get_singleton()->FUNC_NAME(CONV_0(D_0)); \
|
||||
}
|
||||
|
||||
#define FORWARD_1_R_C(CONV_R, FUNC_NAME, T_0, D_0, CONV_0) \
|
||||
GodotNavigationServer2D::FUNC_NAME(T_0 D_0) \
|
||||
const { \
|
||||
return CONV_R(NavigationServer3D::get_singleton()->FUNC_NAME(CONV_0(D_0))); \
|
||||
}
|
||||
|
||||
#define FORWARD_2(FUNC_NAME, T_0, D_0, T_1, D_1, CONV_0, CONV_1) \
|
||||
GodotNavigationServer2D::FUNC_NAME(T_0 D_0, T_1 D_1) { \
|
||||
return NavigationServer3D::get_singleton()->FUNC_NAME(CONV_0(D_0), CONV_1(D_1)); \
|
||||
}
|
||||
|
||||
#define FORWARD_2_C(FUNC_NAME, T_0, D_0, T_1, D_1, CONV_0, CONV_1) \
|
||||
GodotNavigationServer2D::FUNC_NAME(T_0 D_0, T_1 D_1) \
|
||||
const { \
|
||||
return NavigationServer3D::get_singleton()->FUNC_NAME(CONV_0(D_0), CONV_1(D_1)); \
|
||||
}
|
||||
|
||||
#define FORWARD_2_R_C(CONV_R, FUNC_NAME, T_0, D_0, T_1, D_1, CONV_0, CONV_1) \
|
||||
GodotNavigationServer2D::FUNC_NAME(T_0 D_0, T_1 D_1) \
|
||||
const { \
|
||||
return CONV_R(NavigationServer3D::get_singleton()->FUNC_NAME(CONV_0(D_0), CONV_1(D_1))); \
|
||||
}
|
||||
|
||||
#define FORWARD_5_R_C(CONV_R, FUNC_NAME, T_0, D_0, T_1, D_1, T_2, D_2, T_3, D_3, T_4, D_4, CONV_0, CONV_1, CONV_2, CONV_3, CONV_4) \
|
||||
GodotNavigationServer2D::FUNC_NAME(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3, T_4 D_4) \
|
||||
const { \
|
||||
return CONV_R(NavigationServer3D::get_singleton()->FUNC_NAME(CONV_0(D_0), CONV_1(D_1), CONV_2(D_2), CONV_3(D_3), CONV_4(D_4))); \
|
||||
}
|
||||
|
||||
static RID rid_to_rid(const RID d) {
|
||||
return d;
|
||||
}
|
||||
|
||||
static bool bool_to_bool(const bool d) {
|
||||
return d;
|
||||
}
|
||||
|
||||
static int int_to_int(const int d) {
|
||||
return d;
|
||||
}
|
||||
|
||||
static uint32_t uint32_to_uint32(const uint32_t d) {
|
||||
return d;
|
||||
}
|
||||
|
||||
static real_t real_to_real(const real_t d) {
|
||||
return d;
|
||||
}
|
||||
|
||||
static Vector3 v2_to_v3(const Vector2 d) {
|
||||
return Vector3(d.x, 0.0, d.y);
|
||||
}
|
||||
|
||||
static Vector2 v3_to_v2(const Vector3 &d) {
|
||||
return Vector2(d.x, d.z);
|
||||
}
|
||||
|
||||
static Vector<Vector3> vector_v2_to_v3(const Vector<Vector2> &d) {
|
||||
Vector<Vector3> nd;
|
||||
nd.resize(d.size());
|
||||
for (int i(0); i < nd.size(); i++) {
|
||||
nd.write[i] = v2_to_v3(d[i]);
|
||||
}
|
||||
return nd;
|
||||
}
|
||||
|
||||
static Vector<Vector2> vector_v3_to_v2(const Vector<Vector3> &d) {
|
||||
Vector<Vector2> nd;
|
||||
nd.resize(d.size());
|
||||
for (int i(0); i < nd.size(); i++) {
|
||||
nd.write[i] = v3_to_v2(d[i]);
|
||||
}
|
||||
return nd;
|
||||
}
|
||||
|
||||
static Transform3D trf2_to_trf3(const Transform2D &d) {
|
||||
Vector3 o(v2_to_v3(d.get_origin()));
|
||||
Basis b;
|
||||
b.rotate(Vector3(0, -1, 0), d.get_rotation());
|
||||
b.scale(v2_to_v3(d.get_scale()));
|
||||
return Transform3D(b, o);
|
||||
}
|
||||
|
||||
static Transform2D trf3_to_trf2(const Transform3D &d) {
|
||||
Vector3 o = d.get_origin();
|
||||
Vector3 nx = d.xform(Vector3(1, 0, 0)) - o;
|
||||
Vector3 nz = d.xform(Vector3(0, 0, 1)) - o;
|
||||
return Transform2D(nx.x, nx.z, nz.x, nz.z, o.x, o.z);
|
||||
}
|
||||
|
||||
static ObjectID id_to_id(const ObjectID &id) {
|
||||
return id;
|
||||
}
|
||||
|
||||
static Callable callable_to_callable(const Callable &c) {
|
||||
return c;
|
||||
}
|
||||
|
||||
static Ref<NavigationMesh> poly_to_mesh(Ref<NavigationPolygon> d) {
|
||||
if (d.is_valid()) {
|
||||
return d->get_navigation_mesh();
|
||||
} else {
|
||||
return Ref<NavigationMesh>();
|
||||
}
|
||||
}
|
||||
|
||||
void GodotNavigationServer2D::init() {
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
navmesh_generator_2d = memnew(NavMeshGenerator2D);
|
||||
ERR_FAIL_NULL_MSG(navmesh_generator_2d, "Failed to init NavMeshGenerator2D.");
|
||||
#endif // CLIPPER2_ENABLED
|
||||
}
|
||||
|
||||
void GodotNavigationServer2D::sync() {
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
if (navmesh_generator_2d) {
|
||||
navmesh_generator_2d->sync();
|
||||
}
|
||||
#endif // CLIPPER2_ENABLED
|
||||
}
|
||||
|
||||
void GodotNavigationServer2D::finish() {
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
if (navmesh_generator_2d) {
|
||||
navmesh_generator_2d->finish();
|
||||
memdelete(navmesh_generator_2d);
|
||||
navmesh_generator_2d = nullptr;
|
||||
}
|
||||
#endif // CLIPPER2_ENABLED
|
||||
}
|
||||
|
||||
void GodotNavigationServer2D::parse_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback) {
|
||||
ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "The SceneTree can only be parsed on the main thread. Call this function from the main thread or use call_deferred().");
|
||||
ERR_FAIL_COND_MSG(!p_navigation_mesh.is_valid(), "Invalid navigation polygon.");
|
||||
ERR_FAIL_NULL_MSG(p_root_node, "No parsing root node specified.");
|
||||
ERR_FAIL_COND_MSG(!p_root_node->is_inside_tree(), "The root node needs to be inside the SceneTree.");
|
||||
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
ERR_FAIL_NULL(NavMeshGenerator2D::get_singleton());
|
||||
NavMeshGenerator2D::get_singleton()->parse_source_geometry_data(p_navigation_mesh, p_source_geometry_data, p_root_node, p_callback);
|
||||
#endif // CLIPPER2_ENABLED
|
||||
}
|
||||
|
||||
void GodotNavigationServer2D::bake_from_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback) {
|
||||
ERR_FAIL_COND_MSG(!p_navigation_mesh.is_valid(), "Invalid navigation polygon.");
|
||||
ERR_FAIL_COND_MSG(!p_source_geometry_data.is_valid(), "Invalid NavigationMeshSourceGeometryData2D.");
|
||||
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
ERR_FAIL_NULL(NavMeshGenerator2D::get_singleton());
|
||||
NavMeshGenerator2D::get_singleton()->bake_from_source_geometry_data(p_navigation_mesh, p_source_geometry_data, p_callback);
|
||||
#endif // CLIPPER2_ENABLED
|
||||
}
|
||||
|
||||
void GodotNavigationServer2D::bake_from_source_geometry_data_async(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback) {
|
||||
ERR_FAIL_COND_MSG(!p_navigation_mesh.is_valid(), "Invalid navigation mesh.");
|
||||
ERR_FAIL_COND_MSG(!p_source_geometry_data.is_valid(), "Invalid NavigationMeshSourceGeometryData2D.");
|
||||
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
ERR_FAIL_NULL(NavMeshGenerator2D::get_singleton());
|
||||
NavMeshGenerator2D::get_singleton()->bake_from_source_geometry_data_async(p_navigation_mesh, p_source_geometry_data, p_callback);
|
||||
#endif // CLIPPER2_ENABLED
|
||||
}
|
||||
|
||||
bool GodotNavigationServer2D::is_baking_navigation_polygon(Ref<NavigationPolygon> p_navigation_polygon) const {
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
return NavMeshGenerator2D::get_singleton()->is_baking(p_navigation_polygon);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
Vector<Vector2> GodotNavigationServer2D::simplify_path(const Vector<Vector2> &p_path, real_t p_epsilon) {
|
||||
return vector_v3_to_v2(NavigationServer3D::get_singleton()->simplify_path(vector_v2_to_v3(p_path), p_epsilon));
|
||||
}
|
||||
|
||||
GodotNavigationServer2D::GodotNavigationServer2D() {}
|
||||
|
||||
GodotNavigationServer2D::~GodotNavigationServer2D() {}
|
||||
|
||||
TypedArray<RID> FORWARD_0_C(get_maps);
|
||||
|
||||
TypedArray<RID> FORWARD_1_C(map_get_links, RID, p_map, rid_to_rid);
|
||||
|
||||
TypedArray<RID> FORWARD_1_C(map_get_regions, RID, p_map, rid_to_rid);
|
||||
|
||||
TypedArray<RID> FORWARD_1_C(map_get_agents, RID, p_map, rid_to_rid);
|
||||
|
||||
TypedArray<RID> FORWARD_1_C(map_get_obstacles, RID, p_map, rid_to_rid);
|
||||
|
||||
RID FORWARD_1_C(region_get_map, RID, p_region, rid_to_rid);
|
||||
|
||||
RID FORWARD_1_C(agent_get_map, RID, p_agent, rid_to_rid);
|
||||
|
||||
RID FORWARD_0(map_create);
|
||||
|
||||
void FORWARD_2(map_set_active, RID, p_map, bool, p_active, rid_to_rid, bool_to_bool);
|
||||
|
||||
bool FORWARD_1_C(map_is_active, RID, p_map, rid_to_rid);
|
||||
|
||||
void GodotNavigationServer2D::map_force_update(RID p_map) {
|
||||
NavigationServer3D::get_singleton()->map_force_update(p_map);
|
||||
}
|
||||
|
||||
uint32_t GodotNavigationServer2D::map_get_iteration_id(RID p_map) const {
|
||||
return NavigationServer3D::get_singleton()->map_get_iteration_id(p_map);
|
||||
}
|
||||
|
||||
void FORWARD_2(map_set_cell_size, RID, p_map, real_t, p_cell_size, rid_to_rid, real_to_real);
|
||||
real_t FORWARD_1_C(map_get_cell_size, RID, p_map, rid_to_rid);
|
||||
|
||||
void FORWARD_2(map_set_use_edge_connections, RID, p_map, bool, p_enabled, rid_to_rid, bool_to_bool);
|
||||
bool FORWARD_1_C(map_get_use_edge_connections, RID, p_map, rid_to_rid);
|
||||
|
||||
void FORWARD_2(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin, rid_to_rid, real_to_real);
|
||||
real_t FORWARD_1_C(map_get_edge_connection_margin, RID, p_map, rid_to_rid);
|
||||
|
||||
void FORWARD_2(map_set_link_connection_radius, RID, p_map, real_t, p_connection_radius, rid_to_rid, real_to_real);
|
||||
real_t FORWARD_1_C(map_get_link_connection_radius, RID, p_map, rid_to_rid);
|
||||
|
||||
Vector<Vector2> FORWARD_5_R_C(vector_v3_to_v2, map_get_path, RID, p_map, Vector2, p_origin, Vector2, p_destination, bool, p_optimize, uint32_t, p_layers, rid_to_rid, v2_to_v3, v2_to_v3, bool_to_bool, uint32_to_uint32);
|
||||
|
||||
Vector2 FORWARD_2_R_C(v3_to_v2, map_get_closest_point, RID, p_map, const Vector2 &, p_point, rid_to_rid, v2_to_v3);
|
||||
RID FORWARD_2_C(map_get_closest_point_owner, RID, p_map, const Vector2 &, p_point, rid_to_rid, v2_to_v3);
|
||||
|
||||
Vector2 GodotNavigationServer2D::map_get_random_point(RID p_map, uint32_t p_naviation_layers, bool p_uniformly) const {
|
||||
Vector3 result = NavigationServer3D::get_singleton()->map_get_random_point(p_map, p_naviation_layers, p_uniformly);
|
||||
return v3_to_v2(result);
|
||||
}
|
||||
|
||||
RID FORWARD_0(region_create);
|
||||
void FORWARD_2(region_set_enabled, RID, p_region, bool, p_enabled, rid_to_rid, bool_to_bool);
|
||||
bool FORWARD_1_C(region_get_enabled, RID, p_region, rid_to_rid);
|
||||
void FORWARD_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled, rid_to_rid, bool_to_bool);
|
||||
bool FORWARD_1_C(region_get_use_edge_connections, RID, p_region, rid_to_rid);
|
||||
|
||||
void FORWARD_2(region_set_enter_cost, RID, p_region, real_t, p_enter_cost, rid_to_rid, real_to_real);
|
||||
real_t FORWARD_1_C(region_get_enter_cost, RID, p_region, rid_to_rid);
|
||||
void FORWARD_2(region_set_travel_cost, RID, p_region, real_t, p_travel_cost, rid_to_rid, real_to_real);
|
||||
real_t FORWARD_1_C(region_get_travel_cost, RID, p_region, rid_to_rid);
|
||||
void FORWARD_2(region_set_owner_id, RID, p_region, ObjectID, p_owner_id, rid_to_rid, id_to_id);
|
||||
ObjectID FORWARD_1_C(region_get_owner_id, RID, p_region, rid_to_rid);
|
||||
bool FORWARD_2_C(region_owns_point, RID, p_region, const Vector2 &, p_point, rid_to_rid, v2_to_v3);
|
||||
|
||||
void FORWARD_2(region_set_map, RID, p_region, RID, p_map, rid_to_rid, rid_to_rid);
|
||||
void FORWARD_2(region_set_navigation_layers, RID, p_region, uint32_t, p_navigation_layers, rid_to_rid, uint32_to_uint32);
|
||||
uint32_t FORWARD_1_C(region_get_navigation_layers, RID, p_region, rid_to_rid);
|
||||
void FORWARD_2(region_set_transform, RID, p_region, Transform2D, p_transform, rid_to_rid, trf2_to_trf3);
|
||||
|
||||
Transform2D GodotNavigationServer2D::region_get_transform(RID p_region) const {
|
||||
return trf3_to_trf2(NavigationServer3D::get_singleton()->region_get_transform(p_region));
|
||||
}
|
||||
|
||||
void GodotNavigationServer2D::region_set_navigation_polygon(RID p_region, Ref<NavigationPolygon> p_navigation_polygon) {
|
||||
NavigationServer3D::get_singleton()->region_set_navigation_mesh(p_region, poly_to_mesh(p_navigation_polygon));
|
||||
}
|
||||
|
||||
int FORWARD_1_C(region_get_connections_count, RID, p_region, rid_to_rid);
|
||||
Vector2 FORWARD_2_R_C(v3_to_v2, region_get_connection_pathway_start, RID, p_region, int, p_connection_id, rid_to_rid, int_to_int);
|
||||
Vector2 FORWARD_2_R_C(v3_to_v2, region_get_connection_pathway_end, RID, p_region, int, p_connection_id, rid_to_rid, int_to_int);
|
||||
|
||||
Vector2 GodotNavigationServer2D::region_get_random_point(RID p_region, uint32_t p_navigation_layers, bool p_uniformly) const {
|
||||
Vector3 result = NavigationServer3D::get_singleton()->region_get_random_point(p_region, p_navigation_layers, p_uniformly);
|
||||
return v3_to_v2(result);
|
||||
}
|
||||
|
||||
RID FORWARD_0(link_create);
|
||||
|
||||
void FORWARD_2(link_set_map, RID, p_link, RID, p_map, rid_to_rid, rid_to_rid);
|
||||
RID FORWARD_1_C(link_get_map, RID, p_link, rid_to_rid);
|
||||
void FORWARD_2(link_set_enabled, RID, p_link, bool, p_enabled, rid_to_rid, bool_to_bool);
|
||||
bool FORWARD_1_C(link_get_enabled, RID, p_link, rid_to_rid);
|
||||
void FORWARD_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional, rid_to_rid, bool_to_bool);
|
||||
bool FORWARD_1_C(link_is_bidirectional, RID, p_link, rid_to_rid);
|
||||
void FORWARD_2(link_set_navigation_layers, RID, p_link, uint32_t, p_navigation_layers, rid_to_rid, uint32_to_uint32);
|
||||
uint32_t FORWARD_1_C(link_get_navigation_layers, RID, p_link, rid_to_rid);
|
||||
void FORWARD_2(link_set_start_position, RID, p_link, Vector2, p_position, rid_to_rid, v2_to_v3);
|
||||
Vector2 FORWARD_1_R_C(v3_to_v2, link_get_start_position, RID, p_link, rid_to_rid);
|
||||
void FORWARD_2(link_set_end_position, RID, p_link, Vector2, p_position, rid_to_rid, v2_to_v3);
|
||||
Vector2 FORWARD_1_R_C(v3_to_v2, link_get_end_position, RID, p_link, rid_to_rid);
|
||||
void FORWARD_2(link_set_enter_cost, RID, p_link, real_t, p_enter_cost, rid_to_rid, real_to_real);
|
||||
real_t FORWARD_1_C(link_get_enter_cost, RID, p_link, rid_to_rid);
|
||||
void FORWARD_2(link_set_travel_cost, RID, p_link, real_t, p_travel_cost, rid_to_rid, real_to_real);
|
||||
real_t FORWARD_1_C(link_get_travel_cost, RID, p_link, rid_to_rid);
|
||||
void FORWARD_2(link_set_owner_id, RID, p_link, ObjectID, p_owner_id, rid_to_rid, id_to_id);
|
||||
ObjectID FORWARD_1_C(link_get_owner_id, RID, p_link, rid_to_rid);
|
||||
|
||||
RID GodotNavigationServer2D::agent_create() {
|
||||
RID agent = NavigationServer3D::get_singleton()->agent_create();
|
||||
return agent;
|
||||
}
|
||||
|
||||
void FORWARD_2(agent_set_avoidance_enabled, RID, p_agent, bool, p_enabled, rid_to_rid, bool_to_bool);
|
||||
bool FORWARD_1_C(agent_get_avoidance_enabled, RID, p_agent, rid_to_rid);
|
||||
void FORWARD_2(agent_set_map, RID, p_agent, RID, p_map, rid_to_rid, rid_to_rid);
|
||||
void FORWARD_2(agent_set_neighbor_distance, RID, p_agent, real_t, p_dist, rid_to_rid, real_to_real);
|
||||
real_t GodotNavigationServer2D::agent_get_neighbor_distance(RID p_agent) const {
|
||||
return NavigationServer3D::get_singleton()->agent_get_neighbor_distance(p_agent);
|
||||
}
|
||||
void FORWARD_2(agent_set_max_neighbors, RID, p_agent, int, p_count, rid_to_rid, int_to_int);
|
||||
int GodotNavigationServer2D::agent_get_max_neighbors(RID p_agent) const {
|
||||
return NavigationServer3D::get_singleton()->agent_get_max_neighbors(p_agent);
|
||||
}
|
||||
void FORWARD_2(agent_set_time_horizon_agents, RID, p_agent, real_t, p_time_horizon, rid_to_rid, real_to_real);
|
||||
real_t GodotNavigationServer2D::agent_get_time_horizon_agents(RID p_agent) const {
|
||||
return NavigationServer3D::get_singleton()->agent_get_time_horizon_agents(p_agent);
|
||||
}
|
||||
void FORWARD_2(agent_set_time_horizon_obstacles, RID, p_agent, real_t, p_time_horizon, rid_to_rid, real_to_real);
|
||||
real_t GodotNavigationServer2D::agent_get_time_horizon_obstacles(RID p_agent) const {
|
||||
return NavigationServer3D::get_singleton()->agent_get_time_horizon_obstacles(p_agent);
|
||||
}
|
||||
void FORWARD_2(agent_set_radius, RID, p_agent, real_t, p_radius, rid_to_rid, real_to_real);
|
||||
real_t GodotNavigationServer2D::agent_get_radius(RID p_agent) const {
|
||||
return NavigationServer3D::get_singleton()->agent_get_radius(p_agent);
|
||||
}
|
||||
void FORWARD_2(agent_set_max_speed, RID, p_agent, real_t, p_max_speed, rid_to_rid, real_to_real);
|
||||
real_t GodotNavigationServer2D::agent_get_max_speed(RID p_agent) const {
|
||||
return NavigationServer3D::get_singleton()->agent_get_max_speed(p_agent);
|
||||
}
|
||||
void FORWARD_2(agent_set_velocity_forced, RID, p_agent, Vector2, p_velocity, rid_to_rid, v2_to_v3);
|
||||
void FORWARD_2(agent_set_velocity, RID, p_agent, Vector2, p_velocity, rid_to_rid, v2_to_v3);
|
||||
Vector2 GodotNavigationServer2D::agent_get_velocity(RID p_agent) const {
|
||||
return v3_to_v2(NavigationServer3D::get_singleton()->agent_get_velocity(p_agent));
|
||||
}
|
||||
void FORWARD_2(agent_set_position, RID, p_agent, Vector2, p_position, rid_to_rid, v2_to_v3);
|
||||
Vector2 GodotNavigationServer2D::agent_get_position(RID p_agent) const {
|
||||
return v3_to_v2(NavigationServer3D::get_singleton()->agent_get_position(p_agent));
|
||||
}
|
||||
bool FORWARD_1_C(agent_is_map_changed, RID, p_agent, rid_to_rid);
|
||||
void FORWARD_2(agent_set_paused, RID, p_agent, bool, p_paused, rid_to_rid, bool_to_bool);
|
||||
bool FORWARD_1_C(agent_get_paused, RID, p_agent, rid_to_rid);
|
||||
|
||||
void GodotNavigationServer2D::free(RID p_object) {
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
if (navmesh_generator_2d && navmesh_generator_2d->owns(p_object)) {
|
||||
navmesh_generator_2d->free(p_object);
|
||||
return;
|
||||
}
|
||||
#endif // CLIPPER2_ENABLED
|
||||
NavigationServer3D::get_singleton()->free(p_object);
|
||||
}
|
||||
|
||||
void FORWARD_2(agent_set_avoidance_callback, RID, p_agent, Callable, p_callback, rid_to_rid, callable_to_callable);
|
||||
bool GodotNavigationServer2D::agent_has_avoidance_callback(RID p_agent) const {
|
||||
return NavigationServer3D::get_singleton()->agent_has_avoidance_callback(p_agent);
|
||||
}
|
||||
|
||||
void FORWARD_2(agent_set_avoidance_layers, RID, p_agent, uint32_t, p_layers, rid_to_rid, uint32_to_uint32);
|
||||
uint32_t GodotNavigationServer2D::agent_get_avoidance_layers(RID p_agent) const {
|
||||
return NavigationServer3D::get_singleton()->agent_get_avoidance_layers(p_agent);
|
||||
}
|
||||
void FORWARD_2(agent_set_avoidance_mask, RID, p_agent, uint32_t, p_mask, rid_to_rid, uint32_to_uint32);
|
||||
uint32_t GodotNavigationServer2D::agent_get_avoidance_mask(RID p_agent) const {
|
||||
return NavigationServer3D::get_singleton()->agent_get_avoidance_mask(p_agent);
|
||||
}
|
||||
void FORWARD_2(agent_set_avoidance_priority, RID, p_agent, real_t, p_priority, rid_to_rid, real_to_real);
|
||||
real_t GodotNavigationServer2D::agent_get_avoidance_priority(RID p_agent) const {
|
||||
return NavigationServer3D::get_singleton()->agent_get_avoidance_priority(p_agent);
|
||||
}
|
||||
|
||||
RID GodotNavigationServer2D::obstacle_create() {
|
||||
RID obstacle = NavigationServer3D::get_singleton()->obstacle_create();
|
||||
return obstacle;
|
||||
}
|
||||
void FORWARD_2(obstacle_set_avoidance_enabled, RID, p_obstacle, bool, p_enabled, rid_to_rid, bool_to_bool);
|
||||
bool FORWARD_1_C(obstacle_get_avoidance_enabled, RID, p_obstacle, rid_to_rid);
|
||||
void FORWARD_2(obstacle_set_map, RID, p_obstacle, RID, p_map, rid_to_rid, rid_to_rid);
|
||||
RID FORWARD_1_C(obstacle_get_map, RID, p_obstacle, rid_to_rid);
|
||||
void FORWARD_2(obstacle_set_paused, RID, p_obstacle, bool, p_paused, rid_to_rid, bool_to_bool);
|
||||
bool FORWARD_1_C(obstacle_get_paused, RID, p_obstacle, rid_to_rid);
|
||||
void FORWARD_2(obstacle_set_radius, RID, p_obstacle, real_t, p_radius, rid_to_rid, real_to_real);
|
||||
real_t GodotNavigationServer2D::obstacle_get_radius(RID p_obstacle) const {
|
||||
return NavigationServer3D::get_singleton()->obstacle_get_radius(p_obstacle);
|
||||
}
|
||||
void FORWARD_2(obstacle_set_velocity, RID, p_obstacle, Vector2, p_velocity, rid_to_rid, v2_to_v3);
|
||||
Vector2 GodotNavigationServer2D::obstacle_get_velocity(RID p_obstacle) const {
|
||||
return v3_to_v2(NavigationServer3D::get_singleton()->obstacle_get_velocity(p_obstacle));
|
||||
}
|
||||
void FORWARD_2(obstacle_set_position, RID, p_obstacle, Vector2, p_position, rid_to_rid, v2_to_v3);
|
||||
Vector2 GodotNavigationServer2D::obstacle_get_position(RID p_obstacle) const {
|
||||
return v3_to_v2(NavigationServer3D::get_singleton()->obstacle_get_position(p_obstacle));
|
||||
}
|
||||
void FORWARD_2(obstacle_set_avoidance_layers, RID, p_obstacle, uint32_t, p_layers, rid_to_rid, uint32_to_uint32);
|
||||
uint32_t GodotNavigationServer2D::obstacle_get_avoidance_layers(RID p_obstacle) const {
|
||||
return NavigationServer3D::get_singleton()->obstacle_get_avoidance_layers(p_obstacle);
|
||||
}
|
||||
|
||||
void GodotNavigationServer2D::obstacle_set_vertices(RID p_obstacle, const Vector<Vector2> &p_vertices) {
|
||||
NavigationServer3D::get_singleton()->obstacle_set_vertices(p_obstacle, vector_v2_to_v3(p_vertices));
|
||||
}
|
||||
Vector<Vector2> GodotNavigationServer2D::obstacle_get_vertices(RID p_obstacle) const {
|
||||
return vector_v3_to_v2(NavigationServer3D::get_singleton()->obstacle_get_vertices(p_obstacle));
|
||||
}
|
||||
|
||||
void GodotNavigationServer2D::query_path(const Ref<NavigationPathQueryParameters2D> &p_query_parameters, Ref<NavigationPathQueryResult2D> p_query_result) const {
|
||||
ERR_FAIL_COND(!p_query_parameters.is_valid());
|
||||
ERR_FAIL_COND(!p_query_result.is_valid());
|
||||
|
||||
const NavigationUtilities::PathQueryResult _query_result = NavigationServer3D::get_singleton()->_query_path(p_query_parameters->get_parameters());
|
||||
|
||||
p_query_result->set_path(vector_v3_to_v2(_query_result.path));
|
||||
p_query_result->set_path_types(_query_result.path_types);
|
||||
p_query_result->set_path_rids(_query_result.path_rids);
|
||||
p_query_result->set_path_owner_ids(_query_result.path_owner_ids);
|
||||
}
|
||||
|
||||
RID GodotNavigationServer2D::source_geometry_parser_create() {
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
if (navmesh_generator_2d) {
|
||||
return navmesh_generator_2d->source_geometry_parser_create();
|
||||
}
|
||||
#endif // CLIPPER2_ENABLED
|
||||
return RID();
|
||||
}
|
||||
|
||||
void GodotNavigationServer2D::source_geometry_parser_set_callback(RID p_parser, const Callable &p_callback) {
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
if (navmesh_generator_2d) {
|
||||
navmesh_generator_2d->source_geometry_parser_set_callback(p_parser, p_callback);
|
||||
}
|
||||
#endif // CLIPPER2_ENABLED
|
||||
}
|
||||
262
engine/modules/navigation/2d/godot_navigation_server_2d.h
Normal file
262
engine/modules/navigation/2d/godot_navigation_server_2d.h
Normal file
|
|
@ -0,0 +1,262 @@
|
|||
/**************************************************************************/
|
||||
/* godot_navigation_server_2d.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef GODOT_NAVIGATION_SERVER_2D_H
|
||||
#define GODOT_NAVIGATION_SERVER_2D_H
|
||||
|
||||
#include "../nav_agent.h"
|
||||
#include "../nav_link.h"
|
||||
#include "../nav_map.h"
|
||||
#include "../nav_obstacle.h"
|
||||
#include "../nav_region.h"
|
||||
|
||||
#include "servers/navigation_server_2d.h"
|
||||
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
class NavMeshGenerator2D;
|
||||
#endif // CLIPPER2_ENABLED
|
||||
|
||||
// This server exposes the `NavigationServer3D` features in the 2D world.
|
||||
class GodotNavigationServer2D : public NavigationServer2D {
|
||||
GDCLASS(GodotNavigationServer2D, NavigationServer2D);
|
||||
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
NavMeshGenerator2D *navmesh_generator_2d = nullptr;
|
||||
#endif // CLIPPER2_ENABLED
|
||||
|
||||
public:
|
||||
GodotNavigationServer2D();
|
||||
virtual ~GodotNavigationServer2D();
|
||||
|
||||
virtual TypedArray<RID> get_maps() const override;
|
||||
|
||||
virtual RID map_create() override;
|
||||
virtual void map_set_active(RID p_map, bool p_active) override;
|
||||
virtual bool map_is_active(RID p_map) const override;
|
||||
virtual void map_set_cell_size(RID p_map, real_t p_cell_size) override;
|
||||
virtual real_t map_get_cell_size(RID p_map) const override;
|
||||
virtual void map_set_use_edge_connections(RID p_map, bool p_enabled) override;
|
||||
virtual bool map_get_use_edge_connections(RID p_map) const override;
|
||||
virtual void map_set_edge_connection_margin(RID p_map, real_t p_connection_margin) override;
|
||||
virtual real_t map_get_edge_connection_margin(RID p_map) const override;
|
||||
virtual void map_set_link_connection_radius(RID p_map, real_t p_connection_radius) override;
|
||||
virtual real_t map_get_link_connection_radius(RID p_map) const override;
|
||||
virtual Vector<Vector2> map_get_path(RID p_map, Vector2 p_origin, Vector2 p_destination, bool p_optimize, uint32_t p_navigation_layers = 1) const override;
|
||||
virtual Vector2 map_get_closest_point(RID p_map, const Vector2 &p_point) const override;
|
||||
virtual RID map_get_closest_point_owner(RID p_map, const Vector2 &p_point) const override;
|
||||
virtual TypedArray<RID> map_get_links(RID p_map) const override;
|
||||
virtual TypedArray<RID> map_get_regions(RID p_map) const override;
|
||||
virtual TypedArray<RID> map_get_agents(RID p_map) const override;
|
||||
virtual TypedArray<RID> map_get_obstacles(RID p_map) const override;
|
||||
virtual void map_force_update(RID p_map) override;
|
||||
virtual Vector2 map_get_random_point(RID p_map, uint32_t p_navigation_layers, bool p_uniformly) const override;
|
||||
virtual uint32_t map_get_iteration_id(RID p_map) const override;
|
||||
|
||||
virtual RID region_create() override;
|
||||
virtual void region_set_enabled(RID p_region, bool p_enabled) override;
|
||||
virtual bool region_get_enabled(RID p_region) const override;
|
||||
virtual void region_set_use_edge_connections(RID p_region, bool p_enabled) override;
|
||||
virtual bool region_get_use_edge_connections(RID p_region) const override;
|
||||
virtual void region_set_enter_cost(RID p_region, real_t p_enter_cost) override;
|
||||
virtual real_t region_get_enter_cost(RID p_region) const override;
|
||||
virtual void region_set_travel_cost(RID p_region, real_t p_travel_cost) override;
|
||||
virtual real_t region_get_travel_cost(RID p_region) const override;
|
||||
virtual void region_set_owner_id(RID p_region, ObjectID p_owner_id) override;
|
||||
virtual ObjectID region_get_owner_id(RID p_region) const override;
|
||||
virtual bool region_owns_point(RID p_region, const Vector2 &p_point) const override;
|
||||
virtual void region_set_map(RID p_region, RID p_map) override;
|
||||
virtual RID region_get_map(RID p_region) const override;
|
||||
virtual void region_set_navigation_layers(RID p_region, uint32_t p_navigation_layers) override;
|
||||
virtual uint32_t region_get_navigation_layers(RID p_region) const override;
|
||||
virtual void region_set_transform(RID p_region, Transform2D p_transform) override;
|
||||
virtual Transform2D region_get_transform(RID p_region) const override;
|
||||
virtual void region_set_navigation_polygon(RID p_region, Ref<NavigationPolygon> p_navigation_polygon) override;
|
||||
virtual int region_get_connections_count(RID p_region) const override;
|
||||
virtual Vector2 region_get_connection_pathway_start(RID p_region, int p_connection_id) const override;
|
||||
virtual Vector2 region_get_connection_pathway_end(RID p_region, int p_connection_id) const override;
|
||||
virtual Vector2 region_get_random_point(RID p_region, uint32_t p_navigation_layers, bool p_uniformly) const override;
|
||||
|
||||
virtual RID link_create() override;
|
||||
|
||||
/// Set the map of this link.
|
||||
virtual void link_set_map(RID p_link, RID p_map) override;
|
||||
virtual RID link_get_map(RID p_link) const override;
|
||||
|
||||
virtual void link_set_enabled(RID p_link, bool p_enabled) override;
|
||||
virtual bool link_get_enabled(RID p_link) const override;
|
||||
|
||||
/// Set whether this link travels in both directions.
|
||||
virtual void link_set_bidirectional(RID p_link, bool p_bidirectional) override;
|
||||
virtual bool link_is_bidirectional(RID p_link) const override;
|
||||
|
||||
/// Set the link's layers.
|
||||
virtual void link_set_navigation_layers(RID p_link, uint32_t p_navigation_layers) override;
|
||||
virtual uint32_t link_get_navigation_layers(RID p_link) const override;
|
||||
|
||||
/// Set the start position of the link.
|
||||
virtual void link_set_start_position(RID p_link, Vector2 p_position) override;
|
||||
virtual Vector2 link_get_start_position(RID p_link) const override;
|
||||
|
||||
/// Set the end position of the link.
|
||||
virtual void link_set_end_position(RID p_link, Vector2 p_position) override;
|
||||
virtual Vector2 link_get_end_position(RID p_link) const override;
|
||||
|
||||
/// Set the enter cost of the link.
|
||||
virtual void link_set_enter_cost(RID p_link, real_t p_enter_cost) override;
|
||||
virtual real_t link_get_enter_cost(RID p_link) const override;
|
||||
|
||||
/// Set the travel cost of the link.
|
||||
virtual void link_set_travel_cost(RID p_link, real_t p_travel_cost) override;
|
||||
virtual real_t link_get_travel_cost(RID p_link) const override;
|
||||
|
||||
/// Set the node which manages this link.
|
||||
virtual void link_set_owner_id(RID p_link, ObjectID p_owner_id) override;
|
||||
virtual ObjectID link_get_owner_id(RID p_link) const override;
|
||||
|
||||
/// Creates the agent.
|
||||
virtual RID agent_create() override;
|
||||
|
||||
/// Put the agent in the map.
|
||||
virtual void agent_set_map(RID p_agent, RID p_map) override;
|
||||
virtual RID agent_get_map(RID p_agent) const override;
|
||||
|
||||
virtual void agent_set_paused(RID p_agent, bool p_paused) override;
|
||||
virtual bool agent_get_paused(RID p_agent) const override;
|
||||
|
||||
virtual void agent_set_avoidance_enabled(RID p_agent, bool p_enabled) override;
|
||||
virtual bool agent_get_avoidance_enabled(RID p_agent) const override;
|
||||
|
||||
/// The maximum distance (center point to
|
||||
/// center point) to other agents this agent
|
||||
/// takes into account in the navigation. The
|
||||
/// larger this number, the longer the running
|
||||
/// time of the simulation. If the number is too
|
||||
/// low, the simulation will not be safe.
|
||||
/// Must be non-negative.
|
||||
virtual void agent_set_neighbor_distance(RID p_agent, real_t p_distance) override;
|
||||
virtual real_t agent_get_neighbor_distance(RID p_agent) const override;
|
||||
|
||||
/// The maximum number of other agents this
|
||||
/// agent takes into account in the navigation.
|
||||
/// The larger this number, the longer the
|
||||
/// running time of the simulation. If the
|
||||
/// number is too low, the simulation will not
|
||||
/// be safe.
|
||||
virtual void agent_set_max_neighbors(RID p_agent, int p_count) override;
|
||||
virtual int agent_get_max_neighbors(RID p_agent) const override;
|
||||
|
||||
/// The minimal amount of time for which this
|
||||
/// agent's velocities that are computed by the
|
||||
/// simulation are safe with respect to other
|
||||
/// agents. The larger this number, the sooner
|
||||
/// this agent will respond to the presence of
|
||||
/// other agents, but the less freedom this
|
||||
/// agent has in choosing its velocities.
|
||||
/// Must be positive.
|
||||
virtual void agent_set_time_horizon_agents(RID p_agent, real_t p_time_horizon) override;
|
||||
virtual real_t agent_get_time_horizon_agents(RID p_agent) const override;
|
||||
virtual void agent_set_time_horizon_obstacles(RID p_agent, real_t p_time_horizon) override;
|
||||
virtual real_t agent_get_time_horizon_obstacles(RID p_agent) const override;
|
||||
|
||||
/// The radius of this agent.
|
||||
/// Must be non-negative.
|
||||
virtual void agent_set_radius(RID p_agent, real_t p_radius) override;
|
||||
virtual real_t agent_get_radius(RID p_agent) const override;
|
||||
|
||||
/// The maximum speed of this agent.
|
||||
/// Must be non-negative.
|
||||
virtual void agent_set_max_speed(RID p_agent, real_t p_max_speed) override;
|
||||
virtual real_t agent_get_max_speed(RID p_agent) const override;
|
||||
|
||||
/// forces and agent velocity change in the avoidance simulation, adds simulation instability if done recklessly
|
||||
virtual void agent_set_velocity_forced(RID p_agent, Vector2 p_velocity) override;
|
||||
|
||||
/// The wanted velocity for the agent as a "suggestion" to the avoidance simulation.
|
||||
/// The simulation will try to fulfill this velocity wish if possible but may change the velocity depending on other agent's and obstacles'.
|
||||
virtual void agent_set_velocity(RID p_agent, Vector2 p_velocity) override;
|
||||
virtual Vector2 agent_get_velocity(RID p_agent) const override;
|
||||
|
||||
/// Position of the agent in world space.
|
||||
virtual void agent_set_position(RID p_agent, Vector2 p_position) override;
|
||||
virtual Vector2 agent_get_position(RID p_agent) const override;
|
||||
|
||||
/// Returns true if the map got changed the previous frame.
|
||||
virtual bool agent_is_map_changed(RID p_agent) const override;
|
||||
|
||||
/// Callback called at the end of the RVO process
|
||||
virtual void agent_set_avoidance_callback(RID p_agent, Callable p_callback) override;
|
||||
virtual bool agent_has_avoidance_callback(RID p_agent) const override;
|
||||
|
||||
virtual void agent_set_avoidance_layers(RID p_agent, uint32_t p_layers) override;
|
||||
virtual uint32_t agent_get_avoidance_layers(RID p_agent) const override;
|
||||
|
||||
virtual void agent_set_avoidance_mask(RID p_agent, uint32_t p_mask) override;
|
||||
virtual uint32_t agent_get_avoidance_mask(RID p_agent) const override;
|
||||
|
||||
virtual void agent_set_avoidance_priority(RID p_agent, real_t p_priority) override;
|
||||
virtual real_t agent_get_avoidance_priority(RID p_agent) const override;
|
||||
|
||||
virtual RID obstacle_create() override;
|
||||
virtual void obstacle_set_avoidance_enabled(RID p_obstacle, bool p_enabled) override;
|
||||
virtual bool obstacle_get_avoidance_enabled(RID p_obstacle) const override;
|
||||
virtual void obstacle_set_map(RID p_obstacle, RID p_map) override;
|
||||
virtual RID obstacle_get_map(RID p_obstacle) const override;
|
||||
virtual void obstacle_set_paused(RID p_obstacle, bool p_paused) override;
|
||||
virtual bool obstacle_get_paused(RID p_obstacle) const override;
|
||||
virtual void obstacle_set_radius(RID p_obstacle, real_t p_radius) override;
|
||||
virtual real_t obstacle_get_radius(RID p_obstacle) const override;
|
||||
virtual void obstacle_set_velocity(RID p_obstacle, Vector2 p_velocity) override;
|
||||
virtual Vector2 obstacle_get_velocity(RID p_obstacle) const override;
|
||||
virtual void obstacle_set_position(RID p_obstacle, Vector2 p_position) override;
|
||||
virtual Vector2 obstacle_get_position(RID p_obstacle) const override;
|
||||
virtual void obstacle_set_vertices(RID p_obstacle, const Vector<Vector2> &p_vertices) override;
|
||||
virtual Vector<Vector2> obstacle_get_vertices(RID p_obstacle) const override;
|
||||
virtual void obstacle_set_avoidance_layers(RID p_obstacle, uint32_t p_layers) override;
|
||||
virtual uint32_t obstacle_get_avoidance_layers(RID p_obstacle) const override;
|
||||
|
||||
virtual void query_path(const Ref<NavigationPathQueryParameters2D> &p_query_parameters, Ref<NavigationPathQueryResult2D> p_query_result) const override;
|
||||
|
||||
virtual void init() override;
|
||||
virtual void sync() override;
|
||||
virtual void finish() override;
|
||||
virtual void free(RID p_object) override;
|
||||
|
||||
virtual void parse_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable()) override;
|
||||
virtual void bake_from_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
|
||||
virtual void bake_from_source_geometry_data_async(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
|
||||
virtual bool is_baking_navigation_polygon(Ref<NavigationPolygon> p_navigation_polygon) const override;
|
||||
|
||||
virtual RID source_geometry_parser_create() override;
|
||||
virtual void source_geometry_parser_set_callback(RID p_parser, const Callable &p_callback) override;
|
||||
|
||||
virtual Vector<Vector2> simplify_path(const Vector<Vector2> &p_path, real_t p_epsilon) override;
|
||||
};
|
||||
|
||||
#endif // GODOT_NAVIGATION_SERVER_2D_H
|
||||
1075
engine/modules/navigation/2d/nav_mesh_generator_2d.cpp
Normal file
1075
engine/modules/navigation/2d/nav_mesh_generator_2d.cpp
Normal file
File diff suppressed because it is too large
Load diff
121
engine/modules/navigation/2d/nav_mesh_generator_2d.h
Normal file
121
engine/modules/navigation/2d/nav_mesh_generator_2d.h
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
/**************************************************************************/
|
||||
/* nav_mesh_generator_2d.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef NAV_MESH_GENERATOR_2D_H
|
||||
#define NAV_MESH_GENERATOR_2D_H
|
||||
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
|
||||
#include "core/object/class_db.h"
|
||||
#include "core/object/worker_thread_pool.h"
|
||||
#include "core/templates/rid_owner.h"
|
||||
|
||||
class Node;
|
||||
class NavigationPolygon;
|
||||
class NavigationMeshSourceGeometryData2D;
|
||||
|
||||
class NavMeshGenerator2D : public Object {
|
||||
static NavMeshGenerator2D *singleton;
|
||||
|
||||
static Mutex baking_navmesh_mutex;
|
||||
static Mutex generator_task_mutex;
|
||||
|
||||
static RWLock generator_rid_rwlock;
|
||||
struct NavMeshGeometryParser2D {
|
||||
RID self;
|
||||
Callable callback;
|
||||
};
|
||||
static RID_Owner<NavMeshGeometryParser2D> generator_parser_owner;
|
||||
static LocalVector<NavMeshGeometryParser2D *> generator_parsers;
|
||||
|
||||
static bool use_threads;
|
||||
static bool baking_use_multiple_threads;
|
||||
static bool baking_use_high_priority_threads;
|
||||
|
||||
struct NavMeshGeneratorTask2D {
|
||||
enum TaskStatus {
|
||||
BAKING_STARTED,
|
||||
BAKING_FINISHED,
|
||||
BAKING_FAILED,
|
||||
CALLBACK_DISPATCHED,
|
||||
CALLBACK_FAILED,
|
||||
};
|
||||
|
||||
Ref<NavigationPolygon> navigation_mesh;
|
||||
Ref<NavigationMeshSourceGeometryData2D> source_geometry_data;
|
||||
Callable callback;
|
||||
WorkerThreadPool::TaskID thread_task_id = WorkerThreadPool::INVALID_TASK_ID;
|
||||
NavMeshGeneratorTask2D::TaskStatus status = NavMeshGeneratorTask2D::TaskStatus::BAKING_STARTED;
|
||||
};
|
||||
|
||||
static HashMap<WorkerThreadPool::TaskID, NavMeshGeneratorTask2D *> generator_tasks;
|
||||
|
||||
static void generator_thread_bake(void *p_arg);
|
||||
|
||||
static HashSet<Ref<NavigationPolygon>> baking_navmeshes;
|
||||
|
||||
static void generator_parse_geometry_node(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node, bool p_recurse_children);
|
||||
static void generator_parse_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_root_node);
|
||||
static void generator_bake_from_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data);
|
||||
|
||||
static void generator_parse_meshinstance2d_node(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node);
|
||||
static void generator_parse_multimeshinstance2d_node(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node);
|
||||
static void generator_parse_polygon2d_node(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node);
|
||||
static void generator_parse_staticbody2d_node(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node);
|
||||
static void generator_parse_tile_map_layer_node(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node);
|
||||
static void generator_parse_navigationobstacle_node(const Ref<NavigationPolygon> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node);
|
||||
|
||||
static bool generator_emit_callback(const Callable &p_callback);
|
||||
|
||||
public:
|
||||
static NavMeshGenerator2D *get_singleton();
|
||||
|
||||
static void sync();
|
||||
static void cleanup();
|
||||
static void finish();
|
||||
|
||||
static void parse_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable());
|
||||
static void bake_from_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, const Callable &p_callback = Callable());
|
||||
static void bake_from_source_geometry_data_async(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, const Callable &p_callback = Callable());
|
||||
static bool is_baking(Ref<NavigationPolygon> p_navigation_polygon);
|
||||
|
||||
static RID source_geometry_parser_create();
|
||||
static void source_geometry_parser_set_callback(RID p_parser, const Callable &p_callback);
|
||||
|
||||
static bool owns(RID p_object);
|
||||
static void free(RID p_object);
|
||||
|
||||
NavMeshGenerator2D();
|
||||
~NavMeshGenerator2D();
|
||||
};
|
||||
|
||||
#endif // CLIPPER2_ENABLED
|
||||
|
||||
#endif // NAV_MESH_GENERATOR_2D_H
|
||||
1575
engine/modules/navigation/3d/godot_navigation_server_3d.cpp
Normal file
1575
engine/modules/navigation/3d/godot_navigation_server_3d.cpp
Normal file
File diff suppressed because it is too large
Load diff
299
engine/modules/navigation/3d/godot_navigation_server_3d.h
Normal file
299
engine/modules/navigation/3d/godot_navigation_server_3d.h
Normal file
|
|
@ -0,0 +1,299 @@
|
|||
/**************************************************************************/
|
||||
/* godot_navigation_server_3d.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef GODOT_NAVIGATION_SERVER_3D_H
|
||||
#define GODOT_NAVIGATION_SERVER_3D_H
|
||||
|
||||
#include "../nav_agent.h"
|
||||
#include "../nav_link.h"
|
||||
#include "../nav_map.h"
|
||||
#include "../nav_obstacle.h"
|
||||
#include "../nav_region.h"
|
||||
|
||||
#include "core/templates/local_vector.h"
|
||||
#include "core/templates/rid.h"
|
||||
#include "core/templates/rid_owner.h"
|
||||
#include "servers/navigation_server_3d.h"
|
||||
|
||||
/// The commands are functions executed during the `sync` phase.
|
||||
|
||||
#define MERGE_INTERNAL(A, B) A##B
|
||||
#define MERGE(A, B) MERGE_INTERNAL(A, B)
|
||||
|
||||
#define COMMAND_1(F_NAME, T_0, D_0) \
|
||||
virtual void F_NAME(T_0 D_0) override; \
|
||||
void MERGE(_cmd_, F_NAME)(T_0 D_0)
|
||||
|
||||
#define COMMAND_2(F_NAME, T_0, D_0, T_1, D_1) \
|
||||
virtual void F_NAME(T_0 D_0, T_1 D_1) override; \
|
||||
void MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1)
|
||||
|
||||
class GodotNavigationServer3D;
|
||||
#ifndef _3D_DISABLED
|
||||
class NavMeshGenerator3D;
|
||||
#endif // _3D_DISABLED
|
||||
|
||||
struct SetCommand {
|
||||
virtual ~SetCommand() {}
|
||||
virtual void exec(GodotNavigationServer3D *server) = 0;
|
||||
};
|
||||
|
||||
class GodotNavigationServer3D : public NavigationServer3D {
|
||||
Mutex commands_mutex;
|
||||
/// Mutex used to make any operation threadsafe.
|
||||
Mutex operations_mutex;
|
||||
|
||||
LocalVector<SetCommand *> commands;
|
||||
|
||||
mutable RID_Owner<NavLink> link_owner;
|
||||
mutable RID_Owner<NavMap> map_owner;
|
||||
mutable RID_Owner<NavRegion> region_owner;
|
||||
mutable RID_Owner<NavAgent> agent_owner;
|
||||
mutable RID_Owner<NavObstacle> obstacle_owner;
|
||||
|
||||
bool active = true;
|
||||
LocalVector<NavMap *> active_maps;
|
||||
LocalVector<uint32_t> active_maps_iteration_id;
|
||||
|
||||
#ifndef _3D_DISABLED
|
||||
NavMeshGenerator3D *navmesh_generator_3d = nullptr;
|
||||
#endif // _3D_DISABLED
|
||||
|
||||
// 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;
|
||||
|
||||
public:
|
||||
GodotNavigationServer3D();
|
||||
virtual ~GodotNavigationServer3D();
|
||||
|
||||
void add_command(SetCommand *command);
|
||||
|
||||
virtual TypedArray<RID> get_maps() const override;
|
||||
|
||||
virtual RID map_create() override;
|
||||
COMMAND_2(map_set_active, RID, p_map, bool, p_active);
|
||||
virtual bool map_is_active(RID p_map) const override;
|
||||
|
||||
COMMAND_2(map_set_up, RID, p_map, Vector3, p_up);
|
||||
virtual Vector3 map_get_up(RID p_map) const override;
|
||||
|
||||
COMMAND_2(map_set_cell_size, RID, p_map, real_t, p_cell_size);
|
||||
virtual real_t map_get_cell_size(RID p_map) const override;
|
||||
|
||||
COMMAND_2(map_set_cell_height, RID, p_map, real_t, p_cell_height);
|
||||
virtual real_t map_get_cell_height(RID p_map) const override;
|
||||
|
||||
COMMAND_2(map_set_merge_rasterizer_cell_scale, RID, p_map, float, p_value);
|
||||
virtual float map_get_merge_rasterizer_cell_scale(RID p_map) const override;
|
||||
|
||||
COMMAND_2(map_set_use_edge_connections, RID, p_map, bool, p_enabled);
|
||||
virtual bool map_get_use_edge_connections(RID p_map) const override;
|
||||
|
||||
COMMAND_2(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin);
|
||||
virtual real_t map_get_edge_connection_margin(RID p_map) const override;
|
||||
|
||||
COMMAND_2(map_set_link_connection_radius, RID, p_map, real_t, p_connection_radius);
|
||||
virtual real_t map_get_link_connection_radius(RID p_map) const override;
|
||||
|
||||
virtual Vector<Vector3> map_get_path(RID p_map, Vector3 p_origin, Vector3 p_destination, bool p_optimize, uint32_t p_navigation_layers = 1) const override;
|
||||
|
||||
virtual Vector3 map_get_closest_point_to_segment(RID p_map, const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision = false) const override;
|
||||
virtual Vector3 map_get_closest_point(RID p_map, const Vector3 &p_point) const override;
|
||||
virtual Vector3 map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const override;
|
||||
virtual RID map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const override;
|
||||
|
||||
virtual TypedArray<RID> map_get_links(RID p_map) const override;
|
||||
virtual TypedArray<RID> map_get_regions(RID p_map) const override;
|
||||
virtual TypedArray<RID> map_get_agents(RID p_map) const override;
|
||||
virtual TypedArray<RID> map_get_obstacles(RID p_map) const override;
|
||||
|
||||
virtual void map_force_update(RID p_map) override;
|
||||
virtual uint32_t map_get_iteration_id(RID p_map) const override;
|
||||
|
||||
virtual Vector3 map_get_random_point(RID p_map, uint32_t p_navigation_layers, bool p_uniformly) const override;
|
||||
|
||||
virtual RID region_create() override;
|
||||
|
||||
COMMAND_2(region_set_enabled, RID, p_region, bool, p_enabled);
|
||||
virtual bool region_get_enabled(RID p_region) const override;
|
||||
|
||||
COMMAND_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled);
|
||||
virtual bool region_get_use_edge_connections(RID p_region) const override;
|
||||
|
||||
COMMAND_2(region_set_enter_cost, RID, p_region, real_t, p_enter_cost);
|
||||
virtual real_t region_get_enter_cost(RID p_region) const override;
|
||||
COMMAND_2(region_set_travel_cost, RID, p_region, real_t, p_travel_cost);
|
||||
virtual real_t region_get_travel_cost(RID p_region) const override;
|
||||
|
||||
COMMAND_2(region_set_owner_id, RID, p_region, ObjectID, p_owner_id);
|
||||
virtual ObjectID region_get_owner_id(RID p_region) const override;
|
||||
|
||||
virtual bool region_owns_point(RID p_region, const Vector3 &p_point) const override;
|
||||
|
||||
COMMAND_2(region_set_map, RID, p_region, RID, p_map);
|
||||
virtual RID region_get_map(RID p_region) const override;
|
||||
COMMAND_2(region_set_navigation_layers, RID, p_region, uint32_t, p_navigation_layers);
|
||||
virtual uint32_t region_get_navigation_layers(RID p_region) const override;
|
||||
COMMAND_2(region_set_transform, RID, p_region, Transform3D, p_transform);
|
||||
virtual Transform3D region_get_transform(RID p_region) const override;
|
||||
COMMAND_2(region_set_navigation_mesh, RID, p_region, Ref<NavigationMesh>, p_navigation_mesh);
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
virtual void region_bake_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh, Node *p_root_node) override;
|
||||
#endif // DISABLE_DEPRECATED
|
||||
virtual int region_get_connections_count(RID p_region) const override;
|
||||
virtual Vector3 region_get_connection_pathway_start(RID p_region, int p_connection_id) const override;
|
||||
virtual Vector3 region_get_connection_pathway_end(RID p_region, int p_connection_id) const override;
|
||||
virtual Vector3 region_get_random_point(RID p_region, uint32_t p_navigation_layers, bool p_uniformly) const override;
|
||||
|
||||
virtual RID link_create() override;
|
||||
COMMAND_2(link_set_map, RID, p_link, RID, p_map);
|
||||
virtual RID link_get_map(RID p_link) const override;
|
||||
COMMAND_2(link_set_enabled, RID, p_link, bool, p_enabled);
|
||||
virtual bool link_get_enabled(RID p_link) const override;
|
||||
COMMAND_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional);
|
||||
virtual bool link_is_bidirectional(RID p_link) const override;
|
||||
COMMAND_2(link_set_navigation_layers, RID, p_link, uint32_t, p_navigation_layers);
|
||||
virtual uint32_t link_get_navigation_layers(RID p_link) const override;
|
||||
COMMAND_2(link_set_start_position, RID, p_link, Vector3, p_position);
|
||||
virtual Vector3 link_get_start_position(RID p_link) const override;
|
||||
COMMAND_2(link_set_end_position, RID, p_link, Vector3, p_position);
|
||||
virtual Vector3 link_get_end_position(RID p_link) const override;
|
||||
COMMAND_2(link_set_enter_cost, RID, p_link, real_t, p_enter_cost);
|
||||
virtual real_t link_get_enter_cost(RID p_link) const override;
|
||||
COMMAND_2(link_set_travel_cost, RID, p_link, real_t, p_travel_cost);
|
||||
virtual real_t link_get_travel_cost(RID p_link) const override;
|
||||
COMMAND_2(link_set_owner_id, RID, p_link, ObjectID, p_owner_id);
|
||||
virtual ObjectID link_get_owner_id(RID p_link) const override;
|
||||
|
||||
virtual RID agent_create() override;
|
||||
COMMAND_2(agent_set_avoidance_enabled, RID, p_agent, bool, p_enabled);
|
||||
virtual bool agent_get_avoidance_enabled(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_use_3d_avoidance, RID, p_agent, bool, p_enabled);
|
||||
virtual bool agent_get_use_3d_avoidance(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_map, RID, p_agent, RID, p_map);
|
||||
virtual RID agent_get_map(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_paused, RID, p_agent, bool, p_paused);
|
||||
virtual bool agent_get_paused(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_neighbor_distance, RID, p_agent, real_t, p_distance);
|
||||
virtual real_t agent_get_neighbor_distance(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_max_neighbors, RID, p_agent, int, p_count);
|
||||
virtual int agent_get_max_neighbors(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_time_horizon_agents, RID, p_agent, real_t, p_time_horizon);
|
||||
virtual real_t agent_get_time_horizon_agents(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_time_horizon_obstacles, RID, p_agent, real_t, p_time_horizon);
|
||||
virtual real_t agent_get_time_horizon_obstacles(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_radius, RID, p_agent, real_t, p_radius);
|
||||
virtual real_t agent_get_radius(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_height, RID, p_agent, real_t, p_height);
|
||||
virtual real_t agent_get_height(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_max_speed, RID, p_agent, real_t, p_max_speed);
|
||||
virtual real_t agent_get_max_speed(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_velocity, RID, p_agent, Vector3, p_velocity);
|
||||
virtual Vector3 agent_get_velocity(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_velocity_forced, RID, p_agent, Vector3, p_velocity);
|
||||
COMMAND_2(agent_set_position, RID, p_agent, Vector3, p_position);
|
||||
virtual Vector3 agent_get_position(RID p_agent) const override;
|
||||
virtual bool agent_is_map_changed(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_avoidance_callback, RID, p_agent, Callable, p_callback);
|
||||
virtual bool agent_has_avoidance_callback(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_avoidance_layers, RID, p_agent, uint32_t, p_layers);
|
||||
virtual uint32_t agent_get_avoidance_layers(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_avoidance_mask, RID, p_agent, uint32_t, p_mask);
|
||||
virtual uint32_t agent_get_avoidance_mask(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_avoidance_priority, RID, p_agent, real_t, p_priority);
|
||||
virtual real_t agent_get_avoidance_priority(RID p_agent) const override;
|
||||
|
||||
virtual RID obstacle_create() override;
|
||||
COMMAND_2(obstacle_set_avoidance_enabled, RID, p_obstacle, bool, p_enabled);
|
||||
virtual bool obstacle_get_avoidance_enabled(RID p_obstacle) const override;
|
||||
COMMAND_2(obstacle_set_use_3d_avoidance, RID, p_obstacle, bool, p_enabled);
|
||||
virtual bool obstacle_get_use_3d_avoidance(RID p_obstacle) const override;
|
||||
COMMAND_2(obstacle_set_map, RID, p_obstacle, RID, p_map);
|
||||
virtual RID obstacle_get_map(RID p_obstacle) const override;
|
||||
COMMAND_2(obstacle_set_paused, RID, p_obstacle, bool, p_paused);
|
||||
virtual bool obstacle_get_paused(RID p_obstacle) const override;
|
||||
COMMAND_2(obstacle_set_radius, RID, p_obstacle, real_t, p_radius);
|
||||
virtual real_t obstacle_get_radius(RID p_obstacle) const override;
|
||||
COMMAND_2(obstacle_set_velocity, RID, p_obstacle, Vector3, p_velocity);
|
||||
virtual Vector3 obstacle_get_velocity(RID p_obstacle) const override;
|
||||
COMMAND_2(obstacle_set_position, RID, p_obstacle, Vector3, p_position);
|
||||
virtual Vector3 obstacle_get_position(RID p_obstacle) const override;
|
||||
COMMAND_2(obstacle_set_height, RID, p_obstacle, real_t, p_height);
|
||||
virtual real_t obstacle_get_height(RID p_obstacle) const override;
|
||||
virtual void obstacle_set_vertices(RID p_obstacle, const Vector<Vector3> &p_vertices) override;
|
||||
virtual Vector<Vector3> obstacle_get_vertices(RID p_obstacle) const override;
|
||||
COMMAND_2(obstacle_set_avoidance_layers, RID, p_obstacle, uint32_t, p_layers);
|
||||
virtual uint32_t obstacle_get_avoidance_layers(RID p_obstacle) const override;
|
||||
|
||||
virtual void parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable()) override;
|
||||
virtual void bake_from_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
|
||||
virtual void bake_from_source_geometry_data_async(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
|
||||
virtual bool is_baking_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh) const override;
|
||||
|
||||
virtual RID source_geometry_parser_create() override;
|
||||
virtual void source_geometry_parser_set_callback(RID p_parser, const Callable &p_callback) override;
|
||||
|
||||
virtual Vector<Vector3> simplify_path(const Vector<Vector3> &p_path, real_t p_epsilon) override;
|
||||
|
||||
private:
|
||||
static void simplify_path_segment(int p_start_inx, int p_end_inx, const Vector<Vector3> &p_points, real_t p_epsilon, LocalVector<bool> &r_valid_points);
|
||||
static LocalVector<uint32_t> get_simplified_path_indices(const Vector<Vector3> &p_path, real_t p_epsilon);
|
||||
|
||||
public:
|
||||
COMMAND_1(free, RID, p_object);
|
||||
|
||||
virtual void set_active(bool p_active) override;
|
||||
|
||||
void flush_queries();
|
||||
virtual void process(real_t p_delta_time) override;
|
||||
virtual void init() override;
|
||||
virtual void sync() override;
|
||||
virtual void finish() override;
|
||||
|
||||
virtual NavigationUtilities::PathQueryResult _query_path(const NavigationUtilities::PathQueryParameters &p_parameters) const override;
|
||||
|
||||
int get_process_info(ProcessInfo p_info) const override;
|
||||
|
||||
private:
|
||||
void internal_free_agent(RID p_object);
|
||||
void internal_free_obstacle(RID p_object);
|
||||
};
|
||||
|
||||
#undef COMMAND_1
|
||||
#undef COMMAND_2
|
||||
|
||||
#endif // GODOT_NAVIGATION_SERVER_3D_H
|
||||
1006
engine/modules/navigation/3d/nav_mesh_generator_3d.cpp
Normal file
1006
engine/modules/navigation/3d/nav_mesh_generator_3d.cpp
Normal file
File diff suppressed because it is too large
Load diff
126
engine/modules/navigation/3d/nav_mesh_generator_3d.h
Normal file
126
engine/modules/navigation/3d/nav_mesh_generator_3d.h
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
/**************************************************************************/
|
||||
/* nav_mesh_generator_3d.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef NAV_MESH_GENERATOR_3D_H
|
||||
#define NAV_MESH_GENERATOR_3D_H
|
||||
|
||||
#ifndef _3D_DISABLED
|
||||
|
||||
#include "core/object/class_db.h"
|
||||
#include "core/object/worker_thread_pool.h"
|
||||
#include "core/templates/rid_owner.h"
|
||||
#include "modules/modules_enabled.gen.h" // For csg, gridmap.
|
||||
|
||||
class Node;
|
||||
class NavigationMesh;
|
||||
class NavigationMeshSourceGeometryData3D;
|
||||
|
||||
class NavMeshGenerator3D : public Object {
|
||||
static NavMeshGenerator3D *singleton;
|
||||
|
||||
static Mutex baking_navmesh_mutex;
|
||||
static Mutex generator_task_mutex;
|
||||
|
||||
static RWLock generator_rid_rwlock;
|
||||
struct NavMeshGeometryParser3D {
|
||||
RID self;
|
||||
Callable callback;
|
||||
};
|
||||
static RID_Owner<NavMeshGeometryParser3D> generator_parser_owner;
|
||||
static LocalVector<NavMeshGeometryParser3D *> generator_parsers;
|
||||
|
||||
static bool use_threads;
|
||||
static bool baking_use_multiple_threads;
|
||||
static bool baking_use_high_priority_threads;
|
||||
|
||||
struct NavMeshGeneratorTask3D {
|
||||
enum TaskStatus {
|
||||
BAKING_STARTED,
|
||||
BAKING_FINISHED,
|
||||
BAKING_FAILED,
|
||||
CALLBACK_DISPATCHED,
|
||||
CALLBACK_FAILED,
|
||||
};
|
||||
|
||||
Ref<NavigationMesh> navigation_mesh;
|
||||
Ref<NavigationMeshSourceGeometryData3D> source_geometry_data;
|
||||
Callable callback;
|
||||
WorkerThreadPool::TaskID thread_task_id = WorkerThreadPool::INVALID_TASK_ID;
|
||||
NavMeshGeneratorTask3D::TaskStatus status = NavMeshGeneratorTask3D::TaskStatus::BAKING_STARTED;
|
||||
};
|
||||
|
||||
static HashMap<WorkerThreadPool::TaskID, NavMeshGeneratorTask3D *> generator_tasks;
|
||||
|
||||
static void generator_thread_bake(void *p_arg);
|
||||
|
||||
static HashSet<Ref<NavigationMesh>> baking_navmeshes;
|
||||
|
||||
static void generator_parse_geometry_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node, bool p_recurse_children);
|
||||
static void generator_parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_root_node);
|
||||
static void generator_bake_from_source_geometry_data(Ref<NavigationMesh> p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data);
|
||||
|
||||
static void generator_parse_meshinstance3d_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
|
||||
static void generator_parse_multimeshinstance3d_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
|
||||
static void generator_parse_staticbody3d_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
|
||||
#ifdef MODULE_CSG_ENABLED
|
||||
static void generator_parse_csgshape3d_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
|
||||
#endif // MODULE_CSG_ENABLED
|
||||
#ifdef MODULE_GRIDMAP_ENABLED
|
||||
static void generator_parse_gridmap_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
|
||||
#endif // MODULE_GRIDMAP_ENABLED
|
||||
static void generator_parse_navigationobstacle_node(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_node);
|
||||
|
||||
static bool generator_emit_callback(const Callable &p_callback);
|
||||
|
||||
public:
|
||||
static NavMeshGenerator3D *get_singleton();
|
||||
|
||||
static void sync();
|
||||
static void cleanup();
|
||||
static void finish();
|
||||
|
||||
static void parse_source_geometry_data(Ref<NavigationMesh> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable());
|
||||
static void bake_from_source_geometry_data(Ref<NavigationMesh> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, const Callable &p_callback = Callable());
|
||||
static void bake_from_source_geometry_data_async(Ref<NavigationMesh> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, const Callable &p_callback = Callable());
|
||||
static bool is_baking(Ref<NavigationMesh> p_navigation_mesh);
|
||||
|
||||
static RID source_geometry_parser_create();
|
||||
static void source_geometry_parser_set_callback(RID p_parser, const Callable &p_callback);
|
||||
|
||||
static bool owns(RID p_object);
|
||||
static void free(RID p_object);
|
||||
|
||||
NavMeshGenerator3D();
|
||||
~NavMeshGenerator3D();
|
||||
};
|
||||
|
||||
#endif // _3D_DISABLED
|
||||
|
||||
#endif // NAV_MESH_GENERATOR_3D_H
|
||||
78
engine/modules/navigation/3d/navigation_mesh_generator.cpp
Normal file
78
engine/modules/navigation/3d/navigation_mesh_generator.cpp
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
/**************************************************************************/
|
||||
/* navigation_mesh_generator.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef _3D_DISABLED
|
||||
|
||||
#include "navigation_mesh_generator.h"
|
||||
|
||||
#include "scene/resources/3d/navigation_mesh_source_geometry_data_3d.h"
|
||||
#include "servers/navigation_server_3d.h"
|
||||
|
||||
NavigationMeshGenerator *NavigationMeshGenerator::singleton = nullptr;
|
||||
|
||||
NavigationMeshGenerator *NavigationMeshGenerator::get_singleton() {
|
||||
return singleton;
|
||||
}
|
||||
|
||||
NavigationMeshGenerator::NavigationMeshGenerator() {
|
||||
singleton = this;
|
||||
}
|
||||
|
||||
NavigationMeshGenerator::~NavigationMeshGenerator() {
|
||||
}
|
||||
|
||||
void NavigationMeshGenerator::bake(const Ref<NavigationMesh> &p_navigation_mesh, Node *p_root_node) {
|
||||
WARN_PRINT_ONCE("NavigationMeshGenerator::bake() is deprecated due to core threading changes. To upgrade existing code, first create a NavigationMeshSourceGeometryData3D resource. Use this resource with method parse_source_geometry_data() to parse the SceneTree for nodes that should contribute to the navigation mesh baking. The SceneTree parsing needs to happen on the main thread. After the parsing is finished use the resource with method bake_from_source_geometry_data() to bake a navigation mesh..");
|
||||
}
|
||||
|
||||
void NavigationMeshGenerator::clear(Ref<NavigationMesh> p_navigation_mesh) {
|
||||
if (p_navigation_mesh.is_valid()) {
|
||||
p_navigation_mesh->clear_polygons();
|
||||
p_navigation_mesh->set_vertices(Vector<Vector3>());
|
||||
}
|
||||
}
|
||||
|
||||
void NavigationMeshGenerator::parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_root_node, const Callable &p_callback) {
|
||||
NavigationServer3D::get_singleton()->parse_source_geometry_data(p_navigation_mesh, p_source_geometry_data, p_root_node, p_callback);
|
||||
}
|
||||
|
||||
void NavigationMeshGenerator::bake_from_source_geometry_data(Ref<NavigationMesh> p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback) {
|
||||
NavigationServer3D::get_singleton()->bake_from_source_geometry_data(p_navigation_mesh, p_source_geometry_data, p_callback);
|
||||
}
|
||||
|
||||
void NavigationMeshGenerator::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("bake", "navigation_mesh", "root_node"), &NavigationMeshGenerator::bake);
|
||||
ClassDB::bind_method(D_METHOD("clear", "navigation_mesh"), &NavigationMeshGenerator::clear);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("parse_source_geometry_data", "navigation_mesh", "source_geometry_data", "root_node", "callback"), &NavigationMeshGenerator::parse_source_geometry_data, DEFVAL(Callable()));
|
||||
ClassDB::bind_method(D_METHOD("bake_from_source_geometry_data", "navigation_mesh", "source_geometry_data", "callback"), &NavigationMeshGenerator::bake_from_source_geometry_data, DEFVAL(Callable()));
|
||||
}
|
||||
|
||||
#endif
|
||||
64
engine/modules/navigation/3d/navigation_mesh_generator.h
Normal file
64
engine/modules/navigation/3d/navigation_mesh_generator.h
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
/**************************************************************************/
|
||||
/* navigation_mesh_generator.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef NAVIGATION_MESH_GENERATOR_H
|
||||
#define NAVIGATION_MESH_GENERATOR_H
|
||||
|
||||
#ifndef _3D_DISABLED
|
||||
|
||||
#include "scene/3d/navigation_region_3d.h"
|
||||
#include "scene/resources/navigation_mesh.h"
|
||||
|
||||
class NavigationMeshSourceGeometryData3D;
|
||||
|
||||
class NavigationMeshGenerator : public Object {
|
||||
GDCLASS(NavigationMeshGenerator, Object);
|
||||
|
||||
static NavigationMeshGenerator *singleton;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
static NavigationMeshGenerator *get_singleton();
|
||||
|
||||
NavigationMeshGenerator();
|
||||
~NavigationMeshGenerator();
|
||||
|
||||
void bake(const Ref<NavigationMesh> &p_navigation_mesh, Node *p_root_node);
|
||||
void clear(Ref<NavigationMesh> p_navigation_mesh);
|
||||
|
||||
void parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, Ref<NavigationMeshSourceGeometryData3D> p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable());
|
||||
void bake_from_source_geometry_data(Ref<NavigationMesh> p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback = Callable());
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif // NAVIGATION_MESH_GENERATOR_H
|
||||
85
engine/modules/navigation/SCsub
Normal file
85
engine/modules/navigation/SCsub
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
Import("env")
|
||||
Import("env_modules")
|
||||
|
||||
env_navigation = env_modules.Clone()
|
||||
|
||||
# Thirdparty source files
|
||||
|
||||
thirdparty_obj = []
|
||||
|
||||
# Recast Thirdparty source files
|
||||
if env["builtin_recastnavigation"]:
|
||||
thirdparty_dir = "#thirdparty/recastnavigation/Recast/"
|
||||
thirdparty_sources = [
|
||||
"Source/Recast.cpp",
|
||||
"Source/RecastAlloc.cpp",
|
||||
"Source/RecastArea.cpp",
|
||||
"Source/RecastAssert.cpp",
|
||||
"Source/RecastContour.cpp",
|
||||
"Source/RecastFilter.cpp",
|
||||
"Source/RecastLayers.cpp",
|
||||
"Source/RecastMesh.cpp",
|
||||
"Source/RecastMeshDetail.cpp",
|
||||
"Source/RecastRasterization.cpp",
|
||||
"Source/RecastRegion.cpp",
|
||||
]
|
||||
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
|
||||
|
||||
env_navigation.Prepend(CPPPATH=[thirdparty_dir + "Include"])
|
||||
|
||||
env_thirdparty = env_navigation.Clone()
|
||||
env_thirdparty.disable_warnings()
|
||||
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_sources)
|
||||
|
||||
# RVO 2D Thirdparty source files
|
||||
if env["builtin_rvo2_2d"]:
|
||||
thirdparty_dir = "#thirdparty/rvo2/rvo2_2d/"
|
||||
thirdparty_sources = [
|
||||
"Agent2d.cpp",
|
||||
"Obstacle2d.cpp",
|
||||
"KdTree2d.cpp",
|
||||
"RVOSimulator2d.cpp",
|
||||
]
|
||||
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
|
||||
|
||||
env_navigation.Prepend(CPPPATH=[thirdparty_dir])
|
||||
|
||||
env_thirdparty = env_navigation.Clone()
|
||||
env_thirdparty.disable_warnings()
|
||||
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_sources)
|
||||
|
||||
# RVO 3D Thirdparty source files
|
||||
if env["builtin_rvo2_3d"]:
|
||||
thirdparty_dir = "#thirdparty/rvo2/rvo2_3d/"
|
||||
thirdparty_sources = [
|
||||
"Agent3d.cpp",
|
||||
"KdTree3d.cpp",
|
||||
"RVOSimulator3d.cpp",
|
||||
]
|
||||
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
|
||||
|
||||
env_navigation.Prepend(CPPPATH=[thirdparty_dir])
|
||||
|
||||
env_thirdparty = env_navigation.Clone()
|
||||
env_thirdparty.disable_warnings()
|
||||
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_sources)
|
||||
|
||||
|
||||
env.modules_sources += thirdparty_obj
|
||||
|
||||
# Godot source files
|
||||
|
||||
module_obj = []
|
||||
|
||||
env_navigation.add_source_files(module_obj, "*.cpp")
|
||||
env_navigation.add_source_files(module_obj, "2d/*.cpp")
|
||||
if not env["disable_3d"]:
|
||||
env_navigation.add_source_files(module_obj, "3d/*.cpp")
|
||||
if env.editor_build:
|
||||
env_navigation.add_source_files(module_obj, "editor/*.cpp")
|
||||
env.modules_sources += module_obj
|
||||
|
||||
# Needed to force rebuilding the module files when the thirdparty library is updated.
|
||||
env.Depends(module_obj, thirdparty_obj)
|
||||
6
engine/modules/navigation/config.py
Normal file
6
engine/modules/navigation/config.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def can_build(env, platform):
|
||||
return not env["disable_3d"]
|
||||
|
||||
|
||||
def configure(env):
|
||||
pass
|
||||
|
|
@ -0,0 +1,191 @@
|
|||
/**************************************************************************/
|
||||
/* navigation_mesh_editor_plugin.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "navigation_mesh_editor_plugin.h"
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
|
||||
#include "core/io/marshalls.h"
|
||||
#include "core/io/resource_saver.h"
|
||||
#include "editor/editor_node.h"
|
||||
#include "editor/editor_string_names.h"
|
||||
#include "scene/3d/mesh_instance_3d.h"
|
||||
#include "scene/3d/navigation_region_3d.h"
|
||||
#include "scene/gui/box_container.h"
|
||||
#include "scene/gui/button.h"
|
||||
#include "scene/gui/dialogs.h"
|
||||
#include "scene/gui/label.h"
|
||||
|
||||
void NavigationMeshEditor::_node_removed(Node *p_node) {
|
||||
if (p_node == node) {
|
||||
node = nullptr;
|
||||
|
||||
hide();
|
||||
}
|
||||
}
|
||||
|
||||
void NavigationMeshEditor::_notification(int p_what) {
|
||||
switch (p_what) {
|
||||
case NOTIFICATION_ENTER_TREE: {
|
||||
button_bake->set_icon(get_theme_icon(SNAME("Bake"), EditorStringName(EditorIcons)));
|
||||
button_reset->set_icon(get_theme_icon(SNAME("Reload"), EditorStringName(EditorIcons)));
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
void NavigationMeshEditor::_bake_pressed() {
|
||||
button_bake->set_pressed(false);
|
||||
|
||||
ERR_FAIL_NULL(node);
|
||||
Ref<NavigationMesh> navmesh = node->get_navigation_mesh();
|
||||
if (!navmesh.is_valid()) {
|
||||
err_dialog->set_text(TTR("A NavigationMesh resource must be set or created for this node to work."));
|
||||
err_dialog->popup_centered();
|
||||
return;
|
||||
}
|
||||
|
||||
String path = navmesh->get_path();
|
||||
if (!path.is_resource_file()) {
|
||||
int srpos = path.find("::");
|
||||
if (srpos != -1) {
|
||||
String base = path.substr(0, srpos);
|
||||
if (ResourceLoader::get_resource_type(base) == "PackedScene") {
|
||||
if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
|
||||
err_dialog->set_text(TTR("Cannot generate navigation mesh because it does not belong to the edited scene. Make it unique first."));
|
||||
err_dialog->popup_centered();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (FileAccess::exists(base + ".import")) {
|
||||
err_dialog->set_text(TTR("Cannot generate navigation mesh because it belongs to a resource which was imported."));
|
||||
err_dialog->popup_centered();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (FileAccess::exists(path + ".import")) {
|
||||
err_dialog->set_text(TTR("Cannot generate navigation mesh because the resource was imported from another type."));
|
||||
err_dialog->popup_centered();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
node->bake_navigation_mesh(true);
|
||||
|
||||
node->update_gizmos();
|
||||
}
|
||||
|
||||
void NavigationMeshEditor::_clear_pressed() {
|
||||
if (node) {
|
||||
if (node->get_navigation_mesh().is_valid()) {
|
||||
node->get_navigation_mesh()->clear();
|
||||
}
|
||||
}
|
||||
|
||||
button_bake->set_pressed(false);
|
||||
bake_info->set_text("");
|
||||
|
||||
if (node) {
|
||||
node->update_gizmos();
|
||||
}
|
||||
}
|
||||
|
||||
void NavigationMeshEditor::edit(NavigationRegion3D *p_nav_region) {
|
||||
if (p_nav_region == nullptr || node == p_nav_region) {
|
||||
return;
|
||||
}
|
||||
|
||||
node = p_nav_region;
|
||||
}
|
||||
|
||||
void NavigationMeshEditor::_bind_methods() {
|
||||
}
|
||||
|
||||
NavigationMeshEditor::NavigationMeshEditor() {
|
||||
bake_hbox = memnew(HBoxContainer);
|
||||
|
||||
button_bake = memnew(Button);
|
||||
button_bake->set_theme_type_variation("FlatButton");
|
||||
bake_hbox->add_child(button_bake);
|
||||
button_bake->set_toggle_mode(true);
|
||||
button_bake->set_text(TTR("Bake NavigationMesh"));
|
||||
button_bake->set_tooltip_text(TTR("Bakes the NavigationMesh by first parsing the scene for source geometry and then creating the navigation mesh vertices and polygons."));
|
||||
button_bake->connect(SceneStringName(pressed), callable_mp(this, &NavigationMeshEditor::_bake_pressed));
|
||||
|
||||
button_reset = memnew(Button);
|
||||
button_reset->set_theme_type_variation("FlatButton");
|
||||
bake_hbox->add_child(button_reset);
|
||||
button_reset->set_text(TTR("Clear NavigationMesh"));
|
||||
button_reset->set_tooltip_text(TTR("Clears the internal NavigationMesh vertices and polygons."));
|
||||
button_reset->connect(SceneStringName(pressed), callable_mp(this, &NavigationMeshEditor::_clear_pressed));
|
||||
|
||||
bake_info = memnew(Label);
|
||||
bake_hbox->add_child(bake_info);
|
||||
|
||||
err_dialog = memnew(AcceptDialog);
|
||||
add_child(err_dialog);
|
||||
node = nullptr;
|
||||
}
|
||||
|
||||
NavigationMeshEditor::~NavigationMeshEditor() {
|
||||
}
|
||||
|
||||
void NavigationMeshEditorPlugin::edit(Object *p_object) {
|
||||
navigation_mesh_editor->edit(Object::cast_to<NavigationRegion3D>(p_object));
|
||||
}
|
||||
|
||||
bool NavigationMeshEditorPlugin::handles(Object *p_object) const {
|
||||
return p_object->is_class("NavigationRegion3D");
|
||||
}
|
||||
|
||||
void NavigationMeshEditorPlugin::make_visible(bool p_visible) {
|
||||
if (p_visible) {
|
||||
navigation_mesh_editor->show();
|
||||
navigation_mesh_editor->bake_hbox->show();
|
||||
} else {
|
||||
navigation_mesh_editor->hide();
|
||||
navigation_mesh_editor->bake_hbox->hide();
|
||||
navigation_mesh_editor->edit(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
NavigationMeshEditorPlugin::NavigationMeshEditorPlugin() {
|
||||
navigation_mesh_editor = memnew(NavigationMeshEditor);
|
||||
EditorNode::get_singleton()->get_main_screen_control()->add_child(navigation_mesh_editor);
|
||||
add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, navigation_mesh_editor->bake_hbox);
|
||||
navigation_mesh_editor->hide();
|
||||
navigation_mesh_editor->bake_hbox->hide();
|
||||
}
|
||||
|
||||
NavigationMeshEditorPlugin::~NavigationMeshEditorPlugin() {
|
||||
}
|
||||
|
||||
#endif // TOOLS_ENABLED
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
/**************************************************************************/
|
||||
/* navigation_mesh_editor_plugin.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef NAVIGATION_MESH_EDITOR_PLUGIN_H
|
||||
#define NAVIGATION_MESH_EDITOR_PLUGIN_H
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
|
||||
#include "editor/plugins/editor_plugin.h"
|
||||
|
||||
class AcceptDialog;
|
||||
class Button;
|
||||
class HBoxContainer;
|
||||
class Label;
|
||||
class NavigationRegion3D;
|
||||
|
||||
class NavigationMeshEditor : public Control {
|
||||
friend class NavigationMeshEditorPlugin;
|
||||
|
||||
GDCLASS(NavigationMeshEditor, Control);
|
||||
|
||||
AcceptDialog *err_dialog = nullptr;
|
||||
|
||||
HBoxContainer *bake_hbox = nullptr;
|
||||
Button *button_bake = nullptr;
|
||||
Button *button_reset = nullptr;
|
||||
Label *bake_info = nullptr;
|
||||
|
||||
NavigationRegion3D *node = nullptr;
|
||||
|
||||
void _bake_pressed();
|
||||
void _clear_pressed();
|
||||
|
||||
protected:
|
||||
void _node_removed(Node *p_node);
|
||||
static void _bind_methods();
|
||||
void _notification(int p_what);
|
||||
|
||||
public:
|
||||
void edit(NavigationRegion3D *p_nav_region);
|
||||
NavigationMeshEditor();
|
||||
~NavigationMeshEditor();
|
||||
};
|
||||
|
||||
class NavigationMeshEditorPlugin : public EditorPlugin {
|
||||
GDCLASS(NavigationMeshEditorPlugin, EditorPlugin);
|
||||
|
||||
NavigationMeshEditor *navigation_mesh_editor = nullptr;
|
||||
|
||||
public:
|
||||
virtual String get_name() const override { return "NavigationMesh"; }
|
||||
bool has_main_screen() const override { return false; }
|
||||
virtual void edit(Object *p_object) override;
|
||||
virtual bool handles(Object *p_object) const override;
|
||||
virtual void make_visible(bool p_visible) override;
|
||||
|
||||
NavigationMeshEditorPlugin();
|
||||
~NavigationMeshEditorPlugin();
|
||||
};
|
||||
|
||||
#endif // TOOLS_ENABLED
|
||||
|
||||
#endif // NAVIGATION_MESH_EDITOR_PLUGIN_H
|
||||
374
engine/modules/navigation/nav_agent.cpp
Normal file
374
engine/modules/navigation/nav_agent.cpp
Normal file
|
|
@ -0,0 +1,374 @@
|
|||
/**************************************************************************/
|
||||
/* nav_agent.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "nav_agent.h"
|
||||
|
||||
#include "nav_map.h"
|
||||
|
||||
NavAgent::NavAgent() {
|
||||
}
|
||||
|
||||
void NavAgent::set_avoidance_enabled(bool p_enabled) {
|
||||
avoidance_enabled = p_enabled;
|
||||
_update_rvo_agent_properties();
|
||||
}
|
||||
|
||||
void NavAgent::set_use_3d_avoidance(bool p_enabled) {
|
||||
use_3d_avoidance = p_enabled;
|
||||
_update_rvo_agent_properties();
|
||||
}
|
||||
|
||||
void NavAgent::_update_rvo_agent_properties() {
|
||||
if (use_3d_avoidance) {
|
||||
rvo_agent_3d.neighborDist_ = neighbor_distance;
|
||||
rvo_agent_3d.maxNeighbors_ = max_neighbors;
|
||||
rvo_agent_3d.timeHorizon_ = time_horizon_agents;
|
||||
rvo_agent_3d.timeHorizonObst_ = time_horizon_obstacles;
|
||||
rvo_agent_3d.radius_ = radius;
|
||||
rvo_agent_3d.maxSpeed_ = max_speed;
|
||||
rvo_agent_3d.position_ = RVO3D::Vector3(position.x, position.y, position.z);
|
||||
// Replacing the internal velocity directly causes major jitter / bugs due to unpredictable velocity jumps, left line here for testing.
|
||||
//rvo_agent_3d.velocity_ = RVO3D::Vector3(velocity.x, velocity.y ,velocity.z);
|
||||
rvo_agent_3d.prefVelocity_ = RVO3D::Vector3(velocity.x, velocity.y, velocity.z);
|
||||
rvo_agent_3d.height_ = height;
|
||||
rvo_agent_3d.avoidance_layers_ = avoidance_layers;
|
||||
rvo_agent_3d.avoidance_mask_ = avoidance_mask;
|
||||
rvo_agent_3d.avoidance_priority_ = avoidance_priority;
|
||||
} else {
|
||||
rvo_agent_2d.neighborDist_ = neighbor_distance;
|
||||
rvo_agent_2d.maxNeighbors_ = max_neighbors;
|
||||
rvo_agent_2d.timeHorizon_ = time_horizon_agents;
|
||||
rvo_agent_2d.timeHorizonObst_ = time_horizon_obstacles;
|
||||
rvo_agent_2d.radius_ = radius;
|
||||
rvo_agent_2d.maxSpeed_ = max_speed;
|
||||
rvo_agent_2d.position_ = RVO2D::Vector2(position.x, position.z);
|
||||
rvo_agent_2d.elevation_ = position.y;
|
||||
// Replacing the internal velocity directly causes major jitter / bugs due to unpredictable velocity jumps, left line here for testing.
|
||||
//rvo_agent_2d.velocity_ = RVO2D::Vector2(velocity.x, velocity.z);
|
||||
rvo_agent_2d.prefVelocity_ = RVO2D::Vector2(velocity.x, velocity.z);
|
||||
rvo_agent_2d.height_ = height;
|
||||
rvo_agent_2d.avoidance_layers_ = avoidance_layers;
|
||||
rvo_agent_2d.avoidance_mask_ = avoidance_mask;
|
||||
rvo_agent_2d.avoidance_priority_ = avoidance_priority;
|
||||
}
|
||||
|
||||
if (map != nullptr) {
|
||||
if (avoidance_enabled) {
|
||||
map->set_agent_as_controlled(this);
|
||||
} else {
|
||||
map->remove_agent_as_controlled(this);
|
||||
}
|
||||
}
|
||||
agent_dirty = true;
|
||||
}
|
||||
|
||||
void NavAgent::set_map(NavMap *p_map) {
|
||||
if (map == p_map) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (map) {
|
||||
map->remove_agent(this);
|
||||
}
|
||||
|
||||
map = p_map;
|
||||
agent_dirty = true;
|
||||
|
||||
if (map) {
|
||||
map->add_agent(this);
|
||||
if (avoidance_enabled) {
|
||||
map->set_agent_as_controlled(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool NavAgent::is_map_changed() {
|
||||
if (map) {
|
||||
bool is_changed = map->get_iteration_id() != last_map_iteration_id;
|
||||
last_map_iteration_id = map->get_iteration_id();
|
||||
return is_changed;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void NavAgent::set_avoidance_callback(Callable p_callback) {
|
||||
avoidance_callback = p_callback;
|
||||
}
|
||||
|
||||
bool NavAgent::has_avoidance_callback() const {
|
||||
return avoidance_callback.is_valid();
|
||||
}
|
||||
|
||||
void NavAgent::dispatch_avoidance_callback() {
|
||||
if (!avoidance_callback.is_valid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3 new_velocity;
|
||||
|
||||
if (use_3d_avoidance) {
|
||||
new_velocity = Vector3(rvo_agent_3d.velocity_.x(), rvo_agent_3d.velocity_.y(), rvo_agent_3d.velocity_.z());
|
||||
} else {
|
||||
new_velocity = Vector3(rvo_agent_2d.velocity_.x(), 0.0, rvo_agent_2d.velocity_.y());
|
||||
}
|
||||
|
||||
if (clamp_speed) {
|
||||
new_velocity = new_velocity.limit_length(max_speed);
|
||||
}
|
||||
|
||||
// Invoke the callback with the new velocity.
|
||||
avoidance_callback.call(new_velocity);
|
||||
}
|
||||
|
||||
void NavAgent::set_neighbor_distance(real_t p_neighbor_distance) {
|
||||
neighbor_distance = p_neighbor_distance;
|
||||
if (use_3d_avoidance) {
|
||||
rvo_agent_3d.neighborDist_ = neighbor_distance;
|
||||
} else {
|
||||
rvo_agent_2d.neighborDist_ = neighbor_distance;
|
||||
}
|
||||
agent_dirty = true;
|
||||
}
|
||||
|
||||
void NavAgent::set_max_neighbors(int p_max_neighbors) {
|
||||
max_neighbors = p_max_neighbors;
|
||||
if (use_3d_avoidance) {
|
||||
rvo_agent_3d.maxNeighbors_ = max_neighbors;
|
||||
} else {
|
||||
rvo_agent_2d.maxNeighbors_ = max_neighbors;
|
||||
}
|
||||
agent_dirty = true;
|
||||
}
|
||||
|
||||
void NavAgent::set_time_horizon_agents(real_t p_time_horizon) {
|
||||
time_horizon_agents = p_time_horizon;
|
||||
if (use_3d_avoidance) {
|
||||
rvo_agent_3d.timeHorizon_ = time_horizon_agents;
|
||||
} else {
|
||||
rvo_agent_2d.timeHorizon_ = time_horizon_agents;
|
||||
}
|
||||
agent_dirty = true;
|
||||
}
|
||||
|
||||
void NavAgent::set_time_horizon_obstacles(real_t p_time_horizon) {
|
||||
time_horizon_obstacles = p_time_horizon;
|
||||
if (use_3d_avoidance) {
|
||||
rvo_agent_3d.timeHorizonObst_ = time_horizon_obstacles;
|
||||
} else {
|
||||
rvo_agent_2d.timeHorizonObst_ = time_horizon_obstacles;
|
||||
}
|
||||
agent_dirty = true;
|
||||
}
|
||||
|
||||
void NavAgent::set_radius(real_t p_radius) {
|
||||
radius = p_radius;
|
||||
if (use_3d_avoidance) {
|
||||
rvo_agent_3d.radius_ = radius;
|
||||
} else {
|
||||
rvo_agent_2d.radius_ = radius;
|
||||
}
|
||||
agent_dirty = true;
|
||||
}
|
||||
|
||||
void NavAgent::set_height(real_t p_height) {
|
||||
height = p_height;
|
||||
if (use_3d_avoidance) {
|
||||
rvo_agent_3d.height_ = height;
|
||||
} else {
|
||||
rvo_agent_2d.height_ = height;
|
||||
}
|
||||
agent_dirty = true;
|
||||
}
|
||||
|
||||
void NavAgent::set_max_speed(real_t p_max_speed) {
|
||||
max_speed = p_max_speed;
|
||||
if (avoidance_enabled) {
|
||||
if (use_3d_avoidance) {
|
||||
rvo_agent_3d.maxSpeed_ = max_speed;
|
||||
} else {
|
||||
rvo_agent_2d.maxSpeed_ = max_speed;
|
||||
}
|
||||
}
|
||||
agent_dirty = true;
|
||||
}
|
||||
|
||||
void NavAgent::set_position(const Vector3 p_position) {
|
||||
position = p_position;
|
||||
if (avoidance_enabled) {
|
||||
if (use_3d_avoidance) {
|
||||
rvo_agent_3d.position_ = RVO3D::Vector3(p_position.x, p_position.y, p_position.z);
|
||||
} else {
|
||||
rvo_agent_2d.elevation_ = p_position.y;
|
||||
rvo_agent_2d.position_ = RVO2D::Vector2(p_position.x, p_position.z);
|
||||
}
|
||||
}
|
||||
agent_dirty = true;
|
||||
}
|
||||
|
||||
void NavAgent::set_target_position(const Vector3 p_target_position) {
|
||||
target_position = p_target_position;
|
||||
}
|
||||
|
||||
void NavAgent::set_velocity(const Vector3 p_velocity) {
|
||||
// Sets the "wanted" velocity for an agent as a suggestion
|
||||
// This velocity is not guaranteed, RVO simulation will only try to fulfill it
|
||||
velocity = p_velocity;
|
||||
if (avoidance_enabled) {
|
||||
if (use_3d_avoidance) {
|
||||
rvo_agent_3d.prefVelocity_ = RVO3D::Vector3(velocity.x, velocity.y, velocity.z);
|
||||
} else {
|
||||
rvo_agent_2d.prefVelocity_ = RVO2D::Vector2(velocity.x, velocity.z);
|
||||
}
|
||||
}
|
||||
agent_dirty = true;
|
||||
}
|
||||
|
||||
void NavAgent::set_velocity_forced(const Vector3 p_velocity) {
|
||||
// This function replaces the internal rvo simulation velocity
|
||||
// should only be used after the agent was teleported
|
||||
// as it destroys consistency in movement in cramped situations
|
||||
// use velocity instead to update with a safer "suggestion"
|
||||
velocity_forced = p_velocity;
|
||||
if (avoidance_enabled) {
|
||||
if (use_3d_avoidance) {
|
||||
rvo_agent_3d.velocity_ = RVO3D::Vector3(p_velocity.x, p_velocity.y, p_velocity.z);
|
||||
} else {
|
||||
rvo_agent_2d.velocity_ = RVO2D::Vector2(p_velocity.x, p_velocity.z);
|
||||
}
|
||||
}
|
||||
agent_dirty = true;
|
||||
}
|
||||
|
||||
void NavAgent::update() {
|
||||
// Updates this agent with the calculated results from the rvo simulation
|
||||
if (avoidance_enabled) {
|
||||
if (use_3d_avoidance) {
|
||||
velocity = Vector3(rvo_agent_3d.velocity_.x(), rvo_agent_3d.velocity_.y(), rvo_agent_3d.velocity_.z());
|
||||
} else {
|
||||
velocity = Vector3(rvo_agent_2d.velocity_.x(), 0.0, rvo_agent_2d.velocity_.y());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NavAgent::set_avoidance_mask(uint32_t p_mask) {
|
||||
avoidance_mask = p_mask;
|
||||
if (use_3d_avoidance) {
|
||||
rvo_agent_3d.avoidance_mask_ = avoidance_mask;
|
||||
} else {
|
||||
rvo_agent_2d.avoidance_mask_ = avoidance_mask;
|
||||
}
|
||||
agent_dirty = true;
|
||||
}
|
||||
|
||||
void NavAgent::set_avoidance_layers(uint32_t p_layers) {
|
||||
avoidance_layers = p_layers;
|
||||
if (use_3d_avoidance) {
|
||||
rvo_agent_3d.avoidance_layers_ = avoidance_layers;
|
||||
} else {
|
||||
rvo_agent_2d.avoidance_layers_ = avoidance_layers;
|
||||
}
|
||||
agent_dirty = true;
|
||||
}
|
||||
|
||||
void NavAgent::set_avoidance_priority(real_t p_priority) {
|
||||
ERR_FAIL_COND_MSG(p_priority < 0.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
|
||||
ERR_FAIL_COND_MSG(p_priority > 1.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
|
||||
avoidance_priority = p_priority;
|
||||
if (use_3d_avoidance) {
|
||||
rvo_agent_3d.avoidance_priority_ = avoidance_priority;
|
||||
} else {
|
||||
rvo_agent_2d.avoidance_priority_ = avoidance_priority;
|
||||
}
|
||||
agent_dirty = true;
|
||||
};
|
||||
|
||||
bool NavAgent::check_dirty() {
|
||||
const bool was_dirty = agent_dirty;
|
||||
agent_dirty = false;
|
||||
return was_dirty;
|
||||
}
|
||||
|
||||
const Dictionary NavAgent::get_avoidance_data() const {
|
||||
// Returns debug data from RVO simulation internals of this agent.
|
||||
Dictionary _avoidance_data;
|
||||
if (use_3d_avoidance) {
|
||||
_avoidance_data["max_neighbors"] = int(rvo_agent_3d.maxNeighbors_);
|
||||
_avoidance_data["max_speed"] = float(rvo_agent_3d.maxSpeed_);
|
||||
_avoidance_data["neighbor_distance"] = float(rvo_agent_3d.neighborDist_);
|
||||
_avoidance_data["new_velocity"] = Vector3(rvo_agent_3d.newVelocity_.x(), rvo_agent_3d.newVelocity_.y(), rvo_agent_3d.newVelocity_.z());
|
||||
_avoidance_data["velocity"] = Vector3(rvo_agent_3d.velocity_.x(), rvo_agent_3d.velocity_.y(), rvo_agent_3d.velocity_.z());
|
||||
_avoidance_data["position"] = Vector3(rvo_agent_3d.position_.x(), rvo_agent_3d.position_.y(), rvo_agent_3d.position_.z());
|
||||
_avoidance_data["preferred_velocity"] = Vector3(rvo_agent_3d.prefVelocity_.x(), rvo_agent_3d.prefVelocity_.y(), rvo_agent_3d.prefVelocity_.z());
|
||||
_avoidance_data["radius"] = float(rvo_agent_3d.radius_);
|
||||
_avoidance_data["time_horizon_agents"] = float(rvo_agent_3d.timeHorizon_);
|
||||
_avoidance_data["time_horizon_obstacles"] = 0.0;
|
||||
_avoidance_data["height"] = float(rvo_agent_3d.height_);
|
||||
_avoidance_data["avoidance_layers"] = int(rvo_agent_3d.avoidance_layers_);
|
||||
_avoidance_data["avoidance_mask"] = int(rvo_agent_3d.avoidance_mask_);
|
||||
_avoidance_data["avoidance_priority"] = float(rvo_agent_3d.avoidance_priority_);
|
||||
} else {
|
||||
_avoidance_data["max_neighbors"] = int(rvo_agent_2d.maxNeighbors_);
|
||||
_avoidance_data["max_speed"] = float(rvo_agent_2d.maxSpeed_);
|
||||
_avoidance_data["neighbor_distance"] = float(rvo_agent_2d.neighborDist_);
|
||||
_avoidance_data["new_velocity"] = Vector3(rvo_agent_2d.newVelocity_.x(), 0.0, rvo_agent_2d.newVelocity_.y());
|
||||
_avoidance_data["velocity"] = Vector3(rvo_agent_2d.velocity_.x(), 0.0, rvo_agent_2d.velocity_.y());
|
||||
_avoidance_data["position"] = Vector3(rvo_agent_2d.position_.x(), 0.0, rvo_agent_2d.position_.y());
|
||||
_avoidance_data["preferred_velocity"] = Vector3(rvo_agent_2d.prefVelocity_.x(), 0.0, rvo_agent_2d.prefVelocity_.y());
|
||||
_avoidance_data["radius"] = float(rvo_agent_2d.radius_);
|
||||
_avoidance_data["time_horizon_agents"] = float(rvo_agent_2d.timeHorizon_);
|
||||
_avoidance_data["time_horizon_obstacles"] = float(rvo_agent_2d.timeHorizonObst_);
|
||||
_avoidance_data["height"] = float(rvo_agent_2d.height_);
|
||||
_avoidance_data["avoidance_layers"] = int(rvo_agent_2d.avoidance_layers_);
|
||||
_avoidance_data["avoidance_mask"] = int(rvo_agent_2d.avoidance_mask_);
|
||||
_avoidance_data["avoidance_priority"] = float(rvo_agent_2d.avoidance_priority_);
|
||||
}
|
||||
return _avoidance_data;
|
||||
}
|
||||
|
||||
void NavAgent::set_paused(bool p_paused) {
|
||||
if (paused == p_paused) {
|
||||
return;
|
||||
}
|
||||
|
||||
paused = p_paused;
|
||||
|
||||
if (map) {
|
||||
if (paused) {
|
||||
map->remove_agent_as_controlled(this);
|
||||
} else {
|
||||
map->set_agent_as_controlled(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool NavAgent::get_paused() const {
|
||||
return paused;
|
||||
}
|
||||
156
engine/modules/navigation/nav_agent.h
Normal file
156
engine/modules/navigation/nav_agent.h
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/**************************************************************************/
|
||||
/* nav_agent.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef NAV_AGENT_H
|
||||
#define NAV_AGENT_H
|
||||
|
||||
#include "nav_rid.h"
|
||||
|
||||
#include "core/object/class_db.h"
|
||||
#include "core/templates/local_vector.h"
|
||||
|
||||
#include <Agent2d.h>
|
||||
#include <Agent3d.h>
|
||||
|
||||
class NavMap;
|
||||
|
||||
class NavAgent : public NavRid {
|
||||
Vector3 position;
|
||||
Vector3 target_position;
|
||||
Vector3 velocity;
|
||||
Vector3 velocity_forced;
|
||||
real_t height = 1.0;
|
||||
real_t radius = 1.0;
|
||||
real_t max_speed = 1.0;
|
||||
real_t time_horizon_agents = 1.0;
|
||||
real_t time_horizon_obstacles = 0.0;
|
||||
int max_neighbors = 5;
|
||||
real_t neighbor_distance = 5.0;
|
||||
Vector3 safe_velocity;
|
||||
bool clamp_speed = true; // Experimental, clamps velocity to max_speed.
|
||||
|
||||
NavMap *map = nullptr;
|
||||
|
||||
RVO2D::Agent2D rvo_agent_2d;
|
||||
RVO3D::Agent3D rvo_agent_3d;
|
||||
bool use_3d_avoidance = false;
|
||||
bool avoidance_enabled = false;
|
||||
|
||||
uint32_t avoidance_layers = 1;
|
||||
uint32_t avoidance_mask = 1;
|
||||
real_t avoidance_priority = 1.0;
|
||||
|
||||
Callable avoidance_callback = Callable();
|
||||
|
||||
bool agent_dirty = true;
|
||||
|
||||
uint32_t last_map_iteration_id = 0;
|
||||
bool paused = false;
|
||||
|
||||
public:
|
||||
NavAgent();
|
||||
|
||||
void set_avoidance_enabled(bool p_enabled);
|
||||
bool is_avoidance_enabled() { return avoidance_enabled; }
|
||||
|
||||
void set_use_3d_avoidance(bool p_enabled);
|
||||
bool get_use_3d_avoidance() { return use_3d_avoidance; }
|
||||
|
||||
void set_map(NavMap *p_map);
|
||||
NavMap *get_map() { return map; }
|
||||
|
||||
bool is_map_changed();
|
||||
|
||||
RVO2D::Agent2D *get_rvo_agent_2d() { return &rvo_agent_2d; }
|
||||
RVO3D::Agent3D *get_rvo_agent_3d() { return &rvo_agent_3d; }
|
||||
|
||||
void set_avoidance_callback(Callable p_callback);
|
||||
bool has_avoidance_callback() const;
|
||||
|
||||
void dispatch_avoidance_callback();
|
||||
|
||||
void set_neighbor_distance(real_t p_neighbor_distance);
|
||||
real_t get_neighbor_distance() const { return neighbor_distance; }
|
||||
|
||||
void set_max_neighbors(int p_max_neighbors);
|
||||
int get_max_neighbors() const { return max_neighbors; }
|
||||
|
||||
void set_time_horizon_agents(real_t p_time_horizon);
|
||||
real_t get_time_horizon_agents() const { return time_horizon_agents; }
|
||||
|
||||
void set_time_horizon_obstacles(real_t p_time_horizon);
|
||||
real_t get_time_horizon_obstacles() const { return time_horizon_obstacles; }
|
||||
|
||||
void set_radius(real_t p_radius);
|
||||
real_t get_radius() const { return radius; }
|
||||
|
||||
void set_height(real_t p_height);
|
||||
real_t get_height() const { return height; }
|
||||
|
||||
void set_max_speed(real_t p_max_speed);
|
||||
real_t get_max_speed() const { return max_speed; }
|
||||
|
||||
void set_position(const Vector3 p_position);
|
||||
const Vector3 &get_position() const { return position; }
|
||||
|
||||
void set_target_position(const Vector3 p_target_position);
|
||||
const Vector3 &get_target_position() const { return target_position; }
|
||||
|
||||
void set_velocity(const Vector3 p_velocity);
|
||||
const Vector3 &get_velocity() const { return velocity; }
|
||||
|
||||
void set_velocity_forced(const Vector3 p_velocity);
|
||||
const Vector3 &get_velocity_forced() const { return velocity_forced; }
|
||||
|
||||
void set_avoidance_layers(uint32_t p_layers);
|
||||
uint32_t get_avoidance_layers() const { return avoidance_layers; };
|
||||
|
||||
void set_avoidance_mask(uint32_t p_mask);
|
||||
uint32_t get_avoidance_mask() const { return avoidance_mask; };
|
||||
|
||||
void set_avoidance_priority(real_t p_priority);
|
||||
real_t get_avoidance_priority() const { return avoidance_priority; };
|
||||
|
||||
void set_paused(bool p_paused);
|
||||
bool get_paused() const;
|
||||
|
||||
bool check_dirty();
|
||||
|
||||
// Updates this agent with rvo data after the rvo simulation avoidance step.
|
||||
void update();
|
||||
|
||||
// RVO debug data from the last frame update.
|
||||
const Dictionary get_avoidance_data() const;
|
||||
|
||||
private:
|
||||
void _update_rvo_agent_properties();
|
||||
};
|
||||
|
||||
#endif // NAV_AGENT_H
|
||||
70
engine/modules/navigation/nav_base.h
Normal file
70
engine/modules/navigation/nav_base.h
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
/**************************************************************************/
|
||||
/* nav_base.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef NAV_BASE_H
|
||||
#define NAV_BASE_H
|
||||
|
||||
#include "nav_rid.h"
|
||||
#include "nav_utils.h"
|
||||
|
||||
#include "servers/navigation/navigation_utilities.h"
|
||||
|
||||
class NavMap;
|
||||
|
||||
class NavBase : public NavRid {
|
||||
protected:
|
||||
uint32_t navigation_layers = 1;
|
||||
real_t enter_cost = 0.0;
|
||||
real_t travel_cost = 1.0;
|
||||
ObjectID owner_id;
|
||||
NavigationUtilities::PathSegmentType type;
|
||||
|
||||
public:
|
||||
NavigationUtilities::PathSegmentType get_type() const { return type; }
|
||||
|
||||
virtual void set_use_edge_connections(bool p_enabled) {}
|
||||
virtual bool get_use_edge_connections() const { return false; }
|
||||
|
||||
void set_navigation_layers(uint32_t p_navigation_layers) { navigation_layers = p_navigation_layers; }
|
||||
uint32_t get_navigation_layers() const { return navigation_layers; }
|
||||
|
||||
void set_enter_cost(real_t p_enter_cost) { enter_cost = MAX(p_enter_cost, 0.0); }
|
||||
real_t get_enter_cost() const { return enter_cost; }
|
||||
|
||||
void set_travel_cost(real_t p_travel_cost) { travel_cost = MAX(p_travel_cost, 0.0); }
|
||||
real_t get_travel_cost() const { return travel_cost; }
|
||||
|
||||
void set_owner_id(ObjectID p_owner_id) { owner_id = p_owner_id; }
|
||||
ObjectID get_owner_id() const { return owner_id; }
|
||||
|
||||
virtual ~NavBase(){};
|
||||
};
|
||||
|
||||
#endif // NAV_BASE_H
|
||||
91
engine/modules/navigation/nav_link.cpp
Normal file
91
engine/modules/navigation/nav_link.cpp
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
/**************************************************************************/
|
||||
/* nav_link.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "nav_link.h"
|
||||
|
||||
#include "nav_map.h"
|
||||
|
||||
void NavLink::set_map(NavMap *p_map) {
|
||||
if (map == p_map) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (map) {
|
||||
map->remove_link(this);
|
||||
}
|
||||
|
||||
map = p_map;
|
||||
link_dirty = true;
|
||||
|
||||
if (map) {
|
||||
map->add_link(this);
|
||||
}
|
||||
}
|
||||
|
||||
void NavLink::set_enabled(bool p_enabled) {
|
||||
if (enabled == p_enabled) {
|
||||
return;
|
||||
}
|
||||
enabled = p_enabled;
|
||||
|
||||
// TODO: This should not require a full rebuild as the link has not really changed.
|
||||
link_dirty = true;
|
||||
};
|
||||
|
||||
void NavLink::set_bidirectional(bool p_bidirectional) {
|
||||
if (bidirectional == p_bidirectional) {
|
||||
return;
|
||||
}
|
||||
bidirectional = p_bidirectional;
|
||||
link_dirty = true;
|
||||
}
|
||||
|
||||
void NavLink::set_start_position(const Vector3 p_position) {
|
||||
if (start_position == p_position) {
|
||||
return;
|
||||
}
|
||||
start_position = p_position;
|
||||
link_dirty = true;
|
||||
}
|
||||
|
||||
void NavLink::set_end_position(const Vector3 p_position) {
|
||||
if (end_position == p_position) {
|
||||
return;
|
||||
}
|
||||
end_position = p_position;
|
||||
link_dirty = true;
|
||||
}
|
||||
|
||||
bool NavLink::check_dirty() {
|
||||
const bool was_dirty = link_dirty;
|
||||
|
||||
link_dirty = false;
|
||||
return was_dirty;
|
||||
}
|
||||
77
engine/modules/navigation/nav_link.h
Normal file
77
engine/modules/navigation/nav_link.h
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
/**************************************************************************/
|
||||
/* nav_link.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef NAV_LINK_H
|
||||
#define NAV_LINK_H
|
||||
|
||||
#include "nav_base.h"
|
||||
#include "nav_utils.h"
|
||||
|
||||
class NavLink : public NavBase {
|
||||
NavMap *map = nullptr;
|
||||
bool bidirectional = true;
|
||||
Vector3 start_position;
|
||||
Vector3 end_position;
|
||||
bool enabled = true;
|
||||
|
||||
bool link_dirty = true;
|
||||
|
||||
public:
|
||||
NavLink() {
|
||||
type = NavigationUtilities::PathSegmentType::PATH_SEGMENT_TYPE_LINK;
|
||||
}
|
||||
|
||||
void set_map(NavMap *p_map);
|
||||
NavMap *get_map() const {
|
||||
return map;
|
||||
}
|
||||
|
||||
void set_enabled(bool p_enabled);
|
||||
bool get_enabled() const { return enabled; }
|
||||
|
||||
void set_bidirectional(bool p_bidirectional);
|
||||
bool is_bidirectional() const {
|
||||
return bidirectional;
|
||||
}
|
||||
|
||||
void set_start_position(Vector3 p_position);
|
||||
Vector3 get_start_position() const {
|
||||
return start_position;
|
||||
}
|
||||
|
||||
void set_end_position(Vector3 p_position);
|
||||
Vector3 get_end_position() const {
|
||||
return end_position;
|
||||
}
|
||||
|
||||
bool check_dirty();
|
||||
};
|
||||
|
||||
#endif // NAV_LINK_H
|
||||
1435
engine/modules/navigation/nav_map.cpp
Normal file
1435
engine/modules/navigation/nav_map.cpp
Normal file
File diff suppressed because it is too large
Load diff
235
engine/modules/navigation/nav_map.h
Normal file
235
engine/modules/navigation/nav_map.h
Normal file
|
|
@ -0,0 +1,235 @@
|
|||
/**************************************************************************/
|
||||
/* nav_map.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef NAV_MAP_H
|
||||
#define NAV_MAP_H
|
||||
|
||||
#include "nav_rid.h"
|
||||
#include "nav_utils.h"
|
||||
|
||||
#include "core/math/math_defs.h"
|
||||
#include "core/object/worker_thread_pool.h"
|
||||
|
||||
#include <KdTree2d.h>
|
||||
#include <KdTree3d.h>
|
||||
#include <RVOSimulator2d.h>
|
||||
#include <RVOSimulator3d.h>
|
||||
|
||||
class NavLink;
|
||||
class NavRegion;
|
||||
class NavAgent;
|
||||
class NavObstacle;
|
||||
|
||||
class NavMap : public NavRid {
|
||||
RWLock map_rwlock;
|
||||
|
||||
/// Map Up
|
||||
Vector3 up = Vector3(0, 1, 0);
|
||||
|
||||
/// To find the polygons edges the vertices are displaced in a grid where
|
||||
/// 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.
|
||||
|
||||
// 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;
|
||||
|
||||
bool use_edge_connections = true;
|
||||
/// This value is used to detect the near edges to connect.
|
||||
real_t edge_connection_margin = 0.25;
|
||||
|
||||
/// This value is used to limit how far links search to find polygons to connect to.
|
||||
real_t link_connection_radius = 1.0;
|
||||
|
||||
bool regenerate_polygons = true;
|
||||
bool regenerate_links = true;
|
||||
|
||||
/// Map regions
|
||||
LocalVector<NavRegion *> regions;
|
||||
|
||||
/// Map links
|
||||
LocalVector<NavLink *> links;
|
||||
LocalVector<gd::Polygon> link_polygons;
|
||||
|
||||
/// Map polygons
|
||||
LocalVector<gd::Polygon> polygons;
|
||||
|
||||
/// 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;
|
||||
|
||||
/// dirty flag when one of the agent's arrays are modified
|
||||
bool agents_dirty = true;
|
||||
|
||||
/// All the Agents (even the controlled one)
|
||||
LocalVector<NavAgent *> agents;
|
||||
|
||||
/// All the avoidance obstacles (both static and dynamic)
|
||||
LocalVector<NavObstacle *> obstacles;
|
||||
|
||||
/// Are rvo obstacles modified?
|
||||
bool obstacles_dirty = true;
|
||||
|
||||
/// Physics delta time
|
||||
real_t deltatime = 0.0;
|
||||
|
||||
/// Change the id each time the map is updated.
|
||||
uint32_t iteration_id = 0;
|
||||
|
||||
bool use_threads = true;
|
||||
bool avoidance_use_multiple_threads = true;
|
||||
bool avoidance_use_high_priority_threads = true;
|
||||
|
||||
// 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;
|
||||
|
||||
public:
|
||||
NavMap();
|
||||
~NavMap();
|
||||
|
||||
uint32_t get_iteration_id() const { return iteration_id; }
|
||||
|
||||
void set_up(Vector3 p_up);
|
||||
Vector3 get_up() const {
|
||||
return up;
|
||||
}
|
||||
|
||||
void set_cell_size(real_t p_cell_size);
|
||||
real_t get_cell_size() const {
|
||||
return cell_size;
|
||||
}
|
||||
|
||||
void set_cell_height(real_t p_cell_height);
|
||||
real_t get_cell_height() const { return cell_height; }
|
||||
|
||||
void set_merge_rasterizer_cell_scale(float p_value);
|
||||
float get_merge_rasterizer_cell_scale() const {
|
||||
return merge_rasterizer_cell_scale;
|
||||
}
|
||||
|
||||
void set_use_edge_connections(bool p_enabled);
|
||||
bool get_use_edge_connections() const {
|
||||
return use_edge_connections;
|
||||
}
|
||||
|
||||
void set_edge_connection_margin(real_t p_edge_connection_margin);
|
||||
real_t get_edge_connection_margin() const {
|
||||
return edge_connection_margin;
|
||||
}
|
||||
|
||||
void set_link_connection_radius(real_t p_link_connection_radius);
|
||||
real_t get_link_connection_radius() const {
|
||||
return link_connection_radius;
|
||||
}
|
||||
|
||||
gd::PointKey get_point_key(const Vector3 &p_pos) const;
|
||||
|
||||
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;
|
||||
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;
|
||||
gd::ClosestPointQueryResult get_closest_point_info(const Vector3 &p_point) const;
|
||||
RID get_closest_point_owner(const Vector3 &p_point) const;
|
||||
|
||||
void add_region(NavRegion *p_region);
|
||||
void remove_region(NavRegion *p_region);
|
||||
const LocalVector<NavRegion *> &get_regions() const {
|
||||
return regions;
|
||||
}
|
||||
|
||||
void add_link(NavLink *p_link);
|
||||
void remove_link(NavLink *p_link);
|
||||
const LocalVector<NavLink *> &get_links() const {
|
||||
return links;
|
||||
}
|
||||
|
||||
bool has_agent(NavAgent *agent) const;
|
||||
void add_agent(NavAgent *agent);
|
||||
void remove_agent(NavAgent *agent);
|
||||
const LocalVector<NavAgent *> &get_agents() const {
|
||||
return agents;
|
||||
}
|
||||
|
||||
void set_agent_as_controlled(NavAgent *agent);
|
||||
void remove_agent_as_controlled(NavAgent *agent);
|
||||
|
||||
bool has_obstacle(NavObstacle *obstacle) const;
|
||||
void add_obstacle(NavObstacle *obstacle);
|
||||
void remove_obstacle(NavObstacle *obstacle);
|
||||
const LocalVector<NavObstacle *> &get_obstacles() const {
|
||||
return obstacles;
|
||||
}
|
||||
|
||||
Vector3 get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const;
|
||||
|
||||
void sync();
|
||||
void step(real_t p_deltatime);
|
||||
void dispatch_callbacks();
|
||||
|
||||
// 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; }
|
||||
|
||||
private:
|
||||
void compute_single_step(uint32_t index, NavAgent **agent);
|
||||
|
||||
void compute_single_avoidance_step_2d(uint32_t index, NavAgent **agent);
|
||||
void compute_single_avoidance_step_3d(uint32_t index, NavAgent **agent);
|
||||
|
||||
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;
|
||||
void _update_rvo_simulation();
|
||||
void _update_rvo_obstacles_tree_2d();
|
||||
void _update_rvo_agents_tree_2d();
|
||||
void _update_rvo_agents_tree_3d();
|
||||
|
||||
void _update_merge_rasterizer_cell_dimensions();
|
||||
};
|
||||
|
||||
#endif // NAV_MAP_H
|
||||
218
engine/modules/navigation/nav_obstacle.cpp
Normal file
218
engine/modules/navigation/nav_obstacle.cpp
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
/**************************************************************************/
|
||||
/* nav_obstacle.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "nav_obstacle.h"
|
||||
|
||||
#include "nav_agent.h"
|
||||
#include "nav_map.h"
|
||||
|
||||
NavObstacle::NavObstacle() {}
|
||||
|
||||
NavObstacle::~NavObstacle() {}
|
||||
|
||||
void NavObstacle::set_agent(NavAgent *p_agent) {
|
||||
if (agent == p_agent) {
|
||||
return;
|
||||
}
|
||||
|
||||
agent = p_agent;
|
||||
|
||||
internal_update_agent();
|
||||
}
|
||||
|
||||
void NavObstacle::set_avoidance_enabled(bool p_enabled) {
|
||||
if (avoidance_enabled == p_enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
avoidance_enabled = p_enabled;
|
||||
obstacle_dirty = true;
|
||||
|
||||
internal_update_agent();
|
||||
}
|
||||
|
||||
void NavObstacle::set_use_3d_avoidance(bool p_enabled) {
|
||||
if (use_3d_avoidance == p_enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
use_3d_avoidance = p_enabled;
|
||||
obstacle_dirty = true;
|
||||
|
||||
if (agent) {
|
||||
agent->set_use_3d_avoidance(use_3d_avoidance);
|
||||
}
|
||||
}
|
||||
|
||||
void NavObstacle::set_map(NavMap *p_map) {
|
||||
if (map == p_map) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (map) {
|
||||
map->remove_obstacle(this);
|
||||
if (agent) {
|
||||
agent->set_map(nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
map = p_map;
|
||||
obstacle_dirty = true;
|
||||
|
||||
if (map) {
|
||||
map->add_obstacle(this);
|
||||
internal_update_agent();
|
||||
}
|
||||
}
|
||||
|
||||
void NavObstacle::set_position(const Vector3 p_position) {
|
||||
if (position == p_position) {
|
||||
return;
|
||||
}
|
||||
|
||||
position = p_position;
|
||||
obstacle_dirty = true;
|
||||
|
||||
if (agent) {
|
||||
agent->set_position(position);
|
||||
}
|
||||
}
|
||||
|
||||
void NavObstacle::set_radius(real_t p_radius) {
|
||||
if (radius == p_radius) {
|
||||
return;
|
||||
}
|
||||
|
||||
radius = p_radius;
|
||||
|
||||
if (agent) {
|
||||
agent->set_radius(radius);
|
||||
}
|
||||
}
|
||||
|
||||
void NavObstacle::set_height(const real_t p_height) {
|
||||
if (height == p_height) {
|
||||
return;
|
||||
}
|
||||
|
||||
height = p_height;
|
||||
obstacle_dirty = true;
|
||||
|
||||
if (agent) {
|
||||
agent->set_height(height);
|
||||
}
|
||||
}
|
||||
|
||||
void NavObstacle::set_velocity(const Vector3 p_velocity) {
|
||||
velocity = p_velocity;
|
||||
|
||||
if (agent) {
|
||||
agent->set_velocity(velocity);
|
||||
}
|
||||
}
|
||||
|
||||
void NavObstacle::set_vertices(const Vector<Vector3> &p_vertices) {
|
||||
if (vertices == p_vertices) {
|
||||
return;
|
||||
}
|
||||
|
||||
vertices = p_vertices;
|
||||
obstacle_dirty = true;
|
||||
}
|
||||
|
||||
bool NavObstacle::is_map_changed() {
|
||||
if (map) {
|
||||
bool is_changed = map->get_iteration_id() != last_map_iteration_id;
|
||||
last_map_iteration_id = map->get_iteration_id();
|
||||
return is_changed;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void NavObstacle::set_avoidance_layers(uint32_t p_layers) {
|
||||
if (avoidance_layers == p_layers) {
|
||||
return;
|
||||
}
|
||||
|
||||
avoidance_layers = p_layers;
|
||||
obstacle_dirty = true;
|
||||
|
||||
if (agent) {
|
||||
agent->set_avoidance_layers(avoidance_layers);
|
||||
}
|
||||
}
|
||||
|
||||
bool NavObstacle::check_dirty() {
|
||||
const bool was_dirty = obstacle_dirty;
|
||||
obstacle_dirty = false;
|
||||
return was_dirty;
|
||||
}
|
||||
|
||||
void NavObstacle::internal_update_agent() {
|
||||
if (agent) {
|
||||
agent->set_neighbor_distance(0.0);
|
||||
agent->set_max_neighbors(0.0);
|
||||
agent->set_time_horizon_agents(0.0);
|
||||
agent->set_time_horizon_obstacles(0.0);
|
||||
agent->set_avoidance_mask(0.0);
|
||||
agent->set_neighbor_distance(0.0);
|
||||
agent->set_avoidance_priority(1.0);
|
||||
agent->set_map(map);
|
||||
agent->set_paused(paused);
|
||||
agent->set_radius(radius);
|
||||
agent->set_height(height);
|
||||
agent->set_position(position);
|
||||
agent->set_avoidance_layers(avoidance_layers);
|
||||
agent->set_avoidance_enabled(avoidance_enabled);
|
||||
agent->set_use_3d_avoidance(use_3d_avoidance);
|
||||
}
|
||||
}
|
||||
|
||||
void NavObstacle::set_paused(bool p_paused) {
|
||||
if (paused == p_paused) {
|
||||
return;
|
||||
}
|
||||
|
||||
paused = p_paused;
|
||||
|
||||
if (map) {
|
||||
if (paused) {
|
||||
map->remove_obstacle(this);
|
||||
} else {
|
||||
map->add_obstacle(this);
|
||||
}
|
||||
}
|
||||
internal_update_agent();
|
||||
}
|
||||
|
||||
bool NavObstacle::get_paused() const {
|
||||
return paused;
|
||||
}
|
||||
106
engine/modules/navigation/nav_obstacle.h
Normal file
106
engine/modules/navigation/nav_obstacle.h
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
/**************************************************************************/
|
||||
/* nav_obstacle.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef NAV_OBSTACLE_H
|
||||
#define NAV_OBSTACLE_H
|
||||
|
||||
#include "nav_rid.h"
|
||||
|
||||
#include "core/object/class_db.h"
|
||||
#include "core/templates/local_vector.h"
|
||||
|
||||
class NavAgent;
|
||||
class NavMap;
|
||||
|
||||
class NavObstacle : public NavRid {
|
||||
NavAgent *agent = nullptr;
|
||||
NavMap *map = nullptr;
|
||||
Vector3 velocity;
|
||||
Vector3 position;
|
||||
Vector<Vector3> vertices;
|
||||
|
||||
real_t radius = 0.0;
|
||||
real_t height = 0.0;
|
||||
|
||||
bool avoidance_enabled = false;
|
||||
bool use_3d_avoidance = false;
|
||||
uint32_t avoidance_layers = 1;
|
||||
|
||||
bool obstacle_dirty = true;
|
||||
|
||||
uint32_t last_map_iteration_id = 0;
|
||||
bool paused = false;
|
||||
|
||||
public:
|
||||
NavObstacle();
|
||||
~NavObstacle();
|
||||
|
||||
void set_avoidance_enabled(bool p_enabled);
|
||||
bool is_avoidance_enabled() { return avoidance_enabled; }
|
||||
|
||||
void set_use_3d_avoidance(bool p_enabled);
|
||||
bool get_use_3d_avoidance() { return use_3d_avoidance; }
|
||||
|
||||
void set_map(NavMap *p_map);
|
||||
NavMap *get_map() { return map; }
|
||||
|
||||
void set_agent(NavAgent *p_agent);
|
||||
NavAgent *get_agent() { return agent; }
|
||||
|
||||
void set_position(const Vector3 p_position);
|
||||
const Vector3 &get_position() const { return position; }
|
||||
|
||||
void set_radius(real_t p_radius);
|
||||
real_t get_radius() const { return radius; }
|
||||
|
||||
void set_height(const real_t p_height);
|
||||
real_t get_height() const { return height; }
|
||||
|
||||
void set_velocity(const Vector3 p_velocity);
|
||||
const Vector3 &get_velocity() const { return velocity; }
|
||||
|
||||
void set_vertices(const Vector<Vector3> &p_vertices);
|
||||
const Vector<Vector3> &get_vertices() const { return vertices; }
|
||||
|
||||
bool is_map_changed();
|
||||
|
||||
void set_avoidance_layers(uint32_t p_layers);
|
||||
uint32_t get_avoidance_layers() const { return avoidance_layers; };
|
||||
|
||||
void set_paused(bool p_paused);
|
||||
bool get_paused() const;
|
||||
|
||||
bool check_dirty();
|
||||
|
||||
private:
|
||||
void internal_update_agent();
|
||||
};
|
||||
|
||||
#endif // NAV_OBSTACLE_H
|
||||
298
engine/modules/navigation/nav_region.cpp
Normal file
298
engine/modules/navigation/nav_region.cpp
Normal file
|
|
@ -0,0 +1,298 @@
|
|||
/**************************************************************************/
|
||||
/* nav_region.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "nav_region.h"
|
||||
|
||||
#include "nav_map.h"
|
||||
|
||||
void NavRegion::set_map(NavMap *p_map) {
|
||||
if (map == p_map) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (map) {
|
||||
map->remove_region(this);
|
||||
}
|
||||
|
||||
map = p_map;
|
||||
polygons_dirty = true;
|
||||
|
||||
connections.clear();
|
||||
|
||||
if (map) {
|
||||
map->add_region(this);
|
||||
}
|
||||
}
|
||||
|
||||
void NavRegion::set_enabled(bool p_enabled) {
|
||||
if (enabled == p_enabled) {
|
||||
return;
|
||||
}
|
||||
enabled = p_enabled;
|
||||
|
||||
// TODO: This should not require a full rebuild as the region has not really changed.
|
||||
polygons_dirty = true;
|
||||
};
|
||||
|
||||
void NavRegion::set_use_edge_connections(bool p_enabled) {
|
||||
if (use_edge_connections != p_enabled) {
|
||||
use_edge_connections = p_enabled;
|
||||
polygons_dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
void NavRegion::set_transform(Transform3D p_transform) {
|
||||
if (transform == p_transform) {
|
||||
return;
|
||||
}
|
||||
transform = p_transform;
|
||||
polygons_dirty = true;
|
||||
|
||||
#ifdef DEBUG_ENABLED
|
||||
if (map && Math::rad_to_deg(map->get_up().angle_to(transform.basis.get_column(1))) >= 90.0f) {
|
||||
ERR_PRINT_ONCE("Attempted to update a navigation region transform rotated 90 degrees or more away from the current navigation map UP orientation.");
|
||||
}
|
||||
#endif // DEBUG_ENABLED
|
||||
}
|
||||
|
||||
void NavRegion::set_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh) {
|
||||
#ifdef DEBUG_ENABLED
|
||||
if (map && p_navigation_mesh.is_valid() && !Math::is_equal_approx(double(map->get_cell_size()), double(p_navigation_mesh->get_cell_size()))) {
|
||||
ERR_PRINT_ONCE(vformat("Attempted to update a navigation region with a navigation mesh that uses a `cell_size` of %s while assigned to a navigation map set to a `cell_size` of %s. The cell size for navigation maps can be changed by using the NavigationServer map_set_cell_size() function. The cell size for default navigation maps can also be changed in the ProjectSettings.", double(p_navigation_mesh->get_cell_size()), double(map->get_cell_size())));
|
||||
}
|
||||
|
||||
if (map && p_navigation_mesh.is_valid() && !Math::is_equal_approx(double(map->get_cell_height()), double(p_navigation_mesh->get_cell_height()))) {
|
||||
ERR_PRINT_ONCE(vformat("Attempted to update a navigation region with a navigation mesh that uses a `cell_height` of %s while assigned to a navigation map set to a `cell_height` of %s. The cell height for navigation maps can be changed by using the NavigationServer map_set_cell_height() function. The cell height for default navigation maps can also be changed in the ProjectSettings.", double(p_navigation_mesh->get_cell_height()), double(map->get_cell_height())));
|
||||
}
|
||||
#endif // DEBUG_ENABLED
|
||||
|
||||
RWLockWrite write_lock(navmesh_rwlock);
|
||||
|
||||
pending_navmesh_vertices.clear();
|
||||
pending_navmesh_polygons.clear();
|
||||
|
||||
if (p_navigation_mesh.is_valid()) {
|
||||
p_navigation_mesh->get_data(pending_navmesh_vertices, pending_navmesh_polygons);
|
||||
}
|
||||
|
||||
polygons_dirty = true;
|
||||
}
|
||||
|
||||
int NavRegion::get_connections_count() const {
|
||||
if (!map) {
|
||||
return 0;
|
||||
}
|
||||
return connections.size();
|
||||
}
|
||||
|
||||
Vector3 NavRegion::get_connection_pathway_start(int p_connection_id) const {
|
||||
ERR_FAIL_NULL_V(map, Vector3());
|
||||
ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3());
|
||||
return connections[p_connection_id].pathway_start;
|
||||
}
|
||||
|
||||
Vector3 NavRegion::get_connection_pathway_end(int p_connection_id) const {
|
||||
ERR_FAIL_NULL_V(map, Vector3());
|
||||
ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3());
|
||||
return connections[p_connection_id].pathway_end;
|
||||
}
|
||||
|
||||
Vector3 NavRegion::get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const {
|
||||
if (!get_enabled()) {
|
||||
return Vector3();
|
||||
}
|
||||
|
||||
const LocalVector<gd::Polygon> ®ion_polygons = get_polygons();
|
||||
|
||||
if (region_polygons.is_empty()) {
|
||||
return Vector3();
|
||||
}
|
||||
|
||||
if (p_uniformly) {
|
||||
real_t accumulated_area = 0;
|
||||
RBMap<real_t, uint32_t> region_area_map;
|
||||
|
||||
for (uint32_t rp_index = 0; rp_index < region_polygons.size(); rp_index++) {
|
||||
const gd::Polygon ®ion_polygon = region_polygons[rp_index];
|
||||
real_t polyon_area = region_polygon.surface_area;
|
||||
|
||||
if (polyon_area == 0.0) {
|
||||
continue;
|
||||
}
|
||||
region_area_map[accumulated_area] = rp_index;
|
||||
accumulated_area += polyon_area;
|
||||
}
|
||||
if (region_area_map.is_empty() || accumulated_area == 0) {
|
||||
// All polygons have no real surface / no area.
|
||||
return Vector3();
|
||||
}
|
||||
|
||||
real_t region_area_map_pos = Math::random(real_t(0), accumulated_area);
|
||||
|
||||
RBMap<real_t, uint32_t>::Iterator region_E = region_area_map.find_closest(region_area_map_pos);
|
||||
ERR_FAIL_COND_V(!region_E, Vector3());
|
||||
uint32_t rrp_polygon_index = region_E->value;
|
||||
ERR_FAIL_UNSIGNED_INDEX_V(rrp_polygon_index, region_polygons.size(), Vector3());
|
||||
|
||||
const gd::Polygon &rr_polygon = region_polygons[rrp_polygon_index];
|
||||
|
||||
real_t accumulated_polygon_area = 0;
|
||||
RBMap<real_t, uint32_t> polygon_area_map;
|
||||
|
||||
for (uint32_t rpp_index = 2; rpp_index < rr_polygon.points.size(); rpp_index++) {
|
||||
real_t face_area = Face3(rr_polygon.points[0].pos, rr_polygon.points[rpp_index - 1].pos, rr_polygon.points[rpp_index].pos).get_area();
|
||||
|
||||
if (face_area == 0.0) {
|
||||
continue;
|
||||
}
|
||||
polygon_area_map[accumulated_polygon_area] = rpp_index;
|
||||
accumulated_polygon_area += face_area;
|
||||
}
|
||||
if (polygon_area_map.is_empty() || accumulated_polygon_area == 0) {
|
||||
// All faces have no real surface / no area.
|
||||
return Vector3();
|
||||
}
|
||||
|
||||
real_t polygon_area_map_pos = Math::random(real_t(0), accumulated_polygon_area);
|
||||
|
||||
RBMap<real_t, uint32_t>::Iterator polygon_E = polygon_area_map.find_closest(polygon_area_map_pos);
|
||||
ERR_FAIL_COND_V(!polygon_E, Vector3());
|
||||
uint32_t rrp_face_index = polygon_E->value;
|
||||
ERR_FAIL_UNSIGNED_INDEX_V(rrp_face_index, rr_polygon.points.size(), Vector3());
|
||||
|
||||
const Face3 face(rr_polygon.points[0].pos, rr_polygon.points[rrp_face_index - 1].pos, rr_polygon.points[rrp_face_index].pos);
|
||||
|
||||
Vector3 face_random_position = face.get_random_point_inside();
|
||||
return face_random_position;
|
||||
|
||||
} else {
|
||||
uint32_t rrp_polygon_index = Math::random(int(0), region_polygons.size() - 1);
|
||||
|
||||
const gd::Polygon &rr_polygon = region_polygons[rrp_polygon_index];
|
||||
|
||||
uint32_t rrp_face_index = Math::random(int(2), rr_polygon.points.size() - 1);
|
||||
|
||||
const Face3 face(rr_polygon.points[0].pos, rr_polygon.points[rrp_face_index - 1].pos, rr_polygon.points[rrp_face_index].pos);
|
||||
|
||||
Vector3 face_random_position = face.get_random_point_inside();
|
||||
return face_random_position;
|
||||
}
|
||||
}
|
||||
|
||||
bool NavRegion::sync() {
|
||||
bool something_changed = polygons_dirty /* || something_dirty? */;
|
||||
|
||||
update_polygons();
|
||||
|
||||
return something_changed;
|
||||
}
|
||||
|
||||
void NavRegion::update_polygons() {
|
||||
if (!polygons_dirty) {
|
||||
return;
|
||||
}
|
||||
polygons.clear();
|
||||
surface_area = 0.0;
|
||||
polygons_dirty = false;
|
||||
|
||||
if (map == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
RWLockRead read_lock(navmesh_rwlock);
|
||||
|
||||
if (pending_navmesh_vertices.is_empty() || pending_navmesh_polygons.is_empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
int len = pending_navmesh_vertices.size();
|
||||
if (len == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const Vector3 *vertices_r = pending_navmesh_vertices.ptr();
|
||||
|
||||
polygons.resize(pending_navmesh_polygons.size());
|
||||
|
||||
real_t _new_region_surface_area = 0.0;
|
||||
|
||||
// Build
|
||||
int navigation_mesh_polygon_index = 0;
|
||||
for (gd::Polygon &polygon : polygons) {
|
||||
polygon.owner = this;
|
||||
polygon.surface_area = 0.0;
|
||||
|
||||
Vector<int> navigation_mesh_polygon = pending_navmesh_polygons[navigation_mesh_polygon_index];
|
||||
navigation_mesh_polygon_index += 1;
|
||||
|
||||
int navigation_mesh_polygon_size = navigation_mesh_polygon.size();
|
||||
if (navigation_mesh_polygon_size < 3) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const int *indices = navigation_mesh_polygon.ptr();
|
||||
bool valid(true);
|
||||
|
||||
polygon.points.resize(navigation_mesh_polygon_size);
|
||||
polygon.edges.resize(navigation_mesh_polygon_size);
|
||||
|
||||
real_t _new_polygon_surface_area = 0.0;
|
||||
|
||||
for (int j(2); j < navigation_mesh_polygon_size; j++) {
|
||||
const Face3 face = Face3(
|
||||
transform.xform(vertices_r[indices[0]]),
|
||||
transform.xform(vertices_r[indices[j - 1]]),
|
||||
transform.xform(vertices_r[indices[j]]));
|
||||
|
||||
_new_polygon_surface_area += face.get_area();
|
||||
}
|
||||
|
||||
polygon.surface_area = _new_polygon_surface_area;
|
||||
_new_region_surface_area += _new_polygon_surface_area;
|
||||
|
||||
for (int j(0); j < navigation_mesh_polygon_size; j++) {
|
||||
int idx = indices[j];
|
||||
if (idx < 0 || idx >= len) {
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
|
||||
Vector3 point_position = transform.xform(vertices_r[idx]);
|
||||
polygon.points[j].pos = point_position;
|
||||
polygon.points[j].key = map->get_point_key(point_position);
|
||||
}
|
||||
|
||||
if (!valid) {
|
||||
ERR_BREAK_MSG(!valid, "The navigation mesh set in this region is not valid!");
|
||||
}
|
||||
}
|
||||
|
||||
surface_area = _new_region_surface_area;
|
||||
}
|
||||
109
engine/modules/navigation/nav_region.h
Normal file
109
engine/modules/navigation/nav_region.h
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
/**************************************************************************/
|
||||
/* nav_region.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef NAV_REGION_H
|
||||
#define NAV_REGION_H
|
||||
|
||||
#include "nav_base.h"
|
||||
#include "nav_utils.h"
|
||||
|
||||
#include "core/os/rw_lock.h"
|
||||
#include "scene/resources/navigation_mesh.h"
|
||||
|
||||
class NavRegion : public NavBase {
|
||||
NavMap *map = nullptr;
|
||||
Transform3D transform;
|
||||
Vector<gd::Edge::Connection> connections;
|
||||
bool enabled = true;
|
||||
|
||||
bool use_edge_connections = true;
|
||||
|
||||
bool polygons_dirty = true;
|
||||
|
||||
/// Cache
|
||||
LocalVector<gd::Polygon> polygons;
|
||||
|
||||
real_t surface_area = 0.0;
|
||||
|
||||
RWLock navmesh_rwlock;
|
||||
Vector<Vector3> pending_navmesh_vertices;
|
||||
Vector<Vector<int>> pending_navmesh_polygons;
|
||||
|
||||
public:
|
||||
NavRegion() {
|
||||
type = NavigationUtilities::PathSegmentType::PATH_SEGMENT_TYPE_REGION;
|
||||
}
|
||||
|
||||
void scratch_polygons() {
|
||||
polygons_dirty = true;
|
||||
}
|
||||
|
||||
void set_enabled(bool p_enabled);
|
||||
bool get_enabled() const { return enabled; }
|
||||
|
||||
void set_map(NavMap *p_map);
|
||||
NavMap *get_map() const {
|
||||
return map;
|
||||
}
|
||||
|
||||
void set_use_edge_connections(bool p_enabled);
|
||||
bool get_use_edge_connections() const {
|
||||
return use_edge_connections;
|
||||
}
|
||||
|
||||
void set_transform(Transform3D transform);
|
||||
const Transform3D &get_transform() const {
|
||||
return transform;
|
||||
}
|
||||
|
||||
void set_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh);
|
||||
|
||||
Vector<gd::Edge::Connection> &get_connections() {
|
||||
return connections;
|
||||
}
|
||||
int get_connections_count() const;
|
||||
Vector3 get_connection_pathway_start(int p_connection_id) const;
|
||||
Vector3 get_connection_pathway_end(int p_connection_id) const;
|
||||
|
||||
LocalVector<gd::Polygon> const &get_polygons() const {
|
||||
return polygons;
|
||||
}
|
||||
|
||||
Vector3 get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const;
|
||||
|
||||
real_t get_surface_area() const { return surface_area; };
|
||||
|
||||
bool sync();
|
||||
|
||||
private:
|
||||
void update_polygons();
|
||||
};
|
||||
|
||||
#endif // NAV_REGION_H
|
||||
44
engine/modules/navigation/nav_rid.h
Normal file
44
engine/modules/navigation/nav_rid.h
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/**************************************************************************/
|
||||
/* nav_rid.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef NAV_RID_H
|
||||
#define NAV_RID_H
|
||||
|
||||
#include "core/templates/rid.h"
|
||||
|
||||
class NavRid {
|
||||
RID self;
|
||||
|
||||
public:
|
||||
_FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
|
||||
_FORCE_INLINE_ RID get_self() const { return self; }
|
||||
};
|
||||
|
||||
#endif // NAV_RID_H
|
||||
151
engine/modules/navigation/nav_utils.h
Normal file
151
engine/modules/navigation/nav_utils.h
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
/**************************************************************************/
|
||||
/* nav_utils.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef NAV_UTILS_H
|
||||
#define NAV_UTILS_H
|
||||
|
||||
#include "core/math/vector3.h"
|
||||
#include "core/templates/hash_map.h"
|
||||
#include "core/templates/hashfuncs.h"
|
||||
#include "core/templates/local_vector.h"
|
||||
|
||||
class NavBase;
|
||||
|
||||
namespace gd {
|
||||
struct Polygon;
|
||||
|
||||
union PointKey {
|
||||
struct {
|
||||
int64_t x : 21;
|
||||
int64_t y : 22;
|
||||
int64_t z : 21;
|
||||
};
|
||||
|
||||
uint64_t key = 0;
|
||||
};
|
||||
|
||||
struct EdgeKey {
|
||||
PointKey a;
|
||||
PointKey b;
|
||||
|
||||
static uint32_t hash(const EdgeKey &p_val) {
|
||||
return hash_one_uint64(p_val.a.key) ^ hash_one_uint64(p_val.b.key);
|
||||
}
|
||||
|
||||
bool operator==(const EdgeKey &p_key) const {
|
||||
return (a.key == p_key.a.key) && (b.key == p_key.b.key);
|
||||
}
|
||||
|
||||
EdgeKey(const PointKey &p_a = PointKey(), const PointKey &p_b = PointKey()) :
|
||||
a(p_a),
|
||||
b(p_b) {
|
||||
if (a.key > b.key) {
|
||||
SWAP(a, b);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct Point {
|
||||
Vector3 pos;
|
||||
PointKey key;
|
||||
};
|
||||
|
||||
struct Edge {
|
||||
/// The gateway in the edge, as, in some case, the whole edge might not be navigable.
|
||||
struct Connection {
|
||||
/// Polygon that this connection leads to.
|
||||
Polygon *polygon = nullptr;
|
||||
|
||||
/// Edge of the source polygon where this connection starts from.
|
||||
int edge = -1;
|
||||
|
||||
/// Point on the edge where the gateway leading to the poly starts.
|
||||
Vector3 pathway_start;
|
||||
|
||||
/// Point on the edge where the gateway leading to the poly ends.
|
||||
Vector3 pathway_end;
|
||||
};
|
||||
|
||||
/// Connections from this edge to other polygons.
|
||||
Vector<Connection> connections;
|
||||
};
|
||||
|
||||
struct Polygon {
|
||||
/// Navigation region or link that contains this polygon.
|
||||
const NavBase *owner = nullptr;
|
||||
|
||||
/// The points of this `Polygon`
|
||||
LocalVector<Point> points;
|
||||
|
||||
/// The edges of this `Polygon`
|
||||
LocalVector<Edge> edges;
|
||||
|
||||
real_t surface_area = 0.0;
|
||||
};
|
||||
|
||||
struct NavigationPoly {
|
||||
uint32_t self_id = 0;
|
||||
/// This poly.
|
||||
const Polygon *poly;
|
||||
|
||||
/// Those 4 variables are used to travel the path backwards.
|
||||
int back_navigation_poly_id = -1;
|
||||
int back_navigation_edge = -1;
|
||||
Vector3 back_navigation_edge_pathway_start;
|
||||
Vector3 back_navigation_edge_pathway_end;
|
||||
|
||||
/// The entry position of this poly.
|
||||
Vector3 entry;
|
||||
/// The distance to the destination.
|
||||
real_t traveled_distance = 0.0;
|
||||
|
||||
NavigationPoly() { poly = nullptr; }
|
||||
|
||||
NavigationPoly(const Polygon *p_poly) :
|
||||
poly(p_poly) {}
|
||||
|
||||
bool operator==(const NavigationPoly &other) const {
|
||||
return poly == other.poly;
|
||||
}
|
||||
|
||||
bool operator!=(const NavigationPoly &other) const {
|
||||
return !operator==(other);
|
||||
}
|
||||
};
|
||||
|
||||
struct ClosestPointQueryResult {
|
||||
Vector3 point;
|
||||
Vector3 normal;
|
||||
RID owner;
|
||||
};
|
||||
|
||||
} // namespace gd
|
||||
|
||||
#endif // NAV_UTILS_H
|
||||
97
engine/modules/navigation/register_types.cpp
Normal file
97
engine/modules/navigation/register_types.cpp
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
/**************************************************************************/
|
||||
/* register_types.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "register_types.h"
|
||||
|
||||
#include "2d/godot_navigation_server_2d.h"
|
||||
#include "3d/godot_navigation_server_3d.h"
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
#ifndef _3D_DISABLED
|
||||
#include "3d/navigation_mesh_generator.h"
|
||||
#endif
|
||||
#endif // DISABLE_DEPRECATED
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
#include "editor/navigation_mesh_editor_plugin.h"
|
||||
#endif
|
||||
|
||||
#include "core/config/engine.h"
|
||||
#include "servers/navigation_server_2d.h"
|
||||
#include "servers/navigation_server_3d.h"
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
#ifndef _3D_DISABLED
|
||||
NavigationMeshGenerator *_nav_mesh_generator = nullptr;
|
||||
#endif
|
||||
#endif // DISABLE_DEPRECATED
|
||||
|
||||
NavigationServer3D *new_navigation_server_3d() {
|
||||
return memnew(GodotNavigationServer3D);
|
||||
}
|
||||
|
||||
NavigationServer2D *new_navigation_server_2d() {
|
||||
return memnew(GodotNavigationServer2D);
|
||||
}
|
||||
|
||||
void initialize_navigation_module(ModuleInitializationLevel p_level) {
|
||||
if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
|
||||
NavigationServer3DManager::set_default_server(new_navigation_server_3d);
|
||||
NavigationServer2DManager::set_default_server(new_navigation_server_2d);
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
#ifndef _3D_DISABLED
|
||||
_nav_mesh_generator = memnew(NavigationMeshGenerator);
|
||||
GDREGISTER_CLASS(NavigationMeshGenerator);
|
||||
Engine::get_singleton()->add_singleton(Engine::Singleton("NavigationMeshGenerator", NavigationMeshGenerator::get_singleton()));
|
||||
#endif
|
||||
#endif // DISABLE_DEPRECATED
|
||||
}
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
if (p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
|
||||
EditorPlugins::add_by_type<NavigationMeshEditorPlugin>();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void uninitialize_navigation_module(ModuleInitializationLevel p_level) {
|
||||
if (p_level != MODULE_INITIALIZATION_LEVEL_SERVERS) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
#ifndef _3D_DISABLED
|
||||
if (_nav_mesh_generator) {
|
||||
memdelete(_nav_mesh_generator);
|
||||
}
|
||||
#endif
|
||||
#endif // DISABLE_DEPRECATED
|
||||
}
|
||||
39
engine/modules/navigation/register_types.h
Normal file
39
engine/modules/navigation/register_types.h
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/**************************************************************************/
|
||||
/* register_types.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* 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. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef NAVIGATION_REGISTER_TYPES_H
|
||||
#define NAVIGATION_REGISTER_TYPES_H
|
||||
|
||||
#include "modules/register_module_types.h"
|
||||
|
||||
void initialize_navigation_module(ModuleInitializationLevel p_level);
|
||||
void uninitialize_navigation_module(ModuleInitializationLevel p_level);
|
||||
|
||||
#endif // NAVIGATION_REGISTER_TYPES_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue