pub struct Vec3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
Expand description
3D f32 Vector
Fields§
§x: f32
§y: f32
§z: f32
Implementations§
Source§impl Vec3
impl Vec3
pub fn new(x: f32, y: f32, z: f32) -> Vec3
Sourcepub fn set(&mut self, x: f32, y: f32, z: f32)
pub fn set(&mut self, x: f32, y: f32, z: f32)
Sets the values of this vector.
§Examples
use gamevecs::Vec3;
let mut vec = Vec3::new(3.0, 4.0, 6.0);
//set the values
vec.set(4.0, 5.0, 5.0);
assert_eq!(Vec3::new(4.0, 5.0, 5.0), vec);
Sourcepub fn magnitude(&self) -> f32
pub fn magnitude(&self) -> f32
Returns the length (distance to (0|0|0)) of this vector.
§Examples
use gamevecs::Vec3;
let vec = Vec3::new(3.0, 4.0, 12.0);
//get the magnitude
let magnitude = vec.magnitude();
assert_eq!(13.0, magnitude);
Sourcepub fn magnitude_squared(&self) -> f32
pub fn magnitude_squared(&self) -> f32
Returns the squared length of this vector.
§Examples
use gamevecs::Vec3;
let vec = Vec3::new(3.0, 4.0, 12.0);
//get the magnitude
let magnitude = vec.magnitude_squared();
assert_eq!(169.0, magnitude);
Sourcepub fn equals(&self, other: Vec3, epsilon: f32) -> bool
pub fn equals(&self, other: Vec3, epsilon: f32) -> bool
Returns true if this vector approximately equals another one.
§Examples
use gamevecs::Vec3;
let vec = Vec3::new(3.000001, 4.000001, 5.000001);
let other_vec = Vec3::new(3.0, 4.0, 5.0);
let equals = vec.equals(other_vec, 1e-5);
assert_eq!(true, equals);
Sourcepub fn normalized(&self) -> Vec3
pub fn normalized(&self) -> Vec3
Returns this vector with a magnitude of 1. Used when only the direction of the Vector is important.
§Examples
use gamevecs::Vec3;
let vec1 = Vec3::new(0.0, 0.0, 5.0);
//normalize
let normalized = vec1.normalized();
assert_eq!(Vec3::new(0.0, 0.0, 1.0), normalized);
Sourcepub fn distance_to(&self, other: Vec3) -> f32
pub fn distance_to(&self, other: Vec3) -> f32
Returns the distance from this vector to another.
§Examples
use gamevecs::Vec3;
let vec1 = Vec3::new(10.0, 20.0, 0.0);
let vec2 = Vec3::new(5.0, 10.0, 10.0);
//get the distance
let distance = vec1.distance_to(vec2);
assert_eq!(15.0, distance);
Sourcepub fn distance_to_squared(&self, other: Vec3) -> f32
pub fn distance_to_squared(&self, other: Vec3) -> f32
Returns the squared distance from this vector to another.
§Examples
use gamevecs::Vec3;
let vec1 = Vec3::new(10.0, 20.0, 0.0);
let vec2 = Vec3::new(5.0, 10.0, 10.0);
//get the distance
let distance: f32 = vec1.distance_to_squared(vec2);
assert_eq!(225.0, distance);
Sourcepub fn dot(&self, other: Vec3) -> f32
pub fn dot(&self, other: Vec3) -> f32
Returns the dot product of this vector and another one.
§Examples
use gamevecs::Vec3;
let vec1 = Vec3::new(1.0, 2.0, 3.0);
let vec2 = Vec3::new(4.0, 5.0, 6.0);
//get the dot product
let dot_product = vec1.dot(vec2);
assert_eq!(32.0, dot_product);
Sourcepub fn cross(&self, other: Vec3) -> Vec3
pub fn cross(&self, other: Vec3) -> Vec3
Returns the cross product of this vector and another one.
§Examples
use gamevecs::Vec3;
let vec1 = Vec3::new(2.0, 3.0, 4.0);
let vec2 = Vec3::new(5.0, 6.0, 7.0);
//get the cross product
let cross_product = vec1.cross(vec2);
assert_eq!(Vec3::new(-3.0, 6.0, -3.0), cross_product);
Sourcepub fn angle_between(&self, other: Vec3) -> f32
pub fn angle_between(&self, other: Vec3) -> f32
Returns the angle between this vector and another one.
§Examples
use gamevecs::Vec3;
let vec1 = Vec3::new(1.0, 1.0, 1.0);
let vec2 = Vec3::new(-1.0, -1.0, -1.0);
//get the dot product
let angle = vec1.angle_between(vec2);
assert_eq!(180.0, angle.to_degrees());
Sourcepub fn lerp(&self, other: Vec3, t: f32) -> Vec3
pub fn lerp(&self, other: Vec3, t: f32) -> Vec3
Returns the linear interpolation by t between this and another vector. T should be between 0 and 1.
§Examples
use gamevecs::Vec3;
let vec1 = Vec3::new(1.0, 2.0, 4.0);
let other = Vec3::new(3.0, 6.0, 8.0);
//lerp
let new_vec = vec1.lerp(other, 0.5);
assert_eq!(Vec3::new(2.0, 4.0, 6.0), new_vec);
Sourcepub fn project(&self, onto: Vec3) -> Vec3
pub fn project(&self, onto: Vec3) -> Vec3
Projects a vector onto another one and returns the result as a new Vec3.
§Examples
use gamevecs::Vec3;
let vec1 = Vec3::new(3.0, 4.0, 5.0);
let vec2 = Vec3::new(1.0, 0.0, 0.0);
//project
let projection = vec1.project(vec2);
assert_eq!(Vec3::new(3.0, 0.0, 0.0), projection);
Trait Implementations§
Source§impl AddAssign for Vec3
impl AddAssign for Vec3
Source§fn add_assign(&mut self, rhs: Vec3)
fn add_assign(&mut self, rhs: Vec3)
Performs the
+=
operation. Read moreSource§impl DivAssign for Vec3
impl DivAssign for Vec3
Source§fn div_assign(&mut self, rhs: Vec3)
fn div_assign(&mut self, rhs: Vec3)
Performs the
/=
operation. Read moreSource§impl MulAssign for Vec3
impl MulAssign for Vec3
Source§fn mul_assign(&mut self, rhs: Vec3)
fn mul_assign(&mut self, rhs: Vec3)
Performs the
*=
operation. Read moreSource§impl SubAssign for Vec3
impl SubAssign for Vec3
Source§fn sub_assign(&mut self, rhs: Vec3)
fn sub_assign(&mut self, rhs: Vec3)
Performs the
-=
operation. Read moreimpl Copy for Vec3
impl StructuralPartialEq for Vec3
Auto Trait Implementations§
impl Freeze for Vec3
impl RefUnwindSafe for Vec3
impl Send for Vec3
impl Sync for Vec3
impl Unpin for Vec3
impl UnwindSafe for Vec3
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
Mutably borrows from an owned value. Read more