#[repr(C)]pub struct Vector3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
Expand description
General purposes 3D vector
Fields§
§x: f32
X coordinate in 3D space
y: f32
Y coordinate in 3D space
z: f32
Z coordinate in 3D space
Implementations§
Source§impl Vector3
impl Vector3
Sourcepub const UNIT_X: Self
pub const UNIT_X: Self
A vector with all components set to 0 except for the x component, which is set to 1.
Sourcepub const UNIT_Y: Self
pub const UNIT_Y: Self
A vector with all components set to 0 except for the y component, which is set to 1.
Sourcepub const UNIT_Z: Self
pub const UNIT_Z: Self
A vector with all components set to 0 except for the z component, which is set to 1.
Sourcepub const fn new(x: f32, y: f32, z: f32) -> Self
pub const fn new(x: f32, y: f32, z: f32) -> Self
Create a new 3D position with the given coordinates
Sourcepub const fn x_axis() -> Self
pub const fn x_axis() -> Self
Returns a vector with all components set to 0 except for the x component, which is set to 1.
Sourcepub const fn y_axis() -> Self
pub const fn y_axis() -> Self
Returns a vector with all components set to 0 except for the y component, which is set to 1.
Sourcepub const fn z_axis() -> Self
pub const fn z_axis() -> Self
Returns a vector with all components set to 0 except for the z component, which is set to 1.
Sourcepub const fn xy(&self) -> Vector2
pub const fn xy(&self) -> Vector2
Returns a 2D vector with the x and y coordinates of the 3D vector
Source§impl Vector3
impl Vector3
Sourcepub fn abs(&self) -> Self
pub fn abs(&self) -> Self
Returns a new vector with all components in absolute values (i.e. positive).
Sourcepub fn angle_to(&self, to: Self) -> f32
pub fn angle_to(&self, to: Self) -> f32
Returns the unsigned minimum angle to the given vector, in radians.
Sourcepub fn bounce(&self, n: Self) -> Self
pub fn bounce(&self, n: Self) -> Self
Returns the vector “bounced off” from a plane defined by the given normal n
.
Note: bounce performs the operation that most engines and frameworks call
reflect()
.
Sourcepub fn ceil(&self) -> Self
pub fn ceil(&self) -> Self
Returns a new vector with all components rounded up (towards positive infinity).
Sourcepub fn clamp(&self, min: f32, max: f32) -> Self
pub fn clamp(&self, min: f32, max: f32) -> Self
Returns a new vector with all components clamped between the components of min
and max
Sourcepub fn clampf(&self, min: f32, max: f32) -> Self
pub fn clampf(&self, min: f32, max: f32) -> Self
Returns a new vector with all components clamped between min
and max
Sourcepub const fn cross(&self, with: Self) -> Self
pub const fn cross(&self, with: Self) -> Self
Returns the cross product of this vector and with
.
This returns a vector perpendicular to both self
and with
, which would be the normal vector of the plane defined by the two vectors.
As there are two such vectors, in opposite directions, this method returns the vector defined by a right-handed coordinate system.
If the two vectors are parallel this returns an empty vector, making it useful for testing if two vectors are parallel.
Sourcepub fn direction_to(&self, to: Self) -> Self
pub fn direction_to(&self, to: Self) -> Self
Returns the normalized vector pointing from this vector to to
. This is equivalent to using (b - a).normalized()
.
Sourcepub fn distance_squared_to(&self, to: Self) -> f32
pub fn distance_squared_to(&self, to: Self) -> f32
Returns the squared distance between this vector and to
.
This method runs faster than Vector3::distance_to
, so prefer it if you need to compare vectors or need the squared distance for some formula.
Sourcepub fn distance_to(&self, to: Self) -> f32
pub fn distance_to(&self, to: Self) -> f32
Returns the distance between this vector and to
.
Sourcepub fn dot(&self, with: Self) -> f32
pub fn dot(&self, with: Self) -> f32
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 right 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 tob.dot(a)
.
Sourcepub fn floor(&self) -> Self
pub fn floor(&self) -> Self
Returns a new vector with all components rounded down (towards negative infinity).
Sourcepub const fn inverse(&self) -> Self
pub const fn inverse(&self) -> Self
Returns the inverse of the vector. This is the same as:
Vector3 {
x: 1.0 / self.x,
y: 1.0 / self.y,
z: 1.0 / self.z
}
Sourcepub fn is_normalized(&self) -> bool
pub fn is_normalized(&self) -> bool
Returns true if the vector is normalized, i.e. its length is approximately equal to 1.
Sourcepub const fn length_squared(&self) -> f32
pub const fn length_squared(&self) -> f32
Returns the squared length (squared magnitude) of this vector.
This method runs faster than Vector3::length
, so prefer it if you need to compare vectors or need the squared distance for some formula.
Sourcepub fn lerp(&self, to: Self, weight: f32) -> Self
pub fn lerp(&self, to: Self, weight: f32) -> Self
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.
Sourcepub fn limit_length(&self, max_length: f32) -> Self
pub fn limit_length(&self, max_length: f32) -> Self
Returns the vector with a maximum length by limiting its length to length
.
Sourcepub fn max(&self, with: Vector3) -> Self
pub fn max(&self, with: Vector3) -> Self
Returns the component-wise minimum of self
and with
, equivalent to:
Vector3 {
x: self.x.max(with.x),
y: self.y.max(with.y),
z: self.z.max(with.z),
}
Sourcepub fn maxf(&self, with: f32) -> Self
pub fn maxf(&self, with: f32) -> Self
Returns the component-wise maximum of self
and with
, equivalent to:
Vector3 {
x: self.x.max(with),
y: self.y.max(with),
z: self.z.max(with),
}
Sourcepub fn min(&self, with: Vector3) -> Self
pub fn min(&self, with: Vector3) -> Self
Returns the component-wise minimum of self
and with
, equivalent to:
Vector3 {
x: self.x.min(with.x),
y: self.y.min(with.y),
z: self.z.min(with.z),
}
Sourcepub fn minf(&self, with: f32) -> Self
pub fn minf(&self, with: f32) -> Self
Returns the component-wise minimum of self
and with
, equivalent to:
Vector3 {
x: self.x.min(with),
y: self.y.min(with),
z: self.z.min(with),
}
Sourcepub fn round(&self) -> Self
pub fn round(&self) -> Self
Returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.
Sourcepub fn rotate(&self, axis: RotateAxis, angle: RotateAmount) -> Self
pub fn rotate(&self, axis: RotateAxis, angle: RotateAmount) -> Self
Rotates the vector around the given axis by the given angle.
Trait Implementations§
Source§impl AddAssign<f32> for Vector3
impl AddAssign<f32> for Vector3
Source§fn add_assign(&mut self, scalar: f32)
fn add_assign(&mut self, scalar: f32)
+=
operation. Read moreSource§impl AddAssign for Vector3
impl AddAssign for Vector3
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
+=
operation. Read moreSource§impl DivAssign<f32> for Vector3
impl DivAssign<f32> for Vector3
Source§fn div_assign(&mut self, scalar: f32)
fn div_assign(&mut self, scalar: f32)
/=
operation. Read moreSource§impl DivAssign for Vector3
impl DivAssign for Vector3
Source§fn div_assign(&mut self, other: Self)
fn div_assign(&mut self, other: Self)
/=
operation. Read moreSource§impl MulAssign<f32> for Vector3
impl MulAssign<f32> for Vector3
Source§fn mul_assign(&mut self, scalar: f32)
fn mul_assign(&mut self, scalar: f32)
*=
operation. Read moreSource§impl MulAssign for Vector3
impl MulAssign for Vector3
Source§fn mul_assign(&mut self, other: Self)
fn mul_assign(&mut self, other: Self)
*=
operation. Read moreSource§impl PartialOrd for Vector3
impl PartialOrd for Vector3
Source§impl SubAssign<f32> for Vector3
impl SubAssign<f32> for Vector3
Source§fn sub_assign(&mut self, scalar: f32)
fn sub_assign(&mut self, scalar: f32)
-=
operation. Read moreSource§impl SubAssign for Vector3
impl SubAssign for Vector3
Source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
-=
operation. Read moreimpl Copy for Vector3
impl Pod for Vector3
impl Send for Vector3
impl StructuralPartialEq for Vector3
impl Sync for Vector3
Auto Trait Implementations§
impl Freeze for Vector3
impl RefUnwindSafe for Vector3
impl Unpin for Vector3
impl UnwindSafe for Vector3
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
Source§type Bits = T
type Bits = T
Self
must have the same layout as the specified Bits
except for
the possible invalid bit patterns being checked during
is_valid_bit_pattern
.Source§fn is_valid_bit_pattern(_bits: &T) -> bool
fn is_valid_bit_pattern(_bits: &T) -> bool
bits
as &Self
.Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian()
.Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.