navigation.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #ifndef NAVIGATION_H
  2. #define NAVIGATION_H
  3. #include "scene/3d/spatial.h"
  4. #include "scene/3d/navigation_mesh.h"
  5. class Navigation : public Spatial {
  6. OBJ_TYPE( Navigation, Spatial);
  7. union Point {
  8. struct {
  9. int64_t x:21;
  10. int64_t y:22;
  11. int64_t z:21;
  12. };
  13. uint64_t key;
  14. bool operator<(const Point& p_key) const { return key < p_key.key; }
  15. };
  16. struct EdgeKey {
  17. Point a;
  18. Point b;
  19. bool operator<(const EdgeKey& p_key) const {
  20. return (a.key==p_key.a.key)?(b.key<p_key.b.key):(a.key<p_key.a.key);
  21. };
  22. EdgeKey(const Point& p_a=Point(),const Point& p_b=Point()) {
  23. a=p_a;
  24. b=p_b;
  25. if (a.key > b.key) {
  26. SWAP(a,b);
  27. }
  28. }
  29. };
  30. struct NavMesh;
  31. struct Polygon;
  32. struct ConnectionPending {
  33. Polygon *polygon;
  34. int edge;
  35. };
  36. struct Polygon {
  37. struct Edge {
  38. Point point;
  39. Polygon *C; //connection
  40. int C_edge;
  41. List<ConnectionPending>::Element *P;
  42. Edge() { C=NULL; C_edge=-1; P=NULL; }
  43. };
  44. Vector<Edge> edges;
  45. Vector3 center;
  46. float distance;
  47. int prev_edge;
  48. bool clockwise;
  49. NavMesh *owner;
  50. };
  51. struct Connection {
  52. Polygon *A;
  53. int A_edge;
  54. Polygon *B;
  55. int B_edge;
  56. List<ConnectionPending> pending;
  57. Connection() { A=NULL; B=NULL; A_edge=-1; B_edge=-1;}
  58. };
  59. Map<EdgeKey,Connection> connections;
  60. struct NavMesh {
  61. Object *owner;
  62. Transform xform;
  63. bool linked;
  64. Ref<NavigationMesh> navmesh;
  65. List<Polygon> polygons;
  66. };
  67. _FORCE_INLINE_ Point _get_point(const Vector3& p_pos) const {
  68. int x = int(Math::floor(p_pos.x/cell_size));
  69. int y = int(Math::floor(p_pos.y/cell_size));
  70. int z = int(Math::floor(p_pos.z/cell_size));
  71. Point p;
  72. p.key=0;
  73. p.x=x;
  74. p.y=y;
  75. p.z=z;
  76. return p;
  77. }
  78. _FORCE_INLINE_ Vector3 _get_vertex(const Point& p_point) const {
  79. return Vector3(p_point.x,p_point.y,p_point.z)*cell_size;
  80. }
  81. void _navmesh_link(int p_id);
  82. void _navmesh_unlink(int p_id);
  83. float cell_size;
  84. Map<int,NavMesh> navmesh_map;
  85. int last_id;
  86. Vector3 up;
  87. void _clip_path(Vector<Vector3>& path,Polygon *from_poly, const Vector3& p_to_point, Polygon* p_to_poly);
  88. protected:
  89. static void _bind_methods();
  90. public:
  91. void set_up_vector(const Vector3& p_up);
  92. Vector3 get_up_vector() const;
  93. //API should be as dynamic as possible
  94. int navmesh_create(const Ref<NavigationMesh>& p_mesh,const Transform& p_xform,Object* p_owner=NULL);
  95. void navmesh_set_transform(int p_id, const Transform& p_xform);
  96. void navmesh_remove(int p_id);
  97. Vector<Vector3> get_simple_path(const Vector3& p_start, const Vector3& p_end,bool p_optimize=true);
  98. Vector3 get_closest_point_to_segment(const Vector3& p_from,const Vector3& p_to,const bool& p_use_collision=false);
  99. Vector3 get_closest_point(const Vector3& p_point);
  100. Vector3 get_closest_point_normal(const Vector3& p_point);
  101. Object* get_closest_point_owner(const Vector3& p_point);
  102. Navigation();
  103. };
  104. #endif // NAVIGATION_H