raycast.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. f32 pt1_distSq = pt1.distanceSq;
  27. // Add some bonus when one of them is an object
  28. if (pt1.type != pt2.type) {
  29. if (pt1.type == POINTEDTHING_OBJECT)
  30. pt1_distSq -= BS * BS;
  31. else if (pt2.type == POINTEDTHING_OBJECT)
  32. pt1_distSq += BS * BS;
  33. }
  34. // returns false if pt1 is nearer than pt2
  35. if (pt1_distSq < pt2.distanceSq) {
  36. return false;
  37. }
  38. if (pt1_distSq == pt2.distanceSq) {
  39. // Sort them to allow only one order
  40. if (pt1.type == POINTEDTHING_OBJECT)
  41. return (pt2.type == POINTEDTHING_OBJECT
  42. && pt1.object_id < pt2.object_id);
  43. return (pt2.type == POINTEDTHING_OBJECT
  44. || pt1.node_undersurface < pt2.node_undersurface);
  45. }
  46. return true;
  47. }
  48. RaycastState::RaycastState(const core::line3d<f32> &shootline,
  49. bool objects_pointable, bool liquids_pointable) :
  50. m_shootline(shootline),
  51. m_iterator(shootline.start / BS, shootline.getVector() / BS),
  52. m_previous_node(m_iterator.m_current_node_pos),
  53. m_objects_pointable(objects_pointable),
  54. m_liquids_pointable(liquids_pointable)
  55. {
  56. }
  57. bool boxLineCollision(const aabb3f &box, const v3f &start,
  58. const v3f &dir, v3f *collision_point, v3s16 *collision_normal)
  59. {
  60. if (box.isPointInside(start)) {
  61. *collision_point = start;
  62. collision_normal->set(0, 0, 0);
  63. return true;
  64. }
  65. float m = 0;
  66. // Test X collision
  67. if (dir.X != 0) {
  68. if (dir.X > 0)
  69. m = (box.MinEdge.X - start.X) / dir.X;
  70. else
  71. m = (box.MaxEdge.X - start.X) / dir.X;
  72. if (m >= 0 && m <= 1) {
  73. *collision_point = start + dir * m;
  74. if ((collision_point->Y >= box.MinEdge.Y)
  75. && (collision_point->Y <= box.MaxEdge.Y)
  76. && (collision_point->Z >= box.MinEdge.Z)
  77. && (collision_point->Z <= box.MaxEdge.Z)) {
  78. collision_normal->set((dir.X > 0) ? -1 : 1, 0, 0);
  79. return true;
  80. }
  81. }
  82. }
  83. // Test Y collision
  84. if (dir.Y != 0) {
  85. if (dir.Y > 0)
  86. m = (box.MinEdge.Y - start.Y) / dir.Y;
  87. else
  88. m = (box.MaxEdge.Y - start.Y) / dir.Y;
  89. if (m >= 0 && m <= 1) {
  90. *collision_point = start + dir * m;
  91. if ((collision_point->X >= box.MinEdge.X)
  92. && (collision_point->X <= box.MaxEdge.X)
  93. && (collision_point->Z >= box.MinEdge.Z)
  94. && (collision_point->Z <= box.MaxEdge.Z)) {
  95. collision_normal->set(0, (dir.Y > 0) ? -1 : 1, 0);
  96. return true;
  97. }
  98. }
  99. }
  100. // Test Z collision
  101. if (dir.Z != 0) {
  102. if (dir.Z > 0)
  103. m = (box.MinEdge.Z - start.Z) / dir.Z;
  104. else
  105. m = (box.MaxEdge.Z - start.Z) / dir.Z;
  106. if (m >= 0 && m <= 1) {
  107. *collision_point = start + dir * m;
  108. if ((collision_point->X >= box.MinEdge.X)
  109. && (collision_point->X <= box.MaxEdge.X)
  110. && (collision_point->Y >= box.MinEdge.Y)
  111. && (collision_point->Y <= box.MaxEdge.Y)) {
  112. collision_normal->set(0, 0, (dir.Z > 0) ? -1 : 1);
  113. return true;
  114. }
  115. }
  116. }
  117. return false;
  118. }