Vector2

package
v0.0.0-...-ae8aae0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package Vector2 provides a 2D vector using floating-point coordinates.

Index

Constants

This section is empty.

Variables

View Source
var (
	Zero  = XY{0, 0}                 // Zero vector, a vector with all components set to 0.
	One   = XY{1, 1}                 // One vector, a vector with all components set to 1.
	Inf   = XY{Float.Inf, Float.Inf} // Infinity vector, a vector with all components set to math.Inf(1).
	Left  = XY{-1, 0}                // Left unit vector. Represents the direction of left.
	Right = XY{1, 0}                 // Right unit vector. Represents the direction of right.
	Up    = XY{0, -1}                // Up unit vector. Y is down in 2D, so this vector points -Y.
	Down  = XY{0, 1}                 // Down unit vector. Y is down in 2D, so this vector points +Y.
)

Functions

func AngleBetween

func AngleBetween(a, b XY) Angle.Radians

AngleBetween returns the angle between the given vectors, in radians.

Illustration of the returned angle. https://raw.githubusercontent.com/godotengine/godot-docs/master/img/vector2_angle_to.png

func AngleRadians

func AngleRadians(vec XY) Angle.Radians

Angle Returns this vector's angle with respect to the positive X axis, or (1, 0) vector, in radians.

For example, Const(Vector2.Right).Angle() will return zero, Const(Vector2.Down).Angle() will return PI / 2 (a quarter turn, or 90 degrees), and Vector2(1, -1).Angle() will return -Pi / 4 (a negative eighth turn, or -45 degrees).

Illustration of the returned angle. https://raw.githubusercontent.com/godotengine/godot-docs/master/img/vector2_angle.png

Equivalent to the result of [Atan2] when called with the vector's y and x as parameters:

Atan2(y, x).

func AngleToPoint

func AngleToPoint(vec, p XY) Angle.Radians

AngleToPoint returns the angle between the line connecting the two points and the X axis, in radians. a.AngleToPoint(b) is equivalent of doing (b - a).Angle().

Illustration of the returned angle. https://raw.githubusercontent.com/godotengine/godot-docs/master/img/vector2_angle_to_point.png

func AsArray

func AsArray(vec XY) [2]Float.X

func Aspect

func Aspect(vec XY) Float.X

Aspect returns the aspect ratio of this vector, the ratio of x to y.

func Cross

func Cross(a, b XY) Float.X

Cross returns the 2D analog of the cross product for this vector and with.

This is the signed area of the parallelogram formed by the two vectors. If the second vector is clockwise from the first vector, then the cross product is the positive area. If counter-clockwise, the cross product is the negative area.

Note: Cross product is not defined in 2D mathematically. This method embeds the 2D vectors in the XY plane of 3D space and uses their cross product's Z component as the analog.

func Distance

func Distance(a, b XY) Float.X

Distance returns the distance between this vector and to.

func DistanceSquared

func DistanceSquared(a, b XY) Float.X

DistanceSquared returns the squared distance between this vector and to.

This method runs faster than distance_to, so prefer it if you need to compare vectors or need the squared distance for some formula.

func Dot

func Dot(a, b XY) Float.X

Dot returns the dot product of this vector and with. This can be used to compare the angle between two vectors. For example, this can be used to determine whether an enemy is facing the player.

The dot product will be 0 for a straight angle (90 degrees), greater than 0 for angles narrower than 90 degrees and lower than 0 for angles wider than 90 degrees.

When using unit (normalized) vectors, the result will always be between -1.0 (180 degree angle) when the vectors are facing opposite directions, and 1.0 (0 degree angle) when the vectors are aligned.

Note: Vector2.Dot(a,b) is equivalent to Vector2.Dot(b,a)

func Index

func Index[I Int.Any](v XY, i I) Float.X

func IsApproximatelyEqual

func IsApproximatelyEqual(a, b XY) bool

IsAproximatelyEqual returns true if this vector and to are approximately equal, by running [IsAproximatelyEqual] on each component.

func IsApproximatelyZero

func IsApproximatelyZero(vec XY) bool

IsAproximatelyZero returns true if this vector is approximately equal to Vector2.Zero.

func IsFinite

func IsFinite(vec XY) bool

IsFinite returns true if this vector is finite, by calling IsFinite on each component.

func IsNormalized

func IsNormalized(vec XY) bool

IsNormalized returns true if the vector is normalized, i.e. its length is approximately equal to 1.

func Length

func Length(vec XY) Float.X

Length returns the length (magnitude) of this vector.

func LengthSquared

func LengthSquared(vec XY) Float.X

LengthSquared returns the squared length (squared magnitude) of this vector.

func Less

func Less(a, b XY) bool

Less compares two Vector2 vectors by first checking if the X value of the left vector is less than the X value of the right vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors. This operator is useful for sorting vectors.

Note: Vectors with NaN elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.

Types

type Axis

type Axis int
const (
	X Axis = iota // Enumerated value for the X axis. Returned by [MaxAxis] and [MinAxis].
	Y             // Enumerated value for the Y axis. Returned by [MaxAxis] and [MinAxis].
)

func MaxAxis

func MaxAxis(vec XY) Axis

MaxAxis returns the axis of the vector's highest value. See Axis constants. If all components are equal, this method returns X.

func MinAxis

func MinAxis(vec XY) Axis

MinAxis returns the axis of the vector's lowest value. See Axis constants. If all components are equal, this method returns Y.

type XY

type XY = struct {
	X Float.X // The vector's X component.
	Y Float.X // The vector's Y component.
}

XY is a 2-element structure that can be used to represent 2D coordinates or any other pair of numeric values.

It uses floating-point coordinates. By default, these floating-point values use 32-bit precision, unlike float which is always 64-bit. If double precision is needed, compile with the Go build tag 'precision_double'.

See [Vector2i.XY] for its integer counterpart.

func Abs

func Abs(vec XY) XY

Abs returns a new vector with all components in absolute values (i.e. positive).

func Add

func Add(a, b XY) XY

func AddX

func AddX[X Float.Any | Int.Any](a XY, b X) XY

func BezierDerivative

func BezierDerivative[X Float.Any](vec, control1, control2, end XY, t X) XY

BezierDerivative returns the derivative at the given t on the Bézier curve defined by this vector and the given control_1, control_2, and end points.

func BezierInterpolate

func BezierInterpolate[X Float.Any](vec, control1, control2, end XY, t X) XY

BezierInterpolate returns the point at the given t on the Bézier curve defined by this vector and the given control_1, control_2, and end points.

func Bounce

func Bounce(vec, normal XY) XY

Bounce returns a new vector "bounced off" from a plane defined by the given normal.

func Ceil

func Ceil(vec XY) XY

Ceil returns a new vector with all components rounded up (towards positive infinity).

func Clamp

func Clamp(vec, from, to XY) XY

Clamp returns a new vector with all components clamped to the given min and max.

func Clampf

func Clampf[X Float.Any](vec XY, from, to X) XY

Clampf returns a new vector with all components clamped to the given min and max.

func CubicInterpolate

func CubicInterpolate[X Float.Any](vec, b, preA, postB XY, weight X) XY

CubicInterpolate performs a cubic interpolation between this vector and b using pre_a and post_b as handles, and returns the result at position weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.

func CubicInterpolateInTime

func CubicInterpolateInTime[X Float.Any](vec, b, preA, postB XY, weight, b_t, pre_a_t, post_b_t X) XY

CubicInterpolateInTime performs a cubic interpolation between this vector and b using pre_a and post_b as handles, and returns the result at position weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.

It can perform smoother interpolation than cubic_interpolate by the time values.

func Direction

func Direction(a, b XY) XY

Direction returns the normalized vector pointing from this vector to to. This is equivalent to using (b - a).Normalized().

func Div

func Div(a, b XY) XY

func DivX

func DivX[X Float.Any | Int.Any](a XY, b X) XY

func Floor

func Floor(vec XY) XY

Floor returns a new vector with all components rounded down (towards negative infinity).

func LengthLimited

func LengthLimited[X Float.Any](vec XY, length X) XY

LengthLimited returns the vector with a maximum length by limiting its length to length.

func Lerp

func Lerp[X Float.Any](vec, to XY, weight X) XY

Lerp returns the result of the linear interpolation between this vector and to by amount weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.

func Max

func Max(a, b XY) XY

Max returns the component-wise minimum of this and with, equivalent to

Vector2.New(max(x, with.x), max(y, with.y))

func Maxf

func Maxf[X Float.Any](v XY, with X) XY

Maxf returns the component-wise maximum of this and with, equivalent to

Vector2.New(max(x, with), max(y, with)).

func Min

func Min(a, b XY) XY

Min returns the component-wise minimum of this and with, equivalent to

Vector2.New(min(x, with.x), min(y, with.y))

func Minf

func Minf[X Float.Any | Int.Any](v XY, with X) XY

Minf returns the component-wise minimum of this and with, equivalent to

Vector2.New(min(x, with), min(y, with)).

func Move

func Move[X Float.Any](a, b XY, delta X) XY

Move returns a new vector moved toward to by the fixed delta amount. Will not go past the final value.

func Mul

func Mul(a, b XY) XY

func MulX

func MulX[X Float.Any | Int.Any](a XY, b X) XY

func Neg

func Neg(v XY) XY

func New

func New[X Int.Any | Float.Any](x, y X) XY

New constructs a new Vector2 from the given x and y.

func Normalized

func Normalized(vec XY) XY

Normalized returns the result of scaling the vector to unit length. Equivalent to v / v.Length(). See also is_normalized.

Note: This function may return incorrect values if the input vector length is near zero.

func Orthogonal

func Orthogonal(vec XY) XY

Orthogonal returns a perpendicular vector rotated 90 degrees counter-clockwise compared to the original, with the same length.

func Posmod

func Posmod[X Float.Any](vec XY, mod X) XY

Posmode returns a vector composed of the [Fposmod] of this vector's components and [Mod].

func PosmodVector

func PosmodVector(vec, mod XY) XY

Posmod returns a vector composed of the [Fposmod] of this vector's components and [Mod].

func Project

func Project(a, b XY) XY

Project returns the result of projecting the vector onto the given vector b.

func Reflect

func Reflect(v, n XY) XY

Reflect returns the result of reflecting the vector from a line defined by the given direction vector n.

func Rotated

func Rotated(v XY, by Angle.Radians) XY

Rotated returns the result of rotating this vector by angle (in radians).

func Round

func Round(v XY) XY

Round returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.

func Sign

func Sign(vec XY) XY

Sign returns a new vector with each component set to 1.0 if it's positive, -1.0 if it's negative, and 0.0 if it's zero. The result is identical to calling [Signf] on each component.

func Slerp

func Slerp(vec, to XY, weight Angle.Radians) XY

Slerp returns the result of spherical linear interpolation between this vector and to, by amount weight. weight is on the range of 0.0 to 1.0, representing the amount of interpolation.

This method also handles interpolating the lengths if the input vectors have different lengths. For the special case of one or both input vectors having zero length, this method behaves like lerp.

func Slide

func Slide(v, n XY) XY

Slide returns the result of sliding the vector along a plane defined by the given normal.

func Snapped

func Snapped(v, step XY) XY

Snapped returns a new vector with all components snapped to the nearest multiple of step.

func Snappedf

func Snappedf[X Float.Any](v XY, step X) XY

Snappedf returns a new vector with all components snapped to the nearest multiple of step.

func Sub

func Sub(a, b XY) XY

func SubX

func SubX[X Float.Any | Int.Any](a XY, b X) XY

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL