💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
4.5 KiB

  1. /*
  2. * Copyright (C) 2017 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef AGENT_HPP
  20. #define AGENT_HPP
  21. #include "navmesh.hpp"
  22. #include <vector>
  23. #include <emergent/emergent.hpp>
  24. using namespace Emergent;
  25. class Obstacle;
  26. /*************88
  27. Ant is an agent.
  28. Ant combines steering behaviors with different weights.
  29. I.E.
  30. seek pheromones * 0.5
  31. separation * 0.1
  32. alignment * 0.1
  33. cohesion * 0.1
  34. followWall * 0.2
  35. */
  36. /**
  37. * An agent which navigates on a navmesh.
  38. */
  39. class Agent
  40. {
  41. public:
  42. Agent();
  43. /**
  44. * Adds a force to the agent's acceleration vector.
  45. *
  46. * @param force Acceleration force
  47. */
  48. void applyForce(const Vector3& force);
  49. /**
  50. * Calculates velocity based on current acceleration vector, then resets acceleration to zero.
  51. */
  52. void updateVelocity();
  53. /**
  54. * Calculates steering force for the wander behavior.
  55. */
  56. Vector3 wander(float dt);
  57. /**
  58. * Calculates steering force for the seek behavior.
  59. */
  60. Vector3 seek(const Vector3& target) const;
  61. /**
  62. * Calculates steering force for the flee behavior.
  63. */
  64. Vector3 flee(const Vector3& target) const;
  65. Vector3 containment(const Vector3& probe) const;
  66. Vector3 separation(const std::list<Agent*>& neighbors) const;
  67. Vector3 forage(const Vector3& leftProbe, const Vector3& rightProbe);
  68. void setMaxSpeed(float speed);
  69. void setVelocity(const Vector3& velocity);
  70. void setMaxAcceleration(float acceleration);
  71. void setMass(float mass);
  72. void setWanderCircleDistance(float distance);
  73. void setWanderCircleRadius(float radius);
  74. void setWanderRate(float angle);
  75. void setSeparationRadius(float radius);
  76. /**
  77. * Sets the position of the agent on a navmesh.
  78. *
  79. * @param triangle Navmesh triangle on which the agent resides
  80. * @param position Barycentric position on the specified triangle
  81. */
  82. void setPosition(Navmesh::Triangle* triangle, const Vector3& position);
  83. /**
  84. * Sets the orientation of the agent. This effectively updates the agent's vector basis and rotation quaternion.
  85. *
  86. * @param forward Normalized forward vector
  87. * @param up Normalized up vector
  88. */
  89. void setOrientation(const Vector3& newForward, const Vector3& newUp);
  90. /*
  91. Vector3 followWall();
  92. // or
  93. Vector3 followEdge();
  94. Vector3 avoidObstacle(const Obstacle* obstacle);
  95. Vector3 alignment(const std::vector<const Agent*>* neighbors);
  96. Vector3 cohesion(const std::vector<const Agent*>* neighbors);
  97. */
  98. const Navmesh::Triangle* getNavmeshTriangle() const;
  99. Navmesh::Triangle* getNavmeshTriangle();
  100. const Vector3& getBarycentricPosition() const;
  101. const Vector3& getPosition() const;
  102. const Vector3& getForward() const;
  103. const Vector3& getUp() const;
  104. const Vector3& getRight() const;
  105. const Quaternion& getRotation() const;
  106. const Vector3& getVelocity() const;
  107. private:
  108. Navmesh::Triangle* navmeshTriangle;
  109. Vector3 barycentricPosition;
  110. Vector3 position;
  111. Vector3 forward;
  112. Vector3 up;
  113. Vector3 right;
  114. Quaternion rotation;
  115. // Limits
  116. float maxSpeed;
  117. float maxAcceleration;
  118. // Steering forces
  119. float mass;
  120. Vector3 acceleration;
  121. Vector3 velocity;
  122. // Wander variables
  123. float wanderCircleDistance;
  124. float wanderCircleRadius;
  125. float wanderRate;
  126. Vector3 wanderDirection;
  127. float separationRadius;
  128. float separationRadiusSquared;
  129. };
  130. inline const Navmesh::Triangle* Agent::getNavmeshTriangle() const
  131. {
  132. return navmeshTriangle;
  133. }
  134. inline Navmesh::Triangle* Agent::getNavmeshTriangle()
  135. {
  136. return navmeshTriangle;
  137. }
  138. inline const Vector3& Agent::getBarycentricPosition() const
  139. {
  140. return barycentricPosition;
  141. }
  142. inline const Vector3& Agent::getPosition() const
  143. {
  144. return position;
  145. }
  146. inline const Vector3& Agent::getForward() const
  147. {
  148. return forward;
  149. }
  150. inline const Vector3& Agent::getUp() const
  151. {
  152. return up;
  153. }
  154. inline const Vector3& Agent::getRight() const
  155. {
  156. return right;
  157. }
  158. inline const Quaternion& Agent::getRotation() const
  159. {
  160. return rotation;
  161. }
  162. inline const Vector3& Agent::getVelocity() const
  163. {
  164. return velocity;
  165. }
  166. #endif // AGENT_HPP