Struct Vector3

Source
#[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

Source

pub const ZERO: Self

A vector with all components set to 0.

Source

pub const ONE: Self

A vector with all components set to 1.

Source

pub const UNIT_X: Self

A vector with all components set to 0 except for the x component, which is set to 1.

Source

pub const UNIT_Y: Self

A vector with all components set to 0 except for the y component, which is set to 1.

Source

pub const UNIT_Z: Self

A vector with all components set to 0 except for the z component, which is set to 1.

Source

pub const fn new(x: f32, y: f32, z: f32) -> Self

Create a new 3D position with the given coordinates

Source

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.

Source

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.

Source

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.

Source

pub const fn xy(&self) -> Vector2

Returns a 2D vector with the x and y coordinates of the 3D vector

Source

pub const fn xz(&self) -> Vector2

Returns a 2D vector with the x and z coordinates of the 3D vector

Source

pub const fn yz(&self) -> Vector2

Returns a 2D vector with the y and z coordinates of the 3D vector

Source§

impl Vector3

Source

pub fn abs(&self) -> Self

Returns a new vector with all components in absolute values (i.e. positive).

Source

pub fn angle_to(&self, to: Self) -> f32

Returns the unsigned minimum angle to the given vector, in radians.

Source

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().

Source

pub fn ceil(&self) -> Self

Returns a new vector with all components rounded up (towards positive infinity).

Source

pub fn clamp(&self, min: f32, max: f32) -> Self

Returns a new vector with all components clamped between the components of min and max

Source

pub fn clampf(&self, min: f32, max: f32) -> Self

Returns a new vector with all components clamped between min and max

Source

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.

Source

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().

Source

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.

Source

pub fn distance_to(&self, to: Self) -> f32

Returns the distance between this vector and to.

Source

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 to b.dot(a).

Source

pub fn floor(&self) -> Self

Returns a new vector with all components rounded down (towards negative infinity).

Source

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
 }
Source

pub fn is_normalized(&self) -> bool

Returns true if the vector is normalized, i.e. its length is approximately equal to 1.

Source

pub fn length(&self) -> f32

Returns the length (magnitude) of this vector.

Source

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.

Source

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.

Source

pub fn limit_length(&self, max_length: f32) -> Self

Returns the vector with a maximum length by limiting its length to length.

Source

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),
}
Source

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),
}
Source

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),
}
Source

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),
}
Source

pub fn normalize(&self) -> Self

Normalize the vector (make the length of the vector 1)

Source

pub fn round(&self) -> Self

Returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.

Source

pub fn rotate(&self, axis: RotateAxis, angle: RotateAmount) -> Self

Rotates the vector around the given axis by the given angle.

Trait Implementations§

Source§

impl Add<f32> for Vector3

Source§

type Output = Vector3

The resulting type after applying the + operator.
Source§

fn add(self, scalar: f32) -> Self

Performs the + operation. Read more
Source§

impl Add for Vector3

Source§

type Output = Vector3

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl AddAssign<f32> for Vector3

Source§

fn add_assign(&mut self, scalar: f32)

Performs the += operation. Read more
Source§

impl AddAssign for Vector3

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl Clone for Vector3

Source§

fn clone(&self) -> Vector3

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Vector3

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Vector3

Source§

fn default() -> Vector3

Returns the “default value” for a type. Read more
Source§

impl Div<f32> for Vector3

Source§

type Output = Vector3

The resulting type after applying the / operator.
Source§

fn div(self, scalar: f32) -> Self

Performs the / operation. Read more
Source§

impl Div for Vector3

Source§

type Output = Vector3

The resulting type after applying the / operator.
Source§

fn div(self, other: Self) -> Self

Performs the / operation. Read more
Source§

impl DivAssign<f32> for Vector3

Source§

fn div_assign(&mut self, scalar: f32)

Performs the /= operation. Read more
Source§

impl DivAssign for Vector3

Source§

fn div_assign(&mut self, other: Self)

Performs the /= operation. Read more
Source§

impl From<[f32; 3]> for Vector3

Source§

fn from(pos: [f32; 3]) -> Self

Converts to this type from the input type.
Source§

impl From<(f32, f32, f32)> for Vector3

Source§

fn from(pos: (f32, f32, f32)) -> Self

Converts to this type from the input type.
Source§

impl From<Matrix<f32, Const<3>, Const<1>, ArrayStorage<f32, 3, 1>>> for Vector3

Source§

fn from(pos: Vec3) -> Self

Converts to this type from the input type.
Source§

impl From<Vector3> for [f32; 3]

Source§

fn from(pos: Vector3) -> Self

Converts to this type from the input type.
Source§

impl From<Vector3> for (f32, f32, f32)

Source§

fn from(pos: Vector3) -> Self

Converts to this type from the input type.
Source§

impl From<Vector3> for Vec3

Source§

fn from(pos: Vector3) -> Self

Converts to this type from the input type.
Source§

impl Index<usize> for Vector3

Source§

type Output = f32

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Mul<f32> for Vector3

Source§

type Output = Vector3

The resulting type after applying the * operator.
Source§

fn mul(self, scalar: f32) -> Self

Performs the * operation. Read more
Source§

impl Mul for Vector3

Source§

type Output = Vector3

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Self

Performs the * operation. Read more
Source§

impl MulAssign<f32> for Vector3

Source§

fn mul_assign(&mut self, scalar: f32)

Performs the *= operation. Read more
Source§

impl MulAssign for Vector3

Source§

fn mul_assign(&mut self, other: Self)

Performs the *= operation. Read more
Source§

impl Neg for Vector3

Source§

type Output = Vector3

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl PartialEq for Vector3

Source§

fn eq(&self, other: &Vector3) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Vector3

Source§

fn partial_cmp(&self, other: &Vector3) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub<f32> for Vector3

Source§

type Output = Vector3

The resulting type after applying the - operator.
Source§

fn sub(self, scalar: f32) -> Self

Performs the - operation. Read more
Source§

impl Sub for Vector3

Source§

type Output = Vector3

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more
Source§

impl SubAssign<f32> for Vector3

Source§

fn sub_assign(&mut self, scalar: f32)

Performs the -= operation. Read more
Source§

impl SubAssign for Vector3

Source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
Source§

impl Zeroable for Vector3

Source§

fn zeroed() -> Self

Source§

impl Copy for Vector3

Source§

impl Pod for Vector3

Source§

impl Send for Vector3

Source§

impl StructuralPartialEq for Vector3

Source§

impl Sync for Vector3

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Any for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Source§

fn type_name(&self) -> &'static str

Source§

impl<T> AnySync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CheckedBitPattern for T
where T: AnyBitPattern,

Source§

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

If this function returns true, then it must be valid to reinterpret bits as &Self.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> AnyBitPattern for T
where T: Pod,

Source§

impl<T, Right> ClosedAdd<Right> for T
where T: Add<Right, Output = T> + AddAssign<Right>,

Source§

impl<T, Right> ClosedAddAssign<Right> for T
where T: ClosedAdd<Right> + AddAssign<Right>,

Source§

impl<T, Right> ClosedDiv<Right> for T
where T: Div<Right, Output = T> + DivAssign<Right>,

Source§

impl<T, Right> ClosedDivAssign<Right> for T
where T: ClosedDiv<Right> + DivAssign<Right>,

Source§

impl<T, Right> ClosedMul<Right> for T
where T: Mul<Right, Output = T> + MulAssign<Right>,

Source§

impl<T, Right> ClosedMulAssign<Right> for T
where T: ClosedMul<Right> + MulAssign<Right>,

Source§

impl<T> ClosedNeg for T
where T: Neg<Output = T>,

Source§

impl<T, Right> ClosedSub<Right> for T
where T: Sub<Right, Output = T> + SubAssign<Right>,

Source§

impl<T, Right> ClosedSubAssign<Right> for T
where T: ClosedSub<Right> + SubAssign<Right>,

Source§

impl<T> NoUninit for T
where T: Pod,

Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,