rvo2-godot-changes.patch 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. diff --git a/thirdparty/rvo2/Agent.cpp b/thirdparty/rvo2/Agent.cpp
  2. index 5e49a3554c..b35eee9c12 100644
  3. --- a/thirdparty/rvo2/Agent.cpp
  4. +++ b/thirdparty/rvo2/Agent.cpp
  5. @@ -105,18 +105,17 @@ namespace RVO {
  6. */
  7. void linearProgram4(const std::vector<Plane> &planes, size_t beginPlane, float radius, Vector3 &result);
  8. - Agent::Agent(RVOSimulator *sim) : sim_(sim), id_(0), maxNeighbors_(0), maxSpeed_(0.0f), neighborDist_(0.0f), radius_(0.0f), timeHorizon_(0.0f) { }
  9. + Agent::Agent() : id_(0), maxNeighbors_(0), maxSpeed_(0.0f), neighborDist_(0.0f), radius_(0.0f), timeHorizon_(0.0f), ignore_y_(false) { }
  10. - void Agent::computeNeighbors()
  11. + void Agent::computeNeighbors(KdTree *kdTree_)
  12. {
  13. agentNeighbors_.clear();
  14. -
  15. if (maxNeighbors_ > 0) {
  16. - sim_->kdTree_->computeAgentNeighbors(this, neighborDist_ * neighborDist_);
  17. + kdTree_->computeAgentNeighbors(this, neighborDist_ * neighborDist_);
  18. }
  19. }
  20. - void Agent::computeNewVelocity()
  21. + void Agent::computeNewVelocity(float timeStep)
  22. {
  23. orcaPlanes_.clear();
  24. const float invTimeHorizon = 1.0f / timeHorizon_;
  25. @@ -124,10 +123,24 @@ namespace RVO {
  26. /* Create agent ORCA planes. */
  27. for (size_t i = 0; i < agentNeighbors_.size(); ++i) {
  28. const Agent *const other = agentNeighbors_[i].second;
  29. - const Vector3 relativePosition = other->position_ - position_;
  30. - const Vector3 relativeVelocity = velocity_ - other->velocity_;
  31. - const float distSq = absSq(relativePosition);
  32. +
  33. + Vector3 relativePosition = other->position_ - position_;
  34. + Vector3 relativeVelocity = velocity_ - other->velocity_;
  35. const float combinedRadius = radius_ + other->radius_;
  36. +
  37. + // This is a Godot feature that allow the agents to avoid the collision
  38. + // by moving only on the horizontal plane relative to the player velocity.
  39. + if (ignore_y_) {
  40. + // Skip if these are in two different heights
  41. +#define ABS(m_v) (((m_v) < 0) ? (-(m_v)) : (m_v))
  42. + if (ABS(relativePosition[1]) > combinedRadius * 2) {
  43. + continue;
  44. + }
  45. + relativePosition[1] = 0;
  46. + relativeVelocity[1] = 0;
  47. + }
  48. +
  49. + const float distSq = absSq(relativePosition);
  50. const float combinedRadiusSq = sqr(combinedRadius);
  51. Plane plane;
  52. @@ -165,7 +178,7 @@ namespace RVO {
  53. }
  54. else {
  55. /* Collision. */
  56. - const float invTimeStep = 1.0f / sim_->timeStep_;
  57. + const float invTimeStep = 1.0f / timeStep;
  58. const Vector3 w = relativeVelocity - invTimeStep * relativePosition;
  59. const float wLength = abs(w);
  60. const Vector3 unitW = w / wLength;
  61. @@ -183,6 +196,11 @@ namespace RVO {
  62. if (planeFail < orcaPlanes_.size()) {
  63. linearProgram4(orcaPlanes_, planeFail, maxSpeed_, newVelocity_);
  64. }
  65. +
  66. + if (ignore_y_) {
  67. + // Not 100% necessary, but better to have.
  68. + newVelocity_[1] = prefVelocity_[1];
  69. + }
  70. }
  71. void Agent::insertAgentNeighbor(const Agent *agent, float &rangeSq)
  72. @@ -211,12 +229,6 @@ namespace RVO {
  73. }
  74. }
  75. - void Agent::update()
  76. - {
  77. - velocity_ = newVelocity_;
  78. - position_ += velocity_ * sim_->timeStep_;
  79. - }
  80. -
  81. bool linearProgram1(const std::vector<Plane> &planes, size_t planeNo, const Line &line, float radius, const Vector3 &optVelocity, bool directionOpt, Vector3 &result)
  82. {
  83. const float dotProduct = line.point * line.direction;
  84. diff --git a/thirdparty/rvo2/Agent.h b/thirdparty/rvo2/Agent.h
  85. index d3922ec645..45fbead2f5 100644
  86. --- a/thirdparty/rvo2/Agent.h
  87. +++ b/thirdparty/rvo2/Agent.h
  88. @@ -41,30 +41,52 @@
  89. #include <utility>
  90. #include <vector>
  91. -#include "RVOSimulator.h"
  92. #include "Vector3.h"
  93. +// Note: Slightly modified to work better in Godot.
  94. +// - The agent can be created by anyone.
  95. +// - The simulator pointer is removed.
  96. +// - The update function is removed.
  97. +// - The compute velocity function now need the timeStep.
  98. +// - Moved the `Plane` class here.
  99. +// - Added a new parameter `ignore_y_` in the `Agent`. This parameter is used to control a godot feature that allows to avoid collisions by moving on the horizontal plane.
  100. namespace RVO {
  101. + /**
  102. + * \brief Defines a plane.
  103. + */
  104. + class Plane {
  105. + public:
  106. + /**
  107. + * \brief A point on the plane.
  108. + */
  109. + Vector3 point;
  110. +
  111. + /**
  112. + * \brief The normal to the plane.
  113. + */
  114. + Vector3 normal;
  115. + };
  116. +
  117. /**
  118. * \brief Defines an agent in the simulation.
  119. */
  120. class Agent {
  121. - private:
  122. + public:
  123. /**
  124. * \brief Constructs an agent instance.
  125. * \param sim The simulator instance.
  126. */
  127. - explicit Agent(RVOSimulator *sim);
  128. + explicit Agent();
  129. /**
  130. * \brief Computes the neighbors of this agent.
  131. */
  132. - void computeNeighbors();
  133. + void computeNeighbors(class KdTree *kdTree_);
  134. /**
  135. * \brief Computes the new velocity of this agent.
  136. */
  137. - void computeNewVelocity();
  138. + void computeNewVelocity(float timeStep);
  139. /**
  140. * \brief Inserts an agent neighbor into the set of neighbors of this agent.
  141. @@ -73,16 +95,10 @@ namespace RVO {
  142. */
  143. void insertAgentNeighbor(const Agent *agent, float &rangeSq);
  144. - /**
  145. - * \brief Updates the three-dimensional position and three-dimensional velocity of this agent.
  146. - */
  147. - void update();
  148. -
  149. Vector3 newVelocity_;
  150. Vector3 position_;
  151. Vector3 prefVelocity_;
  152. Vector3 velocity_;
  153. - RVOSimulator *sim_;
  154. size_t id_;
  155. size_t maxNeighbors_;
  156. float maxSpeed_;
  157. @@ -91,9 +107,11 @@ namespace RVO {
  158. float timeHorizon_;
  159. std::vector<std::pair<float, const Agent *> > agentNeighbors_;
  160. std::vector<Plane> orcaPlanes_;
  161. + /// This is a godot feature that allows the Agent to avoid collision by mooving
  162. + /// on the horizontal plane.
  163. + bool ignore_y_;
  164. friend class KdTree;
  165. - friend class RVOSimulator;
  166. };
  167. }
  168. diff --git a/thirdparty/rvo2/KdTree.cpp b/thirdparty/rvo2/KdTree.cpp
  169. index 5e9e9777a6..c857f299df 100644
  170. --- a/thirdparty/rvo2/KdTree.cpp
  171. +++ b/thirdparty/rvo2/KdTree.cpp
  172. @@ -36,16 +36,15 @@
  173. #include "Agent.h"
  174. #include "Definitions.h"
  175. -#include "RVOSimulator.h"
  176. namespace RVO {
  177. const size_t RVO3D_MAX_LEAF_SIZE = 10;
  178. - KdTree::KdTree(RVOSimulator *sim) : sim_(sim) { }
  179. + KdTree::KdTree() { }
  180. - void KdTree::buildAgentTree()
  181. + void KdTree::buildAgentTree(std::vector<Agent *> agents)
  182. {
  183. - agents_ = sim_->agents_;
  184. + agents_.swap(agents);
  185. if (!agents_.empty()) {
  186. agentTree_.resize(2 * agents_.size() - 1);
  187. diff --git a/thirdparty/rvo2/KdTree.h b/thirdparty/rvo2/KdTree.h
  188. index a09384c20f..69d8920ce0 100644
  189. --- a/thirdparty/rvo2/KdTree.h
  190. +++ b/thirdparty/rvo2/KdTree.h
  191. @@ -41,6 +41,9 @@
  192. #include "Vector3.h"
  193. +// Note: Slightly modified to work better with Godot.
  194. +// - Removed `sim_`.
  195. +// - KdTree things are public
  196. namespace RVO {
  197. class Agent;
  198. class RVOSimulator;
  199. @@ -49,7 +52,7 @@ namespace RVO {
  200. * \brief Defines <i>k</i>d-trees for agents in the simulation.
  201. */
  202. class KdTree {
  203. - private:
  204. + public:
  205. /**
  206. * \brief Defines an agent <i>k</i>d-tree node.
  207. */
  208. @@ -90,12 +93,12 @@ namespace RVO {
  209. * \brief Constructs a <i>k</i>d-tree instance.
  210. * \param sim The simulator instance.
  211. */
  212. - explicit KdTree(RVOSimulator *sim);
  213. + explicit KdTree();
  214. /**
  215. * \brief Builds an agent <i>k</i>d-tree.
  216. */
  217. - void buildAgentTree();
  218. + void buildAgentTree(std::vector<Agent *> agents);
  219. void buildAgentTreeRecursive(size_t begin, size_t end, size_t node);
  220. @@ -110,7 +113,6 @@ namespace RVO {
  221. std::vector<Agent *> agents_;
  222. std::vector<AgentTreeNode> agentTree_;
  223. - RVOSimulator *sim_;
  224. friend class Agent;
  225. friend class RVOSimulator;
  226. diff --git a/thirdparty/rvo2/Vector3.h b/thirdparty/rvo2/Vector3.h
  227. index 6c3223bb87..f44e311f29 100644
  228. --- a/thirdparty/rvo2/Vector3.h
  229. +++ b/thirdparty/rvo2/Vector3.h
  230. @@ -41,7 +41,7 @@
  231. #include <cstddef>
  232. #include <ostream>
  233. -#include "Export.h"
  234. +#define RVO3D_EXPORT
  235. namespace RVO {
  236. /**
  237. @@ -59,17 +59,6 @@ namespace RVO {
  238. val_[2] = 0.0f;
  239. }
  240. - /**
  241. - * \brief Constructs and initializes a three-dimensional vector from the specified three-dimensional vector.
  242. - * \param vector The three-dimensional vector containing the xyz-coordinates.
  243. - */
  244. - inline Vector3(const Vector3 &vector)
  245. - {
  246. - val_[0] = vector[0];
  247. - val_[1] = vector[1];
  248. - val_[2] = vector[2];
  249. - }
  250. -
  251. /**
  252. * \brief Constructs and initializes a three-dimensional vector from the specified three-element array.
  253. * \param val The three-element array containing the xyz-coordinates.