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

91 lines
2.8 KiB

  1. /*
  2. * Copyright (C) 2021 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 "entity/systems/steering.hpp"
  20. #include "entity/components/steering.hpp"
  21. #include "entity/components/transform.hpp"
  22. #include "entity/id.hpp"
  23. #include "ai/steering/behavior/wander.hpp"
  24. #include "ai/steering/behavior/seek.hpp"
  25. #include "config.hpp"
  26. namespace entity {
  27. namespace system {
  28. steering::steering(entity::registry& registry):
  29. updatable(registry)
  30. {}
  31. void steering::update(double t, double dt)
  32. {
  33. registry.view<component::steering, component::transform>().each
  34. (
  35. [&](entity::id entity_id, auto& steering, auto& transform)
  36. {
  37. auto& agent = steering.agent;
  38. // Update agent orientation
  39. agent.orientation = transform.local.rotation;
  40. // Accumulate forces
  41. float3 force = {0, 0, 0};
  42. if (steering.wander_weight)
  43. {
  44. force += ai::steering::behavior::wander_2d(agent, steering.wander_noise * dt, steering.wander_distance, steering.wander_radius, steering.wander_angle) * steering.wander_weight;
  45. }
  46. if (steering.seek_weight)
  47. {
  48. force += ai::steering::behavior::seek(agent, steering.seek_target) * steering.seek_weight;
  49. }
  50. // Normalize force
  51. force /= steering.weight_sum;
  52. // Accelerate
  53. agent.acceleration = force / agent.mass;
  54. agent.velocity += agent.acceleration * static_cast<float>(dt);
  55. // Limit speed
  56. const float speed_squared = math::length_squared(agent.velocity);
  57. if (speed_squared > agent.max_speed_squared)
  58. {
  59. const float speed = std::sqrt(speed_squared);
  60. agent.velocity = (agent.velocity / speed) * agent.max_speed;
  61. }
  62. // Move agent
  63. agent.position += agent.velocity * static_cast<float>(dt);
  64. // Rotate agent
  65. if (speed_squared)
  66. {
  67. agent.orientation = math::look_rotation(agent.velocity / std::sqrt(speed_squared), agent.up);
  68. agent.forward = agent.orientation * config::global_forward;
  69. agent.up = agent.orientation * config::global_up;
  70. }
  71. // Update transform
  72. transform.local.translation = agent.position;
  73. transform.local.rotation = agent.orientation;
  74. }
  75. );
  76. }
  77. } // namespace system
  78. } // namespace entity