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

73 lines
2.3 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. float3 wander_3d(const agent& agent, float noise, float distance, float radius, float& theta, float& phi)
  42. {
  43. // Shift wander angles
  44. theta += math::random(-noise, noise);
  45. phi += math::random(-noise, noise);
  46. // Calculate center of wander sphere
  47. const float3 center = agent.position + agent.forward * distance;
  48. // Convert spherical coordinates to Cartesian point on wander sphere
  49. const float r_cos_theta = radius * std::cos(theta);
  50. const float3 offset =
  51. {
  52. r_cos_theta * std::cos(phi),
  53. r_cos_theta * std::sin(phi),
  54. radius * std::sin(theta)
  55. };
  56. // Seek toward point on wander sphere
  57. return seek(agent, center + offset);
  58. }
  59. } // namespace behavior
  60. } // namespace steering
  61. } // namespace ai