You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CollisionChecker.cs 11 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using Preparation.Interface;
  5. using Preparation.Utility;
  6. namespace GameEngine
  7. {
  8. internal class CollisionChecker
  9. {
  10. /// <summary>
  11. /// 碰撞检测,如果这样行走是否会与之碰撞,返回与之碰撞的物体
  12. /// </summary>
  13. /// <param name="obj">移动的物体</param>
  14. /// <param name="moveVec">移动的位移向量</param>
  15. /// <returns>和它碰撞的物体</returns>
  16. public IGameObj? CheckCollision(IMoveable obj, XY moveVec)
  17. {
  18. XY nextPos = obj.Position + moveVec;
  19. if (!obj.IsRigid)
  20. {
  21. if (gameMap.IsOutOfBound(obj))
  22. return gameMap.GetOutOfBound(nextPos);
  23. return null;
  24. }
  25. // 在列表中检查碰撞
  26. Func<IEnumerable<IGameObj>, ReaderWriterLockSlim, IGameObj?> CheckCollisionInList =
  27. (IEnumerable<IGameObj> lst, ReaderWriterLockSlim listLock) =>
  28. {
  29. IGameObj? collisionObj = null;
  30. listLock.EnterReadLock();
  31. try
  32. {
  33. foreach (var listObj in lst)
  34. {
  35. if (obj.WillCollideWith(listObj, nextPos))
  36. {
  37. collisionObj = listObj;
  38. break;
  39. }
  40. }
  41. }
  42. finally
  43. {
  44. listLock.ExitReadLock();
  45. }
  46. return collisionObj;
  47. };
  48. IGameObj? collisionObj = null;
  49. foreach (var list in lists)
  50. {
  51. if ((collisionObj = CheckCollisionInList(list.Item1, list.Item2)) != null)
  52. {
  53. return collisionObj;
  54. }
  55. }
  56. return null;
  57. }
  58. /// <summary>
  59. /// /// 可移动物体(圆)向矩形物体移动时,可移动且不会碰撞的最大距离。直接用double计算,防止误差
  60. /// </summary>
  61. /// <param name="obj"></param>
  62. /// <param name="square">矩形的中心坐标</param>
  63. /// <returns></returns>
  64. // private double MaxMoveToSquare(IMoveable obj, IGameObj square)
  65. //{
  66. // double tmpMax;
  67. // double angle = Math.Atan2(square.Position.y - obj.Position.y, square.Position.x - obj.Position.x);
  68. // if (obj.WillCollideWith(square, obj.Position))
  69. // tmpMax = 0;
  70. // else tmpMax =
  71. // Math.Abs(XYPosition.Distance(obj.Position, square.Position) - obj.Radius -
  72. // (square.Radius / Math.Min(Math.Abs(Math.Cos(angle)), Math.Abs(Math.Sin(angle)))));
  73. // return tmpMax;
  74. // }
  75. // private double FindMaxOnlyConsiderWall(IMoveable obj, Vector moveVec)
  76. //{
  77. // var desination = moveVec;
  78. // double maxOnlyConsiderWall = moveVec.length;
  79. // if (desination.length > 0) //如果length足够长,还是有可能穿墙的
  80. // {
  81. // XYPosition nextXY = Vector.Vector2XY(desination) + obj.Position + new XYPosition((int)(obj.Radius * Math.Cos(moveVec.angle)), (int)(obj.Radius * Math.Sin(moveVec.angle)));
  82. // if (gameMap.IsWall(nextXY)) //对下一步的位置进行检查,但这里只是考虑移动物体的宽度,只是考虑下一步能达到的最远位置
  83. // {
  84. // maxOnlyConsiderWall = MaxMoveToSquare(obj, gameMap.GetCell(nextXY));
  85. // }
  86. // else //考虑物体宽度
  87. // {
  88. // double dist = 0;
  89. // XYPosition nextXYConsiderWidth;
  90. // nextXYConsiderWidth = nextXY + new XYPosition((int)(obj.Radius * Math.Cos(moveVec.angle + Math.PI / 4)), (int)(obj.Radius * Math.Sin(moveVec.angle + Math.PI / 4)));
  91. // if (gameMap.IsWall(nextXYConsiderWidth)) //对下一步的位置进行检查,但这里只是考虑移动物体的宽度,只是考虑下一步能达到的最远位置
  92. // {
  93. // dist = MaxMoveToSquare(obj, gameMap.GetCell(nextXYConsiderWidth));
  94. // if (dist < maxOnlyConsiderWall)
  95. // maxOnlyConsiderWall = dist;
  96. // }
  97. // nextXYConsiderWidth = nextXY + new XYPosition((int)(obj.Radius * Math.Cos(moveVec.angle - Math.PI / 4)), (int)(obj.Radius * Math.Sin(moveVec.angle - Math.PI / 4)));
  98. // if (gameMap.IsWall(nextXYConsiderWidth)) //对下一步的位置进行检查,但这里只是考虑移动物体的宽度,只是考虑下一步能达到的最远位置
  99. // {
  100. // dist = MaxMoveToSquare(obj, gameMap.GetCell(nextXYConsiderWidth));
  101. // if (dist < maxOnlyConsiderWall)
  102. // maxOnlyConsiderWall = dist;
  103. // }
  104. // }
  105. // }
  106. // return maxOnlyConsiderWall;
  107. // }
  108. /// <summary>
  109. /// 寻找最大可能移动距离
  110. /// </summary>
  111. /// <param name="obj">移动物体,默认obj.Rigid为true</param>
  112. /// <param name="nextPos">下一步要到达的位置</param>
  113. /// <param name="moveVec">移动的位移向量,默认与nextPos协调</param>
  114. /// <returns>最大可能的移动距离</returns>
  115. public double FindMax(IMoveable obj, XY nextPos, XY moveVec)
  116. {
  117. double maxLen = (double)uint.MaxValue;
  118. double tmpMax = maxLen; // 暂存最大值
  119. // 先找只考虑墙的最大距离
  120. // double maxOnlyConsiderWall = FindMaxOnlyConsiderWall(obj, moveVec);
  121. double maxDistance = maxLen;
  122. foreach (var listWithLock in lists)
  123. {
  124. var lst = listWithLock.Item1;
  125. var listLock = listWithLock.Item2;
  126. listLock.EnterReadLock();
  127. try
  128. {
  129. foreach (IGameObj listObj in lst)
  130. {
  131. // 如果再走一步发生碰撞
  132. if (obj.WillCollideWith(listObj, nextPos))
  133. {
  134. {
  135. switch (listObj.Shape) // 默认obj为圆形
  136. {
  137. case ShapeType.Circle:
  138. {
  139. // 计算两者之间的距离
  140. double mod = XY.Distance(listObj.Position, obj.Position);
  141. int orgDeltaX = listObj.Position.x - obj.Position.x;
  142. int orgDeltaY = listObj.Position.y - obj.Position.y;
  143. if (mod < listObj.Radius + obj.Radius) // 如果两者已经重叠
  144. {
  145. tmpMax = 0;
  146. }
  147. else
  148. {
  149. double tmp = mod - obj.Radius - listObj.Radius;
  150. // 计算能走的最长距离,好像这么算有一点误差?
  151. tmp = tmp / Math.Cos(Math.Atan2(orgDeltaY, orgDeltaX) - moveVec.Angle());
  152. if (tmp < 0 || tmp > uint.MaxValue || tmp == double.NaN)
  153. {
  154. tmpMax = uint.MaxValue;
  155. }
  156. else
  157. tmpMax = tmp;
  158. }
  159. break;
  160. }
  161. case ShapeType.Square:
  162. {
  163. // if (obj.WillCollideWith(listObj, obj.Position))
  164. // tmpMax = 0;
  165. // else tmpMax = MaxMoveToSquare(obj, listObj);
  166. // break;
  167. if (obj.WillCollideWith(listObj, obj.Position))
  168. tmpMax = 0;
  169. else
  170. {
  171. // 二分查找最大可能移动距离
  172. int left = 0, right = (int)moveVec.Length();
  173. while (left < right - 1)
  174. {
  175. int mid = (right - left) / 2 + left;
  176. if (obj.WillCollideWith(listObj, obj.Position + new XY((int)(mid * Math.Cos(moveVec.Angle())), (int)(mid * Math.Sin(moveVec.Angle())))))
  177. {
  178. right = mid;
  179. }
  180. else
  181. left = mid;
  182. }
  183. tmpMax = (uint)left;
  184. }
  185. break;
  186. }
  187. default:
  188. tmpMax = uint.MaxValue;
  189. break;
  190. }
  191. if (tmpMax < maxDistance)
  192. maxDistance = tmpMax;
  193. }
  194. }
  195. }
  196. }
  197. finally
  198. {
  199. // maxLen = Math.Min(maxOnlyConsiderWall, maxDistance); //最大可能距离的最小值
  200. listLock.ExitReadLock();
  201. }
  202. }
  203. // return maxLen;
  204. return maxDistance;
  205. }
  206. readonly IMap gameMap;
  207. private readonly Tuple<IEnumerable<IGameObj>, ReaderWriterLockSlim>[] lists;
  208. public CollisionChecker(IMap gameMap)
  209. {
  210. this.gameMap = gameMap;
  211. lists = new Tuple<IEnumerable<IGameObj>, ReaderWriterLockSlim>[gameMap.GameObjDict.Count];
  212. int i = 0;
  213. foreach (var keyValuePair in gameMap.GameObjDict)
  214. {
  215. lists[i++] = new Tuple<IEnumerable<IGameObj>, ReaderWriterLockSlim>(keyValuePair.Value as IList<IGameObj>, gameMap.GameObjLockDict[keyValuePair.Key]);
  216. }
  217. }
  218. }
  219. }