Game Controls and Mechanics

Platforming Video Games

A genre that focuses on jump timing and distances

Requires physics, collision detection, and precise input handling

Physics

How hard can this be?

You move the character left or right when the arrows are pressed and fling them up when jump is pressed

Well...

Game Controls

Players will interact with the game controls constantly while playing

Small irritations can rapidly build up into huge ones!

Storytime: Fallen Order

Thanks Bing

Moving Character with Arrows

  1. Prevents player from easily making small adjustments
  2. Is frustrating to deal with when progressing if movement speed is too slow
  3. Robs player of experiencing a "flow" state since the second you let go of the control, you stop moving.

Almost no platforming games directly change player position based on control inputs.

Instead, apply a mix of forces depending on player state that are tuned to produce the desired effect.

Example: Running

When the character is grounded and an input is made

  • Apply a velocity change to the player
  • Rob the player of some velocity based on their current velocity. This achieves two things:
    • If no controls are held, the character eventually comes to a halt.
    • Leads to a velocity cap so you can't end up going lightspeed
  • Use high friction for "tight" controls.
  • Use low friction for more slippery controls.

Example: Jumping

When the character is airborne

  • Apply gravity to pull player down
  • Apply air friction controls to give some horizontal control

When the character is grounded and jump is input:

  • Apply an initial impulse to launch the player into the air
  • Change player state to airborne

What if jump is input while character is airborne?

The decisions about how and when to apply movement is very important!

No one "correct" way to do character controls

Character State

Control is more important than realism!

Consider a ground-air transition on a slope.

Character state controls almost everything

  • What actions can the character take?
  • What physics do we apply?
  • How do we draw the character?
    • Is the character moving left or right?
    • Is the character idling?
    • Is the character grounded or airborne?
    • Is the character in a damage state?

Character Facing (Direction)

How do we turn a sprite-based character around to face the direction we want?

If we want the character left and character right to look different, we typically have to draw different sprites.

Character Facing (Direction)

Use scale to flip the direction the character is facing.

Character Facing (Direction)

Use scale to flip the direction the character is facing.

We need to make sure the vertical axis is at the center of the character!

We need to make sure the vertical axis is at the center of the character!

Note: This is not always the same thing as making sure the vertical axis is at the center of the sprite...

Center of character

Center of sprite image

But for most sprites you can pretend that it is!

How do we track which way the character should be facing?

Player Collisions

Characters have to be grounded in order to jump

If the game has multiple air jumps, we have to check whether the character is airborne and decrement the jump counter, then reset when we hit the ground.

How do we check if the character is on the ground?

Collisions!

For simplicity, we usually use a "ground box," an AABB which detects collisions with platforms.

Ground Box

When the ground box intersects platform geometry, play the landing animation and then set character state to landed.

Does ground box check always have to be active?

What if we need the character to hit things or get hit by things?

More boxes!

Hitboxes and Hurtboxes

A hitbox is a box (usually AABB) which does damage. Represents the hurt-y part of an attack (like a sword blade or a laser beam).

A hurtbox is a box (usually AABB) which can take damage. Represents the squishy parts of a character.

Damage states occur when a hitbox intersects a hurtbox.

Hurtbox?

Hurtbox?

pew

How do we solve this?

Better Hurtboxes

But this requires more design time---always a tradeoff!

Hitboxes and Hurtboxes

Still exist in 3D games! Use bounding volumes instead of 2D boxes, or tie controls to screen space.

...now what?

The player got hit!

Just subtracting health and doing nothing else is terrible!

  • Player will not have an indication that anything occurred
    • "Where did my health go?"
  • Player can be hit by additional followup attacks

Instead, place players in a special damage-taken state.

Hitstun

A state where the player has taken damage.

  • A few frames of frozen animation to indicate that damage has been taken
  • Character usually made invincible until hitstun ends
  • Character often knocked back from attacking enemy
  • Player controls not processed until after hitstun ends

Input Buffering

Suppose a player has taken damage and wants to jump as soon as hitstun ends.

How should we handle this?

A) The player has to input a jump once hitstun has ended

B) If the player inputs a jump during hitstun, it is stored and applied once hitstun is over.

C) The player should get good and avoid being hit, like, ever.

Input Buffering

Input buffering stores player actions when in an inactionable state and applies them afterwards.

 

One potential implementation of input buffering:

public enum KeyInput { 
  UP, DOWN, LEFT, RIGHT }

boolean [] keyBuffer = new boolean[4];

boolean keyBuffered(KeyInput k){
  int index = k.ordinal();
  
  if(keyBuffer[index]){
    keyBuffer[index] = false;
    return true;
  }
  return false;
}

Sound

To add sound library in Processing, go to Sketch → Import Library →  Sound

// or add this line at the top of your file

import processing.sound.*;

Library contains lots of useful functions for sound manipulation and analysis, including:

  • Play/pause/loop for sound clips
  • Filters for modifying sound
  • Amplitude, beat, and pitch analysis
  • Fast Fourier transforms
  • Noise generators

SoundFile

If you need more functionality, look into Minim: https://github.com/ddf/Minim

SoundFile allows playback and manipulation of sound files

  • play()
  • loop()
  • stop()

Sound playback can start with the start of program, or can be triggered by specific actions

Index Cards!

  1. Your name and EID.
     
  2. One thing that you learned from class today. You are allowed to say "nothing" if you didn't learn anything.
     
  3. One question you have about something covered in class today. You may not respond "nothing".
     
  4. (Optional) Any other comments/questions/thoughts about today's class.