2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2019-02-12 16:18:13 +00:00
|
|
|
/* navigation_2d.h */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2020-01-01 10:16:22 +00:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 12:46:12 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2015-02-14 15:09:52 +00:00
|
|
|
#ifndef NAVIGATION_2D_H
|
|
|
|
#define NAVIGATION_2D_H
|
|
|
|
|
|
|
|
#include "scene/2d/navigation_polygon.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "scene/2d/node_2d.h"
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
class Navigation2D : public Node2D {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
GDCLASS(Navigation2D, Node2D);
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
union Point {
|
|
|
|
|
|
|
|
struct {
|
2017-03-05 15:44:50 +00:00
|
|
|
int64_t x : 32;
|
|
|
|
int64_t y : 32;
|
2015-02-14 15:09:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
uint64_t key;
|
2017-03-05 15:44:50 +00:00
|
|
|
bool operator<(const Point &p_key) const { return key < p_key.key; }
|
2015-02-14 15:09:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct EdgeKey {
|
|
|
|
|
|
|
|
Point a;
|
|
|
|
Point b;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool operator<(const EdgeKey &p_key) const {
|
|
|
|
return (a.key == p_key.a.key) ? (b.key < p_key.b.key) : (a.key < p_key.a.key);
|
2015-02-14 15:09:52 +00:00
|
|
|
};
|
|
|
|
|
2017-12-06 20:36:34 +00:00
|
|
|
EdgeKey(const Point &p_a = Point(), const Point &p_b = Point()) :
|
|
|
|
a(p_a),
|
|
|
|
b(p_b) {
|
2015-02-14 15:09:52 +00:00
|
|
|
if (a.key > b.key) {
|
2017-03-05 15:44:50 +00:00
|
|
|
SWAP(a, b);
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NavMesh;
|
2015-06-01 22:42:34 +00:00
|
|
|
struct Polygon;
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2015-06-01 22:42:34 +00:00
|
|
|
struct ConnectionPending {
|
|
|
|
|
|
|
|
Polygon *polygon;
|
|
|
|
int edge;
|
|
|
|
};
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
struct Polygon {
|
|
|
|
|
|
|
|
struct Edge {
|
|
|
|
Point point;
|
|
|
|
Polygon *C; //connection
|
|
|
|
int C_edge;
|
2015-06-01 22:42:34 +00:00
|
|
|
List<ConnectionPending>::Element *P;
|
2017-03-05 15:44:50 +00:00
|
|
|
Edge() {
|
|
|
|
C = NULL;
|
|
|
|
C_edge = -1;
|
|
|
|
P = NULL;
|
|
|
|
}
|
2015-02-14 15:09:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Vector<Edge> edges;
|
|
|
|
|
|
|
|
Vector2 center;
|
2015-02-19 03:27:02 +00:00
|
|
|
Vector2 entry;
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
float distance;
|
|
|
|
int prev_edge;
|
|
|
|
|
2015-05-02 19:35:43 +00:00
|
|
|
bool clockwise;
|
|
|
|
|
2015-02-14 15:09:52 +00:00
|
|
|
NavMesh *owner;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Connection {
|
|
|
|
|
|
|
|
Polygon *A;
|
|
|
|
int A_edge;
|
|
|
|
Polygon *B;
|
|
|
|
int B_edge;
|
2015-06-01 22:42:34 +00:00
|
|
|
|
|
|
|
List<ConnectionPending> pending;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Connection() {
|
|
|
|
A = NULL;
|
|
|
|
B = NULL;
|
|
|
|
A_edge = -1;
|
|
|
|
B_edge = -1;
|
|
|
|
}
|
2015-02-14 15:09:52 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<EdgeKey, Connection> connections;
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
struct NavMesh {
|
|
|
|
|
|
|
|
Object *owner;
|
2017-01-11 03:52:51 +00:00
|
|
|
Transform2D xform;
|
2015-02-14 15:09:52 +00:00
|
|
|
bool linked;
|
|
|
|
Ref<NavigationPolygon> navpoly;
|
|
|
|
List<Polygon> polygons;
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ Point _get_point(const Vector2 &p_pos) const {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int x = int(Math::floor(p_pos.x / cell_size));
|
|
|
|
int y = int(Math::floor(p_pos.y / cell_size));
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
Point p;
|
2017-03-05 15:44:50 +00:00
|
|
|
p.key = 0;
|
|
|
|
p.x = x;
|
|
|
|
p.y = y;
|
2015-02-14 15:09:52 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ Vector2 _get_vertex(const Point &p_point) const {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return Vector2(p_point.x, p_point.y) * cell_size;
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void _navpoly_link(int p_id);
|
|
|
|
void _navpoly_unlink(int p_id);
|
|
|
|
|
|
|
|
float cell_size;
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<int, NavMesh> navpoly_map;
|
2015-02-14 15:09:52 +00:00
|
|
|
int last_id;
|
2017-08-27 19:07:15 +00:00
|
|
|
|
2015-02-14 15:09:52 +00:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
public:
|
|
|
|
//API should be as dynamic as possible
|
2017-12-15 20:09:06 +00:00
|
|
|
int navpoly_add(const Ref<NavigationPolygon> &p_mesh, const Transform2D &p_xform, Object *p_owner = NULL);
|
2017-03-05 15:44:50 +00:00
|
|
|
void navpoly_set_transform(int p_id, const Transform2D &p_xform);
|
2015-02-14 15:09:52 +00:00
|
|
|
void navpoly_remove(int p_id);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<Vector2> get_simple_path(const Vector2 &p_start, const Vector2 &p_end, bool p_optimize = true);
|
|
|
|
Vector2 get_closest_point(const Vector2 &p_point);
|
|
|
|
Object *get_closest_point_owner(const Vector2 &p_point);
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
Navigation2D();
|
|
|
|
};
|
|
|
|
|
2019-02-12 16:18:13 +00:00
|
|
|
#endif // NAVIGATION_2D_H
|