Пример #1
0
        public Block?RaycastBlock(Ray ray, out Vector3 intersection, out Vector3i blockPosition)
        {
            float distance = 0;

            while (distance < 1.75f * WorldGen.GRID_SIZE)
            {
                Vector3 position = ray.Origin + ray.Direction * distance;
                blockPosition = new Vector3i((int)Math.Floor(position.X), (int)Math.Floor(position.Y), (int)Math.Floor(position.Z));
                AABB blockAABB = new AABB(blockPosition, blockPosition + new Vector3i(1, 1, 1));
                if (!CollisionDetection.RayAABBIntersection(blockAABB, ray, out Vector3 nearIntersection, out Vector3 farIntersection))
                {
                    break;
                }

                Block?block = GetBlockAt(blockPosition - 16 * Position);
                if (block.HasValue && block.Value.Type.Solid)
                {
                    intersection = nearIntersection;
                    return(block);
                }

                float newDistance = (farIntersection - ray.Origin).Length + 10e-3f;
                if (newDistance <= distance)
                {
                    break;
                }

                distance = newDistance;
            }

            intersection  = Vector3.Zero;
            blockPosition = new Vector3i(0, 0, 0);

            return(null);
        }
Пример #2
0
        public Grid RaycastGrid(Ray ray, float minDistance, float maxDistance, out Vector3 intersection)
        {
            while (minDistance < maxDistance)
            {
                Vector3  position     = ray.Origin + ray.Direction * minDistance;
                Vector3i gridPosition = new Vector3i((int)Math.Floor(position.X / 16), (int)Math.Floor(position.Y / 16), (int)Math.Floor(position.Z / 16));
                Grid     grid         = GetGridAt(gridPosition);
                if (grid == null)
                {
                    break;
                }

                AABB gridAABB = new AABB(16 * grid.Position, 16 * grid.Position + grid.Size);
                if (!CollisionDetection.RayAABBIntersection(gridAABB, ray, out _, out Vector3 farIntersection))
                {
                    break;
                }

                if (!grid.IsEmpty)
                {
                    intersection = farIntersection;
                    return(grid);
                }

                minDistance = (farIntersection - ray.Origin).Length + 10e-3f;
            }

            intersection = Vector3.Zero;
            return(null);
        }