Workshop › Angry Birds-style Slingshot › Step 26 of 47
Pushing apart fixes the positions; the velocities must change too. If the boxes are still closing in along the
normal, they bounce: a hit of size j is shared by inverse mass, so a light block hit by a heavy bird flies off.
```js
const closing = (a.vx - b.vx) * nx + (a.vy - b.vy) * ny
if (closing <= 0) return
const j = (1.2 * closing) / (ia + ib) // 1.2: a slightly bouncy hit
a.vx -= j * nx * ia
a.vy -= j * ny * ia
b.vx += j * nx * ib
b.vy += j * ny * ib
```closing is the relative velocity along the normal; 0 or less means they are already moving apart.j is the size of the hit. With 1.2 the boxes end up moving apart a little (a slightly bouncy hit).j times its inverse mass: light bodies change a lot, heavy ones little. Momentum is kept.In collide, under the last push line (b.y += ny * push * ib), write the seven lines.
Skills: Velocity and gravity, Collision detection
← Push overlapping boxes apart Every pair meets →