struct
Eagle::Transform2D
- Eagle::Transform2D
- Struct
- Value
- Object
Overview
A 2D affine transform: translation, rotation and scale packed into a 3x2 matrix.
Most code never builds one by hand, because Node2D#position, Node2D#rotation
and Node2D#scale are composed for you. Reach for Transform2D when you need the
matrix itself: converting points between spaces, drawing with Graphics#apply, or
baking a pose.
The matrix layout is:
[ a c tx ]
[ b d ty ]
Multiplying transforms composes them right to left, and multiplying by a Vec2
transforms a point.
t = Transform2D.trs(v2(200, 150), Mathf.deg2rad(45), v2(2, 2))
tip = t * v2(10, 0) # local point to world
back = t.inverse * tip # world point back to local => (10, 0)
dir = t.transform_dir(Vec2::RIGHT) # direction only, ignores translation
g.with_transform(t) { g.rect(-5, -5, 10, 10) } # draw in the transformed space
Defined in:
eagle/math/transform2d.crConstant Summary
-
IDENTITY =
Transform2D.new -
The transform that changes nothing.
Constructors
-
.identity : Transform2D
Returns
IDENTITY. -
.new(a : Float32 = 1_f32, b : Float32 = 0_f32, c : Float32 = 0_f32, d : Float32 = 1_f32, tx : Float32 = 0_f32, ty : Float32 = 0_f32)
Creates a transform from raw matrix elements.
-
.rotation(rad : Number) : Transform2D
A pure rotation by rad radians around the origin.
-
.scale(v : Vec2) : Transform2D
A pure scale by v.
-
.translation(v : Vec2) : Transform2D
A pure translation by v.
-
.trs(pos : Vec2, rot : Number, scale : Vec2, pivot : Vec2 = Vec2::ZERO) : Transform2D
Builds translation * rotation * scale in one step.
Instance Method Summary
-
#*(o : Transform2D) : Transform2D
Composes two transforms:
(a * b) * p == a * (b * p). -
#*(p : Vec2) : Vec2
Transforms a point, applying translation.
-
#==(o : Transform2D) : Bool
Exact equality.
-
#a : Float32
Matrix element: x-axis x component.
-
#a=(a : Float32)
Matrix element: x-axis x component.
-
#approx?(o : Transform2D, eps = 1e-5) : Bool
Component-wise comparison within eps.
-
#b : Float32
Matrix element: x-axis y component.
-
#b=(b : Float32)
Matrix element: x-axis y component.
-
#c : Float32
Matrix element: y-axis x component.
-
#c=(c : Float32)
Matrix element: y-axis x component.
-
#d : Float32
Matrix element: y-axis y component.
-
#d=(d : Float32)
Matrix element: y-axis y component.
-
#inverse : Transform2D
The inverse transform, which maps back from world to local space.
-
#rotation : Float32
The rotation part in radians.
-
#scale : Vec2
The scale along each axis.
-
#to_mat4 : Mat4
The same transform as a 4x4 matrix, for passing to shaders.
-
#transform_dir(v : Vec2) : Vec2
Transforms a direction, ignoring translation.
-
#translation : Vec2
The translation part.
-
#tx : Float32
Translation x.
-
#tx=(tx : Float32)
Translation x.
-
#ty : Float32
Translation y.
-
#ty=(ty : Float32)
Translation y.
Constructor Detail
Creates a transform from raw matrix elements. The defaults give the identity.
Builds translation * rotation * scale in one step. pivot is the local point that ends up at pos, so the shape rotates and scales around it.
Instance Method Detail
The inverse transform, which maps back from world to local space. Returns identity if the matrix can't be inverted, for example with a zero scale.