- Learnt28 min · sin and cos, heading, the wrap, skids
- Basic25 min · the drifting kart
- Challenge 1–327 min · tarmac only, a handbrake, drift points
- Extrabonus · an oil slick
Review — Kart Racer I 5 min
- Each tick: friction first (× 0.95, or × 0.8 on the grass), then the push (gas × 0.5).
- Top speed = push ÷ leak: 0.5 ÷ 0.05 = 10 on the road, 0.5 ÷ 0.2 = 2.5 on the grass.
drive (gas) (steer) :: customdid all the moving. Steering only worked while the kart was moving.
Quick-fire
The kart points at direction 90 and moves 10 steps. How far does it go right, and how far up? Now point it at direction 45. Same questions.
Reveal the answer
At 90: 10 right, 0 up. At 45 it goes up and right at once — about 7 of each, not 5. (7 and 7 make a diagonal of 10, because a diagonal is longer than either side.) That “about 7” has a name: it is 10 × sin(45).
Today’s Topic 3 min
- sin and cos: how much of a step goes across, and how much up
- Moving with
change x by ()andchange y by ()instead ofmove () steps - A
headingthat lags behind the direction: drift - The wrap: why 170 and −170 are only 20° apart
- Skid marks drawn with the Pen
Learning outcome
By the end of this lesson you will be able to:
- Split a move into its across and up parts with sin and cos.
- Move a sprite in a direction it is not facing.
- Find the shortest way round between two directions.
Learnt — inside the move block 28 min
1 · A step is two smaller steps
Every move on the Stage is some distance across (x) and some distance up (y). The direction decides how the step is shared out. Scratch has two blocks that tell you the share:
| Direction | ([sin v] of (direction)) — across | ([cos v] of (direction)) — up |
|---|---|---|
| 0 (up) | 0 | 1 |
| 30 | 0.5 | 0.87 |
| 45 | 0.71 | 0.71 |
| 90 (right) | 1 | 0 |
| 180 (down) | 0 | −1 |
| −90 (left) | −1 | 0 |
Multiply by the length of the step. A step of 100 at direction 30 is 100 × 0.5 = 50 across and 100 × 0.87 = 87 up. A minus means left, or down.
How to use this: Click the green flag to start it. Nothing else to press — the kart draws three steps and splits each one.
2 · Moving without move
So these two scripts do exactly the same thing:
move (speed) steps
change x by ((speed) * ([sin v] of (direction)))
change y by ((speed) * ([cos v] of (direction)))
How to use this: Click the green flag to start it. Nothing else to press — both karts get the same keys at the same moments.
Why bother? Because now the angle in the sum does not have to be the way the kart is facing.
3 · Facing one way, going another
On a real kart, the tyres slip in a fast corner. The nose turns in, but the kart keeps sliding the old way for a moment. So we give it a second angle:
direction— where the nose points. Steering changes this.heading— where the kart is actually going. It follows the direction, a bit at a time.slip— the gap between them.
set [slip v] to (((((direction) - (heading)) + (540)) mod (360)) - (180))
change [heading v] by ((slip) * (grip))
change x by ((speed) * ([sin v] of (heading)))
change y by ((speed) * ([cos v] of (heading)))
grip is 0.2: the heading closes a fifth of the gap each tick.How to use this: Click the green flag to start it. Nothing else to press — a script drives; watch the slip box in the corners.
4 · The wrap
That long first line needs explaining. Scratch directions run from −180 to 180. Facing 170 and heading −170 look 340° apart. They are really 20° apart, through 180.
Adding 540, taking mod 360 and taking away 180 turns any gap into the short way round, between −180 and 180. Try it: 170 − (−170) = 340, + 540 = 880, mod 360 = 160, − 180 = −20.
How to use this: Click the green flag to start it. Nothing else to press — both karts drive round in circles on the grass.
5 · Skid marks
Add the Pen extension. While the slip is bigger than 15°, the pen is down. The Pen draws from the middle of the kart, so the mark runs down its centre.
if <([abs v] of (slip)) > (15)> then
pen down
else
pen up
end
How to use this: Click the green flag to start it. Nothing else to press — a script drives round; the skids appear in the corners.
Basic — the drifting kart 25 min
Open your kart from Kart Racer I. You will change the end of its drive block.
Step 1 — new variables
- For all sprites:
heading,slipandgrip. - Add the Pen extension.
Step 2 — the green flag
when flag clicked
erase all
go to x: (-37) y: (137)
point in direction (90)
set [speed v] to (0)
set [heading v] to (90)
set pen color to [#2B2B2B]
set pen (transparency v) to (45)
set pen size to (3)
pen up
Step 3 — the drive block
- Delete the
move (speed) stepsat the bottom of the drive block. - In its place, build the four blocks from Learnt section 3.
- Put
set [grip v] to (0.2)just before thechange [heading v] by ((slip) * (grip)). - Add the skid
iffrom Learnt section 5 at the very end.
How to use this: Click the green flag, then click the stage once. Hold ↑ to go, ← and → to steer — throw it into a corner fast.
The kart drifts all the time, even on the straight.
The kart is not moving by heading. Check both change blocks say ([sin v] of (heading)) and ([cos v] of (heading)) — not direction, and not swapped.
Every time I drive left, the kart suddenly lurches.
The slip is not wrapped. Copy the whole first line from Learnt section 3, with the 540, the mod and the 180.
No skids appear at all.
The pen colour may be fully transparent. Set the colour by clicking the swatch, then set the transparency to 45, not 100.
Skids on the tarmac only
Rubber does not leave black lines on grass.
- A slide on the road draws a skid, as now.
- A slide on the grass draws nothing.
- The kart still slides on the grass — only the mark goes.
How to use this: Click the green flag to start it. Nothing else to press — this script never brakes, so it slides off at the corners.
Teacher reveal
if <<([abs v] of (slip)) > (15)> and <not <touching color [#3FA34D]?>>> then
pen down
else
pen up
end
A handbrake
Drift on purpose.
- While space is held, the grip drops to 0.05.
- Let go and it goes back to 0.2.
- So space in a corner swings the back out much further.
How to use this: Click the green flag to start it. Nothing else to press — the script pulls the handbrake in every sharp corner.
Teacher reveal
set [grip v] to (0.2)
if <key (space v) pressed?> then
set [grip v] to (0.05)
end
change [heading v] by ((slip) * (grip))
The keys belong in the key script, but a one-key check inside the drive block is fine here.
Drift points
Racing games score a good drift. A spin scores nothing.
- A
drift pointsvariable, shown on the Stage. - Each tick the slip is between 15° and 60°, at a speed over 4: +1 point.
- A slip over 90° is a spin: drift points back to 0.
How to use this: Click the green flag to start it. Nothing else to press — the script drifts through the corners with the handbrake.
Teacher reveal
if <<<([abs v] of (slip)) > (15)> and <([abs v] of (slip)) < (60)>> and <(speed) > (4)>> then
change [drift points v] by (1)
end
if <([abs v] of (slip)) > (90)> then
set [drift points v] to (0)
end
An oil slick
Something on the track that takes the grip away.
- An
Oilsprite on the top straight (ours is oil.svg). - Touch it, and for one second the grip is 0.02 — the kart barely turns its travel at all.
- Driving straight through it again a moment later does not restart the second.
How to use this: Click the green flag to start it. Nothing else to press — the script drives straight over the oil.
Teacher reveal
if <<touching [Oil v]?> and <(timer) > (slippy until)>> then
set [slippy until v] to ((timer) + (1))
end
In the key script, before the drive block. And inside the drive block:
set [grip v] to (0.2)
if <(timer) < (slippy until)> then
set [grip v] to (0.02)
end
Summary 5 min
- A step of
sat directiondiss × sin(d)across ands × cos(d)up. - Moving by
headinginstead ofdirectionlets the kart slide. - The wrap — + 540, mod 360, − 180 — always finds the short way round.
- The Pen can draw only while a condition is true: skids.
- sin
- The part of a step that goes across, for a direction.
- cos
- The part of a step that goes up, for a direction.
- Heading
- The direction the kart is actually travelling.
- Slip
- How far the nose points away from the heading.
- Grip
- How quickly the heading catches up with the nose.
Hand in
Save your drifting kart. Next lesson it gets laps — and a way to make sure nobody cheats them.