💿🐜 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.

203 lines
6.3 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. #include "ant.hpp"
  20. #include "colony.hpp"
  21. #include "pheromone.hpp"
  22. Ant::Ant(Colony* colony):
  23. colony(colony),
  24. state(Ant::State::IDLE),
  25. transform(Transform::getIdentity()),
  26. skeletonPose(nullptr)
  27. {
  28. modelInstance.setModel(colony->getAntModel());
  29. }
  30. void Ant::move(const Vector3& velocity)
  31. {
  32. std::vector<Navmesh::Step> traversal;
  33. Navmesh::traverse(getNavmeshTriangle(), getBarycentricPosition(), velocity, &traversal);
  34. if (!traversal.empty())
  35. {
  36. const Navmesh::Step& step = traversal.back();
  37. if (step.start != step.end)
  38. {
  39. if (step.triangle != getNavmeshTriangle())
  40. {
  41. Quaternion alignment = glm::rotation(getNavmeshTriangle()->normal, step.triangle->normal);
  42. Vector3 newForward = glm::normalize(project_on_plane(alignment * getForward(), Vector3(0.0f), step.triangle->normal));
  43. setOrientation(newForward, step.triangle->normal);
  44. }
  45. }
  46. setPosition(step.triangle, step.end);
  47. }
  48. }
  49. void Ant::turn(float angle)
  50. {
  51. // Rotate forward vector
  52. Vector3 newForward = glm::normalize(glm::angleAxis(angle, getUp()) * getForward());
  53. setOrientation(newForward, getUp());
  54. }
  55. void Ant::update(float dt)
  56. {
  57. float probeLateralOffset = 0.1f;
  58. float probeForwardOffset = 0.3f;
  59. // Steering
  60. if (state == Ant::State::WANDER)
  61. {
  62. setWanderCircleDistance(4.0f);
  63. setWanderCircleRadius(0.3f);
  64. setWanderRate(glm::radians(90.0f));
  65. setSeparationRadius(0.5f);
  66. setMaxSpeed(0.025f);
  67. // Calculate wander force
  68. Vector3 wanderForce = wander(dt);
  69. // Setup containment probes
  70. Vector3 leftProbe = getForward() * probeForwardOffset - getRight() * probeLateralOffset;
  71. Vector3 rightProbe = getForward() * probeForwardOffset + getRight() * probeLateralOffset;
  72. // Calculate containment force
  73. Vector3 containmentForce = containment(leftProbe) + containment(rightProbe);
  74. // Determine neighbors
  75. float neighborhoodSize = 2.0f;
  76. AABB neighborhoodAABB(getPosition() - Vector3(neighborhoodSize * 0.5f), getPosition() + Vector3(neighborhoodSize * 0.5f));
  77. std::list<Agent*> neighbors;
  78. colony->queryAnts(neighborhoodAABB, &neighbors);
  79. // Calculate separation force
  80. Vector3 separationForce = separation(neighbors);
  81. // Calculate velocity
  82. Vector3 velocity = getVelocity();
  83. velocity += wanderForce;
  84. velocity += containmentForce * 0.0025f;
  85. velocity += separationForce * 0.01f;
  86. velocity = limit(velocity, 0.025f);
  87. setVelocity(velocity);
  88. setOrientation(glm::normalize(velocity), getUp());
  89. // Move ant
  90. move(velocity);
  91. }
  92. else
  93. {
  94. Vector3 leftProbe = getForward() * probeForwardOffset - getRight() * probeLateralOffset;
  95. Vector3 rightProbe = getForward() * probeForwardOffset + getRight() * probeLateralOffset;
  96. Vector3 containmentForce = containment(leftProbe) + containment(rightProbe);
  97. Vector3 velocity = Vector3(0.0f);
  98. velocity += containmentForce;
  99. velocity = limit(velocity, 0.025f);
  100. setVelocity(velocity);
  101. //setOrientation(glm::normalize(velocity), getUp());
  102. // Move ant
  103. move(velocity);
  104. }
  105. // Locomotion
  106. /*
  107. As the ant moves forward, legs in the stance phase are kept grounded via IK. If IK constraints are violated, the swinging legs are grounded
  108. and the grounded legs begin swinging.
  109. Two poses are loaded from the model file: midswing and touchdown.
  110. touchdown is the pose in which all legs are at the end of their swing phases and need to be grounded
  111. midswing is the pose in which all legs are at the highest point in their swing phases
  112. when a grounded leg enters the swing phases, its current pose is saved as the liftoff pose, then an animation is created using the liftoff pose, midswing pose, and touchdown pose.
  113. */
  114. // Update transform
  115. transform.translation = getPosition();
  116. transform.rotation = getRotation();
  117. // Update model instance
  118. modelInstance.setTransform(transform);
  119. }
  120. Vector3 Ant::forage(const Vector3& leftReceptor, const Vector3& rightReceptor)
  121. {
  122. float leftSignal = 0.0f;
  123. float rightSignal = 0.0f;
  124. // Detect pheromones with left receptor
  125. std::list<Pheromone*> leftPheromones;
  126. colony->getPheromoneOctree()->query(AABB(leftReceptor, leftReceptor), &leftPheromones);
  127. for (Pheromone* pheromone: leftPheromones)
  128. {
  129. Vector3 difference = pheromone->getPosition() - rightReceptor;
  130. float distanceSquared = glm::dot(difference, difference);
  131. if (distanceSquared <= pheromone->getRadiusSquared())
  132. {
  133. // Calculate attenuated pheromone strength using inverse-square law
  134. float strength = pheromone->getStrength() / ((distanceSquared == 0.0f) ? 1.0f : distanceSquared);
  135. leftSignal += strength;
  136. }
  137. }
  138. // Detect pheromones with right receptor
  139. std::list<Pheromone*> rightPheromones;
  140. colony->getPheromoneOctree()->query(AABB(rightReceptor, rightReceptor), &rightPheromones);
  141. for (Pheromone* pheromone: rightPheromones)
  142. {
  143. Vector3 difference = pheromone->getPosition() - rightReceptor;
  144. float distanceSquared = glm::dot(difference, difference);
  145. if (distanceSquared <= pheromone->getRadiusSquared())
  146. {
  147. // Calculate attenuated pheromone strength using inverse-square law
  148. float strength = pheromone->getStrength() / ((distanceSquared == 0.0f) ? 1.0f : distanceSquared);
  149. rightSignal += strength;
  150. }
  151. }
  152. // Add noise
  153. const float maxNoise = 0.1f;
  154. leftSignal += frand(0.0f, maxNoise);
  155. rightSignal += frand(0.0f, maxNoise);
  156. if (leftSignal + rightSignal > 0.0f)
  157. {
  158. const float maxPheromoneTurningAngle = 0.1f;
  159. // Use Weber's law (Perna et al.) to calculate turning angle based on pheromone signals
  160. float turningAngle = maxPheromoneTurningAngle * ((leftSignal - rightSignal) / (leftSignal + rightSignal));
  161. }
  162. return Vector3(0.0f);
  163. }
  164. void Ant::setState(Ant::State state)
  165. {
  166. this->state = state;
  167. }