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

96 lines
2.6 KiB

  1. /*
  2. * Copyright (C) 2017-2019 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 "steering-system.hpp"
  20. SteeringSystem::SteeringSystem(ComponentManager* componentManager):
  21. System(componentManager),
  22. boids(componentManager)
  23. {
  24. boids.addGroupObserver(this);
  25. }
  26. SteeringSystem::~SteeringSystem()
  27. {}
  28. void SteeringSystem::update(float t, float dt)
  29. {
  30. auto members = boids.getMembers();
  31. for (const SteeringGroup::Member* member: *members)
  32. {
  33. SteeringComponent* steering = std::get<0>(member->components);
  34. // Initialize summed steering force
  35. steering->force = Vector3(0.0f);
  36. steering->speed = 0.0f;
  37. float speedSquared = 0.0f;
  38. float maxSpeedSquared = steering->maxSpeed * steering->maxSpeed;
  39. bool truncated = false;
  40. if (steering->behaviorCount > 0)
  41. {
  42. // Sort steering beaviors by priority
  43. std::sort(steering->behaviors, steering->behaviors + steering->behaviorCount,
  44. [](const SteeringBehavior& a, const SteeringBehavior& b) -> bool
  45. {
  46. return (a.priority >= b.priority);
  47. }
  48. );
  49. }
  50. // Evaluate steering forces in order
  51. for (std::size_t i = 0; i < steering->behaviorCount; ++i)
  52. {
  53. const SteeringBehavior& behavior = steering->behaviors[i];
  54. // Skip zero-weighted steering behaviors
  55. if (behavior.weight == 0.0f)
  56. {
  57. continue;
  58. }
  59. // Add weighted steering behavior force
  60. steering->force += behavior.function() * behavior.weight;
  61. // Limit speed
  62. speedSquared = glm::length2(steering->force);
  63. if (speedSquared >= maxSpeedSquared)
  64. {
  65. steering->force *= (1.0f / std::sqrt(speedSquared)) * steering->maxSpeed;
  66. steering->speed = steering->maxSpeed;
  67. truncated = true;
  68. break;
  69. }
  70. }
  71. if (!truncated)
  72. {
  73. steering->speed = std::sqrt(speedSquared);
  74. }
  75. }
  76. }
  77. void SteeringSystem::memberRegistered(const SteeringGroup::Member* member)
  78. {}
  79. void SteeringSystem::memberUnregistered(const SteeringGroup::Member* member)
  80. {}