Ejemplo n.º 1
0
        /// <summary>
        ///   Returns the position of the nearest vertex.
        /// </summary>
        /// <returns>
        ///   Position of the nearest vertex.
        /// </returns>
        ///
        /// <param name="grid">
        ///   The rectangular grid instance.
        /// </param>
        /// <param name="point">
        ///   Point in world space.
        /// </param>
        /// <param name="system">
        ///   Coordinate system to use.
        /// </param>
        ///
        /// <remarks>
        ///   Returns the position of the nearest vertex from a given point in
        ///   either grid- or world space.
        /// </remarks>
        public static Vector3 NearestVertex(this HexGrid grid, Vector3 point, CoordinateSystem system)
        {
            // Vertices in the hex-grid are dual to faces in the triangular
            // tessellation of the grid. Convert the point to cubic coordinates
            var cubicPoint = grid.WorldToCubic(point);

            cubicPoint.x = Mathf.Floor(cubicPoint.x) + .5f;
            cubicPoint.y = Mathf.Floor(cubicPoint.y) + .5f;
            cubicPoint.z = Mathf.Floor(cubicPoint.z) + .5f;
            cubicPoint.w = Mathf.Round(cubicPoint.w);

            switch (system)
            {
            case CoordinateSystem.Cubic:
                return(cubicPoint);

            case CoordinateSystem.HerringboneUp:
                return(grid.CubicToHerringU(cubicPoint));

            case CoordinateSystem.HerringboneDown:
                return(grid.CubicToHerringD(cubicPoint));

            case CoordinateSystem.RhombicUp:
                return(grid.CubicToRhombic(cubicPoint));

            case CoordinateSystem.RhombicDown:
                return(grid.CubicToRhombicD(cubicPoint));

            case CoordinateSystem.World:
                return(grid.CubicToWorld(cubicPoint));
            }
            throw new System.ComponentModel.InvalidEnumArgumentException();
        }
Ejemplo n.º 2
0
        /// <summary>
        ///   Returns the position of the nearest face.
        /// </summary>
        /// <returns>
        ///   Position of the nearest face.
        /// </returns>
        /// <param name="grid">
        ///   The rectangular grid instance.
        /// </param>
        /// <param name="point">
        ///   Point in world space.
        /// </param>
        /// <param name="system">
        ///   Coordinate system to use.
        /// </param>
        /// <remarks>
        ///   <para>
        ///     Returns the coordinates of a face on the grid closest to a
        ///     given point. Since the face is enclosed by four vertices, the
        ///     returned value is the point in between all four of the
        ///     vertices. You also need to specify on which plane the face
        ///     lies.
        ///   </para>
        /// </remarks>
        public static Vector3 NearestFace(
            this HexGrid grid, Vector3 point, CoordinateSystem system
            )
        {
            // Faces in the hex-grid are dual to vertices in the triangular
            // tessellation of the grid. Convert the point to cubic coordinates
            var cubic = grid.WorldToCubic(point);
            // We need the original cubic coordinates and the rounded ones, so
            // use a separate variable
            var rounded = new Vector4(
                Mathf.Round(cubic.x),
                Mathf.Round(cubic.y),
                Mathf.Round(cubic.z),
                Mathf.Round(cubic.w)
                );

            // Rounding all three coordinates does not guarantee that their sum
            // is zero. Therefore we will find the coordinate with the largest
            // change and compute its value from the other two instead of its
            // rounded value.
            var deltaX = Mathf.Abs(rounded.x - cubic.x);
            var deltaY = Mathf.Abs(rounded.y - cubic.y);
            var deltaZ = Mathf.Abs(rounded.z - cubic.z);

            if (deltaX > deltaY && deltaX > deltaZ)
            {
                rounded.x = -rounded.y - rounded.z;
            }
            else if (deltaY > deltaZ)
            {
                rounded.y = -rounded.x - rounded.z;
            }
            else
            {
                rounded.z = -rounded.x - rounded.y;
            }

            switch (system)
            {
            case CoordinateSystem.Cubic:
                return(rounded);

            case CoordinateSystem.HerringboneUp:
                return(grid.CubicToHerringU(rounded));

            case CoordinateSystem.HerringboneDown:
                return(grid.CubicToHerringD(rounded));

            case CoordinateSystem.RhombicUp:
                return(grid.CubicToRhombic(rounded));

            case CoordinateSystem.RhombicDown:
                return(grid.CubicToRhombicD(rounded));

            case CoordinateSystem.World:
                return(grid.CubicToWorld(rounded));
            }
            throw new System.ComponentModel.InvalidEnumArgumentException();
        }
Ejemplo n.º 3
0
 /// <summary>
 ///   Aligns a <c>Transform</c> vector onto the nearest face of the
 ///   grid.
 /// </summary>
 /// <param name="grid">
 ///   Instance of the grid to extend.
 /// </param>
 /// <param name="transform">
 ///   The <c>Transform</c> to align.
 /// </param>
 /// <remarks>
 ///   <para>
 ///     This extension method is pretty dumb because it does not take
 ///     the size of an object into account. There are many ways to
 ///     "align" on object to a hex grid and they depend on the shape of
 ///     the object, there is no general way of doing it. This method
 ///     mainly serves as an example for how to make your own extension
 ///     method.
 ///   </para>
 /// </remarks>
 public static void AlignTransform(this HexGrid grid, Transform transform)
 {
     transform.position = AlignVector3(grid, transform.position);
 }
Ejemplo n.º 4
0
 /// <summary>
 ///   Aligns a position vector onto the nearest face of the grid.
 /// </summary>
 /// <param name="grid">
 ///   Instance of the grid to extend.
 /// </param>
 /// <param name="vector">
 ///   The position in world-coordinates.
 /// </param>
 /// <returns>
 ///   Position of the nearest face.
 /// </returns>
 /// <remarks>
 ///   <para>
 ///     This is identical to the extension method for finding the
 ///     nearest face.
 ///   </para>
 /// </remarks>
 public static Vector3 AlignVector3(this HexGrid grid, Vector3 vector)
 {
     return(grid.NearestFace(vector, CoordinateSystem.World));
 }