Пример #1
0
        public Vector3?Raycast(Ray ray)
        {
            var corners = GetCorners();

            for (int i = 0; i < corners.Count; i++)
            {
                var v1 = corners[i];
                var v2 = corners[(i + 1) % corners.Count];

                if (Intersections.LineTriangleIntersection(ray, v1, v2, CenterPoint, out var intersectionPoint) == 1)
                {
                    return(intersectionPoint);
                }
            }

            return(null);
        }
Пример #2
0
        public Vector3?GetRayMapIntersection(Ray ray)
        {
            var from   = new Vector2(ray.origin.x, ray.origin.z);
            var to     = new Vector2(ray.GetPoint(1000).x, ray.GetPoint(1000).z);
            var blocks = Rasterization.DDA(from, to, true);

            foreach (var blockPos in blocks)
            {
                if (_settings.LandBounds.Contains(blockPos))
                {
                    var   chunkPosition = Chunk.GetPosition(blockPos);
                    Chunk chunk;
                    if (Map.TryGetValue(chunkPosition, out chunk))
                    {
                        var localPos = Chunk.GetLocalPosition(blockPos);
                        var tr1      = new Vector3(blockPos.X, chunk.HeightMap[localPos.X, localPos.Z], blockPos.Z);
                        var tr2      = new Vector3(blockPos.X + 1, chunk.HeightMap[localPos.X + 1, localPos.Z], blockPos.Z);
                        var tr3      = new Vector3(blockPos.X, chunk.HeightMap[localPos.X, localPos.Z + 1], blockPos.Z + 1);
                        var tr4      = new Vector3(blockPos.X + 1, chunk.HeightMap[localPos.X + 1, localPos.Z + 1], blockPos.Z + 1);

                        Vector3 i;
                        var     ir = Intersections.LineTriangleIntersection(ray, tr1, tr2, tr3, out i);
                        if (ir == 1)
                        {
                            return(i);
                        }
                        else
                        {
                            ir = Intersections.LineTriangleIntersection(ray, tr2, tr3, tr4, out i);
                            if (ir == 1)
                            {
                                return(i);
                            }
                        }
                    }
                }
            }

            return(null);
        }