VoxelQuery.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. recast4j copyright (c) 2021 Piotr Piastucki piotr@jtilia.org
  3. DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. using System;
  19. using DotRecast.Core;
  20. using DotRecast.Recast;
  21. namespace DotRecast.Detour.Dynamic
  22. {
  23. /**
  24. * Voxel raycast based on the algorithm described in
  25. *
  26. * "A Fast Voxel Traversal Algorithm for Ray Tracing" by John Amanatides and Andrew Woo
  27. */
  28. public class VoxelQuery
  29. {
  30. private readonly RcVec3f origin;
  31. private readonly float tileWidth;
  32. private readonly float tileDepth;
  33. private readonly Func<int, int, RcHeightfield> heightfieldProvider;
  34. public VoxelQuery(RcVec3f origin, float tileWidth, float tileDepth, Func<int, int, RcHeightfield> heightfieldProvider)
  35. {
  36. this.origin = origin;
  37. this.tileWidth = tileWidth;
  38. this.tileDepth = tileDepth;
  39. this.heightfieldProvider = heightfieldProvider;
  40. }
  41. /**
  42. * Perform raycast using voxels heightfields.
  43. *
  44. * @return Optional with hit parameter (t) or empty if no hit found
  45. */
  46. public bool Raycast(RcVec3f start, RcVec3f end, out float hit)
  47. {
  48. return TraverseTiles(start, end, out hit);
  49. }
  50. private bool TraverseTiles(RcVec3f start, RcVec3f end, out float hit)
  51. {
  52. float relStartX = start.x - origin.x;
  53. float relStartZ = start.z - origin.z;
  54. int sx = (int)Math.Floor(relStartX / tileWidth);
  55. int sz = (int)Math.Floor(relStartZ / tileDepth);
  56. int ex = (int)Math.Floor((end.x - origin.x) / tileWidth);
  57. int ez = (int)Math.Floor((end.z - origin.z) / tileDepth);
  58. int dx = ex - sx;
  59. int dz = ez - sz;
  60. int stepX = dx < 0 ? -1 : 1;
  61. int stepZ = dz < 0 ? -1 : 1;
  62. float xRem = (tileWidth + (relStartX % tileWidth)) % tileWidth;
  63. float zRem = (tileDepth + (relStartZ % tileDepth)) % tileDepth;
  64. float tx = end.x - start.x;
  65. float tz = end.z - start.z;
  66. float xOffest = Math.Abs(tx < 0 ? xRem : tileWidth - xRem);
  67. float zOffest = Math.Abs(tz < 0 ? zRem : tileDepth - zRem);
  68. tx = Math.Abs(tx);
  69. tz = Math.Abs(tz);
  70. float tMaxX = xOffest / tx;
  71. float tMaxZ = zOffest / tz;
  72. float tDeltaX = tileWidth / tx;
  73. float tDeltaZ = tileDepth / tz;
  74. float t = 0;
  75. while (true)
  76. {
  77. bool isHit = TraversHeightfield(sx, sz, start, end, t, Math.Min(1, Math.Min(tMaxX, tMaxZ)), out hit);
  78. if (isHit)
  79. {
  80. return true;
  81. }
  82. if ((dx > 0 ? sx >= ex : sx <= ex) && (dz > 0 ? sz >= ez : sz <= ez))
  83. {
  84. break;
  85. }
  86. if (tMaxX < tMaxZ)
  87. {
  88. t = tMaxX;
  89. tMaxX += tDeltaX;
  90. sx += stepX;
  91. }
  92. else
  93. {
  94. t = tMaxZ;
  95. tMaxZ += tDeltaZ;
  96. sz += stepZ;
  97. }
  98. }
  99. return false;
  100. }
  101. private bool TraversHeightfield(int x, int z, RcVec3f start, RcVec3f end, float tMin, float tMax, out float hit)
  102. {
  103. RcHeightfield hf = heightfieldProvider.Invoke(x, z);
  104. if (null != hf)
  105. {
  106. float tx = end.x - start.x;
  107. float ty = end.y - start.y;
  108. float tz = end.z - start.z;
  109. float[] entry = { start.x + tMin * tx, start.y + tMin * ty, start.z + tMin * tz };
  110. float[] exit = { start.x + tMax * tx, start.y + tMax * ty, start.z + tMax * tz };
  111. float relStartX = entry[0] - hf.bmin.x;
  112. float relStartZ = entry[2] - hf.bmin.z;
  113. int sx = (int)Math.Floor(relStartX / hf.cs);
  114. int sz = (int)Math.Floor(relStartZ / hf.cs);
  115. int ex = (int)Math.Floor((exit[0] - hf.bmin.x) / hf.cs);
  116. int ez = (int)Math.Floor((exit[2] - hf.bmin.z) / hf.cs);
  117. int dx = ex - sx;
  118. int dz = ez - sz;
  119. int stepX = dx < 0 ? -1 : 1;
  120. int stepZ = dz < 0 ? -1 : 1;
  121. float xRem = (hf.cs + (relStartX % hf.cs)) % hf.cs;
  122. float zRem = (hf.cs + (relStartZ % hf.cs)) % hf.cs;
  123. float xOffest = Math.Abs(tx < 0 ? xRem : hf.cs - xRem);
  124. float zOffest = Math.Abs(tz < 0 ? zRem : hf.cs - zRem);
  125. tx = Math.Abs(tx);
  126. tz = Math.Abs(tz);
  127. float tMaxX = xOffest / tx;
  128. float tMaxZ = zOffest / tz;
  129. float tDeltaX = hf.cs / tx;
  130. float tDeltaZ = hf.cs / tz;
  131. float t = 0;
  132. while (true)
  133. {
  134. if (sx >= 0 && sx < hf.width && sz >= 0 && sz < hf.height)
  135. {
  136. float y1 = start.y + ty * (tMin + t) - hf.bmin.y;
  137. float y2 = start.y + ty * (tMin + Math.Min(tMaxX, tMaxZ)) - hf.bmin.y;
  138. float ymin = Math.Min(y1, y2) / hf.ch;
  139. float ymax = Math.Max(y1, y2) / hf.ch;
  140. RcSpan span = hf.spans[sx + sz * hf.width];
  141. while (span != null)
  142. {
  143. if (span.smin <= ymin && span.smax >= ymax)
  144. {
  145. hit = Math.Min(1, tMin + t);
  146. return true;
  147. }
  148. span = span.next;
  149. }
  150. }
  151. if ((dx > 0 ? sx >= ex : sx <= ex) && (dz > 0 ? sz >= ez : sz <= ez))
  152. {
  153. break;
  154. }
  155. if (tMaxX < tMaxZ)
  156. {
  157. t = tMaxX;
  158. tMaxX += tDeltaX;
  159. sx += stepX;
  160. }
  161. else
  162. {
  163. t = tMaxZ;
  164. tMaxZ += tDeltaZ;
  165. sz += stepZ;
  166. }
  167. }
  168. }
  169. hit = 0.0f;
  170. return false;
  171. }
  172. }
  173. }