Guest User

Untitled

a guest
Apr 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.35 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Audio;
  3. using Microsoft.Xna.Framework.Content;
  4. using Microsoft.Xna.Framework.GamerServices;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Input;
  7. using Microsoft.Xna.Framework.Media;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11.  
  12. namespace WindowsGame1
  13. {
  14. class Collision
  15. {
  16. public RectangleCustom R;
  17. public RectangleCustom S;
  18.  
  19. public Collision()
  20. {
  21. //R = new RectangleCustom();
  22. //S = new RectangleCustom();
  23. }
  24.  
  25.  
  26. public Collision(RectangleCustom rr, RectangleCustom ss)
  27. {
  28. this.R = rr;
  29. this.S = ss;
  30. }
  31.  
  32.  
  33. public RectangleCustom CollisionDetected()
  34. {
  35. Vector2 RTL = R.getTopLeft();
  36. Vector2 SBR = S.getBottomRight();
  37.  
  38. Vector2 RTR = R.getTopRight();
  39. Vector2 SBL = S.getBottomLeft();
  40.  
  41. Vector2 RBL = R.getBottomLeft();
  42. Vector2 STR = S.getTopRight();
  43.  
  44. Vector2 RBR = R.getBottomRight();
  45. Vector2 STL = S.getTopLeft();
  46.  
  47. if (InsideCollisionDetected() == 1)
  48. {
  49. return S;
  50. }
  51. else if (InsideCollisionDetected() == 2)
  52. {
  53. return R;
  54. }
  55. //R STANDING STILL RECTANGLE
  56. //S MOVING
  57. else if (SideCollisionDetected() == 1)
  58. {
  59. if (RTL.Y > STL.Y)
  60. {
  61. RectangleCustom Rect = new RectangleCustom(R.mainRectangle.X, S.mainRectangle.Y, (S.mainRectangle.Width - R.mainRectangle.X + S.mainRectangle.X), S.mainRectangle.Height);
  62. return Rect;
  63. }
  64. else
  65. {
  66.  
  67. RectangleCustom Rect = new RectangleCustom(R.mainRectangle.X, S.mainRectangle.Y, ((S.mainRectangle.Width + S.mainRectangle.X) - R.mainRectangle.X), S.mainRectangle.Height);
  68. return Rect;
  69. }
  70. }
  71.  
  72. else if (SideCollisionDetected() == 2)
  73. {
  74. if (RTL.Y > STL.Y)
  75. {
  76. RectangleCustom Rect = new RectangleCustom(S.mainRectangle.X, R.mainRectangle.Y, S.mainRectangle.Width, (S.mainRectangle.Height + S.mainRectangle.Y - R.mainRectangle.Y));
  77. return Rect;
  78.  
  79. }
  80. else
  81. {
  82. RectangleCustom Rect = new RectangleCustom(S.mainRectangle.X, S.mainRectangle.Y, S.mainRectangle.Width, (R.mainRectangle.Height + R.mainRectangle.Y - S.mainRectangle.Y));
  83. return Rect;
  84.  
  85. }
  86. }
  87. else if (SideCollisionDetected() == 3)
  88. {
  89. if (RTL.Y > STL.Y)
  90. {
  91. RectangleCustom Rect = new RectangleCustom(S.mainRectangle.X, S.mainRectangle.Y, R.mainRectangle.X + R.mainRectangle.Width - S.mainRectangle.X, S.mainRectangle.Height);
  92. return Rect;
  93. }
  94. else
  95. {
  96. RectangleCustom Rect = new RectangleCustom(S.mainRectangle.X, S.mainRectangle.Y, R.mainRectangle.X + R.mainRectangle.Width - S.mainRectangle.X, S.mainRectangle.Height);
  97. return Rect;
  98. }
  99. }
  100. else if (SideCollisionDetected() == 4)
  101. {
  102. if (RTL.Y > STL.Y)
  103. {
  104. RectangleCustom Rect = new RectangleCustom(S.mainRectangle.X, R.mainRectangle.Y, S.mainRectangle.Width, S.mainRectangle.Y + S.mainRectangle.Height - R.mainRectangle.Y);
  105. return Rect;
  106. }
  107. else
  108. {
  109. RectangleCustom Rect = new RectangleCustom(S.mainRectangle.X, S.mainRectangle.Y, S.mainRectangle.Width, R.mainRectangle.Y + R.mainRectangle.Height - S.mainRectangle.Y);
  110. return Rect;
  111. }
  112. }
  113. else if (CornerCollisionDetected() == 1)
  114. {
  115. RectangleCustom Rect = new RectangleCustom(R.mainRectangle.X, R.mainRectangle.Y, (S.mainRectangle.X + S.mainRectangle.Width)-R.mainRectangle.X, ((S.mainRectangle.Y + S.mainRectangle.Height) - R.mainRectangle.Y));
  116. return Rect;
  117. //return null;
  118. }
  119.  
  120. else if (CornerCollisionDetected() == 2)
  121. {
  122. RectangleCustom Rect = new RectangleCustom(S.mainRectangle.X, R.mainRectangle.Y, (R.mainRectangle.X + R.mainRectangle.Width - (S.mainRectangle.X)), ((S.mainRectangle.Y + S.mainRectangle.Height) - R.mainRectangle.Y));
  123. return Rect;
  124. //return null;
  125. }
  126.  
  127. else if (CornerCollisionDetected() == 3)
  128. {
  129. RectangleCustom Rect = new RectangleCustom(S.mainRectangle.X, S.mainRectangle.Y, (R.mainRectangle.X + R.mainRectangle.Width - S.mainRectangle.X), (R.mainRectangle.Y + R.mainRectangle.Height - S.mainRectangle.Y));
  130. return Rect;
  131. // return null;
  132. }
  133.  
  134. else if (CornerCollisionDetected() == 4)
  135. {
  136. RectangleCustom Rect = new RectangleCustom(R.mainRectangle.X, S.mainRectangle.Y, S.mainRectangle.X + S.mainRectangle.Width - R.mainRectangle.X, R.mainRectangle.Y + R.mainRectangle.Height - S.mainRectangle.Y);
  137. return Rect;
  138. // return null;
  139. }
  140.  
  141. return null;
  142.  
  143. }
  144.  
  145. public int InsideCollisionDetected()
  146. {
  147.  
  148. Vector2 RTL = R.getTopLeft();
  149. Vector2 SBR = S.getBottomRight();
  150.  
  151. Vector2 RTR = R.getTopRight();
  152. Vector2 SBL = S.getBottomLeft();
  153.  
  154. Vector2 RBL = R.getBottomLeft();
  155. Vector2 STR = S.getTopRight();
  156.  
  157. Vector2 RBR = R.getBottomRight();
  158. Vector2 STL = S.getTopLeft();
  159.  
  160. if ((RTL.X <= STL.X && RTL.Y <= STL.Y) && (RTR.X >= STR.X && RTR.Y <= STL.Y) &&
  161. (RBL.X <= SBL.X && RBL.Y >= SBL.Y) && (RBR.X >= SBR.X && RBR.Y >= SBR.Y))
  162. {
  163. return 1; //1 if r inside s
  164. }
  165.  
  166. else if ((RTL.X >= STL.X && RTL.Y >= STL.Y) && (RTR.X <= STR.X && RTR.Y >= STL.Y) &&
  167. (RBL.X >= SBL.X && RBL.Y <= SBL.Y) && (RBR.X <= SBR.X && RBR.Y <= SBR.Y))
  168. {
  169. return 2; //2 if s inside of r
  170. }
  171.  
  172. else return 0;
  173. }
  174.  
  175.  
  176. public int SideCollisionDetected()
  177. {
  178. Vector2 RTL = R.getTopLeft();
  179. Vector2 SBR = S.getBottomRight();
  180.  
  181. Vector2 RTR = R.getTopRight();
  182. Vector2 SBL = S.getBottomLeft();
  183.  
  184. Vector2 RBL = R.getBottomLeft();
  185. Vector2 STR = S.getTopRight();
  186.  
  187. Vector2 RBR = R.getBottomRight();
  188. Vector2 STL = S.getTopLeft();
  189.  
  190. if ((RTR.X >= STL.X && RTR.Y >= STL.Y) && (RTL.X <= STL.X && RBL.Y <= SBL.Y) ||
  191. (STR.X >= RTL.X && STR.Y >= RTL.Y) && (STL.X <= RTL.X && SBL.Y <= RBL.Y))
  192. {
  193. return 1;
  194. }
  195.  
  196. else if (((RBL.X >= STL.X && RBL.Y >= STR.Y) && (RBL.Y <= SBR.Y && RBR.X <= STR.X)) ||
  197. ((SBL.X >= RTL.X && SBL.Y >= RTR.Y) && (SBL.Y <= RBR.Y && SBR.X <= RTR.X)))
  198. {
  199. return 2;
  200. }
  201.  
  202. else if (((RTL.X <= STL.X && RTL.Y >= STL.Y) && (RTR.X <= STR.X && RBL.Y <= SBR.Y)) ||
  203. ((STL.X <= RTR.X && STL.Y >= RTL.Y) && (STR.X >= RTR.X && SBL.Y <= RBR.Y)))
  204. {
  205. return 3;
  206. }
  207.  
  208. else if (((RTL.X >= STL.X && RTL.Y <= SBL.Y) && (RTR.X <= STR.X && RBR.Y >= SBR.Y)) ||
  209. ((STL.X >= RTL.X && STL.Y <= RBL.Y) && (STR.X <= RTR.X && SBR.Y >= RBR.Y)))
  210. {
  211. return 4;
  212. }
  213.  
  214. else return 0;
  215. }
  216.  
  217. public int CornerCollisionDetected()
  218. {
  219. Vector2 RTL = R.getTopLeft();
  220. Vector2 SBR = S.getBottomRight();
  221.  
  222. Vector2 RTR = R.getTopRight();
  223. Vector2 SBL = S.getBottomLeft();
  224.  
  225. Vector2 RBL = R.getBottomLeft();
  226. Vector2 STR = S.getTopRight();
  227.  
  228. Vector2 RBR = R.getBottomRight();
  229. Vector2 STL = S.getTopLeft();
  230.  
  231.  
  232. //if (((RTL.X <= SBR.X) && (RTL.Y <= SBR.Y)) && (RBR.Y > SBR.Y && RTL.X > STL.X))
  233. if (SBR.X >= RTL.X && SBR.Y >= RTL.Y && STL.X < RTL.X && STL.Y < RTL.Y)
  234. {
  235. return 1;
  236. }
  237.  
  238. else if (((RTR.X >= SBL.X) && (RTR.Y <= SBL.Y)) && (RTR.X < STR.X && RTR.Y > STR.Y))
  239. {
  240. return 2;
  241. }
  242.  
  243. else if (((RBR.X >= STL.X) && (RBR.Y >= STL.Y)) && (RBR.X < STR.X && RBR.Y < SBR.Y))
  244. {
  245. return 3;
  246. }
  247.  
  248. else if (((RBL.X <= STR.X) && (RBL.Y >= STR.Y)) && (RBL.X > SBL.X && RBL.Y > STL.Y))
  249. {
  250. return 4;
  251. }
  252.  
  253. else return 0;
  254. }
  255.  
  256.  
  257. public static List<Vector2> CheckCollsion(List<Vector2> r1, List<Vector2> r2)
  258. {
  259. List<Vector2> intersections = new List<Vector2>();
  260.  
  261. for (int x = 0; x < r1.Count - 1; ++x)
  262. {
  263. Vector2 r1p1 = r1[x];
  264. Vector2 r1p2 = r1[x + 1];
  265. ParametricLine l1 = new ParametricLine(r1p1, r1p1 - r1p2);
  266. for (int y = 0; y < r2.Count - 1; ++y)
  267. {
  268. Vector2 r2p1 = r1[y];
  269. Vector2 r2p2 = r2[y + 1];
  270. ParametricLine l2 = new ParametricLine(r2p1, r2p1 - r2p2);
  271. Vector2? Intersect = ParametricLine.findIntersect(l1, l2);
  272. if (Intersect != null && InBetween(r1p1, r1p2, Intersect.Value) && InBetween(r2p1, r2p2, Intersect.Value))
  273. {
  274. intersections.Add(Intersect.Value);
  275. }
  276. }
  277. }
  278. return intersections;
  279. }
  280. public static bool InBetween(Vector2 first, Vector2 second, Vector2 center)
  281. {
  282. if ((first.X < center.X && second.X > center.X) || (first.X > center.X && second.X < center.X))
  283. {
  284. if ((first.Y < center.Y && second.Y > center.Y) || (first.Y > center.Y && second.Y < center.Y))
  285. {
  286. return true;
  287. }
  288. }
  289. return false;
  290. }
  291.  
  292.  
  293. }
  294. }
Add Comment
Please, Sign In to add comment