LearnFirst steps

Lesson 8 of 30 · 55 min

Moving space

Move, rotate and scale by transforming the coordinates instead of the shapes

In this lesson you will

  • Understand moving the coordinates instead of the shape, and why the motion comes out reversed
  • Rotate shapes with rot(a) and mat2 (to turn a shape by a, use rot(-a) * p)
  • Scale with p / s and correct the distance with * s
  • Choose the order of transforms and the center of rotation to spin in place, orbit, or swing like a pendulum
  • Give several shapes their own separate motions

01Move the coordinates, not the shape

A shader has no command like "move this box to the right". As we saw in Your first shader, all it can do is answer "what color is this spot?" for each pixel. So how do we move a shape? The answer: change the coordinates before asking the shape.

sdBox(q, b) always answers for a box centered at the origin. So instead of passing the screen coordinate p directly, we pass q = p - vec2(0.5, 0.2). At the screen point (0.5, 0.2), q comes out as (0.0, 0.0), so the box says "this is my center". The box's center therefore shows up at (0.5, 0.2) on screen. Subtract c from the coordinates and the shape moves to +c.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

Starts when it scrolls into view
Open in /edit ↗
▸Change p - vec2(0.5, 0.2) to p + vec2(0.5, 0.2) and the box moves down-left. What if you subtract vec2(0.5 * sin(time), 0.0)?

q is the coordinate as seen from the box: a coordinate system belonging to the box alone, with the box's center as the origin. This is called local coordinates. Move the box right by 0.5, and from the box's point of view every point on screen is now 0.5 further to the left. That's why moving the shape to +c means subtracting c from the coordinates — the direction is reversed. It's the same reason that, in Time and motion, the wave sin(p.x * k - time) moved right when time was subtracted.

This reversal applies not just to moving but to rotating and scaling too, which we'll look at next. Here is the whole picture up front:

  • Move the shape by c → use p - c (move the coordinates the opposite way)
  • Rotate the shape counterclockwise by a → use rot(-a) * p (rotate the coordinates the opposite way)
  • Scale the shape up by s → use p / s (shrink the coordinates instead)

02Rotation: rot(a) and mat2

For rotation we use mat2, a matrix: numbers arranged in 2 rows and 2 columns. Throughout this course we use the following rot function, always in exactly this form.

1

2

3

4

mat2 rot(float a) {

float c = cos(a), s = sin(a);

return mat2(c, s, -s, c);

}

rot(a) * v returns the vector v rotated counterclockwise by a around the origin. Inside, it computes (c * v.x - s * v.y, s * v.x + c * v.y). Let's check: for a = 1.57 (90°), c = 0 and s = 1, so v = (1.0, 0.0), pointing right, becomes (0.0, 1.0), pointing up. From right to up: a 90° counterclockwise turn.

Angles are in radians, not degrees. A full turn is about 6.28 (2π), half a turn about 3.14 (π), a quarter turn (90°) about 1.57, and 45° about 0.785. 0.5 is roughly 29°.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

Starts when it scrolls into view
Open in /edit ↗
▸Set a to 1.57 to stand it upright. Change rot(-a) to rot(a) and it turns clockwise. What about a = time?

Why -a? Think of it like moving. Suppose we rotate the box 90° counterclockwise. The part that used to be at the right end, (0.5, 0.0), ends up at the top, (0.0, 0.5). So the screen point (0.0, 0.5) has to be colored as "the box's right end". Compute rot(-1.57) * (0.0, 0.5): c = 0 and s = -1, giving (0.5, 0.0) — the box's right end, as required. Rotating the coordinates the opposite way tells you exactly which part of the unrotated shape a pixel lands on.

Watch out

In other people's shaders you will often see p *= rot(a);. That means p = p * rot(a), and multiplying with the vector on the left reverses the direction of rotation, so it's the same as p = rot(-a) * p. In this course we always write rot(-a) * p to avoid confusion.

Tip

The four numbers in mat2(c, s, -s, c) are filled in column by column: the first column is (c, s) and the second is (-s, c). The first column is where the right-pointing (1, 0) ends up after the turn; the second is where the up-pointing (0, 1) ends up.

03The center of rotation and the order of transforms

rot always rotates around the origin (0, 0). To spin a shape that sits away from the origin in place, first bring its center to the origin (p - c), then rotate. Swap the order and you get a completely different motion.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

Starts when it scrolls into view
Open in /edit ↗
▸Swap the lines q = q - vec2(0.7, 0.0); and q = rot(-time) * q; and the box starts orbiting the center of the screen

The operations on the coordinates happen top to bottom, but the shape experiences them bottom to top. Read the code from the bottom up: "rotate the box, then move it to (0.7, 0.0)". It spins at the origin and then gets carried over, so it appears to spin in place where it lands. Swap the two lines and it reads "move to (0.7, 0.0), then rotate around the origin", so the box, now far from the origin, swings around it in a big circle.

The order you'll use most is "translate, then rotate, then scale" on the coordinates. Read bottom up, that's "size the shape, spin it in place, then carry it where you want it" — the easiest order to control.

1

2

3

4

vec2 q = p - c; // 1. bring the target position to the origin

q = rot(-a) * q; // 2. rotate

q = q / s; // 3. scale

float d = sdBox(q, b) * s; // bring the distance back to screen units

04Scaling

To make a shape s times bigger, divide the coordinates by s. Doubling a circle of radius 0.3: the screen point (0.6, 0.0) becomes (0.3, 0.0) after p / 2.0, right on the circle's edge. So on screen it looks like a circle of radius 0.6. Shrink the coordinates and the shape looks bigger.

But the distance you get back is measured in the shrunken coordinates. In the same example, take the screen point (0.8, 0.0): sdCircle(p / 2.0, 0.3) returns 0.4 - 0.3 = 0.1. On screen, though, that point is 0.2 from the radius-0.6 edge. To get the right distance, multiply the answer by s: 0.1 × 2 = 0.2. Forget this and the px-based smooth edge and outline width will stretch along with the shape.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Starts when it scrolls into view
Open in /edit ↗
▸Delete * s and set s to 3.0: the outline turns thick and blurry. At 0.3 it gets so thin it almost vanishes

Remember how the ellipse in Circles and distance had blurry edges at its left and right ends? Same cause: the distance measured in squashed coordinates was used as if it were a screen distance. But an ellipse squashes x and y by different amounts, so multiplying by a single s can't fix it. For an ellipse with x scaled by 0.3, d changes only 0.3 times as fast at the left and right ends, so the edge widens about 3.3 times there, while the top and bottom are unaffected. The error differs from place to place.

So instead of fixing the distance, match the edge width to it. fwidth(d) returns how much d differs from one pixel to the next (the GPU shades pixels in 2×2 groups, so it knows its neighbors' values). Use it instead of px as the smoothstep width, and the edge stays about one pixel wide even where the distance changes at different rates.

1

2

3

4

5

6

7

8

9

10

11

12

Starts when it scrolls into view
Open in /edit ↗
▸Look at the blurry left and right ends, then remove the // at the start of the // w = fwidth(d); line: the edge becomes equally crisp all the way around

Tip

fwidth only fixes the width of the edge blur. An outline like abs(d) - 0.01 stays thicker in the stretched direction. If you need even thickness too, the surest approach is to scale x and y by the same amount and correct with * s.

05A separate motion for each shape

When there are several shapes, leave p itself alone and build a coordinate for each shape: q1, q2 and so on. That way the rotation meant for the first shape doesn't affect the second.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

Starts when it scrolls into view
Open in /edit ↗
▸Change rot(-time) in q1 to rot(-time * 4.0) and the other two are unaffected. Delete / s from q3 and the cross stops pulsing

The middle circle's rot(-time * 2.0) * p - vec2(0.3, 0.0) is the swapped order from the previous section: rotate, then move, so it orbits the origin at radius 0.3. The cross on the right uses rot(time), so it turns clockwise (the a in rot(-a) is -time). And notice that p is never reassigned. If you wrote p = rot(-time) * p; partway through, every shape below that line would rotate along with it.

06Swinging from a pivot: a pendulum

Put the center of rotation at one end of a shape and you get a swinging motion. For a pendulum, bring the pivot on the ceiling to the origin (p - pivot), rotate, and in local coordinates draw the rod and weight hanging straight down from the origin. The swing angle comes from a sin, as in Time and motion.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

Starts when it scrolls into view
Open in /edit ↗
▸Change 0.6 to 1.5 for a wide swing. Replace rot(-a) * (p - pivot) with rot(-a) * p - pivot and the pivot drifts, giving a strange motion

In local coordinates, the rod and weight are a still picture that simply hangs straight down from the origin. All the motion lives in the single line that builds q. Keeping the drawing separate from the movement makes both easier to read. The highlight on the weight is drawn with the same q, so it swings along too.

07Laying out rotated copies

Combine rotation with the for loop from Time and motion and you can lay out copies of a shape, each turned a little more. Copy number i is rotated around the origin by float(i) * TAU / 6.0, a sixth of a turn at a time. Because the shape sits away from the origin, the copies fan out in a circle like petals.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

Starts when it scrolls into view
Open in /edit ↗
▸Put // in front of the q = rot(-time) * q; line and the copies stay pointing outward. What if you change all three 6s to 12 (and 6.0 to 12.0)?

Read one pass of the loop from the bottom up: "spin the box in place, carry it 0.45 to the right, then rotate it by a around the origin". Each stage is its own motion, so you can freely combine things like spinning on an axis while orbiting. In Polar coordinates you'll also learn a lighter way to arrange things in a circle without a loop.

08Exercises

Give shapes some motion by transforming their coordinates. If something moves the wrong way, read the coordinate code from the bottom up to see how the shape should move.

Exercise 1

A spinning diamond

Turn a square by 45° to make a diamond, place it at (0.5, 0.0) on screen, and keep it slowly spinning in place.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Starts when it scrolls into view
Open in /edit ↗
Exercise 2

A clock with a second hand

Use a loop to place 12 tick marks around the clock face. Then make the second hand go around clockwise once every 60 seconds. If you have time, make it jump forward once per second (floor, which rounds down, will help).

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

Starts when it scrolls into view
Open in /edit ↗
Exercise 3

A planet and its moon

Make the planet orbit the sun once every 8 seconds. Then add a moon that orbits the planet once every 2 seconds, following the planet as it goes around.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Starts when it scrolls into view
Open in /edit ↗

09Recap

  • You can't move shapes, so transform the coordinates before asking the shape; coordinates and shapes move in opposite directions
  • Move with p - c, rotate with rot(-a) * p (the shape turns counterclockwise by a), scale with p / s and multiply the distance by s
  • rot always turns around the origin; to spin in place, apply p - c before rotating
  • Reading the coordinate code bottom up gives the order the shape experiences; the usual recipe is translate, rotate, scale
  • Leave p alone and build q1, q2 and so on per shape to move them independently

Next time we learn one more way to transform coordinates. Repeat them with fract, and by drawing just one shape you can tile it across the whole screen.

Finished lessons get a mark in the course contents