If you are referring to that juddering screen shake from an explosion, I have tried to implement this in my prototype game (it's 2d but should translate to 3d quite well, I expect). Take a look at the youtube video and see if that's what you're looking for - the screen shake kicks off about 1:35
Anyway, the way I achieved it was to create a viewport for the sceen and store the centre point (centre
).
When an explosion kicks off, I create a random radius
of ~30px. I then offset the viewport from centre
by this many pixels in a random direction.
For each subsequent update (between draws to screen), I reduce radius
by 10-20% and offset the viewport by this many pixels from centre
again. However, instead of picking a random angle for this offset, I instead take the previous viewport angle from centre
and added 180+/-60 degrees to it.
So basically, each update (or 10-20ms or so), I offset the viewport by a diminishing number of pixels from the centre
of the screen.
Here's some pseudocode:
viewportCentre = (400,300) //Lets say screen size is 800 x 600
radius = 30.0
randomAngle = rand()%360
offset = ( sin(randomAngle) * radius , cos(randomAngle) * radius) //create offset 2d vector
viewport.setCentre(viewportCentre + offset) // set centre of viewport
draw()
while(true) //update about every 10-20ms
{
radius *=0.9 //diminish radius each frame
randomAngle +=(180 +\- rand()%60) //pick new angle
offset = (sin(randomAngle) * radius , cos(randomAngle) * radius) //create offset 2d vector
viewport.setCentre(viewportCentre + offset) //set centre of viewport
draw() //redraw
}
I usually stop the screen shake when radius gets lower than 2.0, at which point I reset the viewport back to centre
.