navigation2d.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #ifndef NAVIGATION_2D_H
  2. #define NAVIGATION_2D_H
  3. #include "scene/2d/node_2d.h"
  4. #include "scene/2d/navigation_polygon.h"
  5. class Navigation2D : public Node2D {
  6. OBJ_TYPE( Navigation2D, Node2D);
  7. union Point {
  8. struct {
  9. int64_t x:32;
  10. int64_t y:32;
  11. };
  12. uint64_t key;
  13. bool operator<(const Point& p_key) const { return key < p_key.key; }
  14. };
  15. struct EdgeKey {
  16. Point a;
  17. Point b;
  18. bool operator<(const EdgeKey& p_key) const {
  19. return (a.key==p_key.a.key)?(b.key<p_key.b.key):(a.key<p_key.a.key);
  20. };
  21. EdgeKey(const Point& p_a=Point(),const Point& p_b=Point()) {
  22. a=p_a;
  23. b=p_b;
  24. if (a.key > b.key) {
  25. SWAP(a,b);
  26. }
  27. }
  28. };
  29. struct NavMesh;
  30. struct Polygon;
  31. struct ConnectionPending {
  32. Polygon *polygon;
  33. int edge;
  34. };
  35. struct Polygon {
  36. struct Edge {
  37. Point point;
  38. Polygon *C; //connection
  39. int C_edge;
  40. List<ConnectionPending>::Element *P;
  41. Edge() { C=NULL; C_edge=-1; P=NULL; }
  42. };
  43. Vector<Edge> edges;
  44. Vector2 center;
  45. Vector2 entry;
  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. Matrix32 xform;
  63. bool linked;
  64. Ref<NavigationPolygon> navpoly;
  65. List<Polygon> polygons;
  66. };
  67. _FORCE_INLINE_ Point _get_point(const Vector2& 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. Point p;
  71. p.key=0;
  72. p.x=x;
  73. p.y=y;
  74. return p;
  75. }
  76. _FORCE_INLINE_ Vector2 _get_vertex(const Point& p_point) const {
  77. return Vector2(p_point.x,p_point.y)*cell_size;
  78. }
  79. void _navpoly_link(int p_id);
  80. void _navpoly_unlink(int p_id);
  81. float cell_size;
  82. Map<int,NavMesh> navpoly_map;
  83. int last_id;
  84. #if 0
  85. void _clip_path(Vector<Vector2>& path,Polygon *from_poly, const Vector2& p_to_point, Polygon* p_to_poly);
  86. #endif
  87. protected:
  88. static void _bind_methods();
  89. public:
  90. //API should be as dynamic as possible
  91. int navpoly_create(const Ref<NavigationPolygon>& p_mesh,const Matrix32& p_xform,Object* p_owner=NULL);
  92. void navpoly_set_transform(int p_id, const Matrix32& p_xform);
  93. void navpoly_remove(int p_id);
  94. Vector<Vector2> get_simple_path(const Vector2& p_start, const Vector2& p_end,bool p_optimize=true);
  95. Vector2 get_closest_point(const Vector2& p_point);
  96. Object* get_closest_point_owner(const Vector2& p_point);
  97. Navigation2D();
  98. };
  99. #endif // Navigation2D2D_H