Beispiel #1
0
        public static double GetRayIntersectDistance(double maxValue, Vector3d direction, Vector3d startPosition)
        {
            Ray ray = new Ray(startPosition, direction);
            double distance = 0.0;
            BlockIndex index;
            double? intersect;

            while (distance <= maxValue)
            {
                index = new BlockIndex(direction * distance + startPosition);

                intersect = index.GetBoundingBox().Intersects(ray);
                if (intersect.HasValue && ChunkManager.GetBlock(index).Solidity)
                {
                    return intersect.Value;
                }

                intersect = (index + BlockIndex.UnitX).GetBoundingBox().Intersects(ray);
                if (intersect.HasValue && ChunkManager.GetBlock(index + BlockIndex.UnitX).Solidity)
                {
                    return intersect.Value;
                }
                intersect = (index - BlockIndex.UnitX).GetBoundingBox().Intersects(ray);
                if (intersect.HasValue && ChunkManager.GetBlock(index - BlockIndex.UnitX).Solidity)
                {
                    return intersect.Value;
                }
                intersect = (index + BlockIndex.UnitY).GetBoundingBox().Intersects(ray);
                if (intersect.HasValue && ChunkManager.GetBlock(index + BlockIndex.UnitY).Solidity)
                {
                    return intersect.Value;
                }
                intersect = (index - BlockIndex.UnitY).GetBoundingBox().Intersects(ray);
                if (intersect.HasValue && ChunkManager.GetBlock(index - BlockIndex.UnitY).Solidity)
                {
                    return intersect.Value;
                }
                intersect = (index + BlockIndex.UnitZ).GetBoundingBox().Intersects(ray);
                if (intersect.HasValue && ChunkManager.GetBlock(index + BlockIndex.UnitZ).Solidity)
                {
                    return intersect.Value;
                }
                intersect = (index - BlockIndex.UnitZ).GetBoundingBox().Intersects(ray);
                if (intersect.HasValue && ChunkManager.GetBlock(index - BlockIndex.UnitZ).Solidity)
                {
                    return intersect.Value;
                }

                distance += 0.5F;
            }

            return maxValue;
        }
        private static BlockIndex GetCursorIntersect(double maxReach, Vector3d direction, Vector3d startPosition, out Vector3d intersectionPoint)
        {
            Ray ray = new Ray(startPosition, direction);
            double distance = 0.0;
            BlockIndex index;
            double? intersect;

            while (distance <= Constants.Player.BlockEditing.Reach)
            {
                index = new BlockIndex(direction * distance + startPosition);

                intersect = index.GetBoundingBox().Intersects(ray);
                if (intersect.HasValue && ChunkManager.GetBlock(index).Solidity)
                {
                    intersectionPoint = intersect.Value * direction + startPosition;
                    return index;
                }

                intersect = (index + BlockIndex.UnitX).GetBoundingBox().Intersects(ray);
                if (intersect.HasValue && ChunkManager.GetBlock(index + BlockIndex.UnitX).Solidity)
                {
                    intersectionPoint = intersect.Value * direction + startPosition;
                    return index + BlockIndex.UnitX;
                }
                intersect = (index - BlockIndex.UnitX).GetBoundingBox().Intersects(ray);
                if (intersect.HasValue && ChunkManager.GetBlock(index - BlockIndex.UnitX).Solidity)
                {
                    intersectionPoint = intersect.Value * direction + startPosition;
                    return index - BlockIndex.UnitX;
                }
                intersect = (index + BlockIndex.UnitY).GetBoundingBox().Intersects(ray);
                if (intersect.HasValue && ChunkManager.GetBlock(index + BlockIndex.UnitY).Solidity)
                {
                    intersectionPoint = intersect.Value * direction + startPosition;
                    return index + BlockIndex.UnitY;
                }
                intersect = (index - BlockIndex.UnitY).GetBoundingBox().Intersects(ray);
                if (intersect.HasValue && ChunkManager.GetBlock(index - BlockIndex.UnitY).Solidity)
                {
                    intersectionPoint = intersect.Value * direction + startPosition;
                    return index - BlockIndex.UnitY;
                }
                intersect = (index + BlockIndex.UnitZ).GetBoundingBox().Intersects(ray);
                if (intersect.HasValue && ChunkManager.GetBlock(index + BlockIndex.UnitZ).Solidity)
                {
                    intersectionPoint = intersect.Value * direction + startPosition;
                    return index + BlockIndex.UnitZ;
                }
                intersect = (index - BlockIndex.UnitZ).GetBoundingBox().Intersects(ray);
                if (intersect.HasValue && ChunkManager.GetBlock(index - BlockIndex.UnitZ).Solidity)
                {
                    intersectionPoint = intersect.Value * direction + startPosition;
                    return index - BlockIndex.UnitZ;
                }

                distance += 0.5F;
            }

            intersectionPoint = Vector3d.Zero;
            return null;
        }
 public double? Intersects(Ray ray)
 {
     return ray.Intersects(this);
 }