Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Vec2

struct Vec2 { x: float, y: float }

A 2D vector with float components x and y. Create one with vec2(x, y), Vec2::new(x, y) or a struct literal such as Vec2 { x = 1.0, y = 2.0 }.

The components are stored as 32-bit floats, and can read back slightly different from the value that was set (0.1 reads back as 0.10000000149011612).

Implementations

new

fn new(x: float, y: float) -> Vec2

Creates a vector from its x and y components. A struct literal such as Vec2 { x = 3.0, y = 4.0 } does the same.

use std::math::Vec2;

let v = Vec2::new(3.0, 4.0);
let y = v.y; // 4.0

from_angle

fn from_angle(radians: float) -> Vec2

Creates a vector of length 1.0 pointing at an angle of radians, measured from the positive x axis toward the positive y axis. Its components are the angle’s cosine and sine.

use std::math::Vec2;

let right = Vec2::from_angle(0.0);
let x = right.x; // 1.0

zero

fn zero() -> Vec2

Creates the vector whose components are both 0.0.

use std::math::Vec2;

let velocity = Vec2::zero();
let x = velocity.x; // 0.0

length

fn length(self) -> float

length_squared

fn length_squared(self) -> float

Returns the vector’s length multiplied by itself, which skips the square root that length takes. Comparing it against a squared distance tells which of two lengths is longer.

use std::math::Vec2;

let n = Vec2::new(3.0, 4.0).length_squared(); // 25.0

normalize

fn normalize(self) -> Vec2

add

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

Returns the sum of the two vectors, adding them component by component.

use std::math::Vec2;

let v = Vec2::new(3.0, 4.0).add(Vec2::new(1.0, 1.0));
let x = v.x; // 4.0

sub

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

Returns this vector minus other, component by component. b.sub(a) is the vector that points from a to b.

use std::math::Vec2;

let player = Vec2::new(1.0, 1.0);
let enemy = Vec2::new(4.0, 5.0);
let offset = enemy.sub(player);
let x = offset.x; // 3.0

scale

fn scale(self, by: float) -> Vec2

Returns the vector with both components multiplied by by.

use std::math::Vec2;

let half = Vec2::new(3.0, 4.0).scale(0.5);
let x = half.x; // 1.5

dot

fn dot(self, other: Vec2) -> float

Returns the dot product, x * other.x + y * other.y. For two vectors of length 1.0 it’s the cosine of the angle between them, and a result of 0.0 means they’re perpendicular.

use std::math::Vec2;

let a = Vec2::new(3.0, 4.0).dot(Vec2::new(1.0, 0.0)); // 3.0

distance

fn distance(self, other: Vec2) -> float

Returns the straight-line distance between the two points.

use std::math::Vec2;

let d = Vec2::new(0.0, 0.0).distance(Vec2::new(3.0, 4.0)); // 5.0

distance_squared

fn distance_squared(self, other: Vec2) -> float

Returns the distance between the two points multiplied by itself, which skips the square root that distance takes.

use std::math::Vec2;

let player = Vec2::new(0.0, 0.0);
let enemy = Vec2::new(3.0, 4.0);
let close = player.distance_squared(enemy) < 6.0 * 6.0; // true

rotate

fn rotate(self, radians: float) -> Vec2

Returns the vector turned by radians around the origin, from the positive x axis toward the positive y axis. Its length doesn’t change.

use std::math::{PI, Vec2};

let up = Vec2::new(1.0, 0.0).rotate(PI / 2.0);
let y = up.y; // 1.0

perp

fn perp(self) -> Vec2

Returns the vector turned a quarter turn, from the positive x axis toward the positive y axis. It’s Vec2::new(-y, x), which is perpendicular to the vector and as long.

use std::math::Vec2;

let side = Vec2::new(1.0, 0.0).perp();
let y = side.y; // 1.0

to_ivec2

fn to_ivec2(self) -> IVec2

Returns the vector as an IVec2, with each component truncated toward zero the way float.to_int does. It’s the step from a position to the pixel it’s in.

use std::math::Vec2;

let pixel = Vec2::new(3.7, -1.2).to_ivec2();
let y = pixel.y; // -1

angle

fn angle(self) -> float

Returns the angle of the vector in radians, measured from the positive x axis toward the positive y axis. The result is between -PI and PI. Vec2::from_angle goes the other way.

use std::math::Vec2;

let a = Vec2::new(1.0, 0.0).angle(); // 0.0

lerp

fn lerp(self, other: Vec2, t: float) -> Vec2

Returns the point that is t of the way from this vector to other, so 0.5 gives the midpoint. t isn’t limited to 0.0 through 1.0: 1.5 goes past other by half the distance again.

use std::math::Vec2;

let start = Vec2::new(0.0, 0.0);
let end = Vec2::new(10.0, 20.0);
let quarter = start.lerp(end, 0.25);
let y = quarter.y; // 5.0