struct Eagle::Vec3

Overview

A 3D vector of Float32, used for positions, directions, scales and velocities in 3D.

Like Vec2, it is an immutable value type, so every operation returns a new vector. Eagle's 3D space is right-handed with +Y up, and cameras look down -Z: Vec3::FORWARD is (0, 0, -1).

pos = v3(0, 1, 5)
target = v3(0, 0, 0)
dir = (target - pos).normalized
pos += dir * 2 * dt

side = dir.cross(Vec3::UP).normalized # a vector pointing to the right of dir
ground = pos.xz                       # drop the height for top-down maths

Defined in:

eagle/math/vec3.cr

Constant Summary

BACK = Vec3.new(0, 0, 1)

(0, 0, 1).

DOWN = Vec3.new(0, -1, 0)

(0, -1, 0), the direction gravity usually points.

FORWARD = Vec3.new(0, 0, -1)

(0, 0, -1), the direction an unrotated Camera3D or Node3D faces.

LEFT = Vec3.new(-1, 0, 0)

(-1, 0, 0).

ONE = Vec3.new(1, 1, 1)

(1, 1, 1), the identity for scaling.

RIGHT = Vec3.new(1, 0, 0)

(1, 0, 0).

UP = Vec3.new(0, 1, 0)

(0, 1, 0). +Y is up in 3D.

ZERO = Vec3.new(0, 0, 0)

(0, 0, 0).

Constructors

Instance Method Summary

Constructor Detail

def self.new(x : Number, y : Number, z : Number) #

Creates a vector from any three numbers. The shorthand is v3(x, y, z).


def self.new(v : Number) #

Creates (v, v, v).


def self.new #

Creates (0, 0, 0).


def self.zero : Vec3 #

Returns (0, 0, 0). Exists so Enumerable#sum works on arrays of vectors.


Instance Method Detail

def *(o : Vec3) : Vec3 #

Component-wise product.


def *(s : Number) : Vec3 #

Scales every component.


def +(o : Vec3) : Vec3 #

Component-wise sum.


def -(o : Vec3) : Vec3 #

Component-wise difference.


def - : Vec3 #

The vector pointing the opposite way.


def /(o : Vec3) : Vec3 #

Component-wise quotient.


def /(s : Number) : Vec3 #

Divides every component.


def ==(o : Vec3) : Bool #

Exact equality. Use #approx? for computed values.


def abs : Vec3 #

Component-wise absolute value.


def approx?(o : Vec3, eps = 1e-5) : Bool #

True when all components are within eps.


def cross(o : Vec3) : Vec3 #

Cross product: a vector perpendicular to both, following the right-hand rule. Use it to build a right vector from forward and up, or a surface normal from two edges.


def distance(o : Vec3) : Float32 #

Distance to another point.


def distance_squared(o : Vec3) : Float32 #

Squared distance to another point.


def dot(o : Vec3) : Float32 #

Dot product. For unit vectors it is the cosine of the angle between them, so a.dot(b) > 0.7 means "roughly the same direction".


def hash(h) #
Description copied from struct Struct

See Object#hash(hasher)


def inspect(io : IO) : Nil #
Description copied from struct Struct

Appends this struct's name and instance variables names and values to the given IO.

struct Point
  def initialize(@x : Int32, @y : Int32)
  end
end

p1 = Point.new 1, 2
p1.to_s    # "Point(@x=1, @y=2)"
p1.inspect # "Point(@x=1, @y=2)"

def length : Float32 #

Length (magnitude).


def length_squared : Float32 #

Squared length. Cheaper than #length for comparisons.


def lerp(o : Vec3, t : Number) : Vec3 #

Linear interpolation toward o.


def max(o : Vec3) : Vec3 #

Component-wise maximum.


def min(o : Vec3) : Vec3 #

Component-wise minimum.


def normalized : Vec3 #

A unit-length vector in the same direction. The zero vector stays zero.


def project(onto : Vec3) : Vec3 #

The component of this vector along onto. Subtract it to slide along a surface: vel - vel.project(normal).


def reflect(n : Vec3) : Vec3 #

Mirrors the vector across a surface with unit normal n, for bounces.


def to_a #

Returns [x, y, z].


def to_s(io : IO) : Nil #
Description copied from struct Struct

Same as #inspect(io).


def to_vec4(w : Number = 1) : Vec4 #

Extends to a Vec4. Use w = 1 for points and w = 0 for directions.


def x : Float32 #

X component: right.


def x=(x : Float32) #

X component: right.


def xy : Vec2 #

The x and y components as a Vec2.


def xz : Vec2 #

The x and z components as a Vec2. This is the ground plane for top-down logic in 3D.


def y : Float32 #

Y component: up.


def y=(y : Float32) #

Y component: up.


def z : Float32 #

Z component: toward the viewer. Forward is -Z.


def z=(z : Float32) #

Z component: toward the viewer. Forward is -Z.


def zero? : Bool #

True for (0, 0, 0) exactly.