Monday, June 25, 2012

My View on 2D Platformer Cameras

Everybody knows games live and die by their camera. It doesn't matter what the nature of the game is, if the camera isn't able to properly frame the action, the player will undoubtedly get frustrated and the game ceases to be enjoyable (obviously this is more common in certain types of games, for example I've never played a Tetris game where I was upset by the camera). This being said, talk of cameras is usually reserved for 3D games, and very rarely do you hear talk of how the camera is handled in a 2D game. Maybe this is because 3D camera work is inherently more complicated, or maybe even that relatively bad 2D cameras are usually not the end of the world and the games are still playable. Regardless, Internet, prepare to be enriched as I am going to post my dissection of camera work in 2D platformers as well as how all this applies to the game I am making.

Why is the Camera so Important?

As I said earlier, the camera is important because it frames the game. The camera, which is just a word for the visible area rendered to the screen, is the player's only gateway into the world you have them playing in. Skew their view too much in one direction, or make their character difficult to follow with a constantly adjusting camera and the play experience is seriously hurt. It's very simple: a player can't react to what they can't see, and a game is no fun when you aren't given the tools or time necessary to react to a situation, and is even less fun when you don't even know what that situation is in the first place. Nobody likes being killed by off-screen enemies or falling into pits that were hidden below their field of view.

How is it Usually Done?

First of all, we have to realize that there are two popular types of platformers each with their own typical camera system. There is the sidescrolling platformer, which usually uses what I like to call a floor-locked camera system, meaning there is little to no movement on the vertical axis and the floor is visible at all times. This type of camera system also usually strives to keep the player in the center of the screen on the horizontal axis (with some games allowing the camera to lead the player somewhat). This works very well in most cases, as the typical primary ability of a platformer character is jumping, which requires vertical visibility. This system is also elegant in it's treatment of the staple sidescroller danger known as bottomless pits as the player knows that any fall going past the bottom of the screen will result in death, avoiding the common annoyance a player faces of deliberately falling down a bottomless pit thinking they will survive (spoiler: they usually don't).  Obviously, as sidescrollers have grown in complexity over the years, the rigidity of this system has loosened significantly and minimal vertical scrolling has been introduced. However, the essence of the system, always keeping what the player is standing on in clear view, has not disappeared.

The NES classic Super Mario Bros. is a prime example
 of the floor-locked camera system

The other most popular type of platformer is the open-world platformer, where levels are non-linear and players are free to explore in every direction. The vast majority of these games use a camera which directly follows the player, attempting to keep him in the center of the screen (or near the center) whenever possible, I call this system player-locked. Often these games also have certain areas like boss fights or smaller rooms where the camera behaves in a more traditional sidescroler manner. In my humble opinion, this system is functional yet flawed. It is functional in the capacity that it allows the player to see equally in every direction, something that is important as the player can be expected to move in any direction at any given point. As a side note, this is much better than a heuristic camera (where the camera tries to predict where the player will be, based on his position and velocity) because it is predictable, something that is very important as an unpredictable camera feels chaotic and annoying. The system is flawed however, in that it very rarely frames the action in the most appropriate way. Typically platformer characters can jump very high and it is not uncommon for the camera to track them in such a way that at the peak of their jump the player can not see where they were standing. This makes any kind of horizontal jumping (jumping across a gap for example) needlessly frustrating as the player needs to remember where platforms are so that he does not overshoot or undershoot them. It is much more enjoyable for the player to be able to see his desired landing point for the entire duration of a jump. The other big problem with a player-locked camera is that it moves around a great deal and feels especially jerky during the typical rapid upwards acceleration of a character's jump.

Super Metroid for the SNES uses a mixture of the floor-locked and player-locked camera 
systems based on the type of room the player finds themselves in

What Happens When it Goes Wrong?

Though I've gone over why the camera is important and the problems a bad camera can cause for the player, I just wanted to use this section to present a concrete example of what a broken platformer camera looks like. Here is a video of Frogger Advance: The Great Quest, for the Game Boy Advance. The game is a sidescroller but uses the player-locked camera system and it seriously hampers gameplay. You will see in the video that it is extremely difficult to judge jump distances as when the player is in the air they cannot see the platforms beneath them. I would wager a guess that without the floating mechanic (where Frogger slowly floats downwards) the game would be unplayable due to the large amount of player deaths caused by the platforms being off-screen. It is worth noting that the camera is not the sole cause of this issue, the enormous character sprites on the small Game Boy Advance screen also add to the difficulty in seeing what is around the player. Earthworm Jim for the Game Boy Advance is another example of large sprites on a small screen with a player-locked camera seriously hurting a game.

There are very few videos of this game on YouTube, but this one
perfectly sums up the issued with the game's camera

So What Did I Do?

As I said yesterday, my game is a platformer with semi-large open levels in which the player can move in any direction. I did not, however, use the player-locked camera system because of the flaws I presented earlier. Instead, I opted for my own system (and this being 2012 I'm sure it's been used before, and has a name, and a wiki about it) that I call a platform-locked camera system. What I initially did was created a rectangle around the player (a window if you will) that could be moved by the player pushing against it's edges both vertically and horizontally. The camera was tied to the position of the window, not the player. This allowed the player some leeway in vertical and horizontal movement without affecting the camera, which I thought was important in making the camera feel stable rather than chaotic. I noticed, however, that this brought up an important issue regarding the rectangle's size. If the rectangle was too short, the camera would still scroll vertically when the player jumped  (again I don't want the camera moving that much). The obvious solution was to make the rectangle taller, but this caused the player to have too much vertical movement when falling before reaching the bottom edge of the rectangle, not offering the player enough downwards visibility. 

So what I have implemented currently is a mix between the floor-locked system and the window system. This system keeps the same functionality as the window system on the horizontal axis, but works differently on the vertical axis. Vertically, the camera keeps itself locked to the height of whichever platform the character was last standing on provided the player stays at that altitude or higher. If the player drops below the platform, the camera will follow him until he lands on a new platform. This allows the player to jump without the camera moving to follow his accelerated upwards momentum and gives a very smooth result (in my humble opinion). 

There was one last question, however, and this was where the player should be located on the screen (for example, the center of the screen as seen in the player-locked camera system). I decided to keep the player on the bottom portion of the screen (centered horizontally) and allow him to look downwards when standing on a platform (with the down arrow key). I chose this position for the simple reason that the player has more control when going upwards (as jumping is the primary means of movement) and should therefore be able to see more of what lies ahead. Given that the camera is locked to always allow the player to see any platform he is trying to jump to laterally (and downwards to a certain degree), the only time the player must move long distances downwards is if they are deciding to fall off a platform. If they chose to do this they can simply look down before committing to their descent.

This will all make more sense when you get to try the system, which incidentally happens to be right now. Keep in mind all art and sound and level design and everything else is place holder. (NOTE: There is a noticeable frame rate drop when uploaded for some reason, I will find the cause later and eradicate it)

Arrow Keys or WASD to move (Down Arrow or S to look down)
X and C for the two bullet types (explanation of this mechanic is in yesterday's post)
T to toggle visibility of the camera rectangle

EXTRA CONTROLS (you don't need these, but they are there)
Press Escape to toggle in and out of editor mode
Hold down F to enable fast scrolling
Use the mouse the place blocks
Hold control and click to remove blocks
Press E to clear the screen
Use Space-S and Space-O to save and open a level

And that's what I think of platformer cameras, I'd be happy to hear your thoughts on the subject in the comments (found below the post's title).


  1. Interesting. The 'platform-locked' camera system you describe is very similar to the camera used in the Sonic games on the Megadrive.

  2. Should there be another persuasive post you can share next time, I’ll be surely waiting for it. Surveillance Cameras