How would one of these work?

(If you don't know what I'm talking about, think like in a game where you shoot a big gun, and your screen bounces in the opposite direction.)

I've been experimenting with a couple different ideas but none are either fully working, or fully smooth.

Using this function for setting the camera position:

x = -target.x + halfScreenW;
y = -target.y + halfScreenH;

One problem I've run into is when setting the camera to move a certain distance in a direction, it will reach its target X or Y before the other, causing it to go travel in a straight line for a short while. I believe the solution to this is using the Bresenham line algorithm?

What would be the best way to achieve a camera shake?

edit: Related - Realistic Camera/Screen Shake from Explosion


1 Answer 1


A favorite way to apply camera shake in 2D (or 3D) is to attach the camera's origin to a physics particle, then to attach the physics particle to the focus of the scene with a heavily-damped spring. This allows for adding variable camera shake quite easily, in any direction you want, by adding momentary acceleration (a poke) to the camera's physics particle. As an added bonus, the inertia of the camera will make it move differently in response to a moving focus, which can make acceleration of the focus feel a lot cooler.

  • \$\begingroup\$ This sounds like something engine-specific, as you don't really define what a "physics particle" is. Essentially a point in space such as the camera's (x,y) would be a "particle", yes? \$\endgroup\$
    – user82739
    Commented Jul 27, 2016 at 0:31
  • 1
    \$\begingroup\$ Yep- all you need for the particle is a velocity vector and a position vector, and all you need for the spring is quick implementation of the formula F=-kX*0.99^|X| where k is a constant for how strong the spring is (probably pretty low- 0.05 would be a good place to start), X is the displacement vector (the difference of the camera's particle and the focus), |X| is the magnitude of the displacement vector, and F is the acceleration to apply to the particle. The code for this is pretty simple- I might post an example later. \$\endgroup\$ Commented Jul 28, 2016 at 15:29
  • 1
    \$\begingroup\$ Sounds like a good idea to me. And simple, as stated. Suggest OP gets the "spring effect" working in 1D first, then progress to using 3D vectors. Plain sailing thereafter. \$\endgroup\$
    – Engineer
    Commented Mar 9, 2017 at 12:47

You must log in to answer this question.