CollisionMath.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CommonLang.Geometry
  6. {
  7. /// <summary>
  8. /// A code container for collision-related mathematical functions.
  9. /// </summary>
  10. static public class CollisionMath
  11. {
  12. /// <summary>
  13. /// Data defining a circle/line collision result.
  14. /// </summary>
  15. /// <remarks>Also used for circle/rectangles.</remarks>
  16. public struct CircleLineCollisionResult
  17. {
  18. public bool Collision;
  19. public Vector2 Point;
  20. public Vector2 Normal;
  21. public float Distance;
  22. }
  23. /// <summary>
  24. /// Determine if two circles intersect or contain each other.
  25. /// </summary>
  26. /// <param name="center1">The center of the first circle.</param>
  27. /// <param name="radius1">The radius of the first circle.</param>
  28. /// <param name="center2">The center of the second circle.</param>
  29. /// <param name="radius2">The radius of the second circle.</param>
  30. /// <returns>True if the circles intersect or contain one another.</returns>
  31. public static bool CircleCircleIntersect(Vector2 center1, float radius1, Vector2 center2, float radius2)
  32. {
  33. Vector2 line = center2 - center1;
  34. // we use LengthSquared to avoid a costly square-root call
  35. return (line.LengthSquared() <= (radius1 + radius2) * (radius1 + radius2));
  36. }
  37. /// <summary>
  38. /// Determines the point of intersection between two line segments,
  39. /// as defined by four points.
  40. /// </summary>
  41. /// <param name="a">The first point on the first line segment.</param>
  42. /// <param name="b">The second point on the first line segment.</param>
  43. /// <param name="c">The first point on the second line segment.</param>
  44. /// <param name="d">The second point on the second line segment.</param>
  45. /// <param name="point">The output value with the interesection, if any.</param>
  46. /// <remarks>The output parameter "point" is only valid
  47. /// when the return value is true.</remarks>
  48. /// <returns>True if intersecting, false otherwise.</returns>
  49. public static bool LineLineIntersect(Vector2 a, Vector2 b, Vector2 c, Vector2 d, out Vector2 point)
  50. {
  51. point = Vector2.Zero;
  52. double r, s;
  53. double denominator = (b.X - a.X) * (d.Y - c.Y) - (b.Y - a.Y) * (d.X - c.X);
  54. // If the denominator in above is zero, AB & CD are colinear
  55. if (denominator == 0)
  56. {
  57. return false;
  58. }
  59. double numeratorR = (a.Y - c.Y) * (d.X - c.X) - (a.X - c.X) * (d.Y - c.Y);
  60. r = numeratorR / denominator;
  61. double numeratorS = (a.Y - c.Y) * (b.X - a.X) - (a.X - c.X) * (b.Y - a.Y);
  62. s = numeratorS / denominator;
  63. // non-intersecting
  64. if (r < 0 || r > 1 || s < 0 || s > 1)
  65. {
  66. return false;
  67. }
  68. // find intersection point
  69. point.X = (float)(a.X + (r * (b.X - a.X)));
  70. point.Y = (float)(a.Y + (r * (b.Y - a.Y)));
  71. return true;
  72. }
  73. public static bool LineLineIntersect(Vector2 a, Vector2 b, Vector2 c, Vector2 d)
  74. {
  75. double r, s;
  76. double denominator = (b.X - a.X) * (d.Y - c.Y) - (b.Y - a.Y) * (d.X - c.X);
  77. // If the denominator in above is zero, AB & CD are colinear
  78. if (denominator == 0)
  79. {
  80. return false;
  81. }
  82. double numeratorR = (a.Y - c.Y) * (d.X - c.X) - (a.X - c.X) * (d.Y - c.Y);
  83. r = numeratorR / denominator;
  84. double numeratorS = (a.Y - c.Y) * (b.X - a.X) - (a.X - c.X) * (b.Y - a.Y);
  85. s = numeratorS / denominator;
  86. // non-intersecting
  87. if (r < 0 || r > 1 || s < 0 || s > 1)
  88. {
  89. return false;
  90. }
  91. return true;
  92. }
  93. /// <summary>
  94. /// Determines if a circle and line segment intersect, and if so, how they do.
  95. /// </summary>
  96. /// <param name="center">The center of the circle.</param>
  97. /// <param name="radius">The radius of the circle.</param>
  98. /// <param name="lineStart">The first point on the line segment.</param>
  99. /// <param name="lineEnd">The second point on the line segment.</param>
  100. /// <param name="result">The result data for the collision.</param>
  101. /// <returns>True if a collision occurs, provided for convenience.</returns>
  102. public static bool CircleLineCollide(Vector2 center, float radius, Vector2 lineStart, Vector2 lineEnd, ref CircleLineCollisionResult result)
  103. {
  104. Vector2 AC = center - lineStart;
  105. Vector2 AB = lineEnd - lineStart;
  106. float ab2 = AB.LengthSquared();
  107. if (ab2 <= 0f)
  108. {
  109. return false;
  110. }
  111. float acab = Vector2.Dot(AC, AB);
  112. float t = acab / ab2;
  113. if (t < 0.0f)
  114. t = 0.0f;
  115. else if (t > 1.0f)
  116. t = 1.0f;
  117. result.Point = lineStart + t * AB;
  118. result.Normal = center - result.Point;
  119. float h2 = result.Normal.LengthSquared();
  120. float r2 = radius * radius;
  121. if ((h2 == 0) || (h2 <= r2))
  122. {
  123. result.Normal.Normalize();
  124. result.Distance = (radius - (center - result.Point).Length());
  125. result.Collision = true;
  126. }
  127. else
  128. {
  129. result.Collision = false;
  130. }
  131. return result.Collision;
  132. }
  133. public static bool CircleLineCollide(Vector2 center, float radius, Vector2 lineStart, Vector2 lineEnd)
  134. {
  135. Vector2 AC = center - lineStart;
  136. Vector2 AB = lineEnd - lineStart;
  137. float ab2 = AB.LengthSquared();
  138. if (ab2 <= 0f)
  139. {
  140. return false;
  141. }
  142. float acab = Vector2.Dot(AC, AB);
  143. float t = acab / ab2;
  144. if (t < 0.0f)
  145. t = 0.0f;
  146. else if (t > 1.0f)
  147. t = 1.0f;
  148. Vector2 resultPoint = lineStart + t * AB;
  149. Vector2 resultNormal = center - resultPoint;
  150. float h2 = resultNormal.LengthSquared();
  151. float r2 = radius * radius;
  152. if ((h2 == 0) || (h2 <= r2))
  153. {
  154. return true;
  155. }
  156. else
  157. {
  158. return false;
  159. }
  160. }
  161. /// <summary>
  162. /// 点在凸多边形内部。
  163. /// </summary>
  164. /// <param name="center"></param>
  165. /// <param name="list"></param>
  166. /// <returns></returns>
  167. public static bool PointInPolygon(Vector2 center, Vector2[] list)
  168. {
  169. int wn = 0, j = 0; //wn 计数器 j第二个
  170. for (int i = 0; i < list.Length; i++)
  171. {
  172. //开始循环
  173. if (i == list.Length - 1)
  174. {
  175. j = 0;//如果 循环到最后一点 第二个指针指向第一点
  176. }
  177. else
  178. {
  179. j = j + 1; //如果不是 ,则找下一点
  180. }
  181. if (list[i].Y <= center.Y) // 如果多边形的点 小于等于 选定点的 Y 坐标
  182. {
  183. if (list[j].Y > center.Y) // 如果多边形的下一点 大于于 选定点的 Y 坐标
  184. {
  185. if (isLeft(list[i], list[j], center) > 0)
  186. {
  187. wn++;
  188. }
  189. }
  190. }
  191. else
  192. {
  193. if (list[j].Y <= center.Y)
  194. {
  195. if (isLeft(list[i], list[j], center) < 0)
  196. {
  197. wn--;
  198. }
  199. }
  200. }
  201. }
  202. if (wn == 0)
  203. {
  204. return false;
  205. }
  206. else
  207. {
  208. return true;
  209. }
  210. }
  211. //判断点在线的一边
  212. public static float isLeft(Vector2 P0, Vector2 P1, Vector2 P2)
  213. {
  214. float abc = ((P1.X - P0.X) * (P2.Y - P0.Y) - (P2.X - P0.X) * (P1.Y - P0.Y));
  215. return abc;
  216. }
  217. public static Vector2 MoveToByRadians(Vector2 p, float degree, float distance)
  218. {
  219. return new Vector2(
  220. p.X + (float)(Math.Cos(degree) * distance),
  221. p.Y + (float)(Math.Sin(degree) * distance));
  222. }
  223. }
  224. }