Skip to content

CameraShaker

Mohamad Dabboussi edited this page Oct 17, 2023 · 3 revisions

CameraShaker Wiki Documentation

Introduction:

The CameraShaker class provides a simple yet effective mechanism to implement a shake effect on any camera in the libGDX framework. This effect can be useful to emphasize events or actions in a game, such as explosions, collisions, or in this particular case, the spawning of each wave. This class is loosely based on 'Mastering LibGDX Game Development' - Chapter 9 - Camera Shake.

Attributes:

  • camera: The camera on which the shake effect will be applied.

  • isShaking: A boolean to determine if the camera is currently shaking or not.

  • shakeRadius: The magnitude of the shake. Greater values will cause a more violent shake.

  • minimumShakeRadius: The minimum radius at which shaking effect should still be perceptible.

  • radiusFallOffFactor: Determines how fast the shake diminishes. The shake radius is multiplied by this factor in each update.

  • randomAngle: A randomly determined angle that's used to determine the direction of the shake.

  • origPosition: The original position of the camera before the shake began.

  • currentPosition: Current position of the camera during the shake.

Update Cycle:

The update cycle is integral to this class. Here's how it functions:

  1. startShaking(): Call this method to initiate the camera shake effect. This will set the isShaking attribute to true and reset all necessary parameters.

  2. update(float delta): This method must be called in the game's main update/render method. If the camera is currently shaking (isShaking is true), the camera's position will be updated based on the shake parameters.

  3. diminishShake(): As the name suggests, this method diminishes the shake effect over time by reducing the shake radius using the radiusFallOffFactor.

  4. reset(): It stops the camera shake effect and returns all parameters to their original state.

Usage:

To use the CameraShaker, first, instantiate the class with the desired parameters:

CameraShaker shaker = new CameraShaker(cameraInstance, shakeRadiusValue, minShakeRadiusValue, radiusFallOffFactorValue);

Or use the simplified constructor for default values:

CameraShaker shaker = new CameraShaker(cameraInstance);

Whenever you want to start the camera shake, for instance, when a wave gets spawned:

shaker.startShaking();

In your game's main update or render method, ensure that you're updating the shaker:

shaker.update(deltaTime);

That's all we need to do! The camera will shake and diminish according to the specified parameters.

Conclusion:

The CameraShaker class is a versatile tool to add immersion and emphasize events in your game. By understanding its attributes and the update cycle, developers can effortlessly integrate dynamic camera shakes to heighten game experiences. Whether it's the appearance of a formidable boss or a massive in-game explosion, the CameraShaker enhances visual effects.

Clone this wiki locally