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

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