Example #1
0
        //private static float HighestAlt(float[] heightmap, int columns, int rows, int xCell, int yCell)
        //{
        //    float height0 = heightmap[yCell * columns + xCell];
        //    float height1 = heightmap[yCell * columns + xCell + 1];
        //    float height2 = heightmap[(yCell + 1) * columns + xCell];
        //    float height3 = heightmap[(yCell + 1) * columns + xCell + 1];

        //    return Math.Max(height0, Math.Max(height1, Math.Max(height2, height3)));
        //}

        private static bool Intersects(Vector3 start, Vector3 direction, float[] heightmap, int columns, int rows, int xCell, int yCell, out float dist)
        {
            xCell = Utils.Clamp(xCell, 0, columns - 2);
            yCell = Utils.Clamp(yCell, 0, rows - 2);

            // 0--1-
            // | /|
            // |/ |
            // 2--3-
            // |  |
            Vector3 v0 = new Vector3(xCell, yCell, heightmap[yCell * columns + xCell]);
            Vector3 v1 = new Vector3(xCell + 1, xCell, heightmap[yCell * columns + (xCell + 1)]);
            Vector3 v2 = new Vector3(xCell, yCell + 1, heightmap[(yCell + 1) * columns + xCell]);
            Vector3 v3 = new Vector3(xCell + 1, yCell + 1, heightmap[(yCell + 1) * columns + (xCell + 1)]);

            return
                (RayTriangle.CollisionTestCull(start, direction, v0, v1, v2, out dist) ||
                 RayTriangle.CollisionTestCull(start, direction, v3, v2, v1, out dist));
        }
Example #2
0
        public static bool CollisionTestSlow(Ray ray, float[] heightmap, int columns, int rows, out float dist)
        {
            // TODO: Optimize this function with a
            Vector3 start     = new Vector3(ray.X, ray.Y, ray.Z);
            Vector3 direction = new Vector3(ray.I, ray.J, ray.K);

            dist = Single.MaxValue;

            // Iterate through all of the triangles in the heightmap, doing a ray-triangle intersection
            for (int y = 0; y < rows - 1; y++)
            {
                for (int x = 0; x < columns - 1; x++)
                {
                    // 0--1-
                    // | /|
                    // |/ |
                    // 2--3-
                    // |  |
                    Vector3 v0 = new Vector3(x, y, heightmap[y * columns + x]);
                    Vector3 v1 = new Vector3(x + 1, x, heightmap[y * columns + (x + 1)]);
                    Vector3 v2 = new Vector3(x, y + 1, heightmap[(y + 1) * columns + x]);
                    Vector3 v3 = new Vector3(x + 1, y + 1, heightmap[(y + 1) * columns + (x + 1)]);

                    float thisDist;
                    if (RayTriangle.CollisionTestCull(start, direction, v0, v1, v2, out thisDist))
                    {
                        if (thisDist < dist)
                        {
                            dist = thisDist;
                        }
                    }
                    if (RayTriangle.CollisionTestCull(start, direction, v3, v2, v1, out thisDist))
                    {
                        if (thisDist < dist)
                        {
                            dist = thisDist;
                        }
                    }
                }
            }

            return(dist < Single.MaxValue);
        }
Example #3
0
        public static bool CollisionTest(Ray ray, IPhysical obj, BasicMesh mesh, out float dist)
        {
            Vector3 start     = new Vector3(ray.X, ray.Y, ray.Z);
            Vector3 direction = new Vector3(ray.I, ray.J, ray.K);

            dist = Single.MaxValue;

            // Construct a matrix to transform to scene space
            Matrix4 transform = Matrix4.Identity;

            transform *= Matrix4.CreateScale(obj.Scale);
            transform *= Matrix4.CreateFromQuaternion(obj.RelativeRotation);
            transform *= Matrix4.CreateTranslation(obj.RelativePosition);

            ILinkable parent = obj.Parent;

            if (parent != null)
            {
                // Apply parent rotation and translation
                transform *= Matrix4.CreateFromQuaternion(parent.RelativeRotation);
                transform *= Matrix4.CreateTranslation(parent.RelativePosition);
            }

            // Iterate through all of the triangles in the mesh, doing a ray-triangle intersection
            for (int i = 0; i < mesh.Indices.Length; i += 3)
            {
                Vector3 point0 = mesh.Vertices[mesh.Indices[i + 0]] * transform;
                Vector3 point1 = mesh.Vertices[mesh.Indices[i + 1]] * transform;
                Vector3 point2 = mesh.Vertices[mesh.Indices[i + 2]] * transform;

                float thisDist;
                if (RayTriangle.CollisionTestCull(start, direction, point0, point1, point2, out thisDist))
                {
                    if (thisDist < dist)
                    {
                        dist = thisDist;
                    }
                }
            }

            return(dist < Single.MaxValue);
        }