How Do You Make the Screen Shake?

Blitz3D Forums/Blitz3D Beginners Area/How Do You Make the Screen Shake?

One way would be to do this:

1. Attach camera to a pivot.

2. Move the pivot instead of the camera for normal camera movements.

3. Define two variables, Camera_Offset_X#, Camera_Offset_Y#.
This is the location in pivot space at which the camera should be positioned. These values should be 0 normally.

4. Each frame, move the camera a bit towards the position defined by these two variables, at a constant rate of speed.

5. When camera shake is needed, every X milliseconds, choose a new offset for the camera. The amount of this offset should be proportional to the amount of camera shake. Ie, Camera_Offset_X# = Rnd(0, 1) * ShakeForce#
Camera_Offset_Y# = Rnd(0, 1) * ShakeForce#


Because you are moving the camera towards the desired location at a constant rate every frame, when you change the offset the camera starts moving towards the new offset. You should make sure the camera can move fast enough to get to the offset before you choose the next one.

Also, the number of times per second you change the offset the camera should move towards affects how much vibration there is. The size of the offsets affects how firmly the camera seems held in place.

If you can simulate springs, then putting the camera on a set of springs and then applying a random force to it in a desired direction might work too and be more realistic.