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