Esempio n. 1
0
        // Checks collision of the given object against the edges of the level
        public void CheckShip(Object toCheck, Levels.SpaceLevel level)
        {
            bool currentHit = false;
            Vector2 translationVector, centerDiff;
            translationVector = Vector2.Zero;
            centerDiff = new Vector2(level.GetLevelWidth() - toCheck.GetCenter().X, level.GetLevelHeight() - toCheck.GetCenter().Y);
            if (CheckIntersection(toCheck.collisionPolygons, level.collisionPolygons, centerDiff, ref translationVector, true))
            {
                toCheck.SetPosition(new Vector2(toCheck.GetPosition().X + translationVector.X, toCheck.GetPosition().Y + translationVector.Y));
                currentHit = true; ;
            }

            if (!currentHit)
            {
                //If they are not currently intersecting, check if they will intersect
                Vector2 oldPosition1 = toCheck.GetPosition();
                toCheck.SetPosition(new Vector2(oldPosition1.X + toCheck.Velocity.X, oldPosition1.Y + toCheck.Velocity.Y));
                bool futureRet = CheckIntersection(toCheck.collisionPolygons, level.collisionPolygons, centerDiff, ref translationVector, true);

                if (futureRet)
                {
                    // Set the position back now that the future collision has been checked
                    toCheck.SetPosition(oldPosition1);
                    currentHit = true;
                }
            }
            if (currentHit)
            {
                float speed = toCheck.Velocity.Length();
                toCheck.Velocity = Vector2.Normalize(translationVector);
                toCheck.Velocity *= speed;
            }
        }