raycast.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include "raycast.h"
  17. #include "irr_v3d.h"
  18. #include "irr_aabb3d.h"
  19. #include "constants.h"
  20. bool RaycastSort::operator() (const PointedThing &pt1,
  21. const PointedThing &pt2) const
  22. {
  23. // "nothing" can not be sorted
  24. assert(pt1.type != POINTEDTHING_NOTHING);
  25. assert(pt2.type != POINTEDTHING_NOTHING);
  26. // returns false if pt1 is nearer than pt2
  27. if (pt1.distanceSq < pt2.distanceSq) {
  28. return false;
  29. }
  30. if (pt1.distanceSq == pt2.distanceSq) {
  31. // Sort them to allow only one order
  32. if (pt1.type == POINTEDTHING_OBJECT)
  33. return (pt2.type == POINTEDTHING_OBJECT
  34. && pt1.object_id < pt2.object_id);
  35. return (pt2.type == POINTEDTHING_OBJECT
  36. || pt1.node_undersurface < pt2.node_undersurface);
  37. }
  38. return true;
  39. }
  40. RaycastState::RaycastState(const core::line3d<f32> &shootline,
  41. bool objects_pointable, bool liquids_pointable) :
  42. m_shootline(shootline),
  43. m_iterator(shootline.start / BS, shootline.getVector() / BS),
  44. m_previous_node(m_iterator.m_current_node_pos),
  45. m_objects_pointable(objects_pointable),
  46. m_liquids_pointable(liquids_pointable)
  47. {
  48. }
  49. bool boxLineCollision(const aabb3f &box, const v3f &start,
  50. const v3f &dir, v3f *collision_point, v3s16 *collision_normal)
  51. {
  52. if (box.isPointInside(start)) {
  53. *collision_point = start;
  54. collision_normal->set(0, 0, 0);
  55. return true;
  56. }
  57. float m = 0;
  58. // Test X collision
  59. if (dir.X != 0) {
  60. if (dir.X > 0)
  61. m = (box.MinEdge.X - start.X) / dir.X;
  62. else
  63. m = (box.MaxEdge.X - start.X) / dir.X;
  64. if (m >= 0 && m <= 1) {
  65. *collision_point = start + dir * m;
  66. if ((collision_point->Y >= box.MinEdge.Y)
  67. && (collision_point->Y <= box.MaxEdge.Y)
  68. && (collision_point->Z >= box.MinEdge.Z)
  69. && (collision_point->Z <= box.MaxEdge.Z)) {
  70. collision_normal->set((dir.X > 0) ? -1 : 1, 0, 0);
  71. return true;
  72. }
  73. }
  74. }
  75. // Test Y collision
  76. if (dir.Y != 0) {
  77. if (dir.Y > 0)
  78. m = (box.MinEdge.Y - start.Y) / dir.Y;
  79. else
  80. m = (box.MaxEdge.Y - start.Y) / dir.Y;
  81. if (m >= 0 && m <= 1) {
  82. *collision_point = start + dir * m;
  83. if ((collision_point->X >= box.MinEdge.X)
  84. && (collision_point->X <= box.MaxEdge.X)
  85. && (collision_point->Z >= box.MinEdge.Z)
  86. && (collision_point->Z <= box.MaxEdge.Z)) {
  87. collision_normal->set(0, (dir.Y > 0) ? -1 : 1, 0);
  88. return true;
  89. }
  90. }
  91. }
  92. // Test Z collision
  93. if (dir.Z != 0) {
  94. if (dir.Z > 0)
  95. m = (box.MinEdge.Z - start.Z) / dir.Z;
  96. else
  97. m = (box.MaxEdge.Z - start.Z) / dir.Z;
  98. if (m >= 0 && m <= 1) {
  99. *collision_point = start + dir * m;
  100. if ((collision_point->X >= box.MinEdge.X)
  101. && (collision_point->X <= box.MaxEdge.X)
  102. && (collision_point->Y >= box.MinEdge.Y)
  103. && (collision_point->Y <= box.MaxEdge.Y)) {
  104. collision_normal->set(0, 0, (dir.Z > 0) ? -1 : 1);
  105. return true;
  106. }
  107. }
  108. }
  109. return false;
  110. }