diff options
Diffstat (limited to 'modules/gdnavigation')
-rw-r--r-- | modules/gdnavigation/gd_navigation_server.cpp | 10 | ||||
-rw-r--r-- | modules/gdnavigation/gd_navigation_server.h | 2 | ||||
-rw-r--r-- | modules/gdnavigation/nav_map.cpp | 59 | ||||
-rw-r--r-- | modules/gdnavigation/nav_map.h | 19 | ||||
-rw-r--r-- | modules/gdnavigation/nav_region.cpp | 13 | ||||
-rw-r--r-- | modules/gdnavigation/nav_region.h | 6 | ||||
-rw-r--r-- | modules/gdnavigation/nav_utils.h | 46 | ||||
-rw-r--r-- | modules/gdnavigation/navigation_mesh_editor_plugin.cpp | 20 | ||||
-rw-r--r-- | modules/gdnavigation/navigation_mesh_editor_plugin.h | 5 | ||||
-rw-r--r-- | modules/gdnavigation/navigation_mesh_generator.cpp | 63 | ||||
-rw-r--r-- | modules/gdnavigation/rvo_agent.cpp | 3 | ||||
-rw-r--r-- | modules/gdnavigation/rvo_agent.h | 2 |
12 files changed, 99 insertions, 149 deletions
diff --git a/modules/gdnavigation/gd_navigation_server.cpp b/modules/gdnavigation/gd_navigation_server.cpp index 278c27ae22..c80cdcfeab 100644 --- a/modules/gdnavigation/gd_navigation_server.cpp +++ b/modules/gdnavigation/gd_navigation_server.cpp @@ -114,8 +114,7 @@ void GdNavigationServer::MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3) GdNavigationServer::GdNavigationServer() : - NavigationServer3D(), - active(true) { + NavigationServer3D() { } GdNavigationServer::~GdNavigationServer() { @@ -250,9 +249,9 @@ COMMAND_2(region_set_map, RID, p_region, RID, p_map) { ERR_FAIL_COND(region == nullptr); if (region->get_map() != nullptr) { - - if (region->get_map()->get_self() == p_map) + if (region->get_map()->get_self() == p_map) { return; // Pointless + } region->get_map()->remove_region(region); region->set_map(nullptr); @@ -305,8 +304,9 @@ COMMAND_2(agent_set_map, RID, p_agent, RID, p_map) { ERR_FAIL_COND(agent == nullptr); if (agent->get_map()) { - if (agent->get_map()->get_self() == p_map) + if (agent->get_map()->get_self() == p_map) { return; // Pointless + } agent->get_map()->remove_agent(agent); } diff --git a/modules/gdnavigation/gd_navigation_server.h b/modules/gdnavigation/gd_navigation_server.h index 01d1a4fba9..e3e02f3d7c 100644 --- a/modules/gdnavigation/gd_navigation_server.h +++ b/modules/gdnavigation/gd_navigation_server.h @@ -78,7 +78,7 @@ class GdNavigationServer : public NavigationServer3D { mutable RID_PtrOwner<NavRegion> region_owner; mutable RID_PtrOwner<RvoAgent> agent_owner; - bool active; + bool active = true; Vector<NavMap *> active_maps; public: diff --git a/modules/gdnavigation/nav_map.cpp b/modules/gdnavigation/nav_map.cpp index 7e6a3f7a26..7919e6a01f 100644 --- a/modules/gdnavigation/nav_map.cpp +++ b/modules/gdnavigation/nav_map.cpp @@ -33,6 +33,7 @@ #include "core/os/threaded_array_processor.h" #include "nav_region.h" #include "rvo_agent.h" + #include <algorithm> /** @@ -41,16 +42,6 @@ #define USE_ENTRY_POINT -NavMap::NavMap() : - up(0, 1, 0), - cell_size(0.3), - edge_connection_margin(5.0), - regenerate_polygons(true), - regenerate_links(true), - agents_dirty(false), - deltatime(0.0), - map_update_id(0) {} - void NavMap::set_up(Vector3 p_up) { up = p_up; regenerate_polygons = true; @@ -80,7 +71,6 @@ gd::PointKey NavMap::get_point_key(const Vector3 &p_pos) const { } Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p_optimize) const { - const gd::Polygon *begin_poly = nullptr; const gd::Polygon *end_poly = nullptr; Vector3 begin_point; @@ -94,7 +84,6 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p // For each point cast a face and check the distance between the origin/destination for (size_t point_id = 2; point_id < p.points.size(); point_id++) { - Face3 f(p.points[point_id - 2].pos, p.points[point_id - 1].pos, p.points[point_id].pos); Vector3 spoint = f.get_closest_point_to(p_origin); float dpoint = spoint.distance_to(p_origin); @@ -120,7 +109,6 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p } if (begin_poly == end_poly) { - Vector<Vector3> path; path.resize(2); path.write[0] = begin_point; @@ -151,15 +139,15 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p bool is_reachable = true; while (found_route == false) { - { // Takes the current least_cost_poly neighbors and compute the traveled_distance of each for (size_t i = 0; i < navigation_polys[least_cost_id].poly->edges.size(); i++) { gd::NavigationPoly *least_cost_poly = &navigation_polys[least_cost_id]; const gd::Edge &edge = least_cost_poly->poly->edges[i]; - if (!edge.other_polygon) + if (!edge.other_polygon) { continue; + } #ifdef USE_ENTRY_POINT Vector3 edge_line[2] = { @@ -167,7 +155,7 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p least_cost_poly->poly->points[(i + 1) % least_cost_poly->poly->points.size()].pos }; - const Vector3 new_entry = Geometry::get_closest_point_to_segment(least_cost_poly->entry, edge_line); + const Vector3 new_entry = Geometry3D::get_closest_point_to_segment(least_cost_poly->entry, edge_line); const float new_distance = least_cost_poly->entry.distance_to(new_entry) + least_cost_poly->traveled_distance; #else const float new_distance = least_cost_poly->poly->center.distance_to(edge.other_polygon->center) + least_cost_poly->traveled_distance; @@ -181,7 +169,6 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p if (it != navigation_polys.end()) { // Oh this was visited already, can we win the cost? if (it->traveled_distance > new_distance) { - it->prev_navigation_poly_id = least_cost_id; it->back_navigation_edge = edge.other_edge; it->traveled_distance = new_distance; @@ -283,10 +270,8 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p } if (found_route) { - Vector<Vector3> path; if (p_optimize) { - // String pulling gd::NavigationPoly *apex_poly = &navigation_polys[least_cost_id]; @@ -300,7 +285,6 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p path.push_back(end_point); while (p) { - Vector3 left; Vector3 right; @@ -315,7 +299,6 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p left = p->poly->points[prev].pos; right = p->poly->points[prev_n].pos; - //if (CLOCK_TANGENT(apex_point,left,(left+right)*0.5).dot(up) < 0){ if (p->poly->clockwise) { SWAP(left, right); } @@ -329,7 +312,6 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p left_poly = p; portal_left = left; } else { - clip_path(navigation_polys, path, apex_poly, portal_right, right_poly); apex_point = portal_right; @@ -349,7 +331,6 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p right_poly = p; portal_right = right; } else { - clip_path(navigation_polys, path, apex_poly, portal_left, left_poly); apex_point = portal_left; @@ -362,15 +343,17 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p } } - if (p->prev_navigation_poly_id != -1) + if (p->prev_navigation_poly_id != -1) { p = &navigation_polys[p->prev_navigation_poly_id]; - else + } else { // The end p = nullptr; + } } - if (path[path.size() - 1] != begin_point) + if (path[path.size() - 1] != begin_point) { path.push_back(begin_point); + } path.invert(); @@ -380,7 +363,6 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p // Add mid points int np_id = least_cost_id; while (np_id != -1) { - #ifdef USE_ENTRY_POINT Vector3 point = navigation_polys[np_id].entry; #else @@ -402,7 +384,6 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p } Vector3 NavMap::get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const { - bool use_collision = p_use_collision; Vector3 closest_point; real_t closest_point_d = 1e20; @@ -413,7 +394,6 @@ Vector3 NavMap::get_closest_point_to_segment(const Vector3 &p_from, const Vector // For each point cast a face and check the distance to the segment for (size_t point_id = 2; point_id < p.points.size(); point_id += 1) { - const Face3 f(p.points[point_id - 2].pos, p.points[point_id - 1].pos, p.points[point_id].pos); Vector3 inters; if (f.intersects_segment(p_from, p_to, &inters)) { @@ -423,7 +403,6 @@ Vector3 NavMap::get_closest_point_to_segment(const Vector3 &p_from, const Vector use_collision = true; closest_point_d = d; } else if (closest_point_d > d) { - closest_point = inters; closest_point_d = d; } @@ -431,12 +410,10 @@ Vector3 NavMap::get_closest_point_to_segment(const Vector3 &p_from, const Vector } if (use_collision == false) { - for (size_t point_id = 0; point_id < p.points.size(); point_id += 1) { - Vector3 a, b; - Geometry::get_closest_points_between_segments( + Geometry3D::get_closest_points_between_segments( p_from, p_to, p.points[point_id].pos, @@ -446,7 +423,6 @@ Vector3 NavMap::get_closest_point_to_segment(const Vector3 &p_from, const Vector const real_t d = a.distance_to(b); if (d < closest_point_d) { - closest_point_d = d; closest_point = b; } @@ -469,7 +445,6 @@ Vector3 NavMap::get_closest_point(const Vector3 &p_point) const { // For each point cast a face and check the distance to the point for (size_t point_id = 2; point_id < p.points.size(); point_id += 1) { - const Face3 f(p.points[point_id - 2].pos, p.points[point_id - 1].pos, p.points[point_id].pos); const Vector3 inters = f.get_closest_point_to(p_point); const real_t d = inters.distance_to(p_point); @@ -496,7 +471,6 @@ Vector3 NavMap::get_closest_point_normal(const Vector3 &p_point) const { // For each point cast a face and check the distance to the point for (size_t point_id = 2; point_id < p.points.size(); point_id += 1) { - const Face3 f(p.points[point_id - 2].pos, p.points[point_id - 1].pos, p.points[point_id].pos); const Vector3 inters = f.get_closest_point_to(p_point); const real_t d = inters.distance_to(p_point); @@ -524,7 +498,6 @@ RID NavMap::get_closest_point_owner(const Vector3 &p_point) const { // For each point cast a face and check the distance to the point for (size_t point_id = 2; point_id < p.points.size(); point_id += 1) { - const Face3 f(p.points[point_id - 2].pos, p.points[point_id - 1].pos, p.points[point_id].pos); const Vector3 inters = f.get_closest_point_to(p_point); const real_t d = inters.distance_to(p_point); @@ -588,7 +561,6 @@ void NavMap::remove_agent_as_controlled(RvoAgent *agent) { } void NavMap::sync() { - if (regenerate_polygons) { for (size_t r(0); r < regions.size(); r++) { regions[r]->scratch_polygons(); @@ -741,8 +713,9 @@ void NavMap::sync() { if (agents_dirty) { std::vector<RVO::Agent *> raw_agents; raw_agents.reserve(agents.size()); - for (size_t i(0); i < agents.size(); i++) + for (size_t i(0); i < agents.size(); i++) { raw_agents.push_back(agents[i]->get_agent()); + } rvo.buildAgentTree(raw_agents); } @@ -776,17 +749,18 @@ void NavMap::dispatch_callbacks() { void NavMap::clip_path(const std::vector<gd::NavigationPoly> &p_navigation_polys, Vector<Vector3> &path, const gd::NavigationPoly *from_poly, const Vector3 &p_to_point, const gd::NavigationPoly *p_to_poly) const { Vector3 from = path[path.size() - 1]; - if (from.distance_to(p_to_point) < CMP_EPSILON) + if (from.distance_to(p_to_point) < CMP_EPSILON) { return; + } Plane cut_plane; cut_plane.normal = (from - p_to_point).cross(up); - if (cut_plane.normal == Vector3()) + if (cut_plane.normal == Vector3()) { return; + } cut_plane.normal.normalize(); cut_plane.d = cut_plane.normal.dot(from); while (from_poly != p_to_poly) { - int back_nav_edge = from_poly->back_navigation_edge; Vector3 a = from_poly->poly->points[back_nav_edge].pos; Vector3 b = from_poly->poly->points[(back_nav_edge + 1) % from_poly->poly->points.size()].pos; @@ -795,7 +769,6 @@ void NavMap::clip_path(const std::vector<gd::NavigationPoly> &p_navigation_polys from_poly = &p_navigation_polys[from_poly->prev_navigation_poly_id]; if (a.distance_to(b) > CMP_EPSILON) { - Vector3 inters; if (cut_plane.intersects_segment(a, b, &inters)) { if (inters.distance_to(p_to_point) > CMP_EPSILON && inters.distance_to(path[path.size() - 1]) > CMP_EPSILON) { diff --git a/modules/gdnavigation/nav_map.h b/modules/gdnavigation/nav_map.h index 4543f00926..892755f3f9 100644 --- a/modules/gdnavigation/nav_map.h +++ b/modules/gdnavigation/nav_map.h @@ -46,19 +46,18 @@ class RvoAgent; class NavRegion; class NavMap : public NavRid { - /// Map Up - Vector3 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. - real_t cell_size; + real_t cell_size = 0.3; /// This value is used to detect the near edges to connect. - real_t edge_connection_margin; + real_t edge_connection_margin = 5.0; - bool regenerate_polygons; - bool regenerate_links; + bool regenerate_polygons = true; + bool regenerate_links = true; std::vector<NavRegion *> regions; @@ -69,7 +68,7 @@ class NavMap : public NavRid { RVO::KdTree rvo; /// Is agent array modified? - bool agents_dirty; + bool agents_dirty = false; /// All the Agents (even the controlled one) std::vector<RvoAgent *> agents; @@ -78,13 +77,13 @@ class NavMap : public NavRid { std::vector<RvoAgent *> controlled_agents; /// Physics delta time - real_t deltatime; + real_t deltatime = 0.0; /// Change the id each time the map is updated. - uint32_t map_update_id; + uint32_t map_update_id = 0; public: - NavMap(); + NavMap() {} void set_up(Vector3 p_up); Vector3 get_up() const { diff --git a/modules/gdnavigation/nav_region.cpp b/modules/gdnavigation/nav_region.cpp index b91376f761..51fba67cc3 100644 --- a/modules/gdnavigation/nav_region.cpp +++ b/modules/gdnavigation/nav_region.cpp @@ -36,11 +36,6 @@ @author AndreaCatania */ -NavRegion::NavRegion() : - map(nullptr), - polygons_dirty(true) { -} - void NavRegion::set_map(NavMap *p_map) { map = p_map; polygons_dirty = true; @@ -75,13 +70,15 @@ void NavRegion::update_polygons() { return; } - if (mesh.is_null()) + if (mesh.is_null()) { return; + } Vector<Vector3> vertices = mesh->get_vertices(); int len = vertices.size(); - if (len == 0) + if (len == 0) { return; + } const Vector3 *vertices_r = vertices.ptr(); @@ -89,7 +86,6 @@ void NavRegion::update_polygons() { // Build for (size_t i(0); i < polygons.size(); i++) { - gd::Polygon &p = polygons[i]; p.owner = this; @@ -103,7 +99,6 @@ void NavRegion::update_polygons() { float sum(0); for (int j(0); j < mesh_poly.size(); j++) { - int idx = indices[j]; if (idx < 0 || idx >= len) { valid = false; diff --git a/modules/gdnavigation/nav_region.h b/modules/gdnavigation/nav_region.h index f35ee4bea0..731855bfb5 100644 --- a/modules/gdnavigation/nav_region.h +++ b/modules/gdnavigation/nav_region.h @@ -45,17 +45,17 @@ class NavMap; class NavRegion; class NavRegion : public NavRid { - NavMap *map; + NavMap *map = nullptr; Transform transform; Ref<NavigationMesh> mesh; - bool polygons_dirty; + bool polygons_dirty = true; /// Cache std::vector<gd::Polygon> polygons; public: - NavRegion(); + NavRegion() {} void scratch_polygons() { polygons_dirty = true; diff --git a/modules/gdnavigation/nav_utils.h b/modules/gdnavigation/nav_utils.h index 3401284c31..40e54df553 100644 --- a/modules/gdnavigation/nav_utils.h +++ b/modules/gdnavigation/nav_utils.h @@ -32,6 +32,7 @@ #define NAV_UTILS_H #include "core/math/vector3.h" + #include <vector> /** @@ -44,7 +45,6 @@ namespace gd { struct Polygon; union PointKey { - struct { int64_t x : 21; int64_t y : 22; @@ -56,7 +56,6 @@ union PointKey { }; struct EdgeKey { - PointKey a; PointKey b; @@ -80,19 +79,15 @@ struct Point { struct Edge { /// This edge ID - int this_edge; + int this_edge = -1; /// Other Polygon - Polygon *other_polygon; + Polygon *other_polygon = nullptr; /// The other `Polygon` at this edge id has this `Polygon`. - int other_edge; + int other_edge = -1; - Edge() { - this_edge = -1; - other_polygon = nullptr; - other_edge = -1; - } + Edge() {} }; struct Polygon { @@ -112,40 +107,29 @@ struct Polygon { }; struct Connection { + Polygon *A = nullptr; + int A_edge = -1; + Polygon *B = nullptr; + int B_edge = -1; - Polygon *A; - int A_edge; - Polygon *B; - int B_edge; - - Connection() { - A = nullptr; - B = nullptr; - A_edge = -1; - B_edge = -1; - } + Connection() {} }; struct NavigationPoly { - uint32_t self_id; + uint32_t self_id = 0; /// This poly. const Polygon *poly; /// The previous navigation poly (id in the `navigation_poly` array). - int prev_navigation_poly_id; + int prev_navigation_poly_id = -1; /// The edge id in this `Poly` to reach the `prev_navigation_poly_id`. - uint32_t back_navigation_edge; + uint32_t back_navigation_edge = 0; /// The entry location of this poly. Vector3 entry; /// The distance to the destination. - float traveled_distance; + float traveled_distance = 0.0; NavigationPoly(const Polygon *p_poly) : - self_id(0), - poly(p_poly), - prev_navigation_poly_id(-1), - back_navigation_edge(0), - traveled_distance(0.0) { - } + poly(p_poly) {} bool operator==(const NavigationPoly &other) const { return this->poly == other.poly; diff --git a/modules/gdnavigation/navigation_mesh_editor_plugin.cpp b/modules/gdnavigation/navigation_mesh_editor_plugin.cpp index abaf73ba6a..648f4f7cdd 100644 --- a/modules/gdnavigation/navigation_mesh_editor_plugin.cpp +++ b/modules/gdnavigation/navigation_mesh_editor_plugin.cpp @@ -38,7 +38,6 @@ #include "scene/gui/box_container.h" void NavigationMeshEditor::_node_removed(Node *p_node) { - if (p_node == node) { node = nullptr; @@ -47,9 +46,7 @@ void NavigationMeshEditor::_node_removed(Node *p_node) { } void NavigationMeshEditor::_notification(int p_option) { - if (p_option == NOTIFICATION_ENTER_TREE) { - button_bake->set_icon(get_theme_icon("Bake", "EditorIcons")); button_reset->set_icon(get_theme_icon("Reload", "EditorIcons")); } @@ -72,9 +69,9 @@ void NavigationMeshEditor::_bake_pressed() { } void NavigationMeshEditor::_clear_pressed() { - - if (node) + if (node) { NavigationMeshGenerator::get_singleton()->clear(node->get_navigation_mesh()); + } button_bake->set_pressed(false); bake_info->set_text(""); @@ -85,7 +82,6 @@ void NavigationMeshEditor::_clear_pressed() { } void NavigationMeshEditor::edit(NavigationRegion3D *p_nav_region) { - if (p_nav_region == nullptr || node == p_nav_region) { return; } @@ -97,16 +93,17 @@ void NavigationMeshEditor::_bind_methods() { } NavigationMeshEditor::NavigationMeshEditor() { - bake_hbox = memnew(HBoxContainer); - button_bake = memnew(ToolButton); + button_bake = memnew(Button); + button_bake->set_flat(true); bake_hbox->add_child(button_bake); button_bake->set_toggle_mode(true); button_bake->set_text(TTR("Bake NavMesh")); button_bake->connect("pressed", callable_mp(this, &NavigationMeshEditor::_bake_pressed)); - button_reset = memnew(ToolButton); + button_reset = memnew(Button); + button_reset->set_flat(true); bake_hbox->add_child(button_reset); // No button text, we only use a revert icon which is set when entering the tree. button_reset->set_tooltip(TTR("Clear the navigation mesh.")); @@ -124,22 +121,18 @@ 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); @@ -147,7 +140,6 @@ void NavigationMeshEditorPlugin::make_visible(bool p_visible) { } NavigationMeshEditorPlugin::NavigationMeshEditorPlugin(EditorNode *p_node) { - editor = p_node; navigation_mesh_editor = memnew(NavigationMeshEditor); editor->get_viewport()->add_child(navigation_mesh_editor); diff --git a/modules/gdnavigation/navigation_mesh_editor_plugin.h b/modules/gdnavigation/navigation_mesh_editor_plugin.h index 434981c9e0..728f958eaa 100644 --- a/modules/gdnavigation/navigation_mesh_editor_plugin.h +++ b/modules/gdnavigation/navigation_mesh_editor_plugin.h @@ -46,8 +46,8 @@ class NavigationMeshEditor : public Control { AcceptDialog *err_dialog; HBoxContainer *bake_hbox; - ToolButton *button_bake; - ToolButton *button_reset; + Button *button_bake; + Button *button_reset; Label *bake_info; NavigationRegion3D *node; @@ -67,7 +67,6 @@ public: }; class NavigationMeshEditorPlugin : public EditorPlugin { - GDCLASS(NavigationMeshEditorPlugin, EditorPlugin); NavigationMeshEditor *navigation_mesh_editor; diff --git a/modules/gdnavigation/navigation_mesh_generator.cpp b/modules/gdnavigation/navigation_mesh_generator.cpp index acb4f0461f..5329600e39 100644 --- a/modules/gdnavigation/navigation_mesh_generator.cpp +++ b/modules/gdnavigation/navigation_mesh_generator.cpp @@ -74,8 +74,9 @@ void NavigationMeshGenerator::_add_mesh(const Ref<Mesh> &p_mesh, const Transform for (int i = 0; i < p_mesh->get_surface_count(); i++) { current_vertex_count = p_verticies.size() / 3; - if (p_mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) + if (p_mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) { continue; + } int index_count = 0; if (p_mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) { @@ -94,7 +95,6 @@ void NavigationMeshGenerator::_add_mesh(const Ref<Mesh> &p_mesh, const Transform const Vector3 *vr = mesh_vertices.ptr(); if (p_mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_INDEX) { - Vector<int> mesh_indices = a[Mesh::ARRAY_INDEX]; const int *ir = mesh_indices.ptr(); @@ -139,9 +139,7 @@ void NavigationMeshGenerator::_add_faces(const PackedVector3Array &p_faces, cons } void NavigationMeshGenerator::_parse_geometry(Transform p_accumulated_transform, Node *p_node, Vector<float> &p_verticies, Vector<int> &p_indices, int p_generate_from, uint32_t p_collision_mask, bool p_recurse_children) { - if (Object::cast_to<MeshInstance3D>(p_node) && p_generate_from != NavigationMesh::PARSED_GEOMETRY_STATIC_COLLIDERS) { - MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(p_node); Ref<Mesh> mesh = mesh_instance->get_mesh(); if (mesh.is_valid()) { @@ -151,7 +149,6 @@ void NavigationMeshGenerator::_parse_geometry(Transform p_accumulated_transform, #ifdef MODULE_CSG_ENABLED if (Object::cast_to<CSGShape3D>(p_node) && p_generate_from != NavigationMesh::PARSED_GEOMETRY_STATIC_COLLIDERS) { - CSGShape3D *csg_shape = Object::cast_to<CSGShape3D>(p_node); Array meshes = csg_shape->get_meshes(); if (!meshes.empty()) { @@ -167,7 +164,6 @@ void NavigationMeshGenerator::_parse_geometry(Transform p_accumulated_transform, StaticBody3D *static_body = Object::cast_to<StaticBody3D>(p_node); if (static_body->get_collision_layer() & p_collision_mask) { - for (int i = 0; i < p_node->get_child_count(); ++i) { Node *child = p_node->get_child(i); if (Object::cast_to<CollisionShape3D>(child)) { @@ -222,7 +218,7 @@ void NavigationMeshGenerator::_parse_geometry(Transform p_accumulated_transform, ConvexPolygonShape3D *convex_polygon = Object::cast_to<ConvexPolygonShape3D>(*s); if (convex_polygon) { Vector<Vector3> varr = Variant(convex_polygon->get_points()); - Geometry::MeshData md; + Geometry3D::MeshData md; Error err = QuickHull::build(varr, md); @@ -230,7 +226,7 @@ void NavigationMeshGenerator::_parse_geometry(Transform p_accumulated_transform, PackedVector3Array faces; for (int j = 0; j < md.faces.size(); ++j) { - Geometry::MeshData::Face face = md.faces[j]; + Geometry3D::MeshData::Face face = md.faces[j]; for (int k = 2; k < face.indices.size(); ++k) { faces.push_back(md.vertices[face.indices[0]]); @@ -278,7 +274,6 @@ void NavigationMeshGenerator::_parse_geometry(Transform p_accumulated_transform, } void NavigationMeshGenerator::_convert_detail_mesh_to_native_navigation_mesh(const rcPolyMeshDetail *p_detail_mesh, Ref<NavigationMesh> p_nav_mesh) { - Vector<Vector3> nav_vertices; for (int i = 0; i < p_detail_mesh->nverts; i++) { @@ -320,8 +315,9 @@ void NavigationMeshGenerator::_build_recast_navigation_mesh( rcContext ctx; #ifdef TOOLS_ENABLED - if (ep) + if (ep) { ep->step(TTR("Setting up Configuration..."), 1); + } #endif const float *verts = vertices.ptr(); @@ -357,14 +353,16 @@ void NavigationMeshGenerator::_build_recast_navigation_mesh( cfg.bmax[2] = bmax[2]; #ifdef TOOLS_ENABLED - if (ep) + if (ep) { ep->step(TTR("Calculating grid size..."), 2); + } #endif rcCalcGridSize(cfg.bmin, cfg.bmax, cfg.cs, &cfg.width, &cfg.height); #ifdef TOOLS_ENABLED - if (ep) + if (ep) { ep->step(TTR("Creating heightfield..."), 3); + } #endif hf = rcAllocHeightfield(); @@ -372,8 +370,9 @@ void NavigationMeshGenerator::_build_recast_navigation_mesh( ERR_FAIL_COND(!rcCreateHeightfield(&ctx, *hf, cfg.width, cfg.height, cfg.bmin, cfg.bmax, cfg.cs, cfg.ch)); #ifdef TOOLS_ENABLED - if (ep) + if (ep) { ep->step(TTR("Marking walkable triangles..."), 4); + } #endif { Vector<unsigned char> tri_areas; @@ -387,16 +386,20 @@ void NavigationMeshGenerator::_build_recast_navigation_mesh( ERR_FAIL_COND(!rcRasterizeTriangles(&ctx, verts, nverts, tris, tri_areas.ptr(), ntris, *hf, cfg.walkableClimb)); } - if (p_nav_mesh->get_filter_low_hanging_obstacles()) + if (p_nav_mesh->get_filter_low_hanging_obstacles()) { rcFilterLowHangingWalkableObstacles(&ctx, cfg.walkableClimb, *hf); - if (p_nav_mesh->get_filter_ledge_spans()) + } + if (p_nav_mesh->get_filter_ledge_spans()) { rcFilterLedgeSpans(&ctx, cfg.walkableHeight, cfg.walkableClimb, *hf); - if (p_nav_mesh->get_filter_walkable_low_height_spans()) + } + if (p_nav_mesh->get_filter_walkable_low_height_spans()) { rcFilterWalkableLowHeightSpans(&ctx, cfg.walkableHeight, *hf); + } #ifdef TOOLS_ENABLED - if (ep) + if (ep) { ep->step(TTR("Constructing compact heightfield..."), 5); + } #endif chf = rcAllocCompactHeightfield(); @@ -408,15 +411,17 @@ void NavigationMeshGenerator::_build_recast_navigation_mesh( hf = nullptr; #ifdef TOOLS_ENABLED - if (ep) + if (ep) { ep->step(TTR("Eroding walkable area..."), 6); + } #endif ERR_FAIL_COND(!rcErodeWalkableArea(&ctx, cfg.walkableRadius, *chf)); #ifdef TOOLS_ENABLED - if (ep) + if (ep) { ep->step(TTR("Partitioning..."), 7); + } #endif if (p_nav_mesh->get_sample_partition_type() == NavigationMesh::SAMPLE_PARTITION_WATERSHED) { @@ -429,8 +434,9 @@ void NavigationMeshGenerator::_build_recast_navigation_mesh( } #ifdef TOOLS_ENABLED - if (ep) + if (ep) { ep->step(TTR("Creating contours..."), 8); + } #endif cset = rcAllocContourSet(); @@ -439,8 +445,9 @@ void NavigationMeshGenerator::_build_recast_navigation_mesh( ERR_FAIL_COND(!rcBuildContours(&ctx, *chf, cfg.maxSimplificationError, cfg.maxEdgeLen, *cset)); #ifdef TOOLS_ENABLED - if (ep) + if (ep) { ep->step(TTR("Creating polymesh..."), 9); + } #endif poly_mesh = rcAllocPolyMesh(); @@ -457,8 +464,9 @@ void NavigationMeshGenerator::_build_recast_navigation_mesh( cset = nullptr; #ifdef TOOLS_ENABLED - if (ep) + if (ep) { ep->step(TTR("Converting to native navigation mesh..."), 10); + } #endif _convert_detail_mesh_to_native_navigation_mesh(detail_mesh, p_nav_mesh); @@ -481,7 +489,6 @@ NavigationMeshGenerator::~NavigationMeshGenerator() { } void NavigationMeshGenerator::bake(Ref<NavigationMesh> p_nav_mesh, Node *p_node) { - ERR_FAIL_COND(!p_nav_mesh.is_valid()); #ifdef TOOLS_ENABLED @@ -490,8 +497,9 @@ void NavigationMeshGenerator::bake(Ref<NavigationMesh> p_nav_mesh, Node *p_node) ep = memnew(EditorProgress("bake", TTR("Navigation Mesh Generator Setup:"), 11)); } - if (ep) + if (ep) { ep->step(TTR("Parsing Geometry..."), 0); + } #endif Vector<float> vertices; @@ -514,7 +522,6 @@ void NavigationMeshGenerator::bake(Ref<NavigationMesh> p_nav_mesh, Node *p_node) } if (vertices.size() > 0 && indices.size() > 0) { - rcHeightfield *hf = nullptr; rcCompactHeightfield *chf = nullptr; rcContourSet *cset = nullptr; @@ -551,11 +558,13 @@ void NavigationMeshGenerator::bake(Ref<NavigationMesh> p_nav_mesh, Node *p_node) } #ifdef TOOLS_ENABLED - if (ep) + if (ep) { ep->step(TTR("Done!"), 11); + } - if (ep) + if (ep) { memdelete(ep); + } #endif } diff --git a/modules/gdnavigation/rvo_agent.cpp b/modules/gdnavigation/rvo_agent.cpp index 3c39f02c26..1e1bdbd07d 100644 --- a/modules/gdnavigation/rvo_agent.cpp +++ b/modules/gdnavigation/rvo_agent.cpp @@ -36,8 +36,7 @@ @author AndreaCatania */ -RvoAgent::RvoAgent() : - map(nullptr) { +RvoAgent::RvoAgent() { callback.id = ObjectID(); } diff --git a/modules/gdnavigation/rvo_agent.h b/modules/gdnavigation/rvo_agent.h index 914cbaa7d9..f5c579ba84 100644 --- a/modules/gdnavigation/rvo_agent.h +++ b/modules/gdnavigation/rvo_agent.h @@ -49,7 +49,7 @@ class RvoAgent : public NavRid { Variant new_velocity; }; - NavMap *map; + NavMap *map = nullptr; RVO::Agent agent; AvoidanceComputedCallback callback; uint32_t map_update_id; |