Skip to content

atl::math

Math helpers and the value types Vec3, Quat, Color and Uuid. Their math runs in WASM.

pub struct LuaNumber(pub f64)

Numbers: whole numbers print without a fraction, anything else through Lua's tostring, so the output matches what a Lua mod would print.

Implements Display

pub struct Vec3

A 3D vector (sm.vec3). Plain value type; arithmetic happens in WASM.

Fields

pub x: f64
pub y: f64
pub z: f64

Methods and constants

pub const fn new(x: f64, y: f64, z: f64) -> Vec3

A vector from its components.

pub const ZERO: Vec3 = Vec3::new(0.0, 0.0, 0.0)

(0, 0, 0).

pub const ONE: Vec3 = Vec3::new(1.0, 1.0, 1.0)

(1, 1, 1).

pub fn dot(self, o: Vec3) -> f64

Dot product.

pub fn cross(self, o: Vec3) -> Vec3

Cross product.

pub fn length2(self) -> f64

Squared length (cheaper than length()).

pub fn length(self) -> f64

Length.

pub fn distance(self, o: Vec3) -> f64

Distance to another point.

pub fn distance2(self, o: Vec3) -> f64

Squared distance to another point.

pub fn normalize(self) -> Vec3

Unit vector in the same direction; a zero vector stays zero.

pub fn safe_normalize(self, fallback: Vec3) -> Vec3

Unit vector in the same direction, or fallback for a zero vector.

pub fn lerp(self, to: Vec3, t: f64) -> Vec3

Linear interpolation towards to.

pub fn fuzzy_equals(self, o: Vec3, eps: f64) -> bool

True when the two vectors are within eps of each other.

pub fn abs(self) -> Vec3

Component-wise absolute value, floor and ceil.

pub fn floor(self) -> Vec3
pub fn ceil(self) -> Vec3
pub fn min(self, o: Vec3) -> Vec3

Component-wise minimum and maximum.

pub fn max(self, o: Vec3) -> Vec3
pub fn rotate(self, radians: f64, axis: Vec3) -> Vec3

This vector rotated radians around axis (same convention as Quat::angle_axis).

pub fn rotate_x(self, radians: f64) -> Vec3

Rotation around a world axis.

pub fn rotate_y(self, radians: f64) -> Vec3
pub fn rotate_z(self, radians: f64) -> Vec3
pub fn closest_axis(self) -> Vec3

The axis-aligned unit vector closest to this direction.

pub fn bezier2(c0: Vec3, c1: Vec3, c2: Vec3, t: f64) -> Vec3

Point on a quadratic Bezier curve at t (0..1).

pub fn bezier3(c0: Vec3, c1: Vec3, c2: Vec3, c3: Vec3, t: f64) -> Vec3

Point on a cubic Bezier curve at t (0..1).

Implements Add, AddAssign, Clone, Codec, Copy, Debug, Default, Display, Div, Div<f64>, DivAssign<f64>, FromLua, IntoLua, Mul, Mul<f64>, Mul<Vec3>, MulAssign<f64>, Neg, PartialEq, Sub, SubAssign

pub struct Quat

A rotation quaternion (sm.quat). Plain value type; w = 1 is no rotation.

Fields

pub x: f64
pub y: f64
pub z: f64
pub w: f64

Methods and constants

pub const fn new(x: f64, y: f64, z: f64, w: f64) -> Quat

A quaternion from its components.

pub const IDENTITY: Quat = Quat::new(0.0, 0.0, 0.0, 1.0)

No rotation.

pub const ROT_X90: Quat = Quat::new(HALF_SQRT2, 0.0, 0.0, HALF_SQRT2)

Fixed 90 and 180 degree rotations (sm.quat.rotX90() and so on).

pub const ROT_Y90: Quat = Quat::new(0.0, HALF_SQRT2, 0.0, HALF_SQRT2)
pub const ROT_Z90: Quat = Quat::new(0.0, 0.0, HALF_SQRT2, HALF_SQRT2)
pub const ROT_NEG_X90: Quat = Quat::new(-HALF_SQRT2, 0.0, 0.0, HALF_SQRT2)
pub const ROT_NEG_Y90: Quat = Quat::new(0.0, -HALF_SQRT2, 0.0, HALF_SQRT2)
pub const ROT_NEG_Z90: Quat = Quat::new(0.0, 0.0, -HALF_SQRT2, HALF_SQRT2)
pub const ROT_X180: Quat = Quat::new(1.0, 0.0, 0.0, 0.0)
pub const ROT_Y180: Quat = Quat::new(0.0, 1.0, 0.0, 0.0)
pub const ROT_Z180: Quat = Quat::new(0.0, 0.0, 1.0, 0.0)
pub fn angle_axis(radians: f64, axis: Vec3) -> Quat

A rotation of radians around axis.

pub fn from_euler(degrees: Vec3) -> Quat

A rotation from Euler angles in degrees (sm.quat.fromEuler).

pub fn look_rotation(at: Vec3, up: Vec3) -> Quat

A rotation facing at with up as up (sm.quat.lookRotation).

pub fn from_to(from: Vec3, to: Vec3) -> Quat

The shortest rotation taking direction from to direction to (sm.vec3.getRotation).

pub fn at(self) -> Vec3

The rotated forward, up and right directions (Quat:getAt/getUp/getRight).

pub fn up(self) -> Vec3
pub fn right(self) -> Vec3
pub fn round90(self) -> Quat

Snapped to the nearest 90 degree steps.

pub fn safe_normalize(self, fallback: Quat) -> Quat

Normalized, or fallback if the quaternion has zero length.

pub fn normalize(self) -> Quat

Unit length; a zero quaternion becomes the identity.

pub fn inverse(self) -> Quat

The opposite rotation.

pub fn slerp(self, mut to: Quat, t: f64) -> Quat

Spherical interpolation towards to along the shortest path.

Implements Clone, Codec, Copy, Debug, Default, Display, FromLua, IntoLua, Mul, Mul<Vec3>, PartialEq

pub struct Color

An RGBA color (sm.color) with channels from 0 to 1. Defaults to white.

Fields

pub r: f64
pub g: f64
pub b: f64
pub a: f64

Methods and constants

pub const fn new(r: f64, g: f64, b: f64, a: f64) -> Color

A color from its channels.

pub const WHITE: Color = Color::new(1.0, 1.0, 1.0, 1.0)

Opaque white.

pub const fn rgb(hex: u32) -> Color

A color from 0xRRGGBB, e.g. Color::rgb(0x4a90d9).

pub const fn rgba(hex: u32) -> Color

A color from 0xRRGGBBAA.

pub fn hex(s: &str) -> Color

A color from "#rrggbb", "rrggbb", "#rrggbbaa" or "rrggbbaa".

pub fn lerp(self, to: Color, t: f64) -> Color

Linear interpolation towards to.

pub fn hex_string(self) -> String

"rrggbbaa", as Color:getHexStr().

pub fn tag(self) -> String

"#rrggbb", the color tag used in chat and GUI text.

pub fn gui_string(self) -> String

The engine's GUI color string, as Color:getGuiColorStr().

pub fn uint_rgba(self) -> u32

0xRRGGBBAA, as Color:getUintRgba().

Implements Clone, Codec, Copy, Debug, Default, Display, FromLua, IntoLua, PartialEq

pub struct Uuid

A uuid (sm.uuid), stored as its 36-character text.

Methods and constants

pub const NIL: Uuid = Uuid::new("00000000-0000-0000-0000-000000000000")

The nil uuid (all zeros).

pub const fn new(s: &str) -> Uuid

Parses the text form; missing characters become '0'.

pub fn random() -> Uuid

A random uuid.

pub fn named(ns: Uuid, name: &str) -> Uuid

A version 5 uuid: the same name in the same ns always gives the same uuid.

pub fn is_nil(&self) -> bool

True for the nil uuid.

pub fn as_str(&self) -> &str

The 36-character text.

pub fn bytes(&self) -> &[u8; 36]

The 36 bytes of text.

Implements Clone, Codec, Copy, Debug, Default, Display, Eq, FromLua, Hash, IntoLua, PartialEq

pub fn sqrt(x: f64) -> f64

Square root.

pub fn abs(x: f64) -> f64

Absolute value.

pub fn floor(x: f64) -> f64

Rounds down / up to a whole number.

pub fn ceil(x: f64) -> f64
pub fn sin(x: f64) -> f64

Trigonometry and pow, computed by Lua's math library.

pub fn cos(x: f64) -> f64
pub fn tan(x: f64) -> f64
pub fn asin(x: f64) -> f64
pub fn acos(x: f64) -> f64
pub fn atan2(y: f64, x: f64) -> f64
pub fn pow(x: f64, y: f64) -> f64
pub fn clamp(x: f64, lo: f64, hi: f64) -> f64

x limited to the range lo..=hi.

pub fn lerp(a: f64, b: f64, t: f64) -> f64

Linear interpolation from a (t = 0) to b (t = 1).

pub const fn vec3(x: f64, y: f64, z: f64) -> Vec3

Shorthand for Vec3::new.

pub const PI: f64 = core::f64::consts::PI

Pi.

pub const DEG: f64 = PI / 180.0

Degrees to radians: 90.0 * DEG.