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

121 lines
2.8 KiB

  1. /*
  2. * Copyright (C) 2023 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. #ifndef ANTKEEPER_PHYSICS_SPHERE_COLLIDER_HPP
  20. #define ANTKEEPER_PHYSICS_SPHERE_COLLIDER_HPP
  21. #include <engine/physics/kinematics/collider.hpp>
  22. #include <engine/geom/primitives/sphere.hpp>
  23. namespace physics {
  24. /**
  25. * Sphere collision object.
  26. */
  27. class sphere_collider: public collider
  28. {
  29. public:
  30. /// Sphere type.
  31. using sphere_type = geom::sphere<float>;
  32. [[nodiscard]] inline constexpr collider_type type() const noexcept override
  33. {
  34. return collider_type::sphere;
  35. }
  36. /**
  37. * Constructs a sphere collider from a sphere.
  38. *
  39. * @param sphere Sphere shape.
  40. */
  41. inline explicit sphere_collider(const sphere_type& sphere) noexcept:
  42. m_sphere{sphere}
  43. {}
  44. /**
  45. * Constructs a sphere collider.
  46. *
  47. * @param center Sphere center.
  48. * @param radius Sphere radius.
  49. */
  50. /// @{
  51. inline sphere_collider(const math::fvec3& center, float radius) noexcept:
  52. m_sphere{center, radius}
  53. {}
  54. inline explicit sphere_collider(float radius) noexcept:
  55. m_sphere{{0.0f, 0.0f, 0.0f}, radius}
  56. {}
  57. sphere_collider() noexcept = default;
  58. /// @}
  59. /**
  60. * Sets the collider's sphere.
  61. *
  62. * @param sphere Sphere shape.
  63. */
  64. inline void set_sphere(const sphere_type& sphere) noexcept
  65. {
  66. m_sphere = sphere;
  67. }
  68. /**
  69. * Sets the center of the sphere.
  70. *
  71. * @param center Sphere center, in object space.
  72. */
  73. inline void set_center(const math::fvec3& center) noexcept
  74. {
  75. m_sphere.center = center;
  76. }
  77. /**
  78. * Sets the radius of the sphere.
  79. *
  80. * @param radius Sphere radius.
  81. */
  82. inline void set_radius(float radius) noexcept
  83. {
  84. m_sphere.radius = radius;
  85. }
  86. /// Returns the sphere shape.
  87. [[nodiscard]] inline const sphere_type& get_sphere() const noexcept
  88. {
  89. return m_sphere;
  90. }
  91. /// Returns the center of the sphere, in object space.
  92. [[nodiscard]] inline const math::fvec3& get_center() const noexcept
  93. {
  94. return m_sphere.center;
  95. }
  96. /// Returns the radius of the sphere.
  97. [[nodiscard]] inline float get_radius() const noexcept
  98. {
  99. return m_sphere.radius;
  100. }
  101. private:
  102. sphere_type m_sphere{{0.0f, 0.0f, 0.0f}, 0.0f};
  103. };
  104. } // namespace physics
  105. #endif // ANTKEEPER_PHYSICS_SPHERE_COLLIDER_HPP