nav_agent.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**************************************************************************/
  2. /* nav_agent.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef NAV_AGENT_H
  31. #define NAV_AGENT_H
  32. #include "nav_agent.h"
  33. #include "nav_rid.h"
  34. #include "core/object/class_db.h"
  35. #include "core/templates/local_vector.h"
  36. #include <Agent2d.h>
  37. #include <Agent3d.h>
  38. class NavMap;
  39. class NavAgent : public NavRid {
  40. Vector3 position;
  41. Vector3 target_position;
  42. Vector3 velocity;
  43. Vector3 velocity_forced;
  44. real_t height = 1.0;
  45. real_t radius = 1.0;
  46. real_t max_speed = 1.0;
  47. real_t time_horizon_agents = 1.0;
  48. real_t time_horizon_obstacles = 0.0;
  49. int max_neighbors = 5;
  50. real_t neighbor_distance = 5.0;
  51. Vector3 safe_velocity;
  52. bool clamp_speed = true; // Experimental, clamps velocity to max_speed.
  53. NavMap *map = nullptr;
  54. RVO2D::Agent2D rvo_agent_2d;
  55. RVO3D::Agent3D rvo_agent_3d;
  56. bool use_3d_avoidance = false;
  57. bool avoidance_enabled = false;
  58. uint32_t avoidance_layers = 1;
  59. uint32_t avoidance_mask = 1;
  60. real_t avoidance_priority = 1.0;
  61. Callable avoidance_callback = Callable();
  62. bool agent_dirty = true;
  63. uint32_t map_update_id = 0;
  64. bool paused = false;
  65. public:
  66. NavAgent();
  67. void set_avoidance_enabled(bool p_enabled);
  68. bool is_avoidance_enabled() { return avoidance_enabled; }
  69. void set_use_3d_avoidance(bool p_enabled);
  70. bool get_use_3d_avoidance() { return use_3d_avoidance; }
  71. void set_map(NavMap *p_map);
  72. NavMap *get_map() { return map; }
  73. bool is_map_changed();
  74. RVO2D::Agent2D *get_rvo_agent_2d() { return &rvo_agent_2d; }
  75. RVO3D::Agent3D *get_rvo_agent_3d() { return &rvo_agent_3d; }
  76. void set_avoidance_callback(Callable p_callback);
  77. bool has_avoidance_callback() const;
  78. void dispatch_avoidance_callback();
  79. void set_neighbor_distance(real_t p_neighbor_distance);
  80. real_t get_neighbor_distance() const { return neighbor_distance; }
  81. void set_max_neighbors(int p_max_neighbors);
  82. int get_max_neighbors() const { return max_neighbors; }
  83. void set_time_horizon_agents(real_t p_time_horizon);
  84. real_t get_time_horizon_agents() const { return time_horizon_agents; }
  85. void set_time_horizon_obstacles(real_t p_time_horizon);
  86. real_t get_time_horizon_obstacles() const { return time_horizon_obstacles; }
  87. void set_radius(real_t p_radius);
  88. real_t get_radius() const { return radius; }
  89. void set_height(real_t p_height);
  90. real_t get_height() const { return height; }
  91. void set_max_speed(real_t p_max_speed);
  92. real_t get_max_speed() const { return max_speed; }
  93. void set_position(const Vector3 p_position);
  94. const Vector3 &get_position() const { return position; }
  95. void set_target_position(const Vector3 p_target_position);
  96. const Vector3 &get_target_position() const { return target_position; }
  97. void set_velocity(const Vector3 p_velocity);
  98. const Vector3 &get_velocity() const { return velocity; }
  99. void set_velocity_forced(const Vector3 p_velocity);
  100. const Vector3 &get_velocity_forced() const { return velocity_forced; }
  101. void set_avoidance_layers(uint32_t p_layers);
  102. uint32_t get_avoidance_layers() const { return avoidance_layers; };
  103. void set_avoidance_mask(uint32_t p_mask);
  104. uint32_t get_avoidance_mask() const { return avoidance_mask; };
  105. void set_avoidance_priority(real_t p_priority);
  106. real_t get_avoidance_priority() const { return avoidance_priority; };
  107. void set_paused(bool p_paused);
  108. bool get_paused() const;
  109. bool check_dirty();
  110. // Updates this agent with rvo data after the rvo simulation avoidance step.
  111. void update();
  112. // RVO debug data from the last frame update.
  113. const Dictionary get_avoidance_data() const;
  114. private:
  115. void _update_rvo_agent_properties();
  116. };
  117. #endif // NAV_AGENT_H