Camera
Learning paths through this topic
Dragging the map should glide on after letting go, but not when I stop first
Grabbing the map and throwing it feels natural, yet the usual version either never glides or flies off after a quiet pause before letting go. Here is how to read the throw from the last moments only, which way the map moves, and how it slows to a stop.
Dragging out a road wobbles between horizontal and vertical as the mouse moves
Laying a road by dragging should give a clean straight line, yet a diagonal mouse movement makes the line flip direction from one moment to the next. Here is how to pick the direction once, when to allow a change, and how the line shrinks back to a single piece at the start.
A large building is drawn over the small house in front of it, or under the one behind
Sorting buildings by the position of one corner works for equal tiles and fails as soon as a long building stands next to a small one. The picture then shows a wall poking through a house. Here is a rule that compares the footprints themselves and an ordering that always ends.
Mouse-at-the-edge scrolling is faster in corners and weaker on one side
Scrolling by moving the mouse to the edge feels uneven: corners rush ahead, one side is slower than the other, and a cursor that leaves the window can leave the view running. Here is how the edge strip should be read, and the calculation that makes all eight directions feel equal.
A far explosion shakes my screen as hard as one at my feet, and clicks go astray
In a game where you look at a large battlefield, a shake that ignores where the blast was is confusing, and a shake that moves the picture also moves what the mouse points at. Here is how the strength depends on distance and zoom, how it dies down, and what must not shake.
A unit disappears behind a tall building and the player cannot give orders
In a slanted view the buildings in front of a unit hide it. Fading every tall building looks messy, and fading none loses the unit. Here is how to find exactly the buildings that cover a unit, how tall they must be to do so, and how to fade them in and out without flicker.
Looking into a multi-storey building: show one floor without hiding the rest suddenly
Builders with tall buildings let players peel off the upper floors to work inside. When the floors simply vanish one by one, the picture jumps and a click can land on something that is no longer drawn. Here is a smooth value for the viewed floor, what fades when, and what can still be clicked.
Following a selected unit should end when I scroll away, but not on a twitch
A camera locked to a unit is useful until you want to look elsewhere, and then it fights you or lets go at the slightest touch of the mouse at the screen edge. Here is how the camera should decide that you really meant to look away, and when it must let go by itself.
Centering on my army shows empty ground because of one unit left far behind
The average position of a group is dragged away by a single unit that wandered off, so the view centers on nothing while the army stands elsewhere. Here is a way to find the middle of the main body instead, how many units to set aside, and what to do for tiny groups.
Far from the map's center the buildings and units tremble and snap
On a very large map, things drawn far from the starting point start to jitter, and slow movements turn into little jumps, because the graphics card works with less precise numbers than the game does. Here is the cure: draw everything relative to the camera and move the zero point when the camera gets far from it.
A key that takes me to where something just happened, then to the one before
When a base is attacked or a building finishes far away, players want one key to go there. Naive versions jump to places already on screen, forget old events or never let you reach the second one. Here is the order, the time limits and how long the trip should take.
Arrow-key scrolling starts and stops abruptly, and left plus right misbehaves
A camera that jumps to full speed and stops dead feels cheap, while one that coasts feels sluggish. Pressing two opposite keys together and turning around are where most versions go wrong. Here is a ramp with different rates for speeding up and braking, and what to do on reversal.