raycast.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. Minetest
  3. Copyright (C) 2016 juhdanad, Daniel Juhasz <juhdanad@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "voxelalgorithms.h"
  18. #include "util/pointedthing.h"
  19. //! Sorts PointedThings based on their distance.
  20. struct RaycastSort
  21. {
  22. bool operator() (const PointedThing &pt1, const PointedThing &pt2) const;
  23. };
  24. //! Describes the state of a raycast.
  25. class RaycastState
  26. {
  27. public:
  28. /*!
  29. * Creates a raycast.
  30. * @param objects_pointable if false, only nodes will be found
  31. * @param liquids pointable if false, liquid nodes won't be found
  32. */
  33. RaycastState(const core::line3d<f32> &shootline, bool objects_pointable,
  34. bool liquids_pointable);
  35. //! Shootline of the raycast.
  36. core::line3d<f32> m_shootline;
  37. //! Iterator to store the progress of the raycast.
  38. voxalgo::VoxelLineIterator m_iterator;
  39. //! Previous tested node during the raycast.
  40. v3s16 m_previous_node;
  41. /*!
  42. * This priority queue stores the found pointed things
  43. * waiting to be returned.
  44. */
  45. std::priority_queue<PointedThing, std::vector<PointedThing>, RaycastSort> m_found;
  46. bool m_objects_pointable;
  47. bool m_liquids_pointable;
  48. //! The code needs to search these nodes around the center node.
  49. core::aabbox3d<s16> m_search_range { 0, 0, 0, 0, 0, 0 };
  50. //! If true, the Environment will initialize this state.
  51. bool m_initialization_needed = true;
  52. };
  53. /*!
  54. * Checks if a line and a box intersects.
  55. * @param[in] box box to test collision
  56. * @param[in] start starting point of the line
  57. * @param[in] dir direction and length of the line
  58. * @param[out] collision_point first point of the collision
  59. * @param[out] collision_normal normal vector at the collision, points
  60. * outwards of the surface. If start is in the box, zero vector.
  61. * @returns true if a collision point was found
  62. */
  63. bool boxLineCollision(const aabb3f &box, const v3f &start, const v3f &dir,
  64. v3f *collision_point, v3s16 *collision_normal);