navigation2d.h
1 #ifndef NAVIGATION_2D_H
2 #define NAVIGATION_2D_H
3 
4 #include "scene/2d/node_2d.h"
5 #include "scene/2d/navigation_polygon.h"
6 
7 class Navigation2D : public Node2D {
8 
9  OBJ_TYPE( Navigation2D, Node2D);
10 
11 
12  union Point {
13 
14  struct {
15  int64_t x:32;
16  int64_t y:32;
17  };
18 
19  uint64_t key;
20  bool operator<(const Point& p_key) const { return key < p_key.key; }
21  };
22 
23 
24  struct EdgeKey {
25 
26  Point a;
27  Point b;
28 
29  bool operator<(const EdgeKey& p_key) const {
30  return (a.key==p_key.a.key)?(b.key<p_key.b.key):(a.key<p_key.a.key);
31  };
32 
33  EdgeKey(const Point& p_a=Point(),const Point& p_b=Point()) {
34  a=p_a;
35  b=p_b;
36  if (a.key > b.key) {
37  SWAP(a,b);
38  }
39  }
40  };
41 
42 
43  struct NavMesh;
44  struct Polygon;
45 
46  struct ConnectionPending {
47 
48  Polygon *polygon;
49  int edge;
50  };
51 
52  struct Polygon {
53 
54  struct Edge {
55  Point point;
56  Polygon *C; //connection
57  int C_edge;
59  Edge() { C=NULL; C_edge=-1; P=NULL; }
60  };
61 
62  Vector<Edge> edges;
63 
64  Vector2 center;
65  Vector2 entry;
66 
67  float distance;
68  int prev_edge;
69 
70  bool clockwise;
71 
72  NavMesh *owner;
73  };
74 
75 
76  struct Connection {
77 
78  Polygon *A;
79  int A_edge;
80  Polygon *B;
81  int B_edge;
82 
84 
85  Connection() { A=NULL; B=NULL; A_edge=-1; B_edge=-1;}
86  };
87 
88  Map<EdgeKey,Connection> connections;
89 
90 
91  struct NavMesh {
92 
93  Object *owner;
94  Matrix32 xform;
95  bool linked;
96  Ref<NavigationPolygon> navpoly;
97  List<Polygon> polygons;
98 
99  };
100 
101 
102 
103  _FORCE_INLINE_ Point _get_point(const Vector2& p_pos) const {
104 
105  int x = int(Math::floor(p_pos.x/cell_size));
106  int y = int(Math::floor(p_pos.y/cell_size));
107 
108  Point p;
109  p.key=0;
110  p.x=x;
111  p.y=y;
112  return p;
113 
114  }
115 
116  _FORCE_INLINE_ Vector2 _get_vertex(const Point& p_point) const {
117 
118  return Vector2(p_point.x,p_point.y)*cell_size;
119  }
120 
121 
122 
123  void _navpoly_link(int p_id);
124  void _navpoly_unlink(int p_id);
125 
126  float cell_size;
127  Map<int,NavMesh> navpoly_map;
128  int last_id;
129 #if 0
130  void _clip_path(Vector<Vector2>& path,Polygon *from_poly, const Vector2& p_to_point, Polygon* p_to_poly);
131 #endif
132 protected:
133 
134  static void _bind_methods();
135 
136 public:
137 
138  //API should be as dynamic as possible
139  int navpoly_create(const Ref<NavigationPolygon>& p_mesh,const Matrix32& p_xform,Object* p_owner=NULL);
140  void navpoly_set_transform(int p_id, const Matrix32& p_xform);
141  void navpoly_remove(int p_id);
142 
143  Vector<Vector2> get_simple_path(const Vector2& p_start, const Vector2& p_end,bool p_optimize=true);
144  Vector2 get_closest_point(const Vector2& p_point);
145  Object* get_closest_point_owner(const Vector2& p_point);
146 
147  Navigation2D();
148 };
149 
150 
151 #endif // Navigation2D2D_H
Definition: navigation2d.h:7
Definition: navigation2d.h:54
Definition: list.h:44
Definition: math_2d.h:65
Definition: math_2d.h:554
Definition: node_2d.h:34
Definition: object.h:317