Screen Shake
Screen Shake¶
function updateScreenShake(shake, dt):
if shake.timeRemaining <= 0:
return (0, 0)
shake.timeRemaining -= dt
magnitude = shake.strength * (shake.timeRemaining / shake.duration) // decay
offset = (randomRange(-1,1) * magnitude, randomRange(-1,1) * magnitude)
return offset
function triggerShake(shake, strength, duration):
shake.strength = strength
shake.duration = duration
shake.timeRemaining = duration
Screen shake works by applying a small, temporary random offset to the camera's rendered position, with magnitude decaying over time (here, linearly as timeRemaining / duration shrinks from 1 to 0). The randomness per frame gives the "shake" look; the decay envelope is what makes it feel like an impulse (sudden, strong, fading) rather than constant vibration. Swapping linear decay for a different curve (quadratic falloff, or decaying sine wave for a more rhythmic shake) is a common tweak once the basic version works.