raycast.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 3.0 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 "irr_v3d.h"
  17. #include "irr_aabb3d.h"
  18. bool boxLineCollision(const aabb3f &box, const v3f &start,
  19. const v3f &dir, v3f *collision_point, v3s16 *collision_normal) {
  20. if (box.isPointInside(start)) {
  21. *collision_point = start;
  22. collision_normal->set(0, 0, 0);
  23. return true;
  24. }
  25. float m = 0;
  26. // Test X collision
  27. if (dir.X != 0) {
  28. if (dir.X > 0)
  29. m = (box.MinEdge.X - start.X) / dir.X;
  30. else
  31. m = (box.MaxEdge.X - start.X) / dir.X;
  32. if (m >= 0 && m <= 1) {
  33. *collision_point = start + dir * m;
  34. if ((collision_point->Y >= box.MinEdge.Y)
  35. && (collision_point->Y <= box.MaxEdge.Y)
  36. && (collision_point->Z >= box.MinEdge.Z)
  37. && (collision_point->Z <= box.MaxEdge.Z)) {
  38. collision_normal->set((dir.X > 0) ? -1 : 1, 0, 0);
  39. return true;
  40. }
  41. }
  42. }
  43. // Test Y collision
  44. if (dir.Y != 0) {
  45. if (dir.Y > 0)
  46. m = (box.MinEdge.Y - start.Y) / dir.Y;
  47. else
  48. m = (box.MaxEdge.Y - start.Y) / dir.Y;
  49. if (m >= 0 && m <= 1) {
  50. *collision_point = start + dir * m;
  51. if ((collision_point->X >= box.MinEdge.X)
  52. && (collision_point->X <= box.MaxEdge.X)
  53. && (collision_point->Z >= box.MinEdge.Z)
  54. && (collision_point->Z <= box.MaxEdge.Z)) {
  55. collision_normal->set(0, (dir.Y > 0) ? -1 : 1, 0);
  56. return true;
  57. }
  58. }
  59. }
  60. // Test Z collision
  61. if (dir.Z != 0) {
  62. if (dir.Z > 0)
  63. m = (box.MinEdge.Z - start.Z) / dir.Z;
  64. else
  65. m = (box.MaxEdge.Z - start.Z) / dir.Z;
  66. if (m >= 0 && m <= 1) {
  67. *collision_point = start + dir * m;
  68. if ((collision_point->X >= box.MinEdge.X)
  69. && (collision_point->X <= box.MaxEdge.X)
  70. && (collision_point->Y >= box.MinEdge.Y)
  71. && (collision_point->Y <= box.MaxEdge.Y)) {
  72. collision_normal->set(0, 0, (dir.Z > 0) ? -1 : 1);
  73. return true;
  74. }
  75. }
  76. }
  77. return false;
  78. }