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