# Does anybody have a good (and simple) 2D camera shaking algorithm?

## Recommended Posts

sofakng    102
Right now I'm basically doing this:
// During an update shake the camera (executed 60 times per second)
{
xAdjustment = rnd.Next(-15, 15); // get random number between -15 and 15
yAdjustment = rnd.Next(-15, 15); // get random number between -15 and 15

}
It actually doesn't look that bad, but I'm wondering if any better (and simple) algorithms exist for 2D. Thanks for any help!

##### Share on other sites
Wouldn't it be better to tilt the camera to shake, rather than move up and down?

Of course, this is easier if your camera class has a forward vector.

Add a bit of realism with some motion blur, too?

##### Share on other sites
Hodgman    51230
If you want something more smooth, you could use waves. To get more random looking smooth waves, just add sin/cos waves of differing frequencies together.
if (cameraShakingUpdatesLeft != 0){  float shakeFreqX = 10;  float shakeFreqY = 8;  float shakeFreqY2 = 20;  float shakeSizeX = 15;  float shakeSizeY = 10;  float shakeSizeY2 = 5;  float t = theCurrentTimeInSeconds;  xAdjustment = sin( t*shakeRateX )*shakeSizeX;  yAdjustment = sin( t*shakeRateY )*shakeSizeY + cos( t*shakeFreqY2 )*shakeSizeY2;  camera.X += xAdjustment;  camera.Y += yAdjustment;  cameraShakingUpdatesLeft--;}

Quote:
 Original post by deadstarWouldn't it be better to tilt the camera to shake, rather than move up and down?

Not in 2D ;)

##### Share on other sites
Quote:
 Original post by HodgmanNot in 2D ;)

My mistake.

##### Share on other sites
sofakng    102
Quote:
 Original post by HodgmanIf you want something more smooth, you could use waves. To get more random looking smooth waves, just add sin/cos waves of differing frequencies together.

Thanks for that!

What type of value do I want to use for shakeRateX and shakeRateY?

Also, what should I initialize t to? (eg. does it start at 0 and then I add in how many real seconds have passed since my last update?)

##### Share on other sites
Hodgman    51230
Quote:
 Original post by sofakngWhat type of value do I want to use for shakeRateX and shakeRateY?Also, what should I initialize t to? (eg. does it start at 0 and then I add in how many real seconds have passed since my last update?)

You'll probably have to experiment with the shakeRate variables. If you set it to 1, then the wave will repeat itself once every ~6.3 (Pi*2) seconds. If you set it to 63, the wave will repeat ~10 times per second (assuming that t is increasing at a rate of +1 per second).

t should be a variable that is constantly incrementing. So yeah, you could start at 0 and add the elapsed time each frame.

cyyt2008    100
mark

##### Share on other sites
Stowelly    156
y(or x) = waveFrequency * sin(2 * pi*(time / waveLength))

time++

at least I think, really should know, just had an exam on this

##### Share on other sites
Hodgman    51230
Quote:
 Original post by Stowellyy(or x) = waveFrequency * sin(2 * pi*(time / waveLength))at least I think, really should know, just had an exam on this

I think "waveFrequency" should be called "waveAmplitude" ;)

##### Share on other sites
Stowelly    156
Quote:
Original post by Hodgman
Quote:
 Original post by Stowellyy(or x) = waveFrequency * sin(2 * pi*(time / waveLength))at least I think, really should know, just had an exam on this

I think "waveFrequency" should be called "waveAmplitude" ;)

y=R sin 2 Pi(x/Y - t/T)

R being amplitude

the Y representing the half life symbol being the wavelength

and T being 1/f

f being the frequency in htz

but just resolving it for which dependencies you actually require i.e in this case its not dependent on the X, can be rearranged and hacked to get the desired effect

##### Share on other sites
Daaark    3553
Just be sure to be subtle and don't violently shake the camera around. Less is more.