Ray2.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Diagnostics;
  3. using Microsoft.Xna.Framework;
  4. namespace MonoGame.Extended
  5. {
  6. // Real-Time Collision Detection, Christer Ericson, 2005. Chapter 3.5; A Math and Geometry Primer - Lines, Rays, and Segments. pg 53-54
  7. /// <summary>
  8. /// A two dimensional ray defined by a starting <see cref="Vector2" /> and a direction <see cref="Vector2" />.
  9. /// </summary>
  10. /// <seealso cref="IEquatable{T}" />
  11. /// <seealso cref="IEquatableByRef{Ray2}" />
  12. [DebuggerDisplay("{DebugDisplayString,nq}")]
  13. public struct Ray2 : IEquatable<Ray2>, IEquatableByRef<Ray2>
  14. {
  15. /// <summary>
  16. /// The starting <see cref="Vector2" /> of this <see cref="Ray2" />.
  17. /// </summary>
  18. public Vector2 Position;
  19. /// <summary>
  20. /// The direction <see cref="Vector2" /> of this <see cref="Ray2" />.
  21. /// </summary>
  22. public Vector2 Direction;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="Ray2" /> structure from the specified position and direction.
  25. /// </summary>
  26. /// <param name="position">The starting point.</param>
  27. /// <param name="direction">The direction vector.</param>
  28. public Ray2(Vector2 position, Vector2 direction)
  29. {
  30. Position = position;
  31. Direction = direction;
  32. }
  33. /// <summary>
  34. /// Determines whether this <see cref="Ray2" /> intersects with a specified <see cref="BoundingRectangle" />.
  35. /// </summary>
  36. /// <param name="boundingRectangle">The bounding rectangle.</param>
  37. /// <param name="rayNearDistance">
  38. /// When this method returns, contains the distance along the ray to the first intersection
  39. /// point with the <paramref name="boundingRectangle" />, if an intersection was found; otherwise,
  40. /// <see cref="float.NaN" />.
  41. /// This parameter is passed uninitialized.
  42. /// </param>
  43. /// <param name="rayFarDistance">
  44. /// When this method returns, contains the distance along the ray to the second intersection
  45. /// point with the <paramref name="boundingRectangle" />, if an intersection was found; otherwise,
  46. /// <see cref="float.NaN" />.
  47. /// This parameter is passed uninitialized.
  48. /// </param>
  49. /// <returns>
  50. /// <c>true</c> if this <see cref="Ray2" /> intersects with <paramref name="boundingRectangle" />; otherwise,
  51. /// <c>false</c>.
  52. /// </returns>
  53. public bool Intersects(BoundingRectangle boundingRectangle, out float rayNearDistance, out float rayFarDistance)
  54. {
  55. // Real-Time Collision Detection, Christer Ericson, 2005. Chapter 5.3; Basic Primitive Tests - Intersecting Lines, Rays, and (Directed Segments). pg 179-181
  56. var minimum = boundingRectangle.Center - boundingRectangle.HalfExtents;
  57. var maximum = boundingRectangle.Center + boundingRectangle.HalfExtents;
  58. // Set to the smallest possible value so the algorithm can find the first hit along the ray
  59. var minimumDistanceAlongRay = float.MinValue;
  60. // Set to the maximum possible value so the algorithm can find the last hit along the ray
  61. var maximumDistanceAlongRay = float.MaxValue;
  62. // For all relevant slabs which in this case is two.
  63. // The first, horizontal, slab.
  64. if (!PrimitivesHelper.IntersectsSlab(Position.X, Direction.X, minimum.X, maximum.X,
  65. ref minimumDistanceAlongRay,
  66. ref maximumDistanceAlongRay))
  67. {
  68. rayNearDistance = rayFarDistance = float.NaN;
  69. return false;
  70. }
  71. // The second, vertical, slab.
  72. if (!PrimitivesHelper.IntersectsSlab(Position.Y, Direction.Y, minimum.Y, maximum.Y,
  73. ref minimumDistanceAlongRay,
  74. ref maximumDistanceAlongRay))
  75. {
  76. rayNearDistance = rayFarDistance = float.NaN;
  77. return false;
  78. }
  79. // Ray intersects the 2 slabs.
  80. rayNearDistance = minimumDistanceAlongRay < 0 ? 0 : minimumDistanceAlongRay;
  81. rayFarDistance = maximumDistanceAlongRay;
  82. return true;
  83. }
  84. /// <summary>
  85. /// Compares two <see cref="Ray2" /> structures. The result specifies whether the values of the
  86. /// <see cref="Position" />
  87. /// and <see cref="Direction" /> fields of the two <see cref="Ray2" /> structures are equal.
  88. /// </summary>
  89. /// <param name="first">The first ray.</param>
  90. /// <param name="second">The second ray.</param>
  91. /// <returns>
  92. /// <c>true</c> if the <see cref="Position" /> and <see cref="Direction" />
  93. /// fields of the two <see cref="Ray2" />
  94. /// structures are equal; otherwise, <c>false</c>.
  95. /// </returns>
  96. public static bool operator ==(Ray2 first, Ray2 second)
  97. {
  98. return first.Equals(ref second);
  99. }
  100. /// <summary>
  101. /// Indicates whether this <see cref="Ray2" /> is equal to another <see cref="Ray2" />.
  102. /// </summary>
  103. /// <param name="ray">The ray.</param>
  104. /// <returns>
  105. /// <c>true</c> if this <see cref="Ray2" /> is equal to the <paramref name="ray" /> parameter; otherwise,
  106. /// <c>false</c>.
  107. /// </returns>
  108. public bool Equals(Ray2 ray)
  109. {
  110. return Equals(ref ray);
  111. }
  112. /// <summary>
  113. /// Indicates whether this <see cref="Ray2" /> is equal to another <see cref="Ray2" />.
  114. /// </summary>
  115. /// <param name="ray">The ray.</param>
  116. /// <returns>
  117. /// <c>true</c> if this <see cref="Ray2" /> is equal to the <paramref name="ray" />; otherwise,
  118. /// <c>false</c>.
  119. /// </returns>
  120. public bool Equals(ref Ray2 ray)
  121. {
  122. // ReSharper disable CompareOfFloatsByEqualityOperator
  123. return (ray.Position == Position) && (ray.Direction == Direction);
  124. // ReSharper restore CompareOfFloatsByEqualityOperator
  125. }
  126. /// <summary>
  127. /// Returns a value indicating whether this <see cref="Ray2" /> is equal to a specified object.
  128. /// </summary>
  129. /// <param name="obj">The object to make the comparison with.</param>
  130. /// <returns>
  131. /// <c>true</c> if this <see cref="Ray2" /> is equal to <paramref name="obj" />; otherwise, <c>false</c>.
  132. /// </returns>
  133. public override bool Equals(object obj)
  134. {
  135. if (obj is Ray2)
  136. return Equals((Ray2) obj);
  137. return false;
  138. }
  139. /// <summary>
  140. /// Compares two <see cref="Ray2" /> structures. The result specifies whether the values of the
  141. /// <see cref='Position' />
  142. /// and <see cref="Direction" /> fields of the two <see cref="Ray2" /> structures are unequal.
  143. /// </summary>
  144. /// <param name="first">The first ray.</param>
  145. /// <param name="second">The second ray.</param>
  146. /// <returns>
  147. /// <c>true</c> if the <see cref="Position" /> and <see cref="Direction" />
  148. /// fields of the two <see cref="Ray2" />
  149. /// structures are unequal; otherwise, <c>false</c>.
  150. /// </returns>
  151. public static bool operator !=(Ray2 first, Ray2 second)
  152. {
  153. return !(first == second);
  154. }
  155. /// <summary>
  156. /// Returns a hash code of this <see cref="Ray2" /> suitable for use in hashing algorithms and data
  157. /// structures like a hash table.
  158. /// </summary>
  159. /// <returns>
  160. /// A hash code of this <see cref="Ray2" />.
  161. /// </returns>
  162. public override int GetHashCode()
  163. {
  164. unchecked
  165. {
  166. return (Position.GetHashCode()*397) ^ Direction.GetHashCode();
  167. }
  168. }
  169. /// <summary>
  170. /// Returns a <see cref="string" /> that represents this <see cref="Ray2" />.
  171. /// </summary>
  172. /// <returns>
  173. /// A <see cref="string" /> that represents this <see cref="Ray2" />.
  174. /// </returns>
  175. public override string ToString()
  176. {
  177. return $"Position: {Position}, Direction: {Direction}";
  178. }
  179. internal string DebugDisplayString => ToString();
  180. }
  181. }