/// <summary> /// Checks for collisions, stores them in the stack /// and returns a boolean /// </summary> /// <param name="subject"></param> /// <param name="movingObjectArray"></param> /// <param name="stack"></param> /// <returns></returns> public static bool CollisionHappened(MovingObject subject, ArrayList movingObjectArray, Stack stack) { Vector2 subjectOrigin = subject.GetOrigin(); Vector2 subjectPosition = subject.GetPosition(); Texture2D subjectTexture = subject.GetTexture(); float _scale = GameLogic.GetInstance().GetScale(); _subjectTextureData = new Color[subjectTexture.Width * subjectTexture.Height]; subjectTexture.GetData(_subjectTextureData); // Update the subject's transform Matrix subjectTransform = Matrix.CreateTranslation(new Vector3(-subjectOrigin, 0.0f)) * Matrix.CreateScale(_scale) * Matrix.CreateTranslation(new Vector3(subjectPosition, 0.0f)); // Get the bounding rectangle of the subject Rectangle subjectRectangle = CalculateBoundingRectangle( new Rectangle(0, 0, subjectTexture.Width, subjectTexture.Height), subjectTransform); bool collision = false; for (int i = movingObjectArray.Count - 1; i >= 0; i--) { MovingObject movingObject = (MovingObject)movingObjectArray[i]; Vector2 movingObjectOrigin = movingObject.GetOrigin(); Vector2 movingObjectPosition = movingObject.GetPosition(); Texture2D movingObjectTexture = movingObject.GetTexture(); _movingObjectTextureData = new Color[movingObjectTexture.Width * movingObjectTexture.Height]; movingObjectTexture.GetData(_movingObjectTextureData); // Build the movingObject's transform Matrix movingObjectTransform = Matrix.CreateTranslation(new Vector3(-movingObjectOrigin, 0.0f)) * Matrix.CreateScale(_scale) * // Matrix.CreateRotationZ(movingObjectRotation) * Matrix.CreateTranslation(new Vector3(movingObjectPosition, 0.0f)); // Calculate the bounding rectangle of this movingObject in world space Rectangle movingObjectRectangle = CalculateBoundingRectangle( new Rectangle(0, 0, movingObjectTexture.Width, movingObjectTexture.Height), movingObjectTransform); // The per-pixel check is expensive, so check the bounding rectangles // first to prevent testing pixels when collisions are impossible. if (subjectRectangle.Intersects(movingObjectRectangle) || subjectRectangle.Contains(movingObjectRectangle)) { // Check collision with person if (IntersectPixels(subjectTransform, subjectTexture.Width, subjectTexture.Height, _subjectTextureData, movingObjectTransform, movingObjectTexture.Width, movingObjectTexture.Height, _movingObjectTextureData)) { collision = true; stack.Push(movingObject); } } } return collision; }