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

std::math

ItemSummary
PIThe ratio of a circle’s circumference to its diameter, about 3.14159
TAUA full turn in radians, equal to 2.0 * PI (about 6.28319)
EEuler’s number, the base of the natural logarithm, about 2.71828
vec2Creates a Vec2 from its x and y components, as a shorter way to write Vec2::new
vec3Creates a Vec3 from its x, y and z components
ivec2Creates an IVec2 from its x and y components, as a shorter way to write IVec2::new
ivec3Creates an IVec3 from its x, y and z components, as a shorter way to write IVec3::new

Constants

PI

const PI: float

The ratio of a circle’s circumference to its diameter, about 3.14159. An angle of PI radians is half a turn.

TAU

const TAU: float

A full turn in radians, equal to 2.0 * PI (about 6.28319).

E

const E: float

Euler’s number, the base of the natural logarithm, about 2.71828.

Functions

vec2

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

Creates a Vec2 from its x and y components, as a shorter way to write Vec2::new.

use std::math::vec2;

let v = vec2(3.0, 4.0);
let y = v.y; // 4.0

vec3

fn vec3(x: float, y: float, z: float) -> Vec3

Creates a Vec3 from its x, y and z components.

use std::math::vec3;

let v = vec3(2.0, 3.0, 6.0);
let n = v.length(); // 7.0

ivec2

fn ivec2(x: int, y: int) -> IVec2

Creates an IVec2 from its x and y components, as a shorter way to write IVec2::new.

use std::math::ivec2;

let v = ivec2(3, 4);
let y = v.y; // 4

ivec3

fn ivec3(x: int, y: int, z: int) -> IVec3

Creates an IVec3 from its x, y and z components, as a shorter way to write IVec3::new.

use std::math::ivec3;

let v = ivec3(1, 2, 3);
let z = v.z; // 3

Types

Vec2

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

Vec3

A 3D vector with float components x, y, and z. Create one with vec3(x, y, z) or a struct literal such as Vec3 { x = 0.0, y = 1.0, z = 0.0 }.

Quat

A rotation in 3D space, stored as a quaternion with float components x, y, z, and w. Scripts usually create one with Quat::from_rotation_z and pass it to the host, such as for the rotation of a Bevy transform.

IVec2

A 2D vector with int components x and y, for things counted in whole steps such as pixels and grid cells. Create one with ivec2(x, y), IVec2::new(x, y) or a struct literal such as IVec2 { x = 1, y = 2 }.

IVec3

A 3D vector with int components x, y, and z. Create one with ivec3(x, y, z), IVec3::new(x, y, z) or a struct literal such as IVec3 { x = 0, y = 1, z = 0 }.