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