コード例 #1
0
        public bool Collides(SpaceShipControl other)
        {
            double thisWidth        = spaceShipImage.ActualWidth;
            double thisHeight       = spaceShipImage.ActualHeight;
            double thisWidthHalved  = thisWidth / 2;
            double thisHeightHalved = thisHeight / 2;

            Rect thisBoundingBox =
                new Rect(
                    X - thisWidthHalved,
                    Y - thisHeightHalved,
                    thisWidth,
                    thisHeight);

            double otherWidth        = other.spaceShipImage.ActualWidth;
            double otherHeight       = other.spaceShipImage.ActualHeight;
            double otherWidthHalved  = otherWidth / 2;
            double otherHeightHalved = otherHeight / 2;

            Rect otherBoundingBox =
                new Rect(
                    other.X - otherWidthHalved,
                    other.Y - otherHeightHalved,
                    otherWidth,
                    otherHeight);

            thisBoundingBox.Intersect(otherBoundingBox);
            return(thisBoundingBox.Width > 0 && thisBoundingBox.Height > 0);
        }
コード例 #2
0
        public bool Collides(SpaceShipControl spaceShip)
        {
            if (_collisionDetectionCounter < 5)
            {
                _collisionDetectionCounter++;
                return(false);
            }

            double thisWidthHalved  = projectileImage.ActualWidth / 2;
            double thisHeightHalved = projectileImage.ActualHeight / 2;

            Rect thisBoundingBox =
                new Rect(X - thisWidthHalved,
                         Y - thisHeightHalved,
                         projectileImage.ActualWidth,
                         projectileImage.ActualHeight);

            Size   spaceShipSize         = spaceShip.Size();
            double spaceShipWidthHalved  = spaceShipSize.Width / 2;
            double spaceShipHeightHalved = spaceShipSize.Height / 2;

            Rect spaceShipBoundingBox =
                new Rect(spaceShip.X - spaceShipWidthHalved,
                         spaceShip.Y - spaceShipHeightHalved,
                         spaceShipSize.Width,
                         spaceShipSize.Height);

            thisBoundingBox.Intersect(spaceShipBoundingBox);
            return(thisBoundingBox.Width > 0 && thisBoundingBox.Height > 0);
        }