Sign in to follow this  
sofakng

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)
if (cameraShakingUpdatesLeft != 0)
{
  xAdjustment = rnd.Next(-15, 15); // get random number between -15 and 15
  yAdjustment = rnd.Next(-15, 15); // get random number between -15 and 15

  camera.X += xAdjustment;
  camera.Y += yAdjustment;

  cameraShakingUpdatesLeft--;
}
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 this post


Link to post
Share on other sites
deadstar    536
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 this post


Link to post
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 deadstar
Wouldn't it be better to tilt the camera to shake, rather than move up and down?

Not in 2D ;)

Share this post


Link to post
Share on other sites
sofakng    102
Quote:
Original post by Hodgman
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.

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 this post


Link to post
Share on other sites
Hodgman    51230
Quote:
Original post by sofakng
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?)

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.

Share this post


Link to post
Share on other sites
Stowelly    156
Quote:
Original post by Hodgman
Quote:
Original post by Stowelly
y(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" ;)


edit: ah yes your right



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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this