Translate, Rotate, Scale
Every object in a scene has a transform: where it is (translation T), which way it faces (rotation R) and how big it is (scale S). Engines show them as three separate fields in the inspector, and combine them into one matrix for rendering. This lesson builds the 3D scale and rotation matrices and shows why translation needs one more trick.
Scale
A scale matrix has the scale factors on the diagonal. Uniform scale (sx = sy = sz) keeps shapes; non-uniform scale stretches them. A negative factor mirrors the object, which also flips its triangle winding, a common cause of "inside-out" meshes.
S = | sx 0 0 |
| 0 sy 0 |
| 0 0 sz |Rotation about each axis
Rotating about the z axis is the 2D rotation from the Angles section, with z left alone. The x and y versions follow the same pattern, cycling the axes (x → y → z → x). All three turn counter-clockwise when you look down the axis toward the origin, in a right-handed system.
Rx(a) = | 1 0 0 | Ry(a) = | cos a 0 sin a | Rz(a) = | cos a -sin a 0 |
| 0 cos a -sin a | | 0 1 0 | | sin a cos a 0 |
| 0 sin a cos a | | -sin a 0 cos a | | 0 0 1 |Note that Ry has its minus sign in the bottom-left: that is the cyclic order (z × x = y) showing through, not a typo.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>
struct Vec3 { double x, y, z; };
struct Mat3 { double m[3][3]; };
Vec3 operator*(const Mat3& M, Vec3 v) {
return {M.m[0][0] * v.x + M.m[0][1] * v.y + M.m[0][2] * v.z,
M.m[1][0] * v.x + M.m[1][1] * v.y + M.m[1][2] * v.z,
M.m[2][0] * v.x + M.m[2][1] * v.y + M.m[2][2] * v.z};
}
Mat3 rotX(double a) { double c = std::cos(a), s = std::sin(a); return {{{1, 0, 0}, {0, c, -s}, {0, s, c}}}; }
Mat3 rotY(double a) { double c = std::cos(a), s = std::sin(a); return {{{c, 0, s}, {0, 1, 0}, {-s, 0, c}}}; }
Mat3 rotZ(double a) { double c = std::cos(a), s = std::sin(a); return {{{c, -s, 0}, {s, c, 0}, {0, 0, 1}}}; }
Mat3 scale(double x, double y, double z) { return {{{x, 0, 0}, {0, y, 0}, {0, 0, z}}}; }
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
std::ostream& operator<<(std::ostream& os, Vec3 v) {
return os << "(" << z0(v.x) << ", " << z0(v.y) << ", " << z0(v.z) << ")";
}
int main() {
std::cout << std::fixed << std::setprecision(3);
const double q = std::numbers::pi / 2; // 90 degrees
std::cout << "rotX(90) * Y = " << rotX(q) * Vec3{0, 1, 0} << " (Y -> Z)\n";
std::cout << "rotY(90) * Z = " << rotY(q) * Vec3{0, 0, 1} << " (Z -> X)\n";
std::cout << "rotZ(90) * X = " << rotZ(q) * Vec3{1, 0, 0} << " (X -> Y)\n";
std::cout << "rotY(90) * forward(0,0,-1) = " << rotY(q) * Vec3{0, 0, -1} << " (turned left)\n";
std::cout << "scale(2,1,-1) * (1,1,1) = " << scale(2, 1, -1) * Vec3{1, 1, 1} << "\n";
}rotX(90) * Y = (0.000, 0.000, 1.000) (Y -> Z)
rotY(90) * Z = (1.000, 0.000, 0.000) (Z -> X)
rotZ(90) * X = (0.000, 1.000, 0.000) (X -> Y)
rotY(90) * forward(0,0,-1) = (-1.000, 0.000, 0.000) (turned left)
scale(2,1,-1) * (1,1,1) = (2.000, 1.000, -1.000)
A positive rotation about +Y turns the forward direction -Z toward -X, which is a turn to the left in our right-handed, Y-up world. In Unity (left-handed) a positive Y rotation turns you to the right. Same matrix, different picture.
The usual order: scale, then rotate, then translate
To place a model in the world you almost always want to:
- scale it around its own origin,
- rotate it around its own origin,
- move it to its position.
With column vectors that is M = T * R * S. Scaling after rotating would stretch along the world axes instead of the model's own axes, which shears a rotated object. Here is the difference on a model's "right" point:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>
struct Vec3 { double x, y, z; };
struct Mat3 { double m[3][3]; };
Vec3 operator*(const Mat3& M, Vec3 v) {
return {M.m[0][0] * v.x + M.m[0][1] * v.y + M.m[0][2] * v.z,
M.m[1][0] * v.x + M.m[1][1] * v.y + M.m[1][2] * v.z,
M.m[2][0] * v.x + M.m[2][1] * v.y + M.m[2][2] * v.z};
}
Mat3 operator*(const Mat3& A, const Mat3& B) {
Mat3 R{};
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
for (int k = 0; k < 3; k++) R.m[r][c] += A.m[r][k] * B.m[k][c];
return R;
}
Mat3 rotZ(double a) { double c = std::cos(a), s = std::sin(a); return {{{c, -s, 0}, {s, c, 0}, {0, 0, 1}}}; }
Mat3 scale(double x, double y, double z) { return {{{x, 0, 0}, {0, y, 0}, {0, 0, z}}}; }
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
std::ostream& operator<<(std::ostream& os, Vec3 v) {
return os << "(" << z0(v.x) << ", " << z0(v.y) << ", " << z0(v.z) << ")";
}
int main() {
std::cout << std::fixed << std::setprecision(3);
Mat3 R = rotZ(std::numbers::pi / 4); // 45 degrees
Mat3 S = scale(4, 1, 1); // a long plank along its own x axis
Vec3 tip{1, 0, 0}; // the end of the plank in model space
std::cout << "R * S (scale first): " << (R * S) * tip << " length 4, rotated\n";
std::cout << "S * R (rotate first): " << (S * R) * tip << " sheared\n";
}R * S (scale first): (2.828, 2.828, 0.000) length 4, rotated
S * R (rotate first): (2.828, 0.707, 0.000) sheared
Translation is not a 3×3 matrix
A 3×3 matrix always maps (0, 0, 0) to (0, 0, 0): every output is a sum of matrix entry * input component, and the inputs are all zero. So no 3×3 matrix can move the origin, which is exactly what a translation must do. You could keep translation as a separate vector and compute R * S * p + t, and some engines store transforms that way. But then you cannot multiply whole transforms together as simply. The next lesson solves this by adding a fourth coordinate.
Tip: Avoid non-uniform scale on objects that have rotated children. A child rotated inside a non-uniformly scaled parent gets sheared, and no position/rotation/scale triple can describe a shear, so engines show odd values in the inspector (and physics engines often refuse it).
Test yourself
With column vectors, which product scales first, then rotates, then translates?
- S * R * T
- T * R * S
- R * T * S
- T * S * R
Why can't a 3×3 matrix represent a translation of 3D points?
- It has too many numbers
- It always maps the origin to the origin
- Translations are not linear in 2D
- It would need complex numbers
What visible problem does a negative scale factor often cause?
- The object becomes invisible due to NaN
- Triangle winding flips, so the mesh can render inside-out
- The object moves to the origin
- Nothing; it is identical to a rotation