Looking for a code example for shaking the camera on player impact, or big explosion, or big collision?  Look no futher.

GitHub repository:  https://github.com/antzGames/libGDX-cameraShake

A HTML demo is provided above.

How to use the demo

CLICK ME TO RE-SHAKE button

Clicking this button will re-shake the camera with the currently displayed configuration parameters.

Randomize Parameters button

Click this button to randomize the 3 parameters and re-shake.

Default parameters button

Click this button to return to the default parameters. Here are the default parameters:

shakeRadius = 30f;
minimumShakeRadius = 3f;
radiusFallOffFactor = 0.90f;

Turn On/Off Sound

Sound can be toggled on/off with the this button.

Configurable Camera Shake for libGDX

This is a simple configurable camera shaker for libGDX. Has been tested on Desktop, GWT/HTML, and Android.

To use this camera shaker in your project, all you need to do is copy the CameraShaker.java class into your core project.

It is that simple. No dependencies or gradle configuration needed.

A runnable demo is included above.How to use

Create a CameraShaker instance

camera = new OrthographicCamera(); 
batch = new SpriteBatch();  
// Camera Shaker setup with default values 
shakeRadius = 30f; 
minimumShakeRadius = 3f; 
radiusFallOffFactor = 0.90f;  
cameraShaker = new CameraShaker(camera, shakeRadius, minimumShakeRadius, radiusFallOffFactor);


Start a camera shake

After a player was hit, explosion, impact or collision is the best time to shake the camera (and play a cool sound!). To start the camera shaking call the startShaking() method on the cameraShaker object:

// start a camera shake 
cameraShaker.startShaking();

Update the cameraShaker

In your main render/update loop of your game you need to call update() method on the cameraShaker object:

// Call camera shaker update method 
batch.setProjectionMatrix(camera.combined);  // not needed if not using a SpriteBatch
cameraShaker.update(deltaTime);

Look at demo source code if you still need help

If you still cannot get the camera shaker to work then look at the demo source code.

The demo source code is very simple, you can see it here.

Configurable parameters

shakeRadius

shakeRadius value affects the magnitude of the camera shakes. The larger the radius value, the larger the area on the screen that the shakes will affect. Determining the best value depends on your screen size and resolution.

minimumShakeRadius

minimumShakeRadius value should always be less than shakeRadius and your should target this value to be 5%-20% of the shakeRadius.

radiusFallOffFactor

radiusFallOffFactor value determines the speed in which the shaking radius diminishes. The value should be greater than 0 and less than 1, however the optimal ranges are between 0.8f and 0.95f.
Too small a value and the shake happens to fast, and the closer you get to 1 the longer the shake persists.

Limitations

Linear diminishing shake radius

The current code uses a linear diminishing radius for shaking. Future releases might use libGDX's interpolation.

StatusReleased
CategoryTool
PlatformsHTML5
AuthorAntz
Made withlibGDX
TagslibGDX, sourcecode

Development log

Leave a comment

Log in with itch.io to leave a comment.