struct
Eagle::Vec3
- Eagle::Vec3
- Struct
- Value
- Object
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.crConstant 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 unrotatedCamera3DorNode3Dfaces. -
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
-
.new(x : Number, y : Number, z : Number)
Creates a vector from any three numbers.
-
.new(v : Number)
Creates
(v, v, v). -
.new
Creates
(0, 0, 0). -
.zero : Vec3
Returns
(0, 0, 0).
Instance Method Summary
-
#*(o : Vec3) : Vec3
Component-wise product.
-
#*(s : Number) : Vec3
Scales every component.
-
#+(o : Vec3) : Vec3
Component-wise sum.
-
#-(o : Vec3) : Vec3
Component-wise difference.
-
#- : Vec3
The vector pointing the opposite way.
-
#/(o : Vec3) : Vec3
Component-wise quotient.
-
#/(s : Number) : Vec3
Divides every component.
-
#==(o : Vec3) : Bool
Exact equality.
-
#abs : Vec3
Component-wise absolute value.
-
#approx?(o : Vec3, eps = 1e-5) : Bool
True when all components are within eps.
-
#cross(o : Vec3) : Vec3
Cross product: a vector perpendicular to both, following the right-hand rule.
-
#distance(o : Vec3) : Float32
Distance to another point.
-
#distance_squared(o : Vec3) : Float32
Squared distance to another point.
-
#dot(o : Vec3) : Float32
Dot product.
-
#hash(h)
See
Object#hash(hasher) -
#inspect(io : IO) : Nil
Appends this struct's name and instance variables names and values to the given IO.
-
#length : Float32
Length (magnitude).
-
#length_squared : Float32
Squared length.
-
#lerp(o : Vec3, t : Number) : Vec3
Linear interpolation toward o.
-
#max(o : Vec3) : Vec3
Component-wise maximum.
-
#min(o : Vec3) : Vec3
Component-wise minimum.
-
#normalized : Vec3
A unit-length vector in the same direction.
-
#project(onto : Vec3) : Vec3
The component of this vector along onto.
-
#reflect(n : Vec3) : Vec3
Mirrors the vector across a surface with unit normal n, for bounces.
-
#to_a
Returns
[x, y, z]. -
#to_s(io : IO) : Nil
Same as
#inspect(io). -
#to_vec4(w : Number = 1) : Vec4
Extends to a
Vec4. -
#x : Float32
X component: right.
-
#x=(x : Float32)
X component: right.
-
#xy : Vec2
The x and y components as a
Vec2. -
#xz : Vec2
The x and z components as a
Vec2. -
#y : Float32
Y component: up.
-
#y=(y : Float32)
Y component: up.
-
#z : Float32
Z component: toward the viewer.
-
#z=(z : Float32)
Z component: toward the viewer.
-
#zero? : Bool
True for
(0, 0, 0)exactly.
Constructor Detail
Creates a vector from any three numbers. The shorthand is v3(x, y, z).
Instance Method Detail
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.
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".
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)"
The component of this vector along onto. Subtract it to slide along a surface:
vel - vel.project(normal).