Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Vector3

struct in Math

Description

(code of this item is picked from Godot Engine in compliance with MIT license).

3-element structure that can be used to represent positions in 3D space or any other pair of numeric values.

Constructors

SignatureDescription
( float x, float y, float z )Constructs a new Vector3 with the given components.

Methods

Return TypeSignatureDescription
voidDeconstruct ( float x, float y, float z )Helper method for deconstruction into a tuple.
Vector3Abs ( )Returns a new vector with all components in absolute values (i.e. positive).
floatAngleTo ( Vector3 to )Returns the unsigned minimum angle to the given vector, in radians.
Vector3Bounce ( Vector3 normal )Returns this vector "bounced off" from a plane defined by the given normal.
Vector3Ceil ( )Returns a new vector with all components rounded up (towards positive infinity).
Vector3Clamp ( Vector3 min, Vector3 max )Returns a new vector with all components clamped between the components of name and name using Mathf.Clamp(real_t, real_t, real_t) .
Vector3Clamp ( float min, float max )Returns a new vector with all components clamped between the name and name using Mathf.Clamp(real_t, real_t, real_t) .
Vector3Cross ( Vector3 with )Returns the cross product of this vector and name .
Vector3CubicInterpolate ( Vector3 b, Vector3 preA, Vector3 postB, float weight )Performs a cubic interpolation between vectors name , this vector, name , and name , by the given amount name .
Vector3CubicInterpolateInTime ( Vector3 b, Vector3 preA, Vector3 postB, float weight, float t, float preAT, float postBT )Performs a cubic interpolation between vectors name , this vector, name , and name , by the given amount name . It can perform smoother interpolation than CubicInterpolate by the time values.
Vector3BezierInterpolate ( Vector3 control1, Vector3 control2, Vector3 end, float t )Returns the point at the given name on a one-dimensional Bezier curve defined by this vector and the given name , name , and name points.
Vector3BezierDerivative ( Vector3 control1, Vector3 control2, Vector3 end, float t )Returns the derivative at the given name on the Bezier curve defined by this vector and the given name , name , and name points.
Vector3DirectionTo ( Vector3 to )Returns the normalized vector pointing from this vector to name .
floatDistanceSquaredTo ( Vector3 to )Returns the squared distance between this vector and name . This method runs faster than DistanceTo , so prefer it if you need to compare vectors or need the squared distance for some formula.
floatDistanceTo ( Vector3 to )Returns the distance between this vector and name .
floatDot ( Vector3 with )Returns the dot product of this vector and name .
Vector3Floor ( )Returns a new vector with all components rounded down (towards negative infinity).
Vector3Inverse ( )Returns the inverse of this vector. This is the same as new Vector3(1 / v.X, 1 / v.Y, 1 / v.Z) .
boolIsFinite ( )Returns true if this vector is finite, by calling Mathf.IsFinite(real_t) on each component.
boolIsNormalized ( )Returns true if the vector is normalized, and false otherwise.
floatLength ( )Returns the length (magnitude) of this vector.
floatLengthSquared ( )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 length for some formula.
Vector3Lerp ( Vector3 to, float weight )Returns the result of the linear interpolation between this vector and name by amount name .
Vector3LimitLength ( float length )Returns the vector with a maximum length by limiting its length to name .
Vector3Max ( Vector3 with )Returns the result of the component-wise maximum between this vector and name . Equivalent to new Vector3(Mathf.Max(X, with.X), Mathf.Max(Y, with.Y), Mathf.Max(Z, with.Z)) .
Vector3Max ( float with )Returns the result of the component-wise maximum between this vector and name . Equivalent to new Vector3(Mathf.Max(X, with), Mathf.Max(Y, with), Mathf.Max(Z, with)) .
Vector3Min ( Vector3 with )Returns the result of the component-wise minimum between this vector and name . Equivalent to new Vector3(Mathf.Min(X, with.X), Mathf.Min(Y, with.Y), Mathf.Min(Z, with.Z)) .
AxisMaxAxisIndex ( )Returns the axis of the vector's highest value. See Axis . If all components are equal, this method returns Axis.X .
AxisMinAxisIndex ( )Returns the axis of the vector's lowest value. See Axis . If all components are equal, this method returns Axis.Z .
Vector3MoveToward ( Vector3 to, float delta )Moves this vector toward name by the fixed name amount.
Vector3Normalized ( )Returns the vector scaled to unit length. Equivalent to v / v.Length() .
BasisOuter ( Vector3 with )Returns the outer product with name .
Vector3PosMod ( float mod )Returns a vector composed of the Mathf.PosMod(real_t, real_t) of this vector's components and name .
Vector3PosMod ( Vector3 modv )Returns a vector composed of the Mathf.PosMod(real_t, real_t) of this vector's components and name 's components.
Vector3Project ( Vector3 onNormal )Returns a new vector resulting from projecting this vector onto the given vector name . The resulting new vector is parallel to name . See also Slide . Note: If the vector name is a zero vector, the components of the resulting new vector will be real_t.NaN .
Vector3Reflect ( Vector3 normal )Returns this vector reflected from a plane defined by the given name .
Vector3Rotated ( Vector3 axis, float angle )Rotates this vector around a given name vector by name (in radians). The name vector must be a normalized vector.
Vector3Round ( )Returns this vector with all components rounded to the nearest integer, with halfway cases rounded towards the nearest multiple of two.
Vector3Sign ( )Returns a vector with each component set to one or negative one, depending on the signs of this vector's components, or zero if the component is zero, by calling Mathf.Sign(real_t) on each component.
floatSignedAngleTo ( Vector3 to, Vector3 axis )Returns the signed angle to the given vector, in radians. The sign of the angle is positive in a counter-clockwise direction and negative in a clockwise direction when viewed from the side specified by the name .
Vector3Slerp ( Vector3 to, float weight )Returns the result of the spherical linear interpolation between this vector and name by amount name . 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 .
Vector3Slide ( Vector3 normal )Returns a new vector resulting from sliding this vector along a plane with normal name . The resulting new vector is perpendicular to name , and is equivalent to this vector minus its projection on name . See also Project . Note: The vector name must be normalized. See also Normalized .
Vector3Snapped ( Vector3 step )Returns a new vector with each component snapped to the nearest multiple of the corresponding component in name . This can also be used to round to an arbitrary number of decimals.
Vector3Snapped ( float step )Returns a new vector with each component snapped to the nearest multiple of name . This can also be used to round to an arbitrary number of decimals.
boolEquals ( object? obj )Returns true if the vector is exactly equal to the given object ( name ). Note: Due to floating-point precision errors, consider using IsEqualApprox instead, which is more reliable.
boolEquals ( Vector3 other )Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox instead, which is more reliable.
boolIsEqualApprox ( Vector3 other )Returns true if this vector and name are approximately equal, by running Mathf.IsEqualApprox(real_t, real_t) on each component.
boolIsZeroApprox ( )Returns true if this vector's values are approximately zero, by running Mathf.IsZeroApprox(real_t) on each component. This method is faster than using IsEqualApprox with one value as a zero vector.
intGetHashCode ( )Serves as the hash function for Vector3 .
stringToString ( )Converts this Vector3 to a string.
stringToString ( string? format )Converts this Vector3 to a string with the given name .

Constants

NameTypeDescriptionInitializer
ZeroVector3Zero vector, a vector with all components set to 0 . Equivalent to new Vector3(0, 0, 0) .new(0, 0, 0)
OneVector3One vector, a vector with all components set to 1 . Equivalent to new Vector3(1, 1, 1) .new(1, 1, 1)
InfVector3Infinity vector, a vector with all components set to Mathf.Inf . Equivalent to new Vector3(Mathf.Inf, Mathf.Inf, Mathf.Inf) .new(Mathf.Inf, Mathf.Inf, Mathf.Inf)
UpVector3Up unit vector. Equivalent to new Vector3(0, 1, 0) .new(0, 1, 0)
DownVector3Down unit vector. Equivalent to new Vector3(0, -1, 0) .new(0, -1, 0)
RightVector3Right unit vector. Represents the local direction of right, and the global direction of east. Equivalent to new Vector3(1, 0, 0) .new(-1, 0, 0)
LeftVector3Left unit vector. Represents the local direction of left, and the global direction of west. Equivalent to new Vector3(-1, 0, 0) .new(1, 0, 0)
ForwardVector3Forward unit vector. Represents the local direction of forward, and the global direction of north. Equivalent to new Vector3(0, 0, -1) .new(0, 0, 1)
BackVector3Back unit vector. Represents the local direction of back, and the global direction of south. Equivalent to new Vector3(0, 0, 1) .new(0, 0, -1)
ModelLeftVector3Unit vector pointing towards the left side of imported 3D assets.new(1, 0, 0)
ModelRightVector3Unit vector pointing towards the right side of imported 3D assets.new(-1, 0, 0)
ModelTopVector3Unit vector pointing towards the top side (up) of imported 3D assets.new(0, 1, 0)
ModelBottomVector3Unit vector pointing towards the bottom side (down) of imported 3D assets.new(0, -1, 0)
ModelFrontVector3Unit vector pointing towards the front side (facing forward) of imported 3D assets.new(0, 0, 1)
ModelRearVector3Unit vector pointing towards the rear side (back) of imported 3D assets.new(0, 0, -1)

Operators

Return TypeSignatureDescription
Vector3+ ( Vector3 left, Vector3 right )Adds each component of the Vector3 with the components of the given Vector3 .
Vector3- ( Vector3 left, Vector3 right )Subtracts each component of the Vector3 by the components of the given Vector3 .
Vector3- ( Vector3 vec )Returns the negative value of the Vector3 . This is the same as writing new Vector3(-v.X, -v.Y, -v.Z) . This operation flips the direction of the vector while keeping the same magnitude. With floats, the number zero can be either positive or negative.
Vector3* ( Vector3 vec, float scale )Multiplies each component of the Vector3 by the given real_t .
Vector3* ( float scale, Vector3 vec )Multiplies each component of the Vector3 by the given real_t .
Vector3* ( Vector3 left, Vector3 right )Multiplies each component of the Vector3 by the components of the given Vector3 .
Vector3/ ( Vector3 vec, float divisor )Divides each component of the Vector3 by the given real_t .
Vector3/ ( Vector3 vec, Vector3 divisorv )Divides each component of the Vector3 by the components of the given Vector3 .
Vector3% ( Vector3 vec, float divisor )Gets the remainder of each component of the Vector3 with the components of the given real_t . This operation uses truncated division, which is often not desired as it does not work well with negative numbers. Consider using PosMod instead if you want to handle negative numbers.
Vector3% ( Vector3 vec, Vector3 divisorv )Gets the remainder of each component of the Vector3 with the components of the given Vector3 . This operation uses truncated division, which is often not desired as it does not work well with negative numbers. Consider using PosMod instead if you want to handle negative numbers.
bool== ( Vector3 left, Vector3 right )Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox instead, which is more reliable.
bool!= ( Vector3 left, Vector3 right )Returns true if the vectors are not equal. Note: Due to floating-point precision errors, consider using IsEqualApprox instead, which is more reliable.
bool< ( Vector3 left, Vector3 right )Compares two Vector3 vectors by first checking if the X value of the name vector is less than the X value of the name vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors, and then with the Z values. This operator is useful for sorting vectors.
bool> ( Vector3 left, Vector3 right )Compares two Vector3 vectors by first checking if the X value of the name vector is greater than the X value of the name vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors, and then with the Z values. This operator is useful for sorting vectors.
bool<= ( Vector3 left, Vector3 right )Compares two Vector3 vectors by first checking if the X value of the name vector is less than or equal to the X value of the name vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors, and then with the Z values. This operator is useful for sorting vectors.
bool>= ( Vector3 left, Vector3 right )Compares two Vector3 vectors by first checking if the X value of the name vector is greater than or equal to the X value of the name vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors, and then with the Z values. This operator is useful for sorting vectors.