Skip to main content

Vec2

Struct Vec2 

Source
pub struct Vec2 {
    pub x: Fixed,
    pub y: Fixed,
}
Expand description

A two-dimensional vector.

§Contract

  • Every operation is deterministic, because every operation is Fixed arithmetic and nothing else.
  • Saturating throughout, inheriting the scalar’s behaviour: a component that overflows clamps and is counted rather than wrapping.
  • Eq and Hash, so a vector can be a map key or enter a state hash directly — which is the thing a float vector cannot offer.

Fields§

§x: Fixed§y: Fixed

Implementations§

Source§

impl Vec2

Source

pub const ZERO: Self

The origin.

Source

pub const X: Self

One unit along x.

Source

pub const Y: Self

One unit along y.

Source

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

From components.

Source

pub const fn from_ints(x: i32, y: i32) -> Self

From whole numbers, which is how most call sites write a constant.

Source

pub fn dot(self, other: Self) -> Fixed

The dot product.

Source

pub fn cross(self, other: Self) -> Fixed

The 2D cross product: a scalar, the z of the 3D cross of these vectors lifted into the plane. Positive when other is counter-clockwise of self, which is what a winding test reads.

Source

pub fn length_squared(self) -> Fixed

The squared length.

Preferred over Vec2::length wherever a comparison will do, and not only for speed: this is exact where the length is rounded, so two vectors that compare equal by squared length may compare unequal by length.

Source

pub fn length_squared_wide(self) -> Wide

The squared length at full width, which cannot overflow and cannot round.

The form to compare with. Vec2::length_squared narrows to a Fixed and therefore has a floor: a vector whose components are all below 182 raw units squares to zero there, which is how a direction can appear to have no length at all. Nothing is lost here.

Source

pub fn length(self) -> Fixed

The length, floored to the representable value below the exact one.

Computed through the full-width square, so it is exact for short vectors where narrowing first would have lost them entirely — a one-raw-unit vector has length one here and had length zero before.

Source

pub fn distance(self, other: Self) -> Fixed

The distance to another point.

Source

pub fn normalize(self) -> Option<Self>

A unit vector in the same direction, or None for the zero vector.

Fallible rather than asserting, because the zero vector is a value a simulation legitimately produces — a body at rest, a contact between coincident points — and refusing it would put an assertion on a path that runs every frame.

The result is unit-length to within four parts in 65536, which is asserted by a property test over every magnitude including the shortest. Callers wanting an exact equality should compare squared lengths against a tolerance rather than expecting Fixed::ONE.

The direction is scaled up before its length is taken, and that is not an optimisation. Shifting a fixed-point value left is exact, and without it a short direction is divided by a length that rounded to something far too coarse: before this, a direction of 41 raw units came back as a normal forty-one per cent too long, and anything whose components were all below 181 raw had no direction at all.

Source

pub fn lerp(self, other: Self, t: Fixed) -> Self

Linear interpolation, t clamped to [0, 1].

Written as a + (b - a) * t rather than a*(1-t) + b*t: the second is the numerically better form in floating point and the worse one here, because it rounds twice as often and neither form gains anything from exactness at the endpoints — this one is exact at both by construction.

Source

pub fn project_onto_unit(self, direction: Self) -> Self

The component of self along direction, which must be unit-length.

The building block of move-and-slide: removing this from a displacement is what makes a body slide along a wall rather than stop at it.

Source

pub fn slide_along(self, normal: Self) -> Self

self with its component along normal removed.

normal must be unit-length. This is the slide operation itself, named so the physics implementation does not spell it out at each call site and get the sign wrong at one of them.

Source

pub fn rotate(self, angle: Angle) -> Self

Rotated counter-clockwise by angle.

The standard rotation, in fixed point: (x cos − y sin, x sin + y cos). Each component is two rounded products, so a rotated vector keeps its length to a few parts in 65536 rather than exactly — the tests state the bound.

Vec2::perpendicular remains for the quarter turn, and is not the same thing: it is exact, where this rounds.

Source

pub fn perpendicular(self) -> Self

Perpendicular, rotated a quarter turn counter-clockwise.

Exact — a quarter turn is a swap and a negation, needing no trigonometry, which is why this is available when general rotation is not.

Trait Implementations§

Source§

impl Add for Vec2

Source§

type Output = Vec2

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Clone for Vec2

Source§

fn clone(&self) -> Vec2

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Vec2

Source§

impl Debug for Vec2

Source§

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

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

impl Default for Vec2

Source§

fn default() -> Vec2

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

impl Eq for Vec2

Source§

impl Hash for Vec2

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Mul<Fixed> for Vec2

Scaled by a scalar. Saturating componentwise, like everything else here.

Source§

type Output = Vec2

The resulting type after applying the * operator.
Source§

fn mul(self, factor: Fixed) -> Self

Performs the * operation. Read more
Source§

impl Neg for Vec2

Source§

type Output = Vec2

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl PartialEq for Vec2

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Vec2

Source§

impl Sub for Vec2

Source§

type Output = Vec2

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more

Auto Trait Implementations§

§

impl Freeze for Vec2

§

impl RefUnwindSafe for Vec2

§

impl Send for Vec2

§

impl Sync for Vec2

§

impl Unpin for Vec2

§

impl UnsafeUnpin for Vec2

§

impl UnwindSafe for Vec2

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> 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> CloneToUninit for T
where T: Clone,

Source§

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

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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