Documentation ¶
Overview ¶
Package Vector3 provides a 3D vector using floating-point coordinates.
Index ¶
- Variables
- func AngleBetween(a, b XYZ) Angle.Radians
- func AsArray(vec XYZ) [3]Float.X
- func Distance(v, to XYZ) Float.X
- func DistanceSquared(v, to XYZ) Float.X
- func Dot(a, b XYZ) Float.X
- func Index[I Int.Any](v XYZ, i I) Float.X
- func IsApproximatelyEqual(a, b XYZ) bool
- func IsApproximatelyZero(v XYZ) bool
- func IsFinite(v XYZ) bool
- func IsNormalized(v XYZ) bool
- func Length(v XYZ) Float.X
- func LengthSquared(v XYZ) Float.X
- func Less(a, b XYZ) bool
- func OctahedronEncode(v XYZ) Vector2.XY
- func SignedAngle(v, to XYZ, axis XYZ) Angle.Radians
- type Axis
- type XYZ
- func Abs(v XYZ) XYZ
- func Add(a, b XYZ) XYZ
- func AddX[X Float.Any | Int.Any](a XYZ, b X) XYZ
- func BezierDerivative[X Float.Any](v, control1, control2, end XYZ, t X) XYZ
- func BezierInterpolate[X Float.Any](v, control1, control2, end XYZ, t X) XYZ
- func Bounce(v, n XYZ) XYZ
- func Ceil(v XYZ) XYZ
- func Clamp(v, min, max XYZ) XYZ
- func Clampf[X Float.Any](v XYZ, min, max X) XYZ
- func Cross(v, with XYZ) XYZ
- func CubicInterpolate[X Float.Any](v, b, preA, postB XYZ, weight X) XYZ
- func CubicInterpolateInTime[X Float.Any](v, b, preA, postB XYZ, weight, b_t, pre_a_t, post_b_t X) XYZ
- func Direction(v, to XYZ) XYZ
- func Div(a, b XYZ) XYZ
- func DivX[X Float.Any | Int.Any](a XYZ, b X) XYZ
- func Floor(v XYZ) XYZ
- func Inverse(v XYZ) XYZ
- func LengthLimited[X Float.Any](v XYZ, length X) XYZ
- func Lerp[X Float.Any](v, to XYZ, weight X) XYZ
- func Max(a, b XYZ) XYZ
- func Maxf[X Float.Any](v XYZ, with X) XYZ
- func Min(a, b XYZ) XYZ
- func Minf[X Float.Any](v XYZ, with X) XYZ
- func Move[X Float.Any](a, b XYZ, delta X) XYZ
- func Mul(a, b XYZ) XYZ
- func MulX[X Float.Any | Int.Any](a XYZ, b X) XYZ
- func Neg(v XYZ) XYZ
- func New[X Int.Any | Float.Any](x, y, z X) XYZ
- func Normalized(v XYZ) XYZ
- func OctahedronDecode(uv Vector2.XY) XYZ
- func Posmod(v, mod XYZ) XYZ
- func Posmodf[X Float.Any](v XYZ, mod X) XYZ
- func Project(v, b XYZ) XYZ
- func Reflect(v, n XYZ) XYZ
- func Rotated(v, axis XYZ, angle Angle.Radians) XYZ
- func Round(v XYZ) XYZ
- func Sign(vec XYZ) XYZ
- func Slerp[X Float.Any](v, to XYZ, weight X) XYZ
- func Slide(v, n XYZ) XYZ
- func Snapped(v, step XYZ) XYZ
- func Snappedf[X Float.Any](v XYZ, step X) XYZ
- func Sub(a, b XYZ) XYZ
- func SubX[X Float.Any | Int.Any](a XYZ, b X) XYZ
Constants ¶
This section is empty.
Variables ¶
var ( Zero = XYZ{0, 0, 0} // Zero vector, a vector with all components set to 0. One = XYZ{1, 1, 1} // One vector, a vector with all components set to 1. Inf = XYZ{Float.Inf, Float.Inf, Float.Inf} // Inf vector, a vector with all components set to positive infinity. Left = XYZ{-1, 0, 0} // Left unit vector. Represents the local direction of left, and the global direction of west. Right = XYZ{1, 0, 0} // Right unit vector. Represents the local direction of right, and the global direction of east. Up = XYZ{0, 1, 0} // Up unit vector. Down = XYZ{0, -1, 0} // Down unit vector. // Forward unit vector. Represents the local direction of forward, and the global direction // of north. Keep in mind that the forward direction for lights, cameras, etc is different // from 3D assets like characters, which face towards the camera by convention. Use [ModelFront] // and similar constants when working in 3D asset space. Forward = XYZ{0, 0, -1} Back = XYZ{0, 0, 1} // Back unit vector. Represents the local direction of back, and the global direction of south. ModelLeft = XYZ{1, 0, 0} // Unit vector pointing towards the left side of imported 3D assets. ModelRight = XYZ{-1, 0, 0} // Unit vector pointing towards the right side of imported 3D assets. ModelTop = XYZ{0, 1, 0} // Unit vector pointing towards the top side of imported 3D assets. ModelBottom = XYZ{0, -1, 0} // Unit vector pointing towards the bottom side of imported 3D assets. ModelFront = XYZ{0, 0, 1} // Unit vector pointing towards the front side of imported 3D assets. ModelRear = XYZ{0, 0, -1} // Unit vector pointing towards the rear side of imported 3D assets. )
Functions ¶
func AngleBetween ¶
AngleBetween returns the unsigned minimum angle to the given vector, in radians.
func DistanceSquared ¶
DistanceSquared returns the squared distance between this vector and to.
This method runs faster than [Vector3.DistanceTo], 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: a.Dot(b) is equivalent to b.Dot(a).
func IsApproximatelyEqual ¶
IsApproximatelyEqual returns true if this vector and other are approximately equal, by running [Float.IsApproximatelyEqual] on each component.
func IsApproximatelyZero ¶
IsApproximatelyZero returns true if this vector is approximately equal to zero, by running [Float.IsApproximatelyZero] on each component.
func IsFinite ¶
IsFinite returns true if this vector's values are finite, by running [Float.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.
This method runs faster than length, so prefer it if you need to compare vectors or need the squared distance for some formula.
func Less ¶
Less compares two Vector3 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 and Z 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.
func OctahedronEncode ¶
OctahedronEncode returns the octahedral-encoded (oct32) form of this [Vector3] as a [Vector2]. Since a [Vector2] occupies 1/3 less memory compared to [Vector3], this form of compression can be used to pass greater amounts of normalized [Vector3]s without increasing storage or memory requirements. See also [Vector3.OctahedronDecode].
Note: OctahedronEncode can only be used for normalized vectors. OctahedronEncode does not check whether this [Vector3] is normalized, and will return a value that does not decompress to the original value if the [Vector3] is not normalized.
Note: Octahedral compression is lossy, although visual differences are rarely perceptible in real world scenarios.
Types ¶
type Axis ¶
type Axis int
type XYZ ¶
type XYZ = struct { X Float.X // The vector's X component. Y Float.X // The vector's Y component. Z Float.X // The vector's Z component. }
XYZ is a 3-element structure that can be used to represent 3D coordinates or any other triplet 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 [Vector3i.XYZ] 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 Clamp ¶
Clamp returns a new vector with all components clamped between the components of min and max, by running [Float.Clamp] on each component.
func Clampf ¶
Clampf returns a new vector with all components clamped between the components of min and max, by running [Float.Clamp] on each component.
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](v, b, preA, postB XYZ, weight, b_t, pre_a_t, post_b_t X) XYZ
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 [Vector3.CubicInterpolate] by the time values.
func Direction ¶
Direction returns the normalized vector pointing from this vector to to. This is equivalent to using Normalized(b - a).
func Floor ¶
Floor returns a new vector with all components rounded down (towards negative infinity).
func Inverse ¶
Inverse returns the inverse of the vector. This is the same as
XYZ{1/v.X,1/v.Y,1/v.Z}.
func LengthLimited ¶
LimitLength 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
Vector3.New(max(x, with.x), max(y, with.y), max(z, with.z))
func Maxf ¶
Maxf returns the component-wise maximum of this and with, equivalent to
Vector3.New(max(x, with), max(y, with), max(z, with)).
func Min ¶
Min returns the component-wise minimum of this and with, equivalent to
Vector3.New(min(x, with.x), min(y, with.y), min(z, with.z))
func Minf ¶
Minf returns the component-wise minimum of this and with, equivalent to
Vector3.New(min(x, with), min(y, with), min(z, with), min(w, 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 IsNormalized.
Note: This function may return incorrect values if the input vector length is near zero.
func OctahedronDecode ¶
OctahedronDecode returns the XYZ from an octahedral-compressed form created using OctahedronEncode (stored as a [Vector2.XY]).
func Posmodf ¶
Posmodf 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 plane defined by the given normal n.
func Rotated ¶
Rotated returns the result of rotating this vector around a given axis by angle (in radians). The axis must be a normalized vector.
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 Snapped ¶
Snapped returns a new vector with each component snapped to the nearest multiple of the corresponding component in step. This can also be used to round the components to an arbitrary number of decimals.