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

94 lines
3.1 KiB

  1. /*
  2. * Copyright (C) 2020 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 "nest.hpp"
  20. #include "animation/ease.hpp"
  21. #include "math.hpp"
  22. nest::nest()
  23. {
  24. dig_radius = 1.25f;
  25. }
  26. float3 nest::extend_shaft(shaft& shaft)
  27. {
  28. float3 dig_position = get_shaft_position(shaft, shaft.current_depth);
  29. float dr = frand(dig_radius * 0.75f, dig_radius * 1.25f);
  30. shaft.current_depth += dr * 0.1f;
  31. return dig_position;
  32. }
  33. float3 nest::expand_chamber(chamber& chamber)
  34. {
  35. float dig_angle = frand(0.0f, vmq::two_pi<float>);
  36. float2 dig_direction = vmq::normalize(float2{std::cos(dig_angle), std::sin(dig_angle)});
  37. float3 chamber_center = get_shaft_position(*chamber.shaft, chamber.depth);
  38. float3 dig_position = chamber_center;
  39. float dr = frand(dig_radius * 0.75f, dig_radius * 1.25f);
  40. float t = frand(0.0f, 1.0f);
  41. dig_position.x += dig_direction.x * (chamber.outer_radius - dr) * t;
  42. dig_position.z += dig_direction.y * (chamber.outer_radius - dr) * t;
  43. return dig_position;
  44. }
  45. void nest::set_tunnel_radius(float radius)
  46. {
  47. tunnel_radius = radius;
  48. }
  49. float nest::get_shaft_angle(const shaft& shaft, float depth) const
  50. {
  51. float shaft_length = shaft.depth[1] - shaft.depth[0];
  52. float depth_factor = (depth - shaft.depth[0]) / shaft_length;
  53. float pitch = ease<float>::linear(shaft.pitch[0], shaft.pitch[1], depth_factor);
  54. return shaft.rotation + (depth / pitch) * shaft.chirality * vmq::two_pi<float>;
  55. }
  56. float nest::get_shaft_depth(const shaft& shaft, float turns) const
  57. {
  58. return shaft.pitch[0] * turns;
  59. }
  60. float3 nest::get_shaft_position(const shaft& shaft, float depth) const
  61. {
  62. float shaft_length = shaft.depth[1] - shaft.depth[0];
  63. float depth_factor = (depth - shaft.depth[0]) / shaft_length;
  64. float pitch = ease<float>::linear(shaft.pitch[0], shaft.pitch[1], depth_factor);
  65. float radius = ease<float>::out_expo(shaft.radius[0], shaft.radius[1], depth_factor);
  66. float translation_x = ease<float>::linear(shaft.translation[0][0], shaft.translation[1][0], depth_factor);
  67. float translation_z = ease<float>::linear(shaft.translation[0][1], shaft.translation[1][1], depth_factor);
  68. float angle = shaft.rotation + (depth / pitch) * shaft.chirality * vmq::two_pi<float>;
  69. float3 position;
  70. position[0] = std::cos(angle) * radius + translation_x;
  71. position[1] = -std::max<float>(shaft.depth[0], std::min<float>(shaft.depth[1], depth));
  72. position[2] = std::sin(angle) * radius + translation_z;
  73. return position;
  74. }