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

93 lines
3.0 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. force += ai::steering::behavior::wander_3d(agent, steering.wander_noise * dt, steering.wander_distance, steering.wander_radius, steering.wander_angle, steering.wander_angle2) * steering.wander_weight;
  46. }
  47. if (steering.seek_weight)
  48. {
  49. force += ai::steering::behavior::seek(agent, steering.seek_target) * steering.seek_weight;
  50. }
  51. // Normalize force
  52. if (steering.sum_weights)
  53. force /= steering.sum_weights;
  54. // Accelerate
  55. agent.acceleration = force / agent.mass;
  56. agent.velocity += agent.acceleration * static_cast<float>(dt);
  57. // Limit speed
  58. const float speed_squared = math::length_squared(agent.velocity);
  59. if (speed_squared > agent.max_speed_squared)
  60. {
  61. const float speed = std::sqrt(speed_squared);
  62. agent.velocity = (agent.velocity / speed) * agent.max_speed;
  63. }
  64. // Move agent
  65. agent.position += agent.velocity * static_cast<float>(dt);
  66. // Rotate agent
  67. if (speed_squared)
  68. {
  69. agent.orientation = math::look_rotation(agent.velocity / std::sqrt(speed_squared), agent.up);
  70. agent.forward = agent.orientation * config::global_forward;
  71. agent.up = agent.orientation * config::global_up;
  72. }
  73. // Update transform
  74. transform.local.translation = agent.position;
  75. transform.local.rotation = agent.orientation;
  76. }
  77. );
  78. }
  79. } // namespace system
  80. } // namespace entity