glemy/physics/vector2

Types

A 2D vector, used throughout the engine for position, velocity, acceleration and force.

pub type Vector2 {
  Vector2(x: Float, y: Float)
}

Constructors

  • Vector2(x: Float, y: Float)

Values

pub fn add(a: Vector2, b: Vector2) -> Vector2

Adds two vectors together.

add(Vector2(1.0, 2.0), Vector2(3.0, 4.0)) // -> Vector2(4.0, 6.0)
pub fn dot(a: Vector2, b: Vector2) -> Float

The dot product of two vectors.

dot(Vector2(-6.0, 8.0), Vector2(5.0, 12.0)) // -> 66.0
pub fn length(v: Vector2) -> Float

The length (magnitude) of a vector.

length(Vector2(3.0, 4.0)) // -> 5.0
pub fn length_squared(v: Vector2) -> Float

The squared length of a vector. Cheaper than length, so prefer this when only comparing magnitudes (e.g. for collision checks).

length_squared(Vector2(3.0, 4.0)) // -> 25.0
pub fn loosely_equals(
  a: Vector2,
  b: Vector2,
  tolerance: Float,
) -> Bool

Compares two vectors for equality within a tolerance, component-wise. Useful when comparing values that took different precision paths to get here — e.g. a CPU Float (64-bit) computation against a GPU f32 (32-bit) computation — where exact equality is too strict.

loosely_equals(Vector2(1.0, 2.0), Vector2(1.0000001, 2.0), 0.001) // -> True
pub fn scale(v: Vector2, scalar: Float) -> Vector2

Scales a vector by a scalar value.

scale(Vector2(1.0, 2.0), 2.0) // -> Vector2(2.0, 4.0)
pub fn subtract(a: Vector2, b: Vector2) -> Vector2

Subtracts vector b from vector a.

subtract(Vector2(3.0, 4.0), Vector2(1.0, 2.0)) // -> Vector2(2.0, 2.0)
pub const zero: Vector2

The zero vector (0, 0).

Search Document