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

78 lines
2.5 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 "ai/steering/behavior/wander.hpp"
  20. #include "ai/steering/behavior/seek.hpp"
  21. #include "math/random.hpp"
  22. #include "math/quaternion-functions.hpp"
  23. #include "math/quaternion-operators.hpp"
  24. namespace ai {
  25. namespace steering {
  26. namespace behavior {
  27. float3 wander_2d(const agent& agent, float noise, float distance, float radius, float& angle)
  28. {
  29. // Shift wander angle
  30. angle += math::random(-noise, noise);
  31. // Calculate center of wander circle
  32. const float3 center = agent.position + agent.forward * distance;
  33. // Decompose orientation into swing and twist rotations
  34. math::quaternion<float> swing, twist;
  35. math::swing_twist(agent.orientation, agent.up, swing, twist);
  36. // Calculate offset to point on wander circle
  37. const float3 offset = math::conjugate(twist) * (math::angle_axis(angle, agent.up) * agent.forward * radius);
  38. // Seek toward point on wander circle
  39. return seek(agent, center + offset);
  40. }
  41. /*
  42. float3 wander_3d(const agent& agent, float distance, float radius, float delta, float& theta, float& phi)
  43. {
  44. // Shift wander angles
  45. theta += random(-delta, delta);
  46. phi += random(-delta, delta);
  47. // Calculate center of wander sphere
  48. float3 center = agent.position;
  49. const float speed_squared = math::dot(agent.velocity, agent.velocity);
  50. if (speed_squared)
  51. center += (agent.velocity / std::sqrt(speed_squared)) * distance;
  52. // Convert spherical coordinates to Cartesian point on wander sphere
  53. const float r_cos_theta = radius * std::cos(theta);
  54. const float3 offset =
  55. {
  56. r_cos_theta * std::cos(phi),
  57. r_cos_theta * std::sin(phi),
  58. radius * std::sin(theta)
  59. };
  60. // Seek toward point on wander sphere
  61. return seek(agent, center + offset);
  62. }
  63. */
  64. } // namespace behavior
  65. } // namespace steering
  66. } // namespace ai