Sign in to follow this  
ryt

Camera shake

Recommended Posts

I was going to implement a camera shake. I thought to make it with some sin function like sin(2*x)/x. The shake would start at x = 1 and wound run for some time.

 

But this function goes to infinity. So is there a way to stop it so lets say at x = 5 the function is 0 ?

I could force it in code but that wound create a discontinuous function and I would get jerky motion.

 

Also this function at 0 has much higher values. It wound be better if that could be minimized.

Share this post


Link to post
Share on other sites

shape the falloff function so it goes from 1 to 0 in 5 seconds: f(x) = 1-x/5

 

Then that would give you: shake(x) = sin(2*x) * (1-x/5)

 

That would be a linear falloff and assuming x starts at 0.

 

This is called easing and here are some nice examples: http://www.gizma.com/easing/

Edited by Madhed

Share this post


Link to post
Share on other sites

One natural formula to use is sin(2*x)*exp(-x/decay_timescale). It doesn't go to 0 in finite time, but close enough for practical purposes.

 

You could also use a simple controller (e.g., a [url="http://en.wikipedia.org/wiki/PID_controller"]PID controller[/url], or a [url="http://en.wikipedia.org/wiki/Harmonic_oscillator#Damped_harmonic_oscillator"]damped harmonic oscillator[/url]) to make the camera reach the desired position dynamically, and add perturbations to it for shaking. This is a good model of what's actually going on and you can probably get it to look very natural in a wide range of situations.

Edited by Álvaro

Share this post


Link to post
Share on other sites

I've started using perlin noise for camera shakes instead of using spring-like solutions. It gives a similar result visually and will be completely frame-rate independent.

 

Spring/physics style solutions have a habit of behaving differently or even catastrophically falling apart when you run at very low or very fast frame-rates. Of course, that can be fixed by running that bit of the simulation at a fixed frame rate, but that didn't fit in nicely with my camera code which was independent of the fixed-rate update part of my simulation so for me, perlin noise was less hassle.

 

I suppose you lose some control though - e.g. with a physics based solution you could push the camera in a specific direction (say, in response to an explosion or bullet impact) and have it return. With perlin noise, My only control is fading in and fading out the shakiness.

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