Skip to content

A camera that follows the player

In the starter project the window shows a world exactly its own size, and the greeting can be moved off its edge and out of sight. This page makes the world bigger than the window and adds a camera that follows the greeting, so that the greeting stays in the middle and the world moves past it. Following is your game’s own code, a few lines in HelloState; the engine supplies the camera and draws through it. It takes about 20 minutes.

Before you start, do Make a first change, so that you know the edit, build and run loop and have a copy of your game to go back to.

If you have done the F11 section of Colours, the window and the background, undo it first, before making a new copy: put game\states\hello_state.cpp and game\states\confirm_state.cpp back from the copy you made before that section, as Getting a file back shows. That section’s fit_view and this page’s camera both choose the view, and a game has one or the other. Make again any of that page’s other changes to hello_state.cpp that you want to keep, such as the colour, then build, test and run to see that the game works.

Then make a fresh copy with Make a first change’s step 1 line.

Every command runs in the x64 Native Tools Command Prompt, in your project’s folder:

Terminal window
cd /d C:\dev\mygame
  1. Put something in the world to see. With a camera on the greeting, the greeting stays in the middle of the window and only what is around it moves, and the starter’s world is empty. Signposts, each saying where it is, show the world going by. Open the state’s code:

    Terminal window
    notepad game\states\hello_state.cpp

    Search with Ctrl+F for this->greeting_ = this->scene_->add(, in init(). Click at the start of that line and paste these lines above it, with an empty line after them:

    // Signposts every 400 pixels, from -2000 to 2000 across and down, each
    // saying where it is, so that there is something to see go by.
    for (int y = -2000; y <= 2000; y += 400)
    {
    for (int x = -2000; x <= 2000; x += 400)
    {
    this->scene_->add(std::make_unique<Label>(
    L"+ " + std::to_wstring(x) + L", " + std::to_wstring(y),
    font_name, Vector2F(static_cast<float>(x), static_cast<float>(y)),
    }
    }

    The two for lines repeat what is inside them: for each y from -2000 to 2000 in steps of 400, and inside that, for each x the same, one label is added at that point, reading + 400, 800 and so on: 121 signposts over a world 4000 pixels across. Points left of and above the window’s top-left corner have negative numbers. They are added before the greeting, so the greeting is drawn over them.

  2. Keep the hint out of the world. The hint along the bottom is in the scene too, so once the camera moves, it would move away with the world. It is drawn separately instead, over the world, without the camera. Still in init(), search for this->hint_ = this->scene_->add(:

    samples/minimal/states/hello_state.cpp
    this->hint_ = this->scene_->add(std::make_unique<Label>(
    L"Left stick, arrows or WASD move it. B or Escape asks to quit. Touch shows a marker.", font_name,
    Vector2F(24.0f, resolution.y - 40.0f), resources,

    Change the first line to

    this->hint_ = std::make_unique<Label>(

    and the last to end with one ) fewer:

    The hint is now made but not added to the scene. Then go to the end of the file, to draw:

    samples/minimal/states/hello_state.cpp
    this->scene_->draw(renderer);

    Replace that line with:

    this->scene_->draw(renderer, [this](int /*view_index*/, DrawList& list)
    {
    // Over the world, with no camera: the hint stays at the bottom of
    // the window wherever the camera is.
    list.set_camera(Camera::default_camera);
    this->hint_->draw(list);
    });

    What is between [this] and the last } runs after the scene has drawn the world, with list, what is being drawn into. Camera::default_camera is no camera at all: what is drawn after it is placed in the window’s own pixels.

  3. Follow the greeting. Search for void HelloState::on_suspend(). The lines just above it end update():

    samples/minimal/states/hello_state.cpp
    this->scene_->update(dt);
    this->scene_->end_tick();

    Click at the end of this->scene_->end_tick();, press Enter twice, and paste:

    // The camera: the point the greeting circles, in the middle of the
    // window. A camera's translation is the world point it puts at the
    // window's top-left corner.
    const Viewport window =
    this->app_->viewport_manager()->fullscreen_viewport();
    this->scene_->clear_views();
    this->scene_->add_view(window,
    Camera(this->motion_.centre - window.size() * 0.5f, 1.0f));

    A view is a rectangle of the window and the camera it is seen through. Every frame, these lines take away the view the scene had and give it a new one: the whole window, through a camera whose top-left corner is half a window up and left of motion_.centre, the point the greeting circles. That puts the point in the middle of the window. 1.0f is the camera’s scale: 2.0f would show the world twice as big.

  4. Keep the touch markers under the fingers. A finger is reported in the window’s pixels, and the markers are now in the world. Search for marker->set_position(:

    samples/minimal/states/hello_state.cpp
    marker->set_position(Vector2F(
    static_cast<float>(points[i].position.x),
    static_cast<float>(points[i].position.y)) +
    touch_marker_offset);

    Replace those four lines with:

    const Scene::View& view = this->scene_->view(0);
    marker->set_position(
    view.camera.world_position(points[i].position, view.viewport) +
    touch_marker_offset);

    world_position turns a point in the window into the point in the world the camera shows there. Save and close Notepad.

  5. Change the hint’s type in the header. The state’s header says what kind of thing hint_ is: until now, a pointer to a label the scene owns. Open it:

    Terminal window
    notepad game\states\hello_state.h

    Search for hint_ = nullptr; and change the line to:

    std::unique_ptr<labrador::Label> hint_ = nullptr;

    A std::unique_ptr owns what it points at: the state now owns the hint, since the scene does not. Save and close Notepad.

  6. Build, test and run. Quit the game if it is open, then run the line from Make a first change’s step 5:

    Terminal window
    cmake --build --preset x64-debug && ctest --preset x64-debug && out\build\x64-debug\game\MyGame.exe

    It compiles hello_state.cpp and main.cpp, which includes the header; on a fast desktop the line took 5 seconds before the game opened. It ends like this:

    Output
    [3/5] Building CXX object game\CMakeFiles\MyGame.dir\states\hello_state.cpp.obj
    [4/5] Building CXX object game\CMakeFiles\MyGame.dir\main.cpp.obj
    [5/5] Linking CXX executable game\MyGame.exe
    Test project C:/dev/mygame/out/build/x64-debug
    Start 1: MyGameTests
    1/2 Test #1: MyGameTests ...................... Passed 0.01 sec
    Start 2: MyGameSmokeTests
    2/2 Test #2: MyGameSmokeTests ................. Passed 0.78 sec
    100% tests passed, 0 tests failed out of 2

    The greeting circles in the middle of the window, a little to the right because the label starts at the point it circles, with signposts around it: + 0, 0 near the top-left, then + 400, 0 and + 800, 0 across the top. Hold the right arrow: the greeting stays in the middle and the signposts slide left, past + 1200, 400 and + 1600, 400. The hint stays along the bottom, and Escape still shows the quit question in its usual place.

The signposts end at -2000 and 2000, but the camera goes on following, into empty black. A camera that stops at the world’s edge keeps its corner between two limits. In game\states\hello_state.cpp, search for #include <cstddef> and add a line before it:

#include <algorithm>
#include <cstddef>

<algorithm> has std::clamp. Then search for touch_marker_offset =, near the top, click at the end of that line, press Enter twice, and paste:

// The edges of the world, a little outside the signposts: the camera
// shows nothing outside them.
constexpr float world_min = -2100.0f;
constexpr float world_max = 2300.0f;

Last, in the camera lines from step 3, replace the last two lines,

this->scene_->add_view(window,
Camera(this->motion_.centre - window.size() * 0.5f, 1.0f));

with:

Vector2F corner = this->motion_.centre - window.size() * 0.5f;
corner.x = std::clamp(corner.x, world_min, world_max - window.width);
corner.y = std::clamp(corner.y, world_min, world_max - window.height);
this->scene_->add_view(window, Camera(corner, 1.0f));

std::clamp(value, low, high) gives value, or low if it is below that, or high if it is above. The corner never goes left of or above the world’s edge, and never so far that the window would show past its right or bottom edge. Build, test and run, and hold the left arrow: once + -2000, 0 reaches the left of the window, the camera stops and the greeting goes on, off the middle to the left. The greeting itself still walks out of the world and out of sight; keeping it in is a rule of your game, and Add a rule and a test writes one that keeps it in the window. The world must be wider and taller than the window, or the two limits cross; with a world 4400 pixels across, that holds for any window up to 4K.

This camera is locked to the greeting. Games often let the player move a little before the camera follows, a dead zone. The local-multiplayer sample’s FollowCamera supplies that as copyable game code, with world-edge clamping. It adds no engine follow-camera policy or smoothing system.

Copy follow_camera.h and follow_camera.cpp from the development sample into your game. Change the source’s first include to your new header path and list its .cpp in both application build lists. The helper uses the existing Camera API; updating an engine pin alone does not change the code you copied into your game. The copied type is multiplayer::FollowCamera unless you choose another namespace.

Keep one value per followed player. During update, give it the target’s world point, the world view’s size, a dead-zone rectangle in that view’s local pixels and the world bounds. Then copy its camera into the scene’s view list, as the maintained sample does:

samples/local_multiplayer/play_state.cpp
this->cameras_[player].update(this->arena_->position(player),
world_view.size(), dead_zone, this->arena_->bounds());
this->scene_->add_view(world_view, this->cameras_[player].camera());

The first update starts from a centred view, then applies the zone and world constraints. An inclusive dead zone holds the view still until its target crosses an edge; the camera moves only by the excess. The world clamp wins near an edge, so the player can leave the zone while the view stops. A world axis no larger than the view is centred with equal outside space. On resize, the previous world-space view centre is preserved before applying the new zone and bounds. camera() only reads the result; drawing never advances following. The sample keeps a 68-pixel HUD strip outside each world viewport so a player at the top edge stays visible below it.

The helper’s header states its finite-input and positive-viewport contract. The headless tests cover every edge, small worlds, resizing and independent tracking values. Gradual catch-up would be a separate game choice; this component does not interpolate or rotate the camera.

Two players in your game gives each player a camera of their own in half of the window.

If the hint is missing, or moves away with the signposts, step 2 is not complete: the hint is still added to the scene, or draw does not draw it. If the signposts are missing, step 1’s lines are not in init() before the greeting, or are inside another block. The line numbers in the messages are those of a starter with only Make a first change and this page’s steps 1 to 5 done; other changes move them.

Build the page as Build for the browser shows, in a prompt where you have run emsdk_env.bat:

Terminal window
cmake --build --preset wasm-debug && ctest --preset wasm-debug

The page follows the greeting in the same way, and the page check from that guide, with --quit-keys Escape,Enter, still passes.

On 31 October 2026, in a starter project made from a starter ZIP of this tree, with Visual Studio 2026’s compiler 19.51 and Emscripten 6.0.12, every step above was made as worded by an agent editing the files with tools rather than Notepad, and the build and tests ran as shown, with the project’s folder in the quoted output changed to C:\dev\mygame. The game was started and driven with key presses, and screenshots showed the greeting in the middle among the signposts, the signposts moved after holding the right arrow and then the down arrow, the hint fixed along the bottom, and the quit question in its usual place after the camera had moved. After the world-edge section, holding the left arrow stopped the camera with + -2000, 0 at the window’s left while the greeting went on to the left and, held longer, out of sight. The C2440 and the start-up failure were made on purpose; the number in the start-up failure was 7 in that run. The page was built under wasm-debug, its test passed, and headless Chrome showed the signposts moved after the right arrow was held; the page’s run ended through Module.onQuit after Escape and Enter. Touch was not tried: the markers’ new lines were built, and that they sit under a finger is read from the camera’s world_position.

A second agent then followed the page as worded in another fresh starter, after Make a first change’s steps 2 to 4. It built and tested every step on x64-debug and wasm-debug, and in headless Chrome the camera followed the greeting and stopped with + -2000, 0 at the window’s left. It sent no keys to Windows windows after its first runs. The page was corrected for what it found: the C2440 message named line 109, which is the line only after Stop at the edge of the world, and Before you start offered to skip the step that makes the camera follow. The agent that made the corrections repeated steps 1 to 5 in a third fresh starter, made the C2440 at line 103, and at line 109 after the world’s edge, and a screenshot of the game’s own window, taken without sending it keys, showed the greeting in the middle among the signposts and the hint along the bottom.

Documentation for Labrador v0.4.0. Built from Labrador f525bc6 of 2026-11-17.

All documentation versions