/// <summary> /// Check to see if two Rotated Rectangls have collided /// </summary> /// <param name="theRectangle"></param> /// <returns></returns> public bool Intersects(RotatedRectangle theRectangle) { //Calculate the Axis we will use to determine if a collision has occurred //Since the objects are rectangles, we only have to generate 4 Axis (2 for //each rectangle) since we know the other 2 on a rectangle are parallel. List <Vector2> aRectangleAxis = new List <Vector2>(); aRectangleAxis.Add(UpperRightCorner() - UpperLeftCorner()); aRectangleAxis.Add(UpperRightCorner() - LowerRightCorner()); aRectangleAxis.Add(theRectangle.UpperLeftCorner() - theRectangle.LowerLeftCorner()); aRectangleAxis.Add(theRectangle.UpperLeftCorner() - theRectangle.UpperRightCorner()); //Cycle through all of the Axis we need to check. If a collision does not occur //on ALL of the Axis, then a collision is NOT occurring. We can then exit out //immediately and notify the calling function that no collision was detected. If //a collision DOES occur on ALL of the Axis, then there is a collision occurring //between the rotated rectangles. We know this to be true by the Seperating Axis Theorem foreach (Vector2 aAxis in aRectangleAxis) { if (!IsAxisCollision(theRectangle, aAxis)) { return(false); } } return(true); }
/// <summary> /// Determines if a collision has occurred on an Axis of one of the /// planes parallel to the Rectangle /// </summary> /// <param name="theRectangle"></param> /// <param name="aAxis"></param> /// <returns></returns> private bool IsAxisCollision(RotatedRectangle theRectangle, Vector2 aAxis) { //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> aRectangleAScalars = new List <int>(); aRectangleAScalars.Add(GenerateScalar(theRectangle.UpperLeftCorner(), aAxis)); aRectangleAScalars.Add(GenerateScalar(theRectangle.UpperRightCorner(), aAxis)); aRectangleAScalars.Add(GenerateScalar(theRectangle.LowerLeftCorner(), aAxis)); aRectangleAScalars.Add(GenerateScalar(theRectangle.LowerRightCorner(), aAxis)); //Project the corners of the current Rectangle on to the Axis and //get a scalar value of that project we can then use for comparison List <int> aRectangleBScalars = new List <int>(); aRectangleBScalars.Add(GenerateScalar(UpperLeftCorner(), aAxis)); aRectangleBScalars.Add(GenerateScalar(UpperRightCorner(), aAxis)); aRectangleBScalars.Add(GenerateScalar(LowerLeftCorner(), aAxis)); aRectangleBScalars.Add(GenerateScalar(LowerRightCorner(), aAxis)); //Get the Maximum and Minium Scalar values for each of the Rectangles int aRectangleAMinimum = aRectangleAScalars.Min(); int aRectangleAMaximum = aRectangleAScalars.Max(); int aRectangleBMinimum = aRectangleBScalars.Min(); int aRectangleBMaximum = aRectangleBScalars.Max(); //If we have overlaps between the Rectangles (i.e. Min of B is less than Max of A) //then we are detecting a collision between the rectangles on this Axis if (aRectangleBMinimum <= aRectangleAMaximum && aRectangleBMaximum >= aRectangleAMaximum) { return(true); } else if (aRectangleAMinimum <= aRectangleBMaximum && aRectangleAMaximum >= aRectangleBMaximum) { return(true); } return(false); }
public Player(Texture2D playerTexture, Texture2D mouseTexture, Vector2 playerPos, ContentManager content) { _mouse = new Mouse(mouseTexture); _playerTexture = playerTexture; _playerPos = playerPos; _lives = 3; this.score = 0; _health = 100; _playerRot = 0; bulletList = new List <Bullet>(); _playerRec = new Rectangle(); //Load Textures shot = content.Load <SoundEffect>("Sounds\\shot"); _bulletTexture = content.Load <Texture2D>("bullet2"); _healthIn = content.Load <Texture2D>("healthbarInside"); _healthOut = content.Load <Texture2D>("healthbarOutside"); Texture2D idle = content.Load <Texture2D>("manIdle"); die = content.Load <Texture2D>("manDie"); _playerRec = new Rectangle((int)_playerPos.X, (int)_playerPos.Y, playerTexture.Width / 2, playerTexture.Height / 2); rRec = new RotatedRectangle(_playerRec, 0.0f); velocity = Vector2.Zero; dieSprite = new AnimatedSprite(die, AnimatedSprite.AnimType.FLASH, 1, 2, 0.5f, new Vector2(200, 200), 2.5f); moveSprite = new AnimatedSprite(_playerTexture, AnimatedSprite.AnimType.LOOP, 1, 2, 0.5f, new Vector2(200, 200), 2.5f); idleSprite = new AnimatedSprite(idle, AnimatedSprite.AnimType.LOOP, 1, 1, 0.5f, new Vector2(200, 200), 2.5f); pSprite = idleSprite; pmState = PlayerMovementState.IDLE; isAlive = true; level = 1; }
public Player(Texture2D playerTexture, Mouse mouse, Vector2 playerPos, AnimatedSprite animatedSprite, ContentManager content) { _mouse = mouse; _playerTexture = playerTexture; _playerPos = playerPos; _playerRot = 0; bulletList = new List <Bullet>(); pSprite = animatedSprite; _playerRec = new Rectangle(); //Load Textures shot = content.Load <SoundEffect>("Sounds\\shot"); _bulletTexture = content.Load <Texture2D>("bullet2"); _playerRec = new Rectangle((int)_playerPos.X, (int)_playerPos.Y, playerTexture.Width / 2, playerTexture.Height / 2); rRec = new RotatedRectangle(_playerRec, 0.0f); velocity = Vector2.Zero; pState = PlayerState.ALIVE; pmState = PlayerMovementState.IDLE; }
/// <summary> /// Check to see if two Rotated Rectangls have collided /// </summary> /// <param name="theRectangle"></param> /// <returns></returns> public bool Intersects(RotatedRectangle theRectangle) { //Calculate the Axis we will use to determine if a collision has occurred //Since the objects are rectangles, we only have to generate 4 Axis (2 for //each rectangle) since we know the other 2 on a rectangle are parallel. List<Vector2> aRectangleAxis = new List<Vector2>(); aRectangleAxis.Add(UpperRightCorner() - UpperLeftCorner()); aRectangleAxis.Add(UpperRightCorner() - LowerRightCorner()); aRectangleAxis.Add(theRectangle.UpperLeftCorner() - theRectangle.LowerLeftCorner()); aRectangleAxis.Add(theRectangle.UpperLeftCorner() - theRectangle.UpperRightCorner()); //Cycle through all of the Axis we need to check. If a collision does not occur //on ALL of the Axis, then a collision is NOT occurring. We can then exit out //immediately and notify the calling function that no collision was detected. If //a collision DOES occur on ALL of the Axis, then there is a collision occurring //between the rotated rectangles. We know this to be true by the Seperating Axis Theorem foreach (Vector2 aAxis in aRectangleAxis) { if (!IsAxisCollision(theRectangle, aAxis)) { return false; } } return true; }
/// <summary> /// Determines if a collision has occurred on an Axis of one of the /// planes parallel to the Rectangle /// </summary> /// <param name="theRectangle"></param> /// <param name="aAxis"></param> /// <returns></returns> private bool IsAxisCollision(RotatedRectangle theRectangle, Vector2 aAxis) { //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> aRectangleAScalars = new List<int>(); aRectangleAScalars.Add(GenerateScalar(theRectangle.UpperLeftCorner(), aAxis)); aRectangleAScalars.Add(GenerateScalar(theRectangle.UpperRightCorner(), aAxis)); aRectangleAScalars.Add(GenerateScalar(theRectangle.LowerLeftCorner(), aAxis)); aRectangleAScalars.Add(GenerateScalar(theRectangle.LowerRightCorner(), aAxis)); //Project the corners of the current Rectangle on to the Axis and //get a scalar value of that project we can then use for comparison List<int> aRectangleBScalars = new List<int>(); aRectangleBScalars.Add(GenerateScalar(UpperLeftCorner(), aAxis)); aRectangleBScalars.Add(GenerateScalar(UpperRightCorner(), aAxis)); aRectangleBScalars.Add(GenerateScalar(LowerLeftCorner(), aAxis)); aRectangleBScalars.Add(GenerateScalar(LowerRightCorner(), aAxis)); //Get the Maximum and Minium Scalar values for each of the Rectangles int aRectangleAMinimum = aRectangleAScalars.Min(); int aRectangleAMaximum = aRectangleAScalars.Max(); int aRectangleBMinimum = aRectangleBScalars.Min(); int aRectangleBMaximum = aRectangleBScalars.Max(); //If we have overlaps between the Rectangles (i.e. Min of B is less than Max of A) //then we are detecting a collision between the rectangles on this Axis if (aRectangleBMinimum <= aRectangleAMaximum && aRectangleBMaximum >= aRectangleAMaximum) { return true; } else if (aRectangleAMinimum <= aRectangleBMaximum && aRectangleAMaximum >= aRectangleBMaximum) { return true; } return false; }
public Player(Texture2D playerTexture, Texture2D mouseTexture, Vector2 playerPos, ContentManager content) { _mouse = new Mouse(mouseTexture); _playerTexture = playerTexture; _playerPos = playerPos; _lives = 3; this.score = 0; _health = 100; _playerRot = 0; bulletList = new List<Bullet>(); _playerRec = new Rectangle(); //Load Textures shot = content.Load<SoundEffect>("Sounds\\shot"); _bulletTexture = content.Load<Texture2D>("bullet2"); _healthIn = content.Load<Texture2D>("healthbarInside"); _healthOut = content.Load<Texture2D>("healthbarOutside"); Texture2D idle = content.Load<Texture2D>("manIdle"); die = content.Load<Texture2D>("manDie"); _playerRec = new Rectangle((int)_playerPos.X, (int)_playerPos.Y, playerTexture.Width / 2, playerTexture.Height / 2); rRec = new RotatedRectangle(_playerRec, 0.0f); velocity = Vector2.Zero; dieSprite = new AnimatedSprite(die, AnimatedSprite.AnimType.FLASH, 1, 2, 0.5f, new Vector2(200, 200), 2.5f); moveSprite = new AnimatedSprite(_playerTexture, AnimatedSprite.AnimType.LOOP, 1, 2, 0.5f, new Vector2(200, 200), 2.5f); idleSprite = new AnimatedSprite(idle, AnimatedSprite.AnimType.LOOP, 1, 1, 0.5f, new Vector2(200, 200), 2.5f); pSprite = idleSprite; pmState = PlayerMovementState.IDLE; isAlive = true; level = 1; }
public Player(Texture2D playerTexture, Mouse mouse, Vector2 playerPos, AnimatedSprite animatedSprite, ContentManager content) { _mouse = mouse; _playerTexture = playerTexture; _playerPos = playerPos; _playerRot = 0; bulletList = new List<Bullet>(); pSprite = animatedSprite; _playerRec = new Rectangle(); //Load Textures shot = content.Load<SoundEffect>("Sounds\\shot"); _bulletTexture = content.Load<Texture2D>("bullet2"); _playerRec = new Rectangle((int)_playerPos.X, (int)_playerPos.Y, playerTexture.Width / 2, playerTexture.Height / 2); rRec = new RotatedRectangle(_playerRec, 0.0f); velocity = Vector2.Zero; pState = PlayerState.ALIVE; pmState = PlayerMovementState.IDLE; }