Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace WindowsGame1
- {
- class Collision
- {
- public RectangleCustom R;
- public RectangleCustom S;
- public Collision()
- {
- //R = new RectangleCustom();
- //S = new RectangleCustom();
- }
- public Collision(RectangleCustom rr, RectangleCustom ss)
- {
- this.R = rr;
- this.S = ss;
- }
- public RectangleCustom CollisionDetected()
- {
- Vector2 RTL = R.getTopLeft();
- Vector2 SBR = S.getBottomRight();
- Vector2 RTR = R.getTopRight();
- Vector2 SBL = S.getBottomLeft();
- Vector2 RBL = R.getBottomLeft();
- Vector2 STR = S.getTopRight();
- Vector2 RBR = R.getBottomRight();
- Vector2 STL = S.getTopLeft();
- if (InsideCollisionDetected() == 1)
- {
- return S;
- }
- else if (InsideCollisionDetected() == 2)
- {
- return R;
- }
- //R STANDING STILL RECTANGLE
- //S MOVING
- else if (SideCollisionDetected() == 1)
- {
- if (RTL.Y > STL.Y)
- {
- RectangleCustom Rect = new RectangleCustom(R.mainRectangle.X, S.mainRectangle.Y, (S.mainRectangle.Width - R.mainRectangle.X + S.mainRectangle.X), S.mainRectangle.Height);
- return Rect;
- }
- else
- {
- RectangleCustom Rect = new RectangleCustom(R.mainRectangle.X, S.mainRectangle.Y, ((S.mainRectangle.Width + S.mainRectangle.X) - R.mainRectangle.X), S.mainRectangle.Height);
- return Rect;
- }
- }
- else if (SideCollisionDetected() == 2)
- {
- if (RTL.Y > STL.Y)
- {
- RectangleCustom Rect = new RectangleCustom(S.mainRectangle.X, R.mainRectangle.Y, S.mainRectangle.Width, (S.mainRectangle.Height + S.mainRectangle.Y - R.mainRectangle.Y));
- return Rect;
- }
- else
- {
- RectangleCustom Rect = new RectangleCustom(S.mainRectangle.X, S.mainRectangle.Y, S.mainRectangle.Width, (R.mainRectangle.Height + R.mainRectangle.Y - S.mainRectangle.Y));
- return Rect;
- }
- }
- else if (SideCollisionDetected() == 3)
- {
- if (RTL.Y > STL.Y)
- {
- RectangleCustom Rect = new RectangleCustom(S.mainRectangle.X, S.mainRectangle.Y, R.mainRectangle.X + R.mainRectangle.Width - S.mainRectangle.X, S.mainRectangle.Height);
- return Rect;
- }
- else
- {
- RectangleCustom Rect = new RectangleCustom(S.mainRectangle.X, S.mainRectangle.Y, R.mainRectangle.X + R.mainRectangle.Width - S.mainRectangle.X, S.mainRectangle.Height);
- return Rect;
- }
- }
- else if (SideCollisionDetected() == 4)
- {
- if (RTL.Y > STL.Y)
- {
- RectangleCustom Rect = new RectangleCustom(S.mainRectangle.X, R.mainRectangle.Y, S.mainRectangle.Width, S.mainRectangle.Y + S.mainRectangle.Height - R.mainRectangle.Y);
- return Rect;
- }
- else
- {
- RectangleCustom Rect = new RectangleCustom(S.mainRectangle.X, S.mainRectangle.Y, S.mainRectangle.Width, R.mainRectangle.Y + R.mainRectangle.Height - S.mainRectangle.Y);
- return Rect;
- }
- }
- else if (CornerCollisionDetected() == 1)
- {
- 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));
- return Rect;
- //return null;
- }
- else if (CornerCollisionDetected() == 2)
- {
- 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));
- return Rect;
- //return null;
- }
- else if (CornerCollisionDetected() == 3)
- {
- 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));
- return Rect;
- // return null;
- }
- else if (CornerCollisionDetected() == 4)
- {
- 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);
- return Rect;
- // return null;
- }
- return null;
- }
- public int InsideCollisionDetected()
- {
- Vector2 RTL = R.getTopLeft();
- Vector2 SBR = S.getBottomRight();
- Vector2 RTR = R.getTopRight();
- Vector2 SBL = S.getBottomLeft();
- Vector2 RBL = R.getBottomLeft();
- Vector2 STR = S.getTopRight();
- Vector2 RBR = R.getBottomRight();
- Vector2 STL = S.getTopLeft();
- if ((RTL.X <= STL.X && RTL.Y <= STL.Y) && (RTR.X >= STR.X && RTR.Y <= STL.Y) &&
- (RBL.X <= SBL.X && RBL.Y >= SBL.Y) && (RBR.X >= SBR.X && RBR.Y >= SBR.Y))
- {
- return 1; //1 if r inside s
- }
- else if ((RTL.X >= STL.X && RTL.Y >= STL.Y) && (RTR.X <= STR.X && RTR.Y >= STL.Y) &&
- (RBL.X >= SBL.X && RBL.Y <= SBL.Y) && (RBR.X <= SBR.X && RBR.Y <= SBR.Y))
- {
- return 2; //2 if s inside of r
- }
- else return 0;
- }
- public int SideCollisionDetected()
- {
- Vector2 RTL = R.getTopLeft();
- Vector2 SBR = S.getBottomRight();
- Vector2 RTR = R.getTopRight();
- Vector2 SBL = S.getBottomLeft();
- Vector2 RBL = R.getBottomLeft();
- Vector2 STR = S.getTopRight();
- Vector2 RBR = R.getBottomRight();
- Vector2 STL = S.getTopLeft();
- if ((RTR.X >= STL.X && RTR.Y >= STL.Y) && (RTL.X <= STL.X && RBL.Y <= SBL.Y) ||
- (STR.X >= RTL.X && STR.Y >= RTL.Y) && (STL.X <= RTL.X && SBL.Y <= RBL.Y))
- {
- return 1;
- }
- else if (((RBL.X >= STL.X && RBL.Y >= STR.Y) && (RBL.Y <= SBR.Y && RBR.X <= STR.X)) ||
- ((SBL.X >= RTL.X && SBL.Y >= RTR.Y) && (SBL.Y <= RBR.Y && SBR.X <= RTR.X)))
- {
- return 2;
- }
- else if (((RTL.X <= STL.X && RTL.Y >= STL.Y) && (RTR.X <= STR.X && RBL.Y <= SBR.Y)) ||
- ((STL.X <= RTR.X && STL.Y >= RTL.Y) && (STR.X >= RTR.X && SBL.Y <= RBR.Y)))
- {
- return 3;
- }
- else if (((RTL.X >= STL.X && RTL.Y <= SBL.Y) && (RTR.X <= STR.X && RBR.Y >= SBR.Y)) ||
- ((STL.X >= RTL.X && STL.Y <= RBL.Y) && (STR.X <= RTR.X && SBR.Y >= RBR.Y)))
- {
- return 4;
- }
- else return 0;
- }
- public int CornerCollisionDetected()
- {
- Vector2 RTL = R.getTopLeft();
- Vector2 SBR = S.getBottomRight();
- Vector2 RTR = R.getTopRight();
- Vector2 SBL = S.getBottomLeft();
- Vector2 RBL = R.getBottomLeft();
- Vector2 STR = S.getTopRight();
- Vector2 RBR = R.getBottomRight();
- Vector2 STL = S.getTopLeft();
- //if (((RTL.X <= SBR.X) && (RTL.Y <= SBR.Y)) && (RBR.Y > SBR.Y && RTL.X > STL.X))
- if (SBR.X >= RTL.X && SBR.Y >= RTL.Y && STL.X < RTL.X && STL.Y < RTL.Y)
- {
- return 1;
- }
- else if (((RTR.X >= SBL.X) && (RTR.Y <= SBL.Y)) && (RTR.X < STR.X && RTR.Y > STR.Y))
- {
- return 2;
- }
- else if (((RBR.X >= STL.X) && (RBR.Y >= STL.Y)) && (RBR.X < STR.X && RBR.Y < SBR.Y))
- {
- return 3;
- }
- else if (((RBL.X <= STR.X) && (RBL.Y >= STR.Y)) && (RBL.X > SBL.X && RBL.Y > STL.Y))
- {
- return 4;
- }
- else return 0;
- }
- public static List<Vector2> CheckCollsion(List<Vector2> r1, List<Vector2> r2)
- {
- List<Vector2> intersections = new List<Vector2>();
- for (int x = 0; x < r1.Count - 1; ++x)
- {
- Vector2 r1p1 = r1[x];
- Vector2 r1p2 = r1[x + 1];
- ParametricLine l1 = new ParametricLine(r1p1, r1p1 - r1p2);
- for (int y = 0; y < r2.Count - 1; ++y)
- {
- Vector2 r2p1 = r1[y];
- Vector2 r2p2 = r2[y + 1];
- ParametricLine l2 = new ParametricLine(r2p1, r2p1 - r2p2);
- Vector2? Intersect = ParametricLine.findIntersect(l1, l2);
- if (Intersect != null && InBetween(r1p1, r1p2, Intersect.Value) && InBetween(r2p1, r2p2, Intersect.Value))
- {
- intersections.Add(Intersect.Value);
- }
- }
- }
- return intersections;
- }
- public static bool InBetween(Vector2 first, Vector2 second, Vector2 center)
- {
- if ((first.X < center.X && second.X > center.X) || (first.X > center.X && second.X < center.X))
- {
- if ((first.Y < center.Y && second.Y > center.Y) || (first.Y > center.Y && second.Y < center.Y))
- {
- return true;
- }
- }
- return false;
- }
- }
- }
Add Comment
Please, Sign In to add comment