Skip to content
Michael edited this page Oct 16, 2023 · 5 revisions

You know what the GameMaker community really needs more of? Yet another Vector library. Anyway I made a Vector library.

Contains code for 2D (x, y), 3D (x, y, z), and 4D (x, y, z, w) vectors.

Constructors

new Vector2(c)

new Vector3(c)

new Vector4(c)

Constructs a new Vector type. All fields will be set to the same value.

new Vector2(x, y)

new Vector3(x, y, z)

new Vector4(x, y, z, w)

Constructs a new Vector type. Fields will each be set to the specified values.

Static Methods

There are a few things you can access by using the dot operator on the Vector type constructor without needing an instance of one. These return new Vectors and serve as "default" values of sorts. You can probably guess what values they contain.

::Zero()

::One()

::Infinity()

::NegativeInfinity()

Instance Methods

All of the following methods apply to all vector types unless otherwise specified.

::toString()

Returns: string

Returns a string representation of the Vector. It's basically the notation you'd use in math class. GameMaker will call this implicitly if you attempt to string() an instance of a Vector.

::Set(c)

::Set(x, y...)

Returns: N/A

Sets the fields of a Vector. Arguments work the same as the constructors.

::Clone()

Returns: Vector

Returns a copy of the Vector.

::AsLinearArray()

Returns: array

Returns the Vector's fields as an array of elements.

::Add(x, y...)

::Add(vec)

Returns: Vector

Returns a new Vector comprised of the method input(s) added to the calling Vector. Inputs can be a numeric value or another Vector of the same type.

::Sub(x, y...)

::Sub(vec)

Returns: Vector

Returns a new Vector comprised of the method input(s) subtracted from the calling Vector. Inputs can be a numeric value or another Vector of the same type.

::Mul(x, y...)

::Mul(vec)

Returns: Vector

Look you should probably start to be noticing a pattern in how these work by now.

::Div(x, y...)

::Div(vec)

Returns: Vector

Dude cmon.

::Equals(vec)

Returns: boolean

Returns true if each of the components of the calling Vector is equal to its respective component in the input Vector, and false otherwise.

::Magnitude()

Returns: real

Calculates the magnitude of the Vector. This is the distance in space between the Vector's components and the Zero Vector.

::DistanceTo(vec)

Returns: real

Calculates the distance in space between the calling Vector and the input Vector.

::Dot(vec)

Returns: real

Calculates the dot product between the calling Vector and the input Vector. This is defined as each component of one Vector multiplied by the like component of the input Vector, added all together.

Often, you see this used as a measure of how similar two Vectors are to each other. Two Vectors with a magnitude of 1 will have a dot product of 1 if they're pointing in the same direction, 0 if they're perpendicular to each other, and -1 if they're pointing in opposite directions.

This is otherwise known as the cosine of the Angle between two Vectors.

::Cross(vec3)

Returns: Vector3

Returns the cross product of one Vector3 against another. This will be a Vector3 that's at right angles to both the calling Vector3 and the input Vector3.

If the two operands represent the same direction (regardless of magnitude, including the Zero Vector) the output will be a Zero Vector.

The Cross Product is anticommutative, which is the weird nerd way of saying that a x b will produce the same thing as b x a with each of the components negated.

The Cross Product is only defined for 3D vectors. It's kind of a whole thing. Please don't ask for the 7D version.

::Normalize()

Returns: Vector

Returns a new Vector with the same proportions as the calling Vector, scaled up or down such that the new Vector has a magnitude of 1. Calling this on the Zero Vector will give you a whole bunch of NaNs.

::ClampMagnitude(magnitude)

Returns: Vector

Returns a new Vector with the same proportions as the calling Vector, scaled up or down such that the new Vector has a the given magnitude. It's basically a more general version of ::Normalize(). Calling this on the Zero Vector will still give you a whole bunch of NaNs.

::Project(vec)

Returns: Vector

Projects one Vector onto another. This can be used to figure out how much of one Vector is traveling in the same direction as another Vector.

::Min(val)

::Min(vec)

Returns: Vector

Returns a new Vector with each component no larger than the input. If the input is a Vector, the operation will be performed component-wise.

::Max(val)

::Max(vec)

Returns: Vector

Returns a new Vector with each component no smaller than the input. If the input is a Vector, the operation will be performed component-wise.

::Clamp(min, max)

::Clamp(vecmin, vecmax)

Returns: Vector

Returns a new Vector with the components constrained between a min and the max. If the min or max are Vectors, the operation will be performed component-wise. It's basically ::Min() and ::Max() Frankensteined together.

::Floor()

Returns: Vector

Returns a new Vector with each component rounded down to the nearest integer.

::Ceil()

Returns: Vector

Returns a new Vector with each component rounded down to the nearest integer.

::Round()

Returns: Vector

Returns a new Vector with each component rounded to the nearest integer. If a component has a fractional part of exactly 0.5, it will be rounded to the nearest even number. This pisses GameMaker users off to no end for some reason but it's a whole thing in math so please stop reporting this as a bug in the engine.

::Frac()

Returns: Vector

Returns a new Vector with only the fractional part of each component.

::Abs()

Returns: Vector

Returns a new Vector with each component made positive.

::Lerp(vec, amount)

Returns: Vector

Returns a new Vector with each component linearly interpolated to the target value.

An amount value of 0 will return a Vector with the exact same components as the calling Vector. A value of 1 will return a Vector with the exact same components as the input Vector. A value of 0.5 will return a Vector exactly halfway in between the two Vectors. You can lerp with values outside the range [0, 1] to extrapolate or regress.

::Approach(vec, amount)

Returns: Vector

Returns a new Vector with each component incremented towards the target value by the specified fixed amount. The component value will not overshoot the target value.

Unlike with ::Lerp(), in this case amount is an exact value and not a proportion. Most of the time, this is what people actually want when they talk about using ::Lerp().

This is incredibly useful for linear motion in games if you want to make sure something stops at a certain position.

::Slerp(vec, amount)

Returns: Vector

Yes, "slerp" is pronounced the way you think it is. It's short for "spherical lerp."

Interpolates the vector to the target as a direction, rather than a coordinate. You can also think of it as interpolating the angle between two vectors. See the Wikipedia page for a weird nerd explanation.

::Angle(vec)

Returns: real

Returns the angle between two Vectors. This is the inverse cosine of the dot product of two Vectors, ignoring their magnitudes.

Clone this wiki locally