//LoadContent will be called once per game and is the place to load //all of your content. protected override void LoadContent() { //Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); RectangleTexture = Content.Load <Texture2D>("Square"); RectangleA = new RotatedRectangle(new Rectangle(100, 200, 200, 100), 0.0f); RectangleB = new RotatedRectangle(new Rectangle(300, 200, 130, 390), 0.0f); carTexture = Content.Load <Texture2D>("car"); circleTexture = Content.Load <Texture2D>("circle"); //car.Add(new Player(carTexture, new Vector2(200, 200), 45, 0, circleTexture, new Vector2(GraphicsDevice.DisplayMode.Width * 0.2f, GraphicsDevice.DisplayMode.Height * 0.15f))); }
//Check to see if two Rotated Rectangles have collided public bool Intersects(RotatedRectangle theRectangle) { //calculate the axis we will us to check collisions on //make 4, one for each side List<Vector2> rectangleAxis = new List<Vector2>(); rectangleAxis.Add(UpperRightCorner() - UpperLeftCorner()); rectangleAxis.Add(UpperRightCorner() - LowerRightCorner()); rectangleAxis.Add(theRectangle.UpperLeftCorner() - theRectangle.LowerLeftCorner()); rectangleAxis.Add(theRectangle.UpperLeftCorner() - theRectangle.UpperRightCorner()); //loop through each axis, if one of them does not collide then there is no collision foreach (Vector2 axis in rectangleAxis) { if (!IsAxisCollision(theRectangle, axis)) { return false; } } return true; }
//Check to see if two Rotated Rectangles have collided public bool Intersects(RotatedRectangle theRectangle) { //calculate the axis we will us to check collisions on //make 4, one for each side List <Vector2> rectangleAxis = new List <Vector2>(); rectangleAxis.Add(UpperRightCorner() - UpperLeftCorner()); rectangleAxis.Add(UpperRightCorner() - LowerRightCorner()); rectangleAxis.Add(theRectangle.UpperLeftCorner() - theRectangle.LowerLeftCorner()); rectangleAxis.Add(theRectangle.UpperLeftCorner() - theRectangle.UpperRightCorner()); //loop through each axis, if one of them does not collide then there is no collision foreach (Vector2 axis in rectangleAxis) { if (!IsAxisCollision(theRectangle, axis)) { return(false); } } return(true); }
//Determines if a collision has occurred on an Axis of one of the planes parallel to the Rectangle private bool IsAxisCollision(RotatedRectangle theRectangle, Vector2 axis) { //Project the corners of the Rectangle we are checking on to the Axis and //get a scalar value of that project we can then use for comparison List <int> rectangleAScalars = new List <int>(); rectangleAScalars.Add(GenerateScalar(theRectangle.UpperLeftCorner(), axis)); rectangleAScalars.Add(GenerateScalar(theRectangle.UpperRightCorner(), axis)); rectangleAScalars.Add(GenerateScalar(theRectangle.LowerLeftCorner(), axis)); rectangleAScalars.Add(GenerateScalar(theRectangle.LowerRightCorner(), axis)); //Project the corners of the current Rectangle on to the Axis and //get a scalar value of that projection we can then use for comparison List <int> rectangleBScalars = new List <int>(); rectangleBScalars.Add(GenerateScalar(UpperLeftCorner(), axis)); rectangleBScalars.Add(GenerateScalar(UpperRightCorner(), axis)); rectangleBScalars.Add(GenerateScalar(LowerLeftCorner(), axis)); rectangleBScalars.Add(GenerateScalar(LowerRightCorner(), axis)); //Get the Maximum and Minium Scalar values for each of the Rectangles int rectangleAMinimum = rectangleAScalars.Min(); int rectangleAMaximum = rectangleAScalars.Max(); int rectangleBMinimum = rectangleBScalars.Min(); int rectangleBMaximum = rectangleBScalars.Max(); //If we have overlaps between the Rectangles, then there is a collision between the rectangles on this Axis if (rectangleBMinimum <= rectangleAMaximum && rectangleBMaximum >= rectangleAMaximum) { return(true); } else if (rectangleAMinimum <= rectangleBMaximum && rectangleAMaximum >= rectangleBMaximum) { return(true); } return(false); }
//LoadContent will be called once per game and is the place to load //all of your content. protected override void LoadContent() { //Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); RectangleTexture = Content.Load<Texture2D>("Square"); RectangleA = new RotatedRectangle(new Rectangle(100, 200, 200, 100), 0.0f); RectangleB = new RotatedRectangle(new Rectangle(300, 200, 130, 390), 0.0f); carTexture = Content.Load<Texture2D>("car"); circleTexture = Content.Load<Texture2D>("circle"); //car.Add(new Player(carTexture, new Vector2(200, 200), 45, 0, circleTexture, new Vector2(GraphicsDevice.DisplayMode.Width * 0.2f, GraphicsDevice.DisplayMode.Height * 0.15f))); }
//Determines if a collision has occurred on an Axis of one of the planes parallel to the Rectangle private bool IsAxisCollision(RotatedRectangle theRectangle, Vector2 axis) { //Project the corners of the Rectangle we are checking on to the Axis and //get a scalar value of that project we can then use for comparison List<int> rectangleAScalars = new List<int>(); rectangleAScalars.Add(GenerateScalar(theRectangle.UpperLeftCorner(), axis)); rectangleAScalars.Add(GenerateScalar(theRectangle.UpperRightCorner(), axis)); rectangleAScalars.Add(GenerateScalar(theRectangle.LowerLeftCorner(), axis)); rectangleAScalars.Add(GenerateScalar(theRectangle.LowerRightCorner(), axis)); //Project the corners of the current Rectangle on to the Axis and //get a scalar value of that projection we can then use for comparison List<int> rectangleBScalars = new List<int>(); rectangleBScalars.Add(GenerateScalar(UpperLeftCorner(), axis)); rectangleBScalars.Add(GenerateScalar(UpperRightCorner(), axis)); rectangleBScalars.Add(GenerateScalar(LowerLeftCorner(), axis)); rectangleBScalars.Add(GenerateScalar(LowerRightCorner(), axis)); //Get the Maximum and Minium Scalar values for each of the Rectangles int rectangleAMinimum = rectangleAScalars.Min(); int rectangleAMaximum = rectangleAScalars.Max(); int rectangleBMinimum = rectangleBScalars.Min(); int rectangleBMaximum = rectangleBScalars.Max(); //If we have overlaps between the Rectangles, then there is a collision between the rectangles on this Axis if (rectangleBMinimum <= rectangleAMaximum && rectangleBMaximum >= rectangleAMaximum) { return true; } else if (rectangleAMinimum <= rectangleBMaximum && rectangleAMaximum >= rectangleBMaximum) { return true; } return false; }