Screen Shake

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

In order to complete this tutorial you will need a working monogame project, it will need to draw something to be able to see the screen shake.

Variables

Within the Game1.cs class, add the following variables with your existing ones:

bool shakeScreen = false;
Vector2 shakeOffset = new Vector2(15,0);
int shakeCount = 0;
in maxShakes = 10;

The boolean will set when the screen should shake, the vector2 called shakeOffset will be the direction and amount of shake (15 is the X value and 0 is the Y value). Finally shakeCount will be incremented so we can stop the screen shake when we hit a number of shakes.

Update Method

Now in the update method we need to have something to control when to shake, the code below uses the 'S' key. The shake is achieved by multiplying the shakeOffset by -1. This will alternate the shakeOffset between (15,0) and (-15,0):

            if (Keyboard.GetState().IsKeyDown(Keys.S))
                shakeScreen = true;

            if (shakeScreen && shakeCount<maxShakes)
            {
                shakeOffset *= -1;
                shakeCount++;
            }
            else
            {
                shakeCount = 0;
                shakeScreen = false;
            }

We also use shakeCount and MaxShakes to get the same number of shakes every time. The code above also switches the shakeScreen boolean back to false. So pressing the 'S' key once will do the full number of shakes and then stop.

Draw Method

We are going to use the shakeOffset and apply it to the spriteBatch when the shakeScreen boolean is true:

if (shakeScreen)
    spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, Matrix.CreateTranslation(shakeOffset.X, shakeOffset.Y, 0));
else
    spriteBatch.Begin();

If we want to shake the screen we use one spriteBatch.Begin() which applies a translation offset, and if we do not want a shake we use just a normal spriteBatch.Begin().

Alternative Idea

Variables

Within the Game1.cs class, add the following variables with your existing ones:

bool shakeScreen = false;
Vector2 shakeOffsetStart = new Vector2(20, 0);
Vector2 shakeOffset;
Vector2 shakeChange = new Vector2(2, 0);

The boolean will set when the screen should shake, the vector2 called shakeOffset will be the direction and amount of shake (15 is the X value and 0 is the Y value). the shakeChange is a vector which will be taken away from the shakeOffset for each shake. shakeOffsetStart is so we can return to the starting value for the next shake.

LoadContent Method

We need to give shakeOffset a starting value, so add the following to the LoadContent method:

shakeOffset = shakeOffsetStart;

Update Method

Now in the update method we need to have something to control when to shake, the code below uses the 'S' key. The shake is achieved by taking away the shakeChange vector from the shakeOffset vector:

            if (Keyboard.GetState().IsKeyDown(Keys.S))
                shakeScreen = true;

            if (shakeScreen && shakeOffset!=Vector2.Zero)
            {
                shakeOffset -= shakeChange;
            }
            else
            {
                shakeScreen = false;
                shakeOffset = shakeOffsetStart;
            }

When the shakeOffset vector is equal to Vector2.Zero (0,0), the code above also switches the shakeScreen boolean back to false and resets shakeOffset. So pressing the 'S' key once will do the full number of shakes and then stop.

Draw Method

Exactly the same as before.