Workshop › Angry Birds-style Slingshot › Step 28 of 47
Friction
What we are doing
Boxes that slide along each other should rub and slow down, or stacks would skate apart like on ice. Friction works
along the surface, sideways to the normal, and is never stronger than half the hit.
```js
// Friction: slow down the sliding along the surface.
const slide = (a.vx - b.vx) * ny - (a.vy - b.vy) * nx
const f = Math.max(-0.5 * j, Math.min(0.5 * j, slide / (ia + ib)))
a.vx -= f * ny * ia
a.vy += f * nx * ia
b.vx += f * ny * ib
b.vy -= f * nx * ib
```
What it means
The direction along the surface is (ny, -nx), the normal turned a quarter turn. slide is how fast the boxes
slide past each other in that direction.
f is the friction needed to stop the sliding, kept between -0.5 * j and 0.5 * j.
It is shared by inverse mass like the hit, against the sliding.
Your turn
At the end of collide, under b.vy += j * ny * ib, write the friction lines.