示例#1
0
        public static Boolean Detect(BasicModel shot, Collidable item, Boolean isPigeon)
        {
            Vector3 boundingBox = item.GetBoundingBox();
            Vector3 itemPos = item.GetPosition();

            //get the shot's position, rotation and scale
            Matrix shotMat = shot.GetWorld();
            Vector3 shotPos;
            Quaternion shotRotation;
            Vector3 shotScale;
            shotMat.Decompose(out shotScale, out shotRotation, out shotPos);

            //offset to compensate for the camera location not being at the origin.
            //camera loc is 0,0,100
            itemPos.Z -= 100;
            shotPos.Z -= 100;

            //calculate the shot's distance from the origin
            double shotDistance = Math.Sqrt(Math.Pow(shotPos.X, 2) + Math.Pow(shotPos.Y, 2) + Math.Pow(shotPos.Z, 2));

            //normalize the shot's position vector so the distance is equal to 1.0
            Vector3 shotNormalizedPos = new Vector3(shotPos.X / (float)shotDistance, shotPos.Y / (float)shotDistance, shotPos.Z / (float)shotDistance);

            //don't try to check collision detection for shots that have already passed the target in depth
            if ((item.GetDistance()+boundingBox.Z - shotDistance < 0) && isPigeon)
                return false;

            //laser gun effect
            //multiply the normalized shot vector by the target's distance. The effect of this operation is that the shot's distance becomes equal to that of the target's,
            //allowing us to simply check whether the shot is contained inside the target's bounding box to detect a hit.
            shotNormalizedPos *= (float)Math.Sqrt(Math.Pow(itemPos.X, 2) + Math.Pow(itemPos.Y, 2) + Math.Pow(itemPos.Z, 2));

            float xDiff = itemPos.X - shotNormalizedPos.X;
            float yDiff = itemPos.Y - shotNormalizedPos.Y;
            float zDiff = itemPos.Z - shotNormalizedPos.Z;

            //hack to get the shot's vector to face the right way, since shots quickly gain a negative Z value while pidgeons always stay positive
            if (isPigeon) zDiff = (zDiff < 0) ? -zDiff : zDiff;

            //check to see if the shot is inside the bounding box
             if (boundingBox.X / 2 > xDiff && (0 - (boundingBox.X / 2)) < xDiff &&
                boundingBox.Y / 2 > yDiff && (0 - (boundingBox.Y / 2)) < yDiff)
            {
                if (isPigeon && boundingBox.Z / 2 > zDiff && (0 - (boundingBox.Z / 2)) < zDiff)
                    return true;

                else if (!isPigeon)
                    return true;
            }
            return false;
        }