raycast.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <quaternion.h>
  20. #include "constants.h"
  21. bool RaycastSort::operator() (const PointedThing &pt1,
  22. const PointedThing &pt2) const
  23. {
  24. // "nothing" cannot be sorted
  25. assert(pt1.type != POINTEDTHING_NOTHING);
  26. assert(pt2.type != POINTEDTHING_NOTHING);
  27. f32 pt1_distSq = pt1.distanceSq;
  28. // Add some bonus when one of them is an object
  29. if (pt1.type != pt2.type) {
  30. if (pt1.type == POINTEDTHING_OBJECT)
  31. pt1_distSq -= BS * BS;
  32. else if (pt2.type == POINTEDTHING_OBJECT)
  33. pt1_distSq += BS * BS;
  34. }
  35. // returns false if pt1 is nearer than pt2
  36. if (pt1_distSq < pt2.distanceSq) {
  37. return false;
  38. }
  39. if (pt1_distSq == pt2.distanceSq) {
  40. // Sort them to allow only one order
  41. if (pt1.type == POINTEDTHING_OBJECT)
  42. return (pt2.type == POINTEDTHING_OBJECT
  43. && pt1.object_id < pt2.object_id);
  44. return (pt2.type == POINTEDTHING_OBJECT
  45. || pt1.node_undersurface < pt2.node_undersurface);
  46. }
  47. return true;
  48. }
  49. RaycastState::RaycastState(const core::line3d<f32> &shootline,
  50. bool objects_pointable, bool liquids_pointable,
  51. const std::optional<Pointabilities> &pointabilities) :
  52. m_shootline(shootline),
  53. m_iterator(shootline.start / BS, shootline.getVector() / BS),
  54. m_previous_node(m_iterator.m_current_node_pos),
  55. m_objects_pointable(objects_pointable),
  56. m_liquids_pointable(liquids_pointable),
  57. m_pointabilities(pointabilities)
  58. {
  59. }
  60. bool boxLineCollision(const aabb3f &box, const v3f start,
  61. const v3f dir, v3f *collision_point, v3f *collision_normal)
  62. {
  63. if (box.isPointInside(start)) {
  64. *collision_point = start;
  65. collision_normal->set(0, 0, 0);
  66. return true;
  67. }
  68. float m = 0;
  69. // Test X collision
  70. if (dir.X != 0) {
  71. if (dir.X > 0)
  72. m = (box.MinEdge.X - start.X) / dir.X;
  73. else
  74. m = (box.MaxEdge.X - start.X) / dir.X;
  75. if (m >= 0 && m <= 1) {
  76. *collision_point = start + dir * m;
  77. if ((collision_point->Y >= box.MinEdge.Y)
  78. && (collision_point->Y <= box.MaxEdge.Y)
  79. && (collision_point->Z >= box.MinEdge.Z)
  80. && (collision_point->Z <= box.MaxEdge.Z)) {
  81. collision_normal->set((dir.X > 0) ? -1 : 1, 0, 0);
  82. return true;
  83. }
  84. }
  85. }
  86. // Test Y collision
  87. if (dir.Y != 0) {
  88. if (dir.Y > 0)
  89. m = (box.MinEdge.Y - start.Y) / dir.Y;
  90. else
  91. m = (box.MaxEdge.Y - start.Y) / dir.Y;
  92. if (m >= 0 && m <= 1) {
  93. *collision_point = start + dir * m;
  94. if ((collision_point->X >= box.MinEdge.X)
  95. && (collision_point->X <= box.MaxEdge.X)
  96. && (collision_point->Z >= box.MinEdge.Z)
  97. && (collision_point->Z <= box.MaxEdge.Z)) {
  98. collision_normal->set(0, (dir.Y > 0) ? -1 : 1, 0);
  99. return true;
  100. }
  101. }
  102. }
  103. // Test Z collision
  104. if (dir.Z != 0) {
  105. if (dir.Z > 0)
  106. m = (box.MinEdge.Z - start.Z) / dir.Z;
  107. else
  108. m = (box.MaxEdge.Z - start.Z) / dir.Z;
  109. if (m >= 0 && m <= 1) {
  110. *collision_point = start + dir * m;
  111. if ((collision_point->X >= box.MinEdge.X)
  112. && (collision_point->X <= box.MaxEdge.X)
  113. && (collision_point->Y >= box.MinEdge.Y)
  114. && (collision_point->Y <= box.MaxEdge.Y)) {
  115. collision_normal->set(0, 0, (dir.Z > 0) ? -1 : 1);
  116. return true;
  117. }
  118. }
  119. }
  120. return false;
  121. }
  122. bool boxLineCollision(const aabb3f &box, const v3f rotation,
  123. const v3f start, const v3f dir,
  124. v3f *collision_point, v3f *collision_normal, v3f *raw_collision_normal)
  125. {
  126. // Inversely transform the ray rather than rotating the box faces;
  127. // this allows us to continue using a simple ray - AABB intersection
  128. core::quaternion rot(rotation * core::DEGTORAD);
  129. rot.makeInverse();
  130. bool collision = boxLineCollision(box, rot * start, rot * dir, collision_point, collision_normal);
  131. if (!collision)
  132. return collision;
  133. // Transform the results back
  134. rot.makeInverse();
  135. *collision_point = rot * *collision_point;
  136. *raw_collision_normal = *collision_normal;
  137. *collision_normal = rot * *collision_normal;
  138. return collision;
  139. }