Struct Vector3
- Namespace
- Godot
- Assembly
- GodotSharp.dll
3-element structure that can be used to represent positions in 3D space or any other pair of numeric values.
public struct Vector3 : IEquatable<Vector3>
- Implements
- Inherited Members
Constructors
Vector3(float, float, float)
Constructs a new Vector3 with the given components.
public Vector3(float x, float y, float z)
Parameters
x
floatThe vector's X component.
y
floatThe vector's Y component.
z
floatThe vector's Z component.
Fields
X
The vector's X component. Also accessible by using the index position [0]
.
public float X
Field Value
Y
The vector's Y component. Also accessible by using the index position [1]
.
public float Y
Field Value
Z
The vector's Z component. Also accessible by using the index position [2]
.
public float Z
Field Value
Properties
Back
Back unit vector. Represents the local direction of back, and the global direction of south.
public static Vector3 Back { get; }
Property Value
- Vector3
Equivalent to
new Vector3(0, 0, 1)
.
Down
Down unit vector.
public static Vector3 Down { get; }
Property Value
- Vector3
Equivalent to
new Vector3(0, -1, 0)
.
Forward
Forward unit vector. Represents the local direction of forward, and the global direction of north.
public static Vector3 Forward { get; }
Property Value
- Vector3
Equivalent to
new Vector3(0, 0, -1)
.
Inf
Infinity vector, a vector with all components set to Inf.
public static Vector3 Inf { get; }
Property Value
- Vector3
Equivalent to
new Vector3(Mathf.Inf, Mathf.Inf, Mathf.Inf)
.
this[int]
Access vector components using their index.
public float this[int index] { readonly get; set; }
Parameters
index
int
Property Value
Exceptions
- ArgumentOutOfRangeException
index
is not 0, 1 or 2.
Left
Left unit vector. Represents the local direction of left, and the global direction of west.
public static Vector3 Left { get; }
Property Value
- Vector3
Equivalent to
new Vector3(-1, 0, 0)
.
ModelBottom
Unit vector pointing towards the bottom side (down) of imported 3D assets.
public static Vector3 ModelBottom { get; }
Property Value
ModelFront
Unit vector pointing towards the front side (facing forward) of imported 3D assets.
public static Vector3 ModelFront { get; }
Property Value
ModelLeft
Unit vector pointing towards the left side of imported 3D assets.
public static Vector3 ModelLeft { get; }
Property Value
ModelRear
Unit vector pointing towards the rear side (back) of imported 3D assets.
public static Vector3 ModelRear { get; }
Property Value
ModelRight
Unit vector pointing towards the right side of imported 3D assets.
public static Vector3 ModelRight { get; }
Property Value
ModelTop
Unit vector pointing towards the top side (up) of imported 3D assets.
public static Vector3 ModelTop { get; }
Property Value
One
One vector, a vector with all components set to 1
.
public static Vector3 One { get; }
Property Value
- Vector3
Equivalent to
new Vector3(1, 1, 1)
.
Right
Right unit vector. Represents the local direction of right, and the global direction of east.
public static Vector3 Right { get; }
Property Value
- Vector3
Equivalent to
new Vector3(1, 0, 0)
.
Up
Up unit vector.
public static Vector3 Up { get; }
Property Value
- Vector3
Equivalent to
new Vector3(0, 1, 0)
.
Zero
Zero vector, a vector with all components set to 0
.
public static Vector3 Zero { get; }
Property Value
- Vector3
Equivalent to
new Vector3(0, 0, 0)
.
Methods
Abs()
Returns a new vector with all components in absolute values (i.e. positive).
public readonly Vector3 Abs()
Returns
- Vector3
A vector with Abs(float) called on each component.
AngleTo(Vector3)
Returns the unsigned minimum angle to the given vector, in radians.
public readonly float AngleTo(Vector3 to)
Parameters
to
Vector3The other vector to compare this vector to.
Returns
- float
The unsigned angle between the two vectors, in radians.
BezierDerivative(Vector3, Vector3, Vector3, float)
Returns the derivative at the given t
on the Bezier curve defined by this vector
and the given control1
, control2
, and end
points.
public readonly Vector3 BezierDerivative(Vector3 control1, Vector3 control2, Vector3 end, float t)
Parameters
control1
Vector3Control point that defines the bezier curve.
control2
Vector3Control point that defines the bezier curve.
end
Vector3The destination value for the interpolation.
t
floatA value on the range of 0.0 to 1.0, representing the amount of interpolation.
Returns
- Vector3
The resulting value of the interpolation.
BezierInterpolate(Vector3, Vector3, Vector3, float)
Returns the point at the given t
on a one-dimensional Bezier curve defined by this vector
and the given control1
, control2
, and end
points.
public readonly Vector3 BezierInterpolate(Vector3 control1, Vector3 control2, Vector3 end, float t)
Parameters
control1
Vector3Control point that defines the bezier curve.
control2
Vector3Control point that defines the bezier curve.
end
Vector3The destination vector.
t
floatA value on the range of 0.0 to 1.0, representing the amount of interpolation.
Returns
- Vector3
The interpolated vector.
Bounce(Vector3)
Returns this vector "bounced off" from a plane defined by the given normal.
public readonly Vector3 Bounce(Vector3 normal)
Parameters
normal
Vector3The normal vector defining the plane to bounce off. Must be normalized.
Returns
- Vector3
The bounced vector.
Ceil()
Returns a new vector with all components rounded up (towards positive infinity).
public readonly Vector3 Ceil()
Returns
- Vector3
A vector with Ceil(float) called on each component.
Clamp(Vector3, Vector3)
Returns a new vector with all components clamped between the
components of min
and max
using
Clamp(float, float, float).
public readonly Vector3 Clamp(Vector3 min, Vector3 max)
Parameters
min
Vector3The vector with minimum allowed values.
max
Vector3The vector with maximum allowed values.
Returns
- Vector3
The vector with all components clamped.
Cross(Vector3)
Returns the cross product of this vector and with
.
public readonly Vector3 Cross(Vector3 with)
Parameters
with
Vector3The other vector.
Returns
- Vector3
The cross product vector.
CubicInterpolate(Vector3, Vector3, Vector3, float)
Performs a cubic interpolation between vectors preA
, this vector,
b
, and postB
, by the given amount weight
.
public readonly Vector3 CubicInterpolate(Vector3 b, Vector3 preA, Vector3 postB, float weight)
Parameters
b
Vector3The destination vector.
preA
Vector3A vector before this vector.
postB
Vector3A vector after
b
.weight
floatA value on the range of 0.0 to 1.0, representing the amount of interpolation.
Returns
- Vector3
The interpolated vector.
CubicInterpolateInTime(Vector3, Vector3, Vector3, float, float, float, float)
Performs a cubic interpolation between vectors preA
, this vector,
b
, and postB
, by the given amount weight
.
It can perform smoother interpolation than CubicInterpolate(Vector3, Vector3, Vector3, float)
by the time values.
public readonly Vector3 CubicInterpolateInTime(Vector3 b, Vector3 preA, Vector3 postB, float weight, float t, float preAT, float postBT)
Parameters
b
Vector3The destination vector.
preA
Vector3A vector before this vector.
postB
Vector3A vector after
b
.weight
floatA value on the range of 0.0 to 1.0, representing the amount of interpolation.
t
floatpreAT
floatpostBT
float
Returns
- Vector3
The interpolated vector.
Deconstruct(out float, out float, out float)
Helper method for deconstruction into a tuple.
public readonly void Deconstruct(out float x, out float y, out float z)
Parameters
DirectionTo(Vector3)
Returns the normalized vector pointing from this vector to to
.
public readonly Vector3 DirectionTo(Vector3 to)
Parameters
to
Vector3The other vector to point towards.
Returns
- Vector3
The direction from this vector to
to
.
DistanceSquaredTo(Vector3)
Returns the squared distance between this vector and to
.
This method runs faster than DistanceTo(Vector3), so prefer it if
you need to compare vectors or need the squared distance for some formula.
public readonly float DistanceSquaredTo(Vector3 to)
Parameters
to
Vector3The other vector to use.
Returns
- float
The squared distance between the two vectors.
DistanceTo(Vector3)
Returns the distance between this vector and to
.
public readonly float DistanceTo(Vector3 to)
Parameters
to
Vector3The other vector to use.
Returns
- float
The distance between the two vectors.
- See Also
Dot(Vector3)
Returns the dot product of this vector and with
.
public readonly float Dot(Vector3 with)
Parameters
with
Vector3The other vector to use.
Returns
- float
The dot product of the two vectors.
Equals(Vector3)
Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector3) instead, which is more reliable.
public readonly bool Equals(Vector3 other)
Parameters
other
Vector3The other vector.
Returns
- bool
Whether or not the vectors are exactly equal.
Equals(object)
Returns true if the vector is exactly equal
to the given object (obj
).
Note: Due to floating-point precision errors, consider using
IsEqualApprox(Vector3) instead, which is more reliable.
public override readonly bool Equals(object obj)
Parameters
obj
objectThe object to compare with.
Returns
- bool
Whether or not the vector and the object are equal.
Floor()
Returns a new vector with all components rounded down (towards negative infinity).
public readonly Vector3 Floor()
Returns
- Vector3
A vector with Floor(float) called on each component.
GetHashCode()
Serves as the hash function for Vector3.
public override readonly int GetHashCode()
Returns
- int
A hash code for this vector.
Inverse()
Returns the inverse of this vector. This is the same as new Vector3(1 / v.X, 1 / v.Y, 1 / v.Z)
.
public readonly Vector3 Inverse()
Returns
- Vector3
The inverse of this vector.
IsEqualApprox(Vector3)
Returns true if this vector and other
are approximately equal,
by running IsEqualApprox(float, float) on each component.
public readonly bool IsEqualApprox(Vector3 other)
Parameters
other
Vector3The other vector to compare.
Returns
- bool
Whether or not the vectors are approximately equal.
IsFinite()
Returns true if this vector is finite, by calling IsFinite(float) on each component.
public readonly bool IsFinite()
Returns
- bool
Whether this vector is finite or not.
IsNormalized()
public readonly bool IsNormalized()
Returns
IsZeroApprox()
Returns true if this vector's values are approximately zero, by running IsZeroApprox(float) on each component. This method is faster than using IsEqualApprox(Vector3) with one value as a zero vector.
public readonly bool IsZeroApprox()
Returns
- bool
Whether or not the vector is approximately zero.
Length()
Returns the length (magnitude) of this vector.
public readonly float Length()
Returns
- float
The length of this vector.
- See Also
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 length for some formula.
public readonly float LengthSquared()
Returns
- float
The squared length of this vector.
Lerp(Vector3, float)
Returns the result of the linear interpolation between
this vector and to
by amount weight
.
public readonly Vector3 Lerp(Vector3 to, float weight)
Parameters
to
Vector3The destination vector for interpolation.
weight
floatA value on the range of 0.0 to 1.0, representing the amount of interpolation.
Returns
- Vector3
The resulting vector of the interpolation.
LimitLength(float)
Returns the vector with a maximum length by limiting its length to length
.
public readonly Vector3 LimitLength(float length = 1)
Parameters
length
floatThe length to limit to.
Returns
- Vector3
The vector with its length limited.
MaxAxisIndex()
Returns the axis of the vector's highest value. See Vector3.Axis. If all components are equal, this method returns X.
public readonly Vector3.Axis MaxAxisIndex()
Returns
MinAxisIndex()
Returns the axis of the vector's lowest value. See Vector3.Axis. If all components are equal, this method returns Z.
public readonly Vector3.Axis MinAxisIndex()
Returns
MoveToward(Vector3, float)
Moves this vector toward to
by the fixed delta
amount.
public readonly Vector3 MoveToward(Vector3 to, float delta)
Parameters
Returns
- Vector3
The resulting vector.
Normalized()
Returns the vector scaled to unit length. Equivalent to v / v.Length()
.
public readonly Vector3 Normalized()
Returns
- Vector3
A normalized version of the vector.
Outer(Vector3)
Returns the outer product with with
.
public readonly Basis Outer(Vector3 with)
Parameters
with
Vector3The other vector.
Returns
PosMod(Vector3)
Returns a vector composed of the PosMod(float, float) of this vector's components
and modv
's components.
public readonly Vector3 PosMod(Vector3 modv)
Parameters
modv
Vector3A vector representing the divisors of the operation.
Returns
- Vector3
A vector with each component PosMod(float, float) by
modv
's components.
PosMod(float)
Returns a vector composed of the PosMod(float, float) of this vector's components
and mod
.
public readonly Vector3 PosMod(float mod)
Parameters
mod
floatA value representing the divisor of the operation.
Returns
- Vector3
A vector with each component PosMod(float, float) by
mod
.
Project(Vector3)
Returns this vector projected onto another vector onNormal
.
public readonly Vector3 Project(Vector3 onNormal)
Parameters
onNormal
Vector3The vector to project onto.
Returns
- Vector3
The projected vector.
Reflect(Vector3)
Returns this vector reflected from a plane defined by the given normal
.
public readonly Vector3 Reflect(Vector3 normal)
Parameters
normal
Vector3The normal vector defining the plane to reflect from. Must be normalized.
Returns
- Vector3
The reflected vector.
Rotated(Vector3, float)
Rotates this vector around a given axis
vector by angle
(in radians).
The axis
vector must be a normalized vector.
public readonly Vector3 Rotated(Vector3 axis, float angle)
Parameters
axis
Vector3The vector to rotate around. Must be normalized.
angle
floatThe angle to rotate by, in radians.
Returns
- Vector3
The rotated vector.
Round()
Returns this vector with all components rounded to the nearest integer, with halfway cases rounded towards the nearest multiple of two.
public readonly Vector3 Round()
Returns
- Vector3
The rounded vector.
Sign()
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 Sign(float) on each component.
public readonly Vector3 Sign()
Returns
- Vector3
A vector with all components as either
1
,-1
, or0
.
SignedAngleTo(Vector3, Vector3)
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 axis
.
public readonly float SignedAngleTo(Vector3 to, Vector3 axis)
Parameters
to
Vector3The other vector to compare this vector to.
axis
Vector3The reference axis to use for the angle sign.
Returns
- float
The signed angle between the two vectors, in radians.
Slerp(Vector3, float)
Returns the result of the spherical linear interpolation between
this vector and to
by amount weight
.
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(Vector3, float).
public readonly Vector3 Slerp(Vector3 to, float weight)
Parameters
to
Vector3The destination vector for interpolation.
weight
floatA value on the range of 0.0 to 1.0, representing the amount of interpolation.
Returns
- Vector3
The resulting vector of the interpolation.
Slide(Vector3)
Returns this vector slid along a plane defined by the given normal
.
public readonly Vector3 Slide(Vector3 normal)
Parameters
normal
Vector3The normal vector defining the plane to slide on.
Returns
- Vector3
The slid vector.
Snapped(Vector3)
Returns this vector with each component snapped to the nearest multiple of step
.
This can also be used to round to an arbitrary number of decimals.
public readonly Vector3 Snapped(Vector3 step)
Parameters
step
Vector3A vector value representing the step size to snap to.
Returns
- Vector3
The snapped vector.
ToString()
Converts this Vector3 to a string.
public override readonly string ToString()
Returns
- string
A string representation of this vector.
ToString(string)
Converts this Vector3 to a string with the given format
.
public readonly string ToString(string format)
Parameters
format
string
Returns
- string
A string representation of this vector.
Operators
operator +(Vector3, Vector3)
public static Vector3 operator +(Vector3 left, Vector3 right)
Parameters
Returns
- Vector3
The added vector.
operator /(Vector3, Vector3)
public static Vector3 operator /(Vector3 vec, Vector3 divisorv)
Parameters
Returns
- Vector3
The divided vector.
operator /(Vector3, float)
public static Vector3 operator /(Vector3 vec, float divisor)
Parameters
Returns
- Vector3
The divided vector.
operator ==(Vector3, Vector3)
Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector3) instead, which is more reliable.
public static bool operator ==(Vector3 left, Vector3 right)
Parameters
Returns
- bool
Whether or not the vectors are exactly equal.
operator >(Vector3, Vector3)
Compares two Vector3 vectors by first checking if
the X value of the left
vector is greater 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, and then with the Z values.
This operator is useful for sorting vectors.
public static bool operator >(Vector3 left, Vector3 right)
Parameters
Returns
- bool
Whether or not the left is greater than the right.
operator >=(Vector3, Vector3)
Compares two Vector3 vectors by first checking if
the X value of the left
vector is greater than
or equal to 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, and then with the Z values.
This operator is useful for sorting vectors.
public static bool operator >=(Vector3 left, Vector3 right)
Parameters
Returns
- bool
Whether or not the left is greater than or equal to the right.
operator !=(Vector3, Vector3)
Returns true if the vectors are not equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector3) instead, which is more reliable.
public static bool operator !=(Vector3 left, Vector3 right)
Parameters
Returns
- bool
Whether or not the vectors are not equal.
operator <(Vector3, Vector3)
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 values of the two vectors, and then with the Z values.
This operator is useful for sorting vectors.
public static bool operator <(Vector3 left, Vector3 right)
Parameters
Returns
- bool
Whether or not the left is less than the right.
operator <=(Vector3, Vector3)
Compares two Vector3 vectors by first checking if
the X value of the left
vector is less than
or equal to 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, and then with the Z values.
This operator is useful for sorting vectors.
public static bool operator <=(Vector3 left, Vector3 right)
Parameters
Returns
- bool
Whether or not the left is less than or equal to the right.
operator %(Vector3, Vector3)
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(Vector3) instead if you want to handle negative numbers.
public static Vector3 operator %(Vector3 vec, Vector3 divisorv)
Parameters
Returns
- Vector3
The remainder vector.
Examples
GD.Print(new Vector3(10, -20, 30) % new Vector3(7, 8, 9)); // Prints "(3, -4, 3)"
operator %(Vector3, float)
Gets the remainder of each component of the Vector3 with the components of the given float. This operation uses truncated division, which is often not desired as it does not work well with negative numbers. Consider using PosMod(float) instead if you want to handle negative numbers.
public static Vector3 operator %(Vector3 vec, float divisor)
Parameters
Returns
- Vector3
The remainder vector.
Examples
GD.Print(new Vector3(10, -20, 30) % 7); // Prints "(3, -6, 2)"
operator *(Vector3, Vector3)
public static Vector3 operator *(Vector3 left, Vector3 right)
Parameters
Returns
- Vector3
The multiplied vector.
operator *(Vector3, float)
public static Vector3 operator *(Vector3 vec, float scale)
Parameters
Returns
- Vector3
The multiplied vector.
operator *(float, Vector3)
public static Vector3 operator *(float scale, Vector3 vec)
Parameters
Returns
- Vector3
The multiplied vector.
operator -(Vector3, Vector3)
public static Vector3 operator -(Vector3 left, Vector3 right)
Parameters
Returns
- Vector3
The subtracted vector.
operator -(Vector3)
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.
public static Vector3 operator -(Vector3 vec)
Parameters
vec
Vector3The vector to negate/flip.
Returns
- Vector3
The negated/flipped vector.