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