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:
cd /d C:\dev\mygame-
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.cppSearch with Ctrl+F for
this->greeting_ = this->scene_->add(, ininit(). 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){L"+ " + std::to_wstring(x) + L", " + std::to_wstring(y),}}The two
forlines repeat what is inside them: for eachyfrom -2000 to 2000 in steps of 400, and inside that, for eachxthe same, one label is added at that point, reading+ 400, 800and 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. -
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 forthis->hint_ = this->scene_->add(:samples/minimal/states/hello_state.cpp L"Left stick, arrows or WASD move it. B or Escape asks to quit. Touch shows a marker.", font_name,samples/minimal/states/hello_state.cpp, lines 90–93 at
f525bc6Change the first line to
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:
{// Over the world, with no camera: the hint stays at the bottom of// the window wherever the camera is.this->hint_->draw(list);});What is between
[this]and the last}runs after the scene has drawn the world, withlist, what is being drawn into.Camera::default_camerais no camera at all: what is drawn after it is placed in the window’s own pixels. -
Follow the greeting. Search for
void HelloState::on_suspend(). The lines just above it endupdate():samples/minimal/states/hello_state.cpp this->scene_->update(dt);this->scene_->end_tick();samples/minimal/states/hello_state.cpp, lines 184–185 at
f525bc6Click 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.this->app_->viewport_manager()->fullscreen_viewport();this->scene_->clear_views();this->scene_->add_view(window,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.0fis the camera’s scale:2.0fwould show the world twice as big. -
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 static_cast<float>(points[i].position.x),static_cast<float>(points[i].position.y)) +touch_marker_offset);samples/minimal/states/hello_state.cpp, lines 168–171 at
f525bc6Replace those four lines with:
marker->set_position(view.camera.world_position(points[i].position, view.viewport) +touch_marker_offset);world_positionturns a point in the window into the point in the world the camera shows there. Save and close Notepad. -
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.hSearch for
hint_ = nullptr;and change the line to:A
std::unique_ptrowns what it points at: the state now owns the hint, since the scene does not. Save and close Notepad. -
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.exeIt compiles
hello_state.cppandmain.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.exeTest project C:/dev/mygame/out/build/x64-debugStart 1: MyGameTests1/2 Test #1: MyGameTests ...................... Passed 0.01 secStart 2: MyGameSmokeTests2/2 Test #2: MyGameSmokeTests ................. Passed 0.78 sec100% tests passed, 0 tests failed out of 2The 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, 0near the top-left, then+ 400, 0and+ 800, 0across the top. Hold the right arrow: the greeting stays in the middle and the signposts slide left, past+ 1200, 400and+ 1600, 400. The hint stays along the bottom, and Escape still shows the quit question in its usual place.
Stop at the edge of the world
Section titled “Stop at the edge of the world”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,with:
corner.x = std::clamp(corner.x, world_min, world_max - window.width); corner.y = std::clamp(corner.y, world_min, world_max - window.height);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.
A copyable dead-zone follow camera
Section titled “A copyable dead-zone follow camera”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:
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());samples/local_multiplayer/play_state.cpp, lines 95–97 at f525bc6
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 something fails
Section titled “If something fails”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.
In the browser build
Section titled “In the browser build”Build the page as Build for the browser shows, in a prompt where you have
run emsdk_env.bat:
cmake --build --preset wasm-debug && ctest --preset wasm-debugThe page follows the greeting in the same way, and the page check from that guide, with
--quit-keys Escape,Enter, still passes.
Where to go next
Section titled “Where to go next”- Two players in your game adds a second greeting and splits the window between two cameras.
- Drawing explains views, cameras and
Camera::frame, which fits a rectangle of the world into a view. - Local multiplayer reads the sample that follows two players.
- Read a build error has more on compiler messages.
What was checked
Section titled “What was checked”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.