Skip to main content

VectorUtil

A library of useful vector functions.

Types

Vector

type Vector = Vector3 | Vector2

Functions

unpack

VectorUtil.unpack(VectorVector) → ...number

Splits a Vector into its components.

snap

VectorUtil.snap(
vectorVector,
snapToNearestMultiplenumber
) → Vector

Snaps a Vector to the nearest multiple of the given number for each coordinate.

roundLength

VectorUtil.roundLength(
vectorVector,
incrementnumber
) → Vector

Rounds a Vectors length to the nearest multiple of the given number.

randomUnitVector

VectorUtil.randomUnitVector() → Vector3

Returns a random unit vector3. Evenly distributes around the unit sphere.

truncate

VectorUtil.truncate(
VectorVector,--

The vector to truncate.

maxLengthnumber--

The maximum length of the vector.

) → Vector--

The truncated vector.

Truncates the length of a vector such that if it exceeds the given length, it will be truncated to that length.

getAngle

VectorUtil.getAngle(vectorVector2) → number

Returns the angle of a Vector2 relative to the X axis.

getAngleBetween

VectorUtil.getAngleBetween(
firstVectorVector,
secondVectorVector
) → number

Returns the shortest angle between two vectors in Radians.

getSignedAngleBetweenVector3s

VectorUtil.getSignedAngleBetweenVector3s(
firstVectorVector3,--

The start of the angle

secondVectorVector3,--

The end of the angle

axisVector3--

The axis to rotate around

) → number--

The signed angle between the two vectors in radians.

Returns a signed angle in radians between two Vector3s around a given axis. The sign is calculated counter-clockwise, left of first vector is positive, right of first vector is negative.

getSignedAngleBetweenVector2s

VectorUtil.getSignedAngleBetweenVector2s(
firstVectorVector2,--

The start of the angle

secondVectorVector2--

The end of the angle

) → number--

The signed angle between the two vectors in radians.

Returns a signed angle in radians between two vectors. The sign is calculated counter-clockwise, left of first vector is positive, right of first vector is negative.

isNaN

VectorUtil.isNaN(vectorVector) → boolean

Checks if a given vector is NaN.

abs

VectorUtil.abs(VectorVector) → Vector

Returns the absolute value of the Vector

sign

VectorUtil.sign(VectorVector) → Vector

Returns a Vector where each component is the sign of the original Vector.

normalize

VectorUtil.normalize(VectorVector) → Vector

Safely Normalizes a Vector.

flattenY

VectorUtil.flattenY(
VectorVector3,--

The Vector3 to flatten.

newYnumber?--

the height to flatten the vector to. Defaults to 0.

) → Vector3--

The flattened Vector3.

Flattens a Vector3 on its Y axis

flatten

VectorUtil.flatten(
VectorVector3,--

The Vector3 to flatten.

axisEnum.Axis,--

The axis to flatten the vector on. Defaults to "Y".

defaultValuenumber?--

The value to set the flattened axis to. Defaults to 0.

) → Vector3--

The flattened Vector3.

Flattens a given Vector3 on a specified axis

getAxis

VectorUtil.getAxis(
vectorVector3,--

The Vector to pull from

axisEnum.Axis--

The axis to get

) → Vector3--

The returned Vector containing only the specified axis.

Takes a Vector and removes all values except the specified Axis.

setAxis

VectorUtil.setAxis(
vectorVector3,--

The Vector to change from

axisEnum.Axis,--

The axis to set

valuenumber--

The new value of the axis

) → Vector3--

The adjusted Vector

Takes a Vector and sets the axis value to the specified number.

rotateVector3

VectorUtil.rotateVector3(
vectorToRotateVector3,--

The vector to rotate.

anglesToRotateCFrame--

The angles to rotate the vector by. This should be given as a CFrame.Angles

) → Vector3--

The rotated vector.

Rotates a vector about its axis by the given angles. Takes a CFrame.Angles object as the angles to rotate by. Works similarly to rotating a CFrame.

rotateVector(Vector3.new(1,0,0), CFrame.Angles(0,math.pi,0)) -- Output: Vector3.new(-1,0,0)

rotateVector2

VectorUtil.rotateVector2(
vectorToRotateVector2,--

The vector to rotate.

anglenumber--

The angle [In Radians] to rotate the vector by.

) → Vector2--

The rotated vector.

Rotates a vector2 by a given amount of radians.

closestPointOnLine

VectorUtil.closestPointOnLine(
refPointVector3,--

The point to find the closest point to.

linePointVector3,--

A point along the line.

lineDirectionVector3--

The direction of the line.

) → Vector3--

The closest point on the line to the reference point.

Finds the closest point on a line to a given point.

closestPointsBetweenLines

VectorUtil.closestPointsBetweenLines(
point1Vector3,--

A point along the first line.

direction1Vector3,--

The direction of the first line.

point2Vector3,--

A point along the second line.

direction2Vector3--

The direction of the second line.

) → (
Vector2,--

The closest point on the first line.

Vector2--

The closest point on the second line.

)

Finds the closest two points on two lines. The lines are defined by some point along them and a direction

planeIntersectionPoint

VectorUtil.planeIntersectionPoint(
lineOriginVector3,--

A point along the line.

lineDirectionVector3,--

The direction of the line.

planeOriginVector3,--

A point on the plane.

planeNormalVector3--

The normal of the plane.

) → Vector3?--

The intersection point of the line and the plane if one exists.

Finds the intersection point of a line and a plane.

lineIntersectsSphere

VectorUtil.lineIntersectsSphere(
lineOriginVector3,--

A point along the line.

lineDirectionVector3,--

The direction of the line.

sphereOriginVector3,--

The origin of the sphere.

sphereRadiusnumber--

The radius of the sphere.

) → boolean--

Whether or not the line intersects the sphere.

Tests whether or not a line of infinite length intersects a sphere at some point.

lineSegmentIntersectsSphere

VectorUtil.lineSegmentIntersectsSphere(
linePoint1Vector3,--

The start point of the line segment.

linePoint2Vector3,--

The end point of the line segment.

sphereOriginVector3,--

The center point of the sphere.

sphereRadiusnumber--

The radius of the sphere.

) → boolean--

Whether or not the line segment intersects the sphere.

Tests whether or not a line segment intersects a sphere at some point. Only returns true if the intersection point is between the two points of the line segment.

Show raw api
{
    "functions": [
        {
            "name": "unpack",
            "desc": "Splits a Vector into its components.",
            "params": [
                {
                    "name": "Vector",
                    "desc": "",
                    "lua_type": "Vector"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "...number"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 28,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "snap",
            "desc": "Snaps a Vector to the nearest multiple of the given number for each coordinate.",
            "params": [
                {
                    "name": "vector",
                    "desc": "",
                    "lua_type": "Vector"
                },
                {
                    "name": "snapToNearestMultiple",
                    "desc": "",
                    "lua_type": "number"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Vector\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 38,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "roundLength",
            "desc": "Rounds a Vectors length to the nearest multiple of the given number.",
            "params": [
                {
                    "name": "vector",
                    "desc": "",
                    "lua_type": "Vector"
                },
                {
                    "name": "increment",
                    "desc": "",
                    "lua_type": "number"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Vector\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 55,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "randomUnitVector",
            "desc": "Returns a random unit vector3. Evenly distributes around the unit sphere.",
            "params": [],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Vector3\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 62,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "truncate",
            "desc": "Truncates the length of a vector such that if it exceeds the given length, it will be truncated to that length.",
            "params": [
                {
                    "name": "Vector",
                    "desc": "The vector to truncate.",
                    "lua_type": "Vector"
                },
                {
                    "name": "maxLength",
                    "desc": "The maximum length of the vector.",
                    "lua_type": "number"
                }
            ],
            "returns": [
                {
                    "desc": "The truncated vector.",
                    "lua_type": "Vector"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 72,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "getAngle",
            "desc": "Returns the angle of a Vector2 relative to the X axis.",
            "params": [
                {
                    "name": "vector",
                    "desc": "",
                    "lua_type": "Vector2"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "number\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 83,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "getAngleBetween",
            "desc": "Returns the shortest angle between two vectors in Radians.",
            "params": [
                {
                    "name": "firstVector",
                    "desc": "",
                    "lua_type": "Vector"
                },
                {
                    "name": "secondVector",
                    "desc": "",
                    "lua_type": "Vector"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "number\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 90,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "getSignedAngleBetweenVector3s",
            "desc": "Returns a signed angle in radians between two Vector3s around a given axis.\nThe sign is calculated counter-clockwise, left of first vector is positive, right of first vector is negative.",
            "params": [
                {
                    "name": "firstVector",
                    "desc": "The start of the angle",
                    "lua_type": "Vector3"
                },
                {
                    "name": "secondVector",
                    "desc": "The end of the angle",
                    "lua_type": "Vector3"
                },
                {
                    "name": "axis",
                    "desc": "The axis to rotate around",
                    "lua_type": "Vector3"
                }
            ],
            "returns": [
                {
                    "desc": "The signed angle between the two vectors in radians.",
                    "lua_type": "number"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 102,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "getSignedAngleBetweenVector2s",
            "desc": "Returns a signed angle in radians between two vectors.\nThe sign is calculated counter-clockwise, left of first\nvector is positive, right of first vector is negative.",
            "params": [
                {
                    "name": "firstVector",
                    "desc": "The start of the angle",
                    "lua_type": "Vector2"
                },
                {
                    "name": "secondVector",
                    "desc": "The end of the angle",
                    "lua_type": "Vector2"
                }
            ],
            "returns": [
                {
                    "desc": "The signed angle between the two vectors in radians.",
                    "lua_type": "number"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 126,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "isNaN",
            "desc": "Returns a CFrame that ... [DOCUMENTATION NEEDED] (Used for Slerping)",
            "params": [
                {
                    "name": "vector",
                    "desc": "",
                    "lua_type": "Vector"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean\n"
                }
            ],
            "function_type": "static",
            "ignore": true,
            "source": {
                "line": 134,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "isNaN",
            "desc": "Checks if a given vector is NaN.",
            "params": [
                {
                    "name": "vector",
                    "desc": "",
                    "lua_type": "Vector"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "boolean\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 145,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "abs",
            "desc": "Returns the absolute value of the Vector",
            "params": [
                {
                    "name": "Vector",
                    "desc": "",
                    "lua_type": "Vector"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Vector\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 152,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "sign",
            "desc": "Returns a Vector where each component is the sign of the original Vector.",
            "params": [
                {
                    "name": "Vector",
                    "desc": "",
                    "lua_type": "Vector"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Vector\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 162,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "normalize",
            "desc": "Safely Normalizes a Vector.",
            "params": [
                {
                    "name": "Vector",
                    "desc": "",
                    "lua_type": "Vector"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Vector\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 172,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "flattenY",
            "desc": "Flattens a Vector3 on its Y axis",
            "params": [
                {
                    "name": "Vector",
                    "desc": "The Vector3 to flatten.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "newY",
                    "desc": "the height to flatten the vector to. Defaults to 0.",
                    "lua_type": "number?"
                }
            ],
            "returns": [
                {
                    "desc": "The flattened Vector3.",
                    "lua_type": "Vector3"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 182,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "flatten",
            "desc": "Flattens a given Vector3 on a specified axis",
            "params": [
                {
                    "name": "Vector",
                    "desc": "The Vector3 to flatten.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "axis",
                    "desc": "The axis to flatten the vector on. Defaults to \"Y\".",
                    "lua_type": "Enum.Axis"
                },
                {
                    "name": "defaultValue",
                    "desc": "The value to set the flattened axis to. Defaults to 0.",
                    "lua_type": "number?"
                }
            ],
            "returns": [
                {
                    "desc": "The flattened Vector3.",
                    "lua_type": "Vector3"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 193,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "getAxis",
            "desc": "Takes a Vector and removes all values except the specified Axis.",
            "params": [
                {
                    "name": "vector",
                    "desc": "The Vector to pull from",
                    "lua_type": "Vector3"
                },
                {
                    "name": "axis",
                    "desc": "The axis to get",
                    "lua_type": "Enum.Axis"
                }
            ],
            "returns": [
                {
                    "desc": "The returned Vector containing only the specified axis.",
                    "lua_type": "Vector3"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 212,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "setAxis",
            "desc": "Takes a Vector and sets the axis value to the specified number.",
            "params": [
                {
                    "name": "vector",
                    "desc": "The Vector to change from",
                    "lua_type": "Vector3"
                },
                {
                    "name": "axis",
                    "desc": "The axis to set",
                    "lua_type": "Enum.Axis"
                },
                {
                    "name": "value",
                    "desc": "The new value of the axis",
                    "lua_type": "number"
                }
            ],
            "returns": [
                {
                    "desc": "The adjusted Vector",
                    "lua_type": "Vector3"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 223,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "rotateVector3",
            "desc": "Rotates a vector about its axis by the given angles.\nTakes a CFrame.Angles object as the angles to rotate by. Works similarly to rotating a CFrame.\n\nrotateVector(Vector3.new(1,0,0), CFrame.Angles(0,math.pi,0)) -- Output: Vector3.new(-1,0,0)",
            "params": [
                {
                    "name": "vectorToRotate",
                    "desc": "The vector to rotate.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "anglesToRotate",
                    "desc": "The angles to rotate the vector by. This should be given as a CFrame.Angles",
                    "lua_type": "CFrame"
                }
            ],
            "returns": [
                {
                    "desc": "The rotated vector.",
                    "lua_type": "Vector3"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 243,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "rotateVector2",
            "desc": "Rotates a vector2 by a given amount of radians.",
            "params": [
                {
                    "name": "vectorToRotate",
                    "desc": "The vector to rotate.",
                    "lua_type": "Vector2"
                },
                {
                    "name": "angle",
                    "desc": "The angle [In Radians] to rotate the vector by.",
                    "lua_type": "number"
                }
            ],
            "returns": [
                {
                    "desc": "The rotated vector.",
                    "lua_type": "Vector2"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 253,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "rotateVector",
            "desc": "Rotates a vector.",
            "params": [
                {
                    "name": "vector",
                    "desc": "",
                    "lua_type": "Vector"
                },
                {
                    "name": "...",
                    "desc": "",
                    "lua_type": "any"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "Vector"
                }
            ],
            "function_type": "static",
            "ignore": true,
            "source": {
                "line": 268,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "closestPointOnLine",
            "desc": "Finds the closest point on a line to a given point.",
            "params": [
                {
                    "name": "refPoint",
                    "desc": "The point to find the closest point to.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "linePoint",
                    "desc": "A point along the line.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "lineDirection",
                    "desc": "The direction of the line.",
                    "lua_type": "Vector3"
                }
            ],
            "returns": [
                {
                    "desc": "The closest point on the line to the reference point.",
                    "lua_type": "Vector3"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 284,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "closestPointsBetweenLines",
            "desc": "Finds the closest two points on two lines.\nThe lines are defined by some point along them and a direction",
            "params": [
                {
                    "name": "point1",
                    "desc": "A point along the first line.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "direction1",
                    "desc": "The direction of the first line.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "point2",
                    "desc": "A point along the second line.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "direction2",
                    "desc": "The direction of the second line.",
                    "lua_type": "Vector3"
                }
            ],
            "returns": [
                {
                    "desc": "The closest point on the first line.",
                    "lua_type": "Vector2"
                },
                {
                    "desc": "The closest point on the second line.",
                    "lua_type": "Vector2"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 299,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "planeIntersectionPoint",
            "desc": "Finds the intersection point of a line and a plane.",
            "params": [
                {
                    "name": "lineOrigin",
                    "desc": "A point along the line.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "lineDirection",
                    "desc": "The direction of the line.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "planeOrigin",
                    "desc": "A point on the plane.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "planeNormal",
                    "desc": "The normal of the plane.",
                    "lua_type": "Vector3"
                }
            ],
            "returns": [
                {
                    "desc": "The intersection point of the line and the plane if one exists.",
                    "lua_type": "Vector3?"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 323,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "lineIntersectsSphere",
            "desc": "Tests whether or not a line of infinite length intersects a sphere at some point.",
            "params": [
                {
                    "name": "lineOrigin",
                    "desc": "A point along the line.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "lineDirection",
                    "desc": "The direction of the line.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "sphereOrigin",
                    "desc": "The origin of the sphere.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "sphereRadius",
                    "desc": "The radius of the sphere.",
                    "lua_type": "number"
                }
            ],
            "returns": [
                {
                    "desc": "Whether or not the line intersects the sphere.",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 343,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        },
        {
            "name": "lineSegmentIntersectsSphere",
            "desc": "Tests whether or not a line **segment** intersects a sphere at some point.\nOnly returns true if the intersection point is between the two points of the line segment.",
            "params": [
                {
                    "name": "linePoint1",
                    "desc": "The start point of the line segment.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "linePoint2",
                    "desc": "The end point of the line segment.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "sphereOrigin",
                    "desc": "The center point of the sphere.",
                    "lua_type": "Vector3"
                },
                {
                    "name": "sphereRadius",
                    "desc": "The radius of the sphere.",
                    "lua_type": "number"
                }
            ],
            "returns": [
                {
                    "desc": "Whether or not the line segment intersects the sphere.",
                    "lua_type": "boolean"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 359,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        }
    ],
    "properties": [],
    "types": [
        {
            "name": "Vector",
            "desc": "",
            "lua_type": "Vector3 | Vector2",
            "source": {
                "line": 15,
                "path": "src/railutils/src/RailUtil/VectorUtil.lua"
            }
        }
    ],
    "name": "VectorUtil",
    "desc": "A library of useful vector functions.",
    "source": {
        "line": 23,
        "path": "src/railutils/src/RailUtil/VectorUtil.lua"
    }
}