Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 50 ยท 3D Transformations and Viewing

3D Points and Transformation Matrices

Learn right- and left-handed 3D systems, 4x4 homogeneous matrices, translation, scaling and composition order, with a live matrix calculator.

So far, shapes lived on a flat plane. The real world has depth, and so do most interesting scenes: cubes, characters, planets, cameras. Moving to 3D adds one coordinate, z, but it also adds a few ideas that are easy to get wrong: which way does z point, how do you move, resize and combine placements of an object, and why does everyone use 4 x 4 matrices to do it?

This lesson sets up the 3D toolbox. You will see how 3D points are represented, why right-handed and left-handed systems exist, how to build translation and scaling matrices, and why the order in which you multiply them changes the result. Rotation, the most complicated transformation, has the next lesson to itself.

Points and vectors in 3D

A point in 3D is a triple (x, y, z). Using the same notation, a vector is also a triple, but it means a direction and length rather than a position. The difference matters for transformations: moving an object changes its points, but a direction such as "up" or a surface normal should not change when the object is translated.

The usual building blocks are:

  • Addition and subtraction. B - A is the vector that leads from point A to point B.
  • Dot product. a . b = ax*bx + ay*by + az*bz equals |a| |b| cos(angle). It is zero for perpendicular vectors and is the basis of lighting calculations.
  • Cross product. a x b = (ay*bz - az*by, az*bx - ax*bz, ax*by - ay*bx) gives a vector perpendicular to both. It produces surface normals and is the key to handedness.

Right-handed and left-handed systems

Place the x-axis pointing right and the y-axis pointing up. The z-axis can then point in one of two directions: out of the screen toward you, or into the screen away from you. These are the two handedness conventions:

  • Right-handed: with the fingers of your right hand curling from the x-axis to the y-axis, your thumb points along +z. Here, +z comes toward the viewer. Most mathematics, OpenGL's world and view spaces, and glTF use this.
  • Left-handed: use your left hand instead; +z points away from the viewer, "into" the screen. DirectX and Unity traditionally use it, and OpenGL's normalised device coordinates are also left-handed.
flowchart LR
  subgraph RH["Right-handed"]
    direction TB
    R1["x right, y up"]
    R2["z toward the viewer"]
    R3["use the right hand for the thumb rule"]
  end
  subgraph LH["Left-handed"]
    direction TB
    L1["x right, y up"]
    L2["z away from the viewer"]
    L3["use the left hand for the thumb rule"]
  end
  RH -->|"flip the sign of z"| LH

The two systems are mirror images of each other. Converting means negating one axis.

Neither is more correct. What matters is that you pick one and apply it consistently: models, camera code and any imported file must agree. Mixing them produces mirrored models, objects that vanish because the faces are wound the wrong way, and rotations that go the wrong direction. When you load data from a source with the other handedness, the standard fix is to negate one coordinate (usually z) and reverse the order of each triangle's vertices to keep faces outward.

In this tutorial we use the right-handed convention for world and view space, with y up: the camera in the later lessons looks down the negative z-axis.

Homogeneous coordinates in 3D

In 2D we noticed that translation cannot be written as a multiplication by a 2 x 2 matrix, which is why homogeneous coordinates add a third component. The same trick in 3D adds a fourth: the point (x, y, z) becomes the column vector (x, y, z, 1).

The extra w component distinguishes two things:

  • w = 1: a point. Translation moves it.
  • w = 0: a direction. Translation leaves it unchanged (because the translation part gets multiplied by 0), while rotation and scaling still apply.

Every transformation we need becomes a 4 x 4 matrix, and a sequence of transformations becomes a product of matrices. One multiplication per vertex then does the whole job, however many steps you stacked. (After perspective projection w takes another role, covered in the projection lesson.)

The basic matrices

We write points as column vectors and apply a matrix on the left: p' = M * p. This is the convention of OpenGL and most math textbooks.

Identity leaves everything unchanged:

TEXT
| 1 0 0 0 |
| 0 1 0 0 |
| 0 0 1 0 |
| 0 0 0 1 |

Translation by (tx, ty, tz) puts the offsets in the last column:

TEXT
| 1 0 0 tx |   | x |   | x + tx |
| 0 1 0 ty | * | y | = | y + ty |
| 0 0 1 tz |   | z |   | z + tz |
| 0 0 0 1  |   | 1 |   |   1    |

Scaling by (sx, sy, sz) puts the factors on the diagonal:

TEXT
| sx 0  0  0 |   | x |   | sx * x |
| 0  sy 0  0 | * | y | = | sy * y |
| 0  0  sz 0 |   | z |   | sz * z |
| 0  0  0  1 |   | 1 |   |   1    |

Scaling is always about the origin: a cube sitting at (10, 0, 0) that is scaled by 2 ends up at (20, 0, 0), not just twice as big. To scale an object about its own center, first move that center to the origin, scale, then move back, which brings us to composition.

A negative scale factor mirrors the object. scale(1, 1, -1) converts between right- and left-handed coordinates. Its 3 x 3 determinant is -1, and a negative determinant is the sign of a mirror image, meaning triangle winding flips.

Composing transformations

If you first apply matrix A and then matrix B to a point, the result is B * (A * p). Matrix multiplication is associative, so this equals (B * A) * p: the single matrix M = B * A does both steps. Notice the order. With column vectors, the first transformation to apply is the rightmost one in the product.

flowchart LR
  P["point p"] -->|"A = scale"| A["A * p"]
  A -->|"B = translate"| B["B * (A * p)"]
  B --> M["same as (B * A) * p<br/>one matrix M = T * S"]

The matrix next to the point acts first.

Matrix multiplication is not commutative: T * S and S * T are different matrices. Take the point (1, 1, 1), scale by 2 and translate by (5, 0, 0):

  • Scale first, then translate (M = T * S): (1, 1, 1) becomes (2, 2, 2) and then (7, 2, 2).
  • Translate first, then scale (M = S * T): (1, 1, 1) becomes (6, 1, 1) and then (12, 2, 2).

The first is usually what people mean by "make it twice as big and move it there." The second moves it and doubles the distance from the origin as a side effect. A typical object transform in a game is M = T * R * S (scale first, then rotate, then translate), which is the order that places a model correctly in the world.

In code, a 4 x 4 matrix is often stored as a flat array of 16 numbers. Row-major order lists the first row, then the second; column-major order (used by OpenGL and WebGL) lists the first column, then the second. The matrix is the same, but the array layout differs, so check which one your API expects.

Try it: a live matrix calculator

Choose a translation, a scale and the order in which they are applied. The calculator multiplies the matrices, transforms a point of your choice, and draws the unit cube before (grey) and after (blue) in a simple oblique view, with x to the right, y up and z receding to the upper right. The grey axes show the origin.

Translation and scale matrices

With the defaults (scale 1.5, translate (2, 1, 0), scale first), the cube grows to 1.5 units and then sits at (2, 1, 0). Switch the order and the cube lands at (3, 1.5, 0): it moved and was stretched away from the origin. Try a negative scale on z and watch the cube flip to the other side of the plane, which is the right-to-left handed conversion.

Experiments in code

The second example is a plain console program with a tiny matrix library. It checks the cross product against the handedness rules, shows that the order of multiplication matters, and shows how the sign of the determinant detects a mirror.

JS Handedness, order and determinants

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last three lines show the standard recipe for scaling around any point: T(c) * S * T(-c), read right to left. The same sandwich pattern, move, transform, move back, is how rotations about an arbitrary pivot are built in the next lesson.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In a right-handed coordinate system with x pointing right and y pointing up, where does the +z axis point?

    Curling the fingers of the right hand from +x to +y makes the thumb point along +z, which is toward the viewer.
  2. 2. Why do 3D graphics use 4 x 4 matrices and a fourth coordinate w?

    Translation is an addition, which a 3 x 3 matrix cannot express. With homogeneous coordinates all affine transformations become matrix products that can be combined.
  3. 3. With column vectors, M = T * S. Which transformation is applied to a point first?

    The matrix closest to the vector acts first, so S scales the point and T then moves the result.
  4. 4. A direction vector has w = 0. What happens when it is multiplied by a translation matrix?

    The translation sits in the last column, which is multiplied by w. With w = 0 it contributes nothing, so directions are not moved.
  5. 5. What does a negative determinant of the upper-left 3 x 3 block of a transformation indicate?

    A negative determinant means orientation is reversed, as with scale(1, 1, -1). Triangle winding order flips and often has to be corrected.

Summary

  • A 3D point is (x, y, z); right-handed systems have +z toward the viewer and left-handed ones away from it, and converting between them negates one axis.
  • Adding w (1 for points, 0 for directions) lets all affine transformations be 4 x 4 matrices, including translation.
  • Translation puts offsets in the last column, scaling puts factors on the diagonal, and scaling is always about the origin.
  • With column vectors, p' = M * p and the rightmost matrix acts first; matrix products are not commutative, so T * S differs from S * T.
  • Scale or rotate about a pivot c with T(c) * M * T(-c).

Next, we add the hardest transformation: 3D Rotation: Axes, Euler Angles and Quaternions.