Jump to content
  • Advertisement
Sign in to follow this  
sofakng

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

This topic is 3714 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
y(or x) = waveFrequency * sin(2 * pi*(time / waveLength))

time++


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

Share this post


Link to post
Share on other sites
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" ;)

Share this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!