User:Tepples/Platform game structure

From Pin Eight
Jump to: navigation, search

Some notes on what goes into programming a platform game (platformer) for any system:

Platformer physics in a nutshell

  1. Every object has a position, velocity, and facing direction. Velocity precision is finer than 1 pixel per frame, generally fixed-point or floating-point depending on the CPU architecture.
  2. Every frame, add an amount to the vertical velocity, and add the velocity to the position
  3. Push the object out of terrain
  4. If the object was pushed out of the floor, give it an option to jump and add to the horizontal velocity
  5. Move the camera horizontally toward 1/8 of a screen distance in front of the player
  6. Place the camera vertically to center where the player was last time it was pushed out of the floor, or move it downward if descending and there is a possibility of landing below the visible area. Controlled flight instead uses the previous rule.

Once you have the basics down, there are all sorts of fine-tuning knobs for player character control in platformers, such as these:

  • If A has been released and player is still rising, apply gravity at 4 times strength
  • Different amounts of horizontal acceleration depending if on ground (stopping and starting) or in air (bending a jump)
  • Some games don't move the camera while the player moves back and forth in a small space until the player moves farther away
  • Some games let other objects attract the camera while on screen

Some more jump fine-tunes:

  • Make the bounding box for pushing an actor out of the floor slightly wider than you might expect. This adds a bit of forgiveness that some people call "coyote time" in reference to Warner Bros.' Looney Tunes animated shorts.
  • Jump buffering: treat an A press within 8 frames before landing as a jump. This helps when jumping from stairs, especially in engines that treat slopes as stairs.
  • Wall jump: Some characters can jump if A is pressed within 8 frames before or after being pushed out of a wall.
  • Mantling: Some characters detect not only the feet but also the top front corner landing on something, in order to grab a ledge for a boost.