spatial::Transform
On this page
TransformSource
pub struct Transform<T = f32>An object's place in a scene as separate parts: scale first, then
rotation, then translation. Easier to edit and to interpolate than a
matrix; to_mat4 gives the model matrix. Directions follow the
right-handed, y-up, -z-forward convention of look_at_rh.
#[repr(C)]: translation, rotation, scale, ten Ts.
use spatial::{Quat, Transform, Vec3};
let t = Transform::new(Vec3::new(0.0, 1.0, 0.0), Quat::from_rotation_y(f32::PI), Vec3::ONE);
assert(t.transform_point(Vec3::X).approx_eq(Vec3::new(-1.0, 1.0, 0.0), 1e-6));
assert(t.forward().approx_eq(Vec3::Z, 1e-6));
The transform of its three parts.
nabiuse spatial::{Quat, Transform, Vec3}; assert(Transform::new(Vec3::ZERO, Quat::IDENTITY, Vec3::ONE) == Transform::identity());No change.
nabiuse spatial::{Transform, Vec3}; assert(Transform::identity().transform_point(Vec3::ONE) == Vec3::ONE);Only a translation.
nabiuse spatial::{Transform, Vec3}; assert(Transform::from_translation(Vec3::X).transform_point(Vec3::ZERO) == Vec3::X);Only a rotation.
nabiuse spatial::{Quat, Transform}; assert(Transform::from_rotation(Quat::IDENTITY) == Transform::identity());Only a scale.
nabiuse spatial::{Transform, Vec3}; assert(Transform::from_scale(Vec3::splat(2.0)).transform_point(Vec3::ONE) == Vec3::splat(2.0));The parts of a matrix without shear or projection: see
Mat4::to_scale_rotation_translation.nabiuse spatial::{Mat4, Transform, Vec3}; let t = Transform::from_mat4(Mat4::from_translation(Vec3::ONE)); assert(t.translation == Vec3::ONE && t.scale.approx_eq(Vec3::ONE, 1e-6));The model matrix: scale, then rotate, then translate.
nabiuse spatial::{Mat4, Transform, Vec3}; let t = Transform::from_translation(Vec3::X); assert(t.to_mat4() == Mat4::from_translation(Vec3::X));The same transform as an
Affine3.nabiuse spatial::{Affine3, Transform}; assert(Transform::<f32>::identity().to_affine() == Affine3::identity());The point
pscaled, rotated and moved.nabiuse spatial::{Quat, Transform, Vec3}; let t = Transform::new(Vec3::X, Quat::IDENTITY, Vec3::splat(2.0)); assert(t.transform_point(Vec3::ONE) == Vec3::new(3.0, 2.0, 2.0));The direction
vscaled and rotated, not moved.nabiuse spatial::{Transform, Vec3}; assert(Transform::from_translation(Vec3::X).transform_vector(Vec3::Y) == Vec3::Y);The local -z axis in world space: where the object faces.
nabiuse spatial::{Transform, Vec3}; assert(Transform::<f32>::identity().forward() == -Vec3::Z);The local +x axis in world space.
nabiuse spatial::{Transform, Vec3}; assert(Transform::<f32>::identity().right() == Vec3::X);The local +y axis in world space.
nabiuse spatial::{Transform, Vec3}; assert(Transform::<f32>::identity().up() == Vec3::Y);The same transform turned so
forward()points attarget, withuproughly up. Translation and scale stay.nabiuse spatial::{Transform, Vec3}; let t = Transform::from_translation(Vec3::ZERO).looking_at(Vec3::X, Vec3::Y); assert(t.forward().approx_eq(Vec3::X, 1e-6));selfafterchild: the child's transform in this one's space, as a scene graph composes a parent and a child. Exact when the parent's scale is uniform; otherwise the shear that the product would have is dropped (useAffine3orMat4for that).nabiuse spatial::{Transform, Vec3}; let parent = Transform::from_translation(Vec3::X); let child = Transform::from_translation(Vec3::Y); assert(parent.mul_transform(child).translation == Vec3::new(1.0, 1.0, 0.0));The inverse; exact for a uniform scale.
nabiuse spatial::{DQuat, DTransform, DVec3}; let t = DTransform::new(DVec3::ONE, DQuat::from_rotation_x(0.5), DVec3::splat(2.0)); assert(t.inverse().mul_transform(t).transform_point(DVec3::X).approx_eq(DVec3::X, 1e-12));The transform
tof the way fromselftoo: translation and scale linear, rotation byslerp. For animation blending.nabiuse spatial::{Transform, Vec3}; let a = Transform::from_translation(Vec3::ZERO); let b = Transform::from_translation(Vec3::splat(2.0)); assert(a.lerp(b, 0.5).translation == Vec3::ONE);Whether every part is within
epsofo's.nabiuse spatial::{Transform, Vec3}; assert(Transform::identity().approx_eq(Transform::from_translation(Vec3::splat(1e-7)), 1e-6));
Trait implementations 1
impl<T: Real> Mul for Transform<T>