2023-01-05 12:25:55 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2020-01-10 11:22:34 +00:00
|
|
|
|
|
|
|
#ifndef NAV_UTILS_H
|
|
|
|
#define NAV_UTILS_H
|
|
|
|
|
|
|
|
#include "core/math/vector3.h"
|
2022-05-13 13:04:37 +00:00
|
|
|
#include "core/templates/hash_map.h"
|
|
|
|
#include "core/templates/hashfuncs.h"
|
2022-07-28 17:24:14 +00:00
|
|
|
#include "core/templates/local_vector.h"
|
2020-01-10 11:22:34 +00:00
|
|
|
|
2022-01-30 23:39:52 +00:00
|
|
|
class NavBase;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
|
|
|
namespace gd {
|
|
|
|
struct Polygon;
|
|
|
|
|
|
|
|
union PointKey {
|
|
|
|
struct {
|
|
|
|
int64_t x : 21;
|
|
|
|
int64_t y : 22;
|
|
|
|
int64_t z : 21;
|
|
|
|
};
|
|
|
|
|
2021-02-08 09:57:18 +00:00
|
|
|
uint64_t key = 0;
|
2020-01-10 11:22:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct EdgeKey {
|
|
|
|
PointKey a;
|
|
|
|
PointKey b;
|
|
|
|
|
2022-05-13 13:04:37 +00:00
|
|
|
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);
|
2020-01-10 11:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2021-03-15 11:45:28 +00:00
|
|
|
/// The gateway in the edge, as, in some case, the whole edge might not be navigable.
|
|
|
|
struct Connection {
|
2022-01-30 23:39:52 +00:00
|
|
|
/// Polygon that this connection leads to.
|
2021-03-15 11:45:28 +00:00
|
|
|
Polygon *polygon = nullptr;
|
2022-01-30 23:39:52 +00:00
|
|
|
|
|
|
|
/// Edge of the source polygon where this connection starts from.
|
2021-03-15 11:45:28 +00:00
|
|
|
int edge = -1;
|
2022-01-30 23:39:52 +00:00
|
|
|
|
|
|
|
/// Point on the edge where the gateway leading to the poly starts.
|
2021-03-15 11:45:28 +00:00
|
|
|
Vector3 pathway_start;
|
2022-01-30 23:39:52 +00:00
|
|
|
|
|
|
|
/// Point on the edge where the gateway leading to the poly ends.
|
2021-03-15 11:45:28 +00:00
|
|
|
Vector3 pathway_end;
|
|
|
|
};
|
2022-01-30 23:39:52 +00:00
|
|
|
|
|
|
|
/// Connections from this edge to other polygons.
|
2021-03-15 11:45:28 +00:00
|
|
|
Vector<Connection> connections;
|
2020-01-10 11:22:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Polygon {
|
2022-01-30 23:39:52 +00:00
|
|
|
/// Navigation region or link that contains this polygon.
|
|
|
|
const NavBase *owner = nullptr;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
|
|
|
/// The points of this `Polygon`
|
2022-07-28 17:24:14 +00:00
|
|
|
LocalVector<Point> points;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
2022-01-30 23:39:52 +00:00
|
|
|
/// Are the points clockwise?
|
2020-01-10 11:22:34 +00:00
|
|
|
bool clockwise;
|
|
|
|
|
|
|
|
/// The edges of this `Polygon`
|
2022-07-28 17:24:14 +00:00
|
|
|
LocalVector<Edge> edges;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
|
|
|
/// The center of this `Polygon`
|
|
|
|
Vector3 center;
|
2023-10-23 16:16:05 +00:00
|
|
|
|
|
|
|
real_t surface_area = 0.0;
|
2020-01-10 11:22:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct NavigationPoly {
|
2020-05-12 15:01:17 +00:00
|
|
|
uint32_t self_id = 0;
|
2020-01-10 11:22:34 +00:00
|
|
|
/// This poly.
|
|
|
|
const Polygon *poly;
|
2021-03-15 11:45:28 +00:00
|
|
|
|
|
|
|
/// Those 4 variables are used to travel the path backwards.
|
|
|
|
int back_navigation_poly_id = -1;
|
2022-01-30 23:39:52 +00:00
|
|
|
int back_navigation_edge = -1;
|
2021-03-15 11:45:28 +00:00
|
|
|
Vector3 back_navigation_edge_pathway_start;
|
|
|
|
Vector3 back_navigation_edge_pathway_end;
|
|
|
|
|
2022-12-06 22:32:11 +00:00
|
|
|
/// The entry position of this poly.
|
2020-01-10 11:22:34 +00:00
|
|
|
Vector3 entry;
|
|
|
|
/// The distance to the destination.
|
2023-03-07 14:16:07 +00:00
|
|
|
real_t traveled_distance = 0.0;
|
2020-01-10 11:22:34 +00:00
|
|
|
|
2022-07-28 17:24:14 +00:00
|
|
|
NavigationPoly() { poly = nullptr; }
|
|
|
|
|
2020-01-10 11:22:34 +00:00
|
|
|
NavigationPoly(const Polygon *p_poly) :
|
2020-05-12 15:01:17 +00:00
|
|
|
poly(p_poly) {}
|
2020-01-10 11:22:34 +00:00
|
|
|
|
|
|
|
bool operator==(const NavigationPoly &other) const {
|
2024-01-28 20:51:39 +00:00
|
|
|
return poly == other.poly;
|
2020-01-10 11:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const NavigationPoly &other) const {
|
|
|
|
return !operator==(other);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-13 15:07:01 +00:00
|
|
|
struct ClosestPointQueryResult {
|
|
|
|
Vector3 point;
|
|
|
|
Vector3 normal;
|
|
|
|
RID owner;
|
|
|
|
};
|
|
|
|
|
2020-01-10 11:22:34 +00:00
|
|
|
} // namespace gd
|
|
|
|
|
|
|
|
#endif // NAV_UTILS_H
|