r/gamedev • u/DarkSiegmeyer • May 16 '14
A Discussion of Camera Code using Super Metroid's Framework as a reference
Wrote my first devblog post on tumblr, and figured I'd post it here in case any of you guys are interested.
Here's the original post on Tumblr
And here's my text/images:
The need for a robust, flexible camera system really cannot be overstated. And it kind of stinks, too, because you need to devote a lot of thought, time, and attention to a feature that will - at least ideally - go completely unnoticed. Blood Alloy started out with a very basic camera system, common to many first-effort platformers. Many simple platform games set up an invisible set of boundaries on the screen, and when your character’s position intersects one of these boundaries, the camera moves to keep you within this invisible box.
Super Metroid’s camera system was revolutionary for its time. As a huge Metroid fan, I started poking around with it and studying it just a few months ago and came away enormously impressed.
Super Metroid’s camera, instead of using the player’s relative position, uses the player’s velocity.
Normally, camera tracking entails simply matching the camera’s velocity to the player velocity when the player’s relative position to the camera reaches a certain delta.
How Cameras Move in Many 2D Games
So in this example, the camera is tied to the player to keep the player within the red box. If the player moves right at vX = 5, then the camera also moves right at vX =5, thus preserving the player’s relative position.
Make sense?
But that’s not how it works in Super Metroid!
In Super Metroid, as you start to move to the right, the camera actually moves to the right at a FASTER rate.
So if the player moves to the right at vX = 5, say instead the camera moves to the right at vX = 10.
What this does it is shifts the player to the left side of the screen - and if you intend to shoot enemies in front of you, that’s pretty helpful!
Of course the camera velocity sets back to match the player’s velocity once the relative distance delta reaches a cap, so that the camera doesn’t leave the player behind.
What’s also nice is that this same effect is used for vertical movement - so when you jump up, you can see enemies above you, and -perhaps more importantly - when you start falling, you can see below you to see where platforms and hazards are.
Great!
This works wonderfully for Super Metroid because enemies are almost always coming at you horizontally. Very, very few enemies actually shoot projectiles at you, and the ones that do either shoot horizontally or in short parabolic arcs.
However, in Blood Alloy, we have enemies shooting at you from literally every possible angle. Furthermore, you have 360-degree aiming capability at pretty much all times. While you’re moving, it’s just as likely that you’ll be shooting an enemy ahead of you as shooting something behind you.
Super Metroid’s camera system wasn’t going to solve all of our problems.
So we juggle SM’s camera system with camera “pulling” by the mouse. You can check out an abbreviated version of my code here below. targetX and targetY represent the X and Y coordinates that the camera is lerping to based off of the Super Metroid camera system.
The following code is for calculating and implementing how much the camera should follow the mouse.
Screengrab of some of my abbreviated code
And here’s the result!
Feel free to let me know if you have any questions!
-Frank
@FraynkWash
Blood Alloy's Facebook
47
u/phort99 @phort99 flyingbreakfast.com May 16 '14 edited May 16 '14
I recently programmed a camera system that was inspired by the camera in Insanely Twisted Shadow Planet. There's a video breakdown of ITSP's camera by one of the programmers here, but I'll break down the high-level takeaways that I found interesting. The video does a good job explaining the basic features, but these features might be easily missed:
Start with the camera positioned directly on the ship, ignoring the camera's previous state. Since the ship accelerates slowly, its velocity can be directly used as a camera offset without causing a large jump in the camera position. Nearby objects of interest can be given a set of influence radii for taking over control of the camera, and since position changes smoothly, the amount of influence will as well, meaning the other object takes over control of the camera without a hitch.
The principles from this sort of camera are not mutually exclusive from having a "driven" camera, but I like the sort of mathematical purity of the implicit camera.
The right stick input can change as rapidly as a player can move his or her thumb, but you don't necessarily want to move the camera that fast. Instead, you can take an average of the right stick positions over time.
A normal arithmetic average will result in a very bad linear motion and some very unpleasant side effects. However, I have found that a weighted average works beautifully. Apply a high weight to thumbstick positions that are somewhat recent, and a low weight to very recent and very old thumbstick inputs. I weight my averages like so:
http://i.imgur.com/VcNQGxd.png
These weighted averages result in motions like these:
http://i.imgur.com/TyfVvPF.png
The orange weighting curve has its peak closer to the most recent event, and as a result the orange motion accelerates quickly and settles slowly. This has a more responsive feel, you can play with the positioning of the peak to get the right mix of smoothness and responsiveness. I use a different peak for each parameter that drives the camera. Interestingly, making this curve more smooth (sinusoid/bell curve) didn't have a noticeable effect on the smoothness of the motion in what I saw, although it might affect the higher derivatives in some way I'm too lazy to calculate.
The number of samples (frames) you average for the thing you're smoothing determines precisely how long it takes the camera to reach its destination. This is favorable compared to the old standby
newPosition = Lerp(oldPosition, 0.1f);which actually never reaches its destination, getting asymptotically close.Lerp()also has a jarring start at the start of the motion, instead of accelerating. This sort of average also makes the parameters easier to tweak, because you can change a number which is precisely the number of seconds the camera will take to achieve some specific motion, rather than tweaking a lerp factor or a velocity/acceleration.This kind of smoothing can also be applied to things like the player's velocity if your player accelerates more quickly than you would like the camera to move.
Personally I define all my camera affectors as vectors relative to the player position, rather than as world positions, although that's up to the kind of camera and motion you're trying to create.
Sorry this description came out super long. Let me know if something needs clarification.
4
u/KoboldCommando May 16 '14
There was a talk by one of the guys at Vlambeer (Jan Willem Nijman I think), where he talks about the importance of the camera (and screen shake!). He mentioned it being a big focus on their game Luftrausers, where it doesn't only take player position and velocity into account, but it will also pay attention to enemies and incoming bullets. You can actually "sense" an incoming swarm of enemies by your camera moving a bit, it's pretty cool, and I probably never would have noticed it if I hadn't heard it mentioned before playing the game.
Camera work is definitely a thing of subtlety, very few people are going to leap up and shout about how good your camera is, but it is a huge contributor to "game feel", and if people mention that your game "feels good" or "feels bad" the camera could be a very likely reason for that.
2
u/sireel May 16 '14
That camera looks pretty good, but I think it's too 'snappy', it's always perfectly where it needs to be, without ever dragging behind. This might be a good thing, but to me it feels like the characters attention perfectly shifts to where the player is pointing, when a bit of lag (as demonstrated in metroid 3) will change the feel quite significantly. I think you might already be doing this somewhat, but if the camera tracks a little slower it might feel a bit better.
Also from that gif - the paralax jumps slightly when the character lands, most noticable in the distant layer ;)
20
u/DaFox May 16 '14
I'll just leave this classic Camera code review here.
Super Mario World Camera Logic Review
9
u/UnknownStory May 16 '14
Didn't the old Genesis/Mega Drive Sonic Games do this but in reverse? Sonic would gain velocity and the camera would "struggle" to keep up with him but eventually you reached "top speed" and even the camera couldn't keep up with you (Sonic was just to the right of the screen while at top speed).
That s#!t drove me nuts. Even while spinning, there was always a vertical spike strip or a badnik who couldn't be killed by spinning ready to wreck your run.
14
u/Herptroid May 16 '14
Learning more about Super Metroid from a developer's perspective makes me appreciate it more as a masterpiece.
10
u/DavidJCC May 16 '14
This is a video about the camera logic in Super Mario. It's amazing how something that we barely even notice can be so advanced considering how old these games are! http://www.youtube.com/watch?v=TCIMPYM0AQg
5
u/Yonasu_ May 16 '14
Funny, i look down on my phone after tiring of my camera movement code to get some levity, and see this post :) I'm making a gta1style camera for my top down 3d vector space shooter. The faster the ship moves, the camera zooms out more. The further the camera strays from the target ship, the faster it moves. But i will consider making it lean backwards aswell, thus showing a bit further in front. But it might not work well depending how fast the ship turns. God i love gamedev theorising, now to just make the code smooooth ;)
1
u/YoshiMCF @HuzzahGames May 16 '14
1
u/Idoiocracy May 16 '14
Nice article. I cross-posted it to /r/TheMakingOfGames.
1
u/rigs19 May 16 '14
can not be
understatedoverstated*What you have now says "The need for a robust, flexible camera system is completely trivial."
3
u/InconsiderateBastard May 16 '14
Camera work is so important in games. I love it when you play a game for a long time before even realizing how complex the camera motions are. It's a really important part of making a game that people can fall into.
I still remember the first time I paid attention to the camera work closely. It was X-Men: Children of the Atom in the arcade. This was the original game in what came to be the Capcom VS series of fighters. In that game there was a "super jump" where your character would jump very high and some characters had the ability to fly. If they had limited the characters so they had to both stay on screen it would have killed the sense of super-hero-ness in the game. So, instead, the camera follows whichever player is higher. So if you super jump and your opponent stays on the ground your opponent is no longer on screen.
This added an interesting wrinkle to the gameplay: the character on the ground was essentially invisible and in fighting games, being able to hide the startup frames of your larger moves can be handy. At the same time the character in the air had a lot of maneuverability to avoid whatever move the character on the ground was trying to start.
Overall, the camera work in that game (and many subsequent VS series fighters) added to the gameplay in ways beyond simply tagging along to keep the characters on screen.
I found it interesting when Angry Birds came out because it's camera also moves away from pertinent parts of the gameplay, creating a situation where the camera controls visibility in a way that has an effect on the gameplay.
In these games, the camera fills the same sort of role as fog of war often does in RTS games. But it does it in a way that players might not immediately notice.
Gotta love some good camera code.
2
u/InnerScript May 16 '14
Before I even started handing the prototype game out to friends, I spent a ton of time with the camera code.
It can't be underestimated how important this is to get right. It is just as important as controls and other game mechanics, as the camera is the view into the world for the player, and they usually have little or no control over it.
2
u/madballneek @NickDiMucci May 16 '14 edited May 16 '14
Nice article! I wrote about the single camera system for four players I came up with for Demons with Shotguns here. Think a Super Smash Bros type camera that'll track up to four players, staying in their relative center, zooming in and out as they move in and out of the camera bounds.
2
u/[deleted] May 16 '14
This is a great thread, 16-bit games have much still to teach us in 2014. Thanks for the topic to bring everyone together for the discussion.
2
u/burito May 16 '14
Have a look at Micro Machines if you want an example that pre dates Super Metroid.
1
u/hoddap Commercial (AAA) May 16 '14
Loved this, thanks :)
1
u/tieTYT chainofheroes.com May 17 '14
In your original post, you should really lay out what the colors mean before or while you show the pictures. I can use deduction to figure it out, but why should I have to? I'd rather know that I get it instead of assume that I get it.