Documentation ¶
Overview ¶
Package Vector2 provides a 2D vector using floating-point coordinates.
Index ¶
- Variables
- func AngleBetween(a, b XY) Angle.Radians
- func AngleRadians(vec XY) Angle.Radians
- func AngleToPoint(vec, p XY) Angle.Radians
- func AsArray(vec XY) [2]Float.X
- func Aspect(vec XY) Float.X
- func Cross(a, b XY) Float.X
- func Distance(a, b XY) Float.X
- func DistanceSquared(a, b XY) Float.X
- func Dot(a, b XY) Float.X
- func Index[I Int.Any](v XY, i I) Float.X
- func IsApproximatelyEqual(a, b XY) bool
- func IsApproximatelyZero(vec XY) bool
- func IsFinite(vec XY) bool
- func IsNormalized(vec XY) bool
- func Length(vec XY) Float.X
- func LengthSquared(vec XY) Float.X
- func Less(a, b XY) bool
- type Axis
- type XY
- func Abs(vec XY) XY
- func Add(a, b XY) XY
- func AddX[X Float.Any | Int.Any](a XY, b X) XY
- func BezierDerivative[X Float.Any](vec, control1, control2, end XY, t X) XY
- func BezierInterpolate[X Float.Any](vec, control1, control2, end XY, t X) XY
- func Bounce(vec, normal XY) XY
- func Ceil(vec XY) XY
- func Clamp(vec, from, to XY) XY
- func Clampf[X Float.Any](vec XY, from, to X) XY
- func CubicInterpolate[X Float.Any](vec, b, preA, postB XY, weight X) XY
- func CubicInterpolateInTime[X Float.Any](vec, b, preA, postB XY, weight, b_t, pre_a_t, post_b_t X) XY
- func Direction(a, b XY) XY
- func Div(a, b XY) XY
- func DivX[X Float.Any | Int.Any](a XY, b X) XY
- func Floor(vec XY) XY
- func LengthLimited[X Float.Any](vec XY, length X) XY
- func Lerp[X Float.Any](vec, to XY, weight X) XY
- func Max(a, b XY) XY
- func Maxf[X Float.Any](v XY, with X) XY
- func Min(a, b XY) XY
- func Minf[X Float.Any | Int.Any](v XY, with X) XY
- func Move[X Float.Any](a, b XY, delta X) XY
- func Mul(a, b XY) XY
- func MulX[X Float.Any | Int.Any](a XY, b X) XY
- func Neg(v XY) XY
- func New[X Int.Any | Float.Any](x, y X) XY
- func Normalized(vec XY) XY
- func Orthogonal(vec XY) XY
- func Posmod[X Float.Any](vec XY, mod X) XY
- func PosmodVector(vec, mod XY) XY
- func Project(a, b XY) XY
- func Reflect(v, n XY) XY
- func Rotated(v XY, by Angle.Radians) XY
- func Round(v XY) XY
- func Sign(vec XY) XY
- func Slerp(vec, to XY, weight Angle.Radians) XY
- func Slide(v, n XY) XY
- func Snapped(v, step XY) XY
- func Snappedf[X Float.Any](v XY, step X) XY
- func Sub(a, b XY) XY
- func SubX[X Float.Any | Int.Any](a XY, b X) XY
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 Cross ¶
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 DistanceSquared ¶
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 ¶
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 IsApproximatelyEqual ¶
IsAproximatelyEqual returns true if this vector and to are approximately equal, by running [IsAproximatelyEqual] on each component.
func IsApproximatelyZero ¶
IsAproximatelyZero returns true if this vector is approximately equal to Vector2.Zero.
func IsFinite ¶
IsFinite returns true if this vector is finite, by calling IsFinite on each component.
func IsNormalized ¶
IsNormalized returns true if the vector is normalized, i.e. its length is approximately equal to 1.
func LengthSquared ¶
LengthSquared returns the squared length (squared magnitude) of this vector.
func Less ¶
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
type XY ¶
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 BezierDerivative ¶
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 ¶
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 CubicInterpolate ¶
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 ¶
Direction returns the normalized vector pointing from this vector to to. This is equivalent to using (b - a).Normalized().
func Floor ¶
Floor returns a new vector with all components rounded down (towards negative infinity).
func LengthLimited ¶
LengthLimited returns the vector with a maximum length by limiting its length to length.
func Lerp ¶
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 ¶
Max returns the component-wise minimum of this and with, equivalent to
Vector2.New(max(x, with.x), max(y, with.y))
func Maxf ¶
Maxf returns the component-wise maximum of this and with, equivalent to
Vector2.New(max(x, with), max(y, with)).
func Min ¶
Min returns the component-wise minimum of this and with, equivalent to
Vector2.New(min(x, with.x), min(y, with.y))
func Minf ¶
Minf returns the component-wise minimum of this and with, equivalent to
Vector2.New(min(x, with), min(y, with)).
func Move ¶
Move returns a new vector moved toward to by the fixed delta amount. Will not go past the final value.
func Normalized ¶
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 ¶
Orthogonal returns a perpendicular vector rotated 90 degrees counter-clockwise compared to the original, with the same length.
func Posmod ¶
Posmode returns a vector composed of the [Fposmod] of this vector's components and [Mod].
func PosmodVector ¶
Posmod returns a vector composed of the [Fposmod] of this vector's components and [Mod].
func Reflect ¶
Reflect returns the result of reflecting the vector from a line defined by the given direction vector n.
func Round ¶
Round returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.
func Sign ¶
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 ¶
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 ¶
Slide returns the result of sliding the vector along a plane defined by the given normal.
func Snapped ¶
Snapped returns a new vector with all components snapped to the nearest multiple of step.